Annotation of loncom/homework/rankresponse.pm, revision 1.54
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # rank style response
3: #
1.54 ! foxr 4: # $Id: rankresponse.pm,v 1.53 2007/08/29 10:07:42 foxr Exp $
1.1 albertel 5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: package Apache::rankresponse;
29: use strict;
30: use HTML::Entities();
1.30 albertel 31: use Apache::optionresponse();
32: use Apache::lonlocal;
1.1 albertel 33:
34: BEGIN {
35: &Apache::lonxml::register('Apache::rankresponse',('rankresponse'));
36: }
37:
38: sub start_rankresponse {
39: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
40: my $result;
41: #when in a rank response use these
42: &Apache::lonxml::register('Apache::rankresponse',
43: ('foilgroup','foil','conceptgroup'));
44: push (@Apache::lonxml::namespace,'rankresponse');
45: my $id = &Apache::response::start_response($parstack,$safeeval);
46: %Apache::hint::rank=();
1.30 albertel 47: undef(%Apache::response::foilnames);
1.1 albertel 48: if ($target eq 'meta') {
49: $result=&Apache::response::meta_package_write('rankresponse');
50: } elsif ($target eq 'edit' ) {
51: $result.=&Apache::edit::start_table($token).
52: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
53: &Apache::edit::deletelist($target,$token)
54: ."</td><td> ".&Apache::edit::end_row()
55: .&Apache::edit::start_spanning_row();
56:
57: $result.=
58: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
59: &Apache::edit::select_arg('Randomize Foil Order','randomize',
60: ['yes','no'],$token).
61: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
62: } elsif ($target eq 'modified') {
63: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
64: $safeeval,'max',
65: 'randomize');
66: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.25 albertel 67: } elsif ($target eq 'analyze') {
68: my $part_id="$Apache::inputtags::part.$id";
69: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.53 foxr 70: push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} }, 1);
71:
1.1 albertel 72: }
73: return $result;
74: }
75:
76: sub end_rankresponse {
77: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
78: my $result;
79: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
80: &Apache::response::end_response;
81: pop @Apache::lonxml::namespace;
82: &Apache::lonxml::deregister('Apache::rankresponse',
83: ('foilgroup','foil','conceptgroup'));
1.30 albertel 84: undef(%Apache::response::foilnames);
1.1 albertel 85: return $result;
86: }
87:
88: %Apache::response::foilgroup=();
89: sub start_foilgroup {
90: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
91: my $result;
92: %Apache::response::foilgroup=();
93: $Apache::rankresponse::conceptgroup=0;
1.34 albertel 94: &Apache::response::pushrandomnumber();
1.1 albertel 95: return $result;
96: }
97:
98: sub end_foilgroup {
99: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
100: my $result;
1.26 albertel 101: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
102: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 103: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
104: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
105: $safeeval,'-2');
106: my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
107: if (!defined($tol)) { $tol=0; }
1.3 sakharuk 108: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 109: $result=&displayfoils($target,$max,$randomize,$tol);
1.50 albertel 110: $Apache::lonxml::post_evaluate=0;
1.1 albertel 111: } elsif ($target eq 'answer' ) {
112: $result=&displayanswers($max,$randomize,$tol);
113: } elsif ( $target eq 'grade') {
114: &grade_response($max,$randomize,$tol);
1.25 albertel 115: } elsif ( $target eq 'analyze') {
116: my @shown = &whichfoils($max,$randomize);
117: &Apache::response::analyze_store_foilgroup(\@shown,
1.26 albertel 118: ['text','value','location']);
1.41 albertel 119: my $part_id=
120: "$Apache::inputtags::part.$Apache::inputtags::response[-1]";
121: $Apache::lonhomework::analyze{"$part_id.tol"}=$tol;
1.1 albertel 122: }
1.54 ! foxr 123: &Apache::lonxml::increment_counter(&getfoilcounts($max),
! 124: $Apache::inputtags::part);
! 125: if ($target eq 'analyze') {
! 126: &Apache::lonhomework::set_bubble_lines();
! 127: }
1.14 albertel 128: } elsif ($target eq 'edit') {
129: $result=&Apache::edit::end_table();
1.1 albertel 130: }
1.34 albertel 131: &Apache::response::poprandomnumber();
1.1 albertel 132: return $result;
133: }
134:
135: sub get_correct_order {
136: my ($tol,@foils) =@_;
137: my @correctorder;
138: my @value_names;
139: foreach my $name (@foils) {
140: my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
141: push(@value_names,\@pair);
142: }
143: @value_names =
144: sort {
1.17 albertel 145: if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] <=> $b->[0]);}
1.1 albertel 146: return 0;
147: } @value_names;
148: my @value_names_tmp=@value_names;
149: my $firstpair=shift(@value_names_tmp);
150: my $order=1;
151: my %order;
152: my $count=1;
153: my $lastvalue=$firstpair->[0];
154: $order{$firstpair->[1]}=$order;
155: foreach my $pair (@value_names_tmp) {
156: $count++;
157: if (abs($pair->[0]-$lastvalue) > $tol ) {
158: $order=$count;
159: }
160: $order{$pair->[1]}=$order;
161: $lastvalue=$pair->[0];
162: }
163: foreach my $name (@foils) {
164: push(@correctorder,$order{$name});
165: }
166: &Apache::lonhomework::showhash('b' => \@value_names);
167: &Apache::lonhomework::showhash('b' => \@correctorder);
168: return @correctorder;
169: }
170:
171: sub displayanswers {
172: my ($max,$randomize,$tol,@opt)=@_;
173: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
174: my @names = @{ $Apache::response::foilgroup{'names'} };
175: my @whichfoils = &whichfoils($max,$randomize);
176: my @correctorder=&get_correct_order($tol,@whichfoils);
1.49 albertel 177: my $result;
178: if ($Apache::lonhomework::type eq 'exam') {
179: my @alphabet = ('A'..'Z');
180: my $i=0;
181: foreach my $order (@correctorder) {
182: $result.=&Apache::response::answer_header('rankresponse',$i++);
183: $result.=&Apache::response::answer_part('rankresponse',
184: $alphabet[$order-1]);
185: $result.=&Apache::response::answer_part('rankresponse',$order);
186: $result.=&Apache::response::answer_footer('rankresponse');
187: }
188: } else {
189: $result=&Apache::response::answer_header('rankresponse');
190: foreach my $order (@correctorder) {
191: $result.=&Apache::response::answer_part('rankresponse',$order);
192: }
193: $result.=&Apache::response::answer_footer('rankresponse');
1.1 albertel 194: }
195: return $result;
196: }
197:
198: sub check_response_order {
199: my (%responsehash)=@_;
1.45 albertel 200: my @order=sort( {$a <=> $b} values(%responsehash));
1.1 albertel 201: my $lastvalue=0;
202: my $expected=1;
203: my $malformed=0;
204: foreach my $current (@order) {
205: &Apache::lonxml::debug("$lastvalue $expected $malformed");
206: if (!($current == $lastvalue || $current == $expected)) {
207: $malformed=1;
208: }
209: $expected++;
210: $lastvalue=$current;
211: }
212: return $malformed;
213: }
214:
215: sub grade_response {
216: my ($max,$randomize,$tol)=@_;
217: my (@whichfoils)=&whichfoils($max,$randomize);
1.44 albertel 218: if (!&Apache::response::submitted()) { return; }
1.1 albertel 219: my %responsehash;
220: my %grade;
1.28 albertel 221: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1 albertel 222: my @correctorder=&get_correct_order($tol,@whichfoils);
223: foreach my $name (@whichfoils) {
1.38 albertel 224: my $response = &Apache::response::getresponse($temp,'A is 1');
1.1 albertel 225: my $value=shift(@correctorder);
226: if ( $response =~ /[^\s]/) {
1.27 albertel 227: $responsehash{$name}=$response;
1.1 albertel 228: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
229: if ($value eq $response) {
230: $grade{$name}='1'; $right++;
231: } else {
232: $grade{$name}='0'; $wrong++;
233: }
234: } else {
235: $ignored++;
236: }
237: $temp++;
238: }
239: my $malformed=&check_response_order(%responsehash);
240: my $part=$Apache::inputtags::part;
241: my $id = $Apache::inputtags::response['-1'];
242: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
243: my $gradestr =&Apache::lonnet::hash2str(%grade);
244: my %previous=&Apache::response::check_for_previous($responsestr,
245: $part,$id);
246: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
247: my $ad;
248: if ($malformed) {
249: $ad='MISORDERED_RANK';
250: } elsif ($wrong==0 && $ignored==0) {
251: $ad='EXACT_ANS';
252: } elsif ($wrong==0 && $right==0) {
253: #nothing submitted
254: } else {
255: if ($ignored==0) {
256: $ad='INCORRECT';
257: } else {
258: $ad='MISSING_ANSWER';
259: }
260: }
1.40 albertel 261: if ($Apache::lonhomework::type eq 'survey' &&
262: ($ad eq 'INCORRECT' || $ad eq 'EXACT_ANS') ) {
263: $ad='SUBMITTED';
264: } else {
265: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
266: }
1.1 albertel 267: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
268: $responsestr;
269: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
270: &Apache::response::handle_previous(\%previous,$ad);
271: }
272:
1.52 albertel 273: sub format_prior_answer {
274: my ($mode,$answer,$other_data) = @_;
275: my %lastresponse=&Apache::lonnet::str2hash($answer);
276: my $foil_order =$other_data->[0];
277: my %grading =&Apache::lonnet::str2hash($other_data->[1]);
278: my $output;
279: foreach my $name (@{ $foil_order }) {
280: next if (!defined($lastresponse{$name}));
281: $output .= '<tr><td>'.$lastresponse{$name}.'</td></tr>';
282: }
283: return if (!defined($output));
284: $output =
285: '<table class="LC_prior_rank">'.$output.'</table>';
286: return $output;
287: }
288:
1.1 albertel 289: sub displayfoils {
290: my ($target,$max,$randomize,$tol)=@_;
291: my $result;
1.4 sakharuk 292: my @alphabet=('A'..'Z');
1.1 albertel 293: my (@whichfoils)=&whichfoils($max,$randomize);
294: my $part=$Apache::inputtags::part;
295: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
296: my @whichopt=(1..($#whichfoils+1));
297: my @correctorder=&get_correct_order($tol,@whichfoils);
1.20 sakharuk 298: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 299: foreach my $name (@whichfoils) {
300: my $text=$Apache::response::foilgroup{$name.'.text'};
301: my $value=shift(@correctorder);
1.46 albertel 302: if ($target eq 'web') {
303: $result.='<br /><b>'.$value.':</b> '.$text;
304: } else {
305: $result.=' \strut\\\\\strut '.$value.':'.$text;
306: }
1.1 albertel 307: }
308: } else {
309: my $i = 0;
1.29 albertel 310: my $temp=1;
1.1 albertel 311: my $id=$Apache::inputtags::response[-1];
312: my $part=$Apache::inputtags::part;
313: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.5 sakharuk 314: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
315: my @alp = splice @alphabet, 0, $#whichopt + 1;
1.15 sakharuk 316: my $internal_counter=$Apache::lonxml::counter;
1.1 albertel 317: foreach my $name (@whichfoils) {
318: my $lastopt=$lastresponse{$name};
1.3 sakharuk 319: my $optionlist='';
320: if ($target ne 'tex') {$optionlist="<option></option>\n";}
1.1 albertel 321: my $option;
322: foreach $option (@whichopt) {
323: if ($option eq $lastopt) {
1.3 sakharuk 324: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1 albertel 325: } else {
1.3 sakharuk 326: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1 albertel 327: }
328: }
1.12 sakharuk 329: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.51 albertel 330: $optionlist='<select onchange="javascript:setSubmittedPart(\''.
331: $part.'\');" name="HWVAL_'.
1.3 sakharuk 332: $Apache::inputtags::response[-1].':'.$temp.'">'.
333: $optionlist."</select>\n";
334: } else {
335: $optionlist=' '.$temp.' '.$optionlist.' ';
336: }
1.1 albertel 337: my $text=$Apache::response::foilgroup{$name.'.text'};
1.3 sakharuk 338: if ($target ne 'tex') {
1.12 sakharuk 339: if ($Apache::lonhomework::type ne 'exam') {
340: $result.='<br />'.$optionlist.$text."\n";
341: } else {
342: $result.='<br />'.$text."\n";
343: }
1.4 sakharuk 344: if ($Apache::lonhomework::type eq 'exam') {
1.22 albertel 345: my @values=(1..scalar(@whichopt));
346: $result.=&Apache::optionresponse::webbubbles(\@values,\@whichopt,$temp,$lastopt);
1.4 sakharuk 347: }
348: } else {
349: if ($Apache::lonhomework::type eq 'exam') {
1.33 sakharuk 350: $result.='\vskip 0 mm '.$text.' \vskip 0 mm '."\n";
1.36 albertel 351: $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]'.&Apache::optionresponse::bubbles(\@alp,\@whichopt,'rankresponse').'\end{enumerate} \vskip -8 mm \strut ';
1.15 sakharuk 352: $internal_counter++;
1.4 sakharuk 353: } else {
1.16 sakharuk 354: $result.=' \vskip 0mm \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
1.4 sakharuk 355: }
356: }
1.1 albertel 357: $temp++;
358: }
359: }
1.52 albertel 360: if ($target eq 'web') {
361: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
362: [\@whichfoils,
363: 'submissiongrading']);
364: }
1.5 sakharuk 365: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
1.1 albertel 366: return $result;
367: }
368:
369: sub getfoilcounts {
370: my ($max)=@_;
371: # +1 since instructors will count from 1
372: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
373: if (&Apache::response::showallfoils()) { $max=$count; }
374: if ($count>$max) { $count=$max }
375: &Apache::lonxml::debug("Count is $count from $max");
376: return $count;
377: }
378:
379: sub whichfoils {
380: my ($max,$randomize)=@_;
1.13 albertel 381: return &Apache::response::whichorder($max,$randomize,
382: &Apache::response::showallfoils(),
383: \%Apache::response::foilgroup);
1.1 albertel 384: }
385:
386: sub start_conceptgroup {
387: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
388: $Apache::rankresponse::conceptgroup=1;
389: %Apache::response::conceptgroup=();
390: my $result;
391: if ($target eq 'edit') {
392: $result.=&Apache::edit::tag_start($target,$token,
393: "Concept Grouped Foils");
394: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
395: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
396: }
397: if ($target eq 'modified') {
398: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
399: $safeeval,'concept');
400: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
401: }
402: return $result;
403: }
404:
405: sub end_conceptgroup {
406: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
407: $Apache::rankresponse::conceptgroup=0;
408: my $result='';
1.25 albertel 409: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
410: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 411: #if not there aren't any foils to display and thus no question
1.25 albertel 412: &Apache::response::pick_foil_for_concept($target,
413: ['value','text','location'],
414: \%Apache::hint::rank,
415: $parstack,$safeeval);
1.1 albertel 416: } elsif ($target eq 'edit') {
417: $result=&Apache::edit::end_table();
418: }
419: return $result;
420: }
421:
422: sub insert_conceptgroup {
423: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
424: return $result;
425: }
426:
427: sub start_foil {
428: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
429: my $result='';
1.25 albertel 430: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 431: &Apache::lonxml::startredirection;
1.39 albertel 432: if ($target eq 'analyze') {
433: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
434: }
1.1 albertel 435: } elsif ($target eq 'edit') {
436: $result=&Apache::edit::tag_start($target,$token,"Foil");
437: my $level='-2';
438: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
439: $result.=&Apache::edit::text_arg('Name:','name',$token);
440: $result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
441: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
442: $safeeval,'-3');
443: if ($randomize ne 'no') {
444: $result.=&Apache::edit::select_arg('Location:','location',
445: ['random','top','bottom'],$token);
446: }
447: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
448: } elsif ($target eq 'modified') {
449: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
450: $safeeval,'value',
451: 'name','location');
452: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
453: }
454: return $result;
455: }
456:
457: sub end_foil {
458: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
459: my $text ='';
460: my $result = '';
1.25 albertel 461: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 462: $text=&Apache::lonxml::endredirection;
463: }
1.25 albertel 464: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
465: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 466: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.42 albertel 467: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
468: $text='\vskip 5mm $\triangleright$ '.$text;
469: }
1.1 albertel 470: if ($value ne 'unused') {
471: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
472: &Apache::lonxml::debug("Got a name of :$name:");
1.48 albertel 473: if ($name eq "") {
1.47 albertel 474: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.43 albertel 475: $name=$Apache::lonxml::curdepth;
476: }
1.1 albertel 477: &Apache::lonxml::debug("Using a name of :$name:");
1.30 albertel 478: if (defined($Apache::response::foilnames{$name})) {
479: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
480: }
1.31 albertel 481: $Apache::response::foilnames{$name}++;
1.30 albertel 482: my $location =&Apache::lonxml::get_param('location',$parstack,
483: $safeeval);
1.1 albertel 484: if ( $Apache::rankresponse::conceptgroup
485: && !&Apache::response::showallfoils() ) {
486: push @{ $Apache::response::conceptgroup{'names'} }, $name;
487: $Apache::response::conceptgroup{"$name.value"} = $value;
1.42 albertel 488: $Apache::response::conceptgroup{"$name.text"} = $text;
1.1 albertel 489: $Apache::response::conceptgroup{"$name.location"} = $location;
490: } else {
491: push @{ $Apache::response::foilgroup{'names'} }, $name;
492: $Apache::response::foilgroup{"$name.value"} = $value;
1.42 albertel 493: $Apache::response::foilgroup{"$name.text"} = $text;
1.1 albertel 494: $Apache::response::foilgroup{"$name.location"} = $location;
495: }
496: }
497: }
498: if ($target eq 'edit') {
499: $result.= &Apache::edit::tag_end($target,$token,'');
500: }
501: return $result;
502: }
503:
504: sub insert_foil {
505: return '
506: <foil name="" value="unused">
507: <startouttext />
508: <endouttext />
509: </foil>';
510: }
511: 1;
512: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>