Annotation of loncom/homework/rankresponse.pm, revision 1.9
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # rank style response
3: #
1.9 ! sakharuk 4: # $Id: rankresponse.pm,v 1.8 2003/03/25 22:15:04 sakharuk 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: # 2/21 Guy
28:
29: package Apache::rankresponse;
30: use strict;
31: use HTML::Entities();
1.4 sakharuk 32: use Apache::optionresponse;
1.1 albertel 33:
1.9 ! sakharuk 34: my $flag_for_exam_printing = 1;
! 35:
1.1 albertel 36: BEGIN {
37: &Apache::lonxml::register('Apache::rankresponse',('rankresponse'));
38: }
39:
40: sub start_rankresponse {
41: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
42: my $result;
43: #when in a rank response use these
44: &Apache::lonxml::register('Apache::rankresponse',
45: ('foilgroup','foil','conceptgroup'));
46: push (@Apache::lonxml::namespace,'rankresponse');
47: my $id = &Apache::response::start_response($parstack,$safeeval);
48: %Apache::hint::rank=();
49: if ($target eq 'meta') {
50: $result=&Apache::response::meta_package_write('rankresponse');
51: } elsif ($target eq 'edit' ) {
52: $result.=&Apache::edit::start_table($token).
53: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
54: &Apache::edit::deletelist($target,$token)
55: ."</td><td> ".&Apache::edit::end_row()
56: .&Apache::edit::start_spanning_row();
57:
58: $result.=
59: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
60: &Apache::edit::select_arg('Randomize Foil Order','randomize',
61: ['yes','no'],$token).
62: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
63: } elsif ($target eq 'modified') {
64: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
65: $safeeval,'max',
66: 'randomize');
67: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
68: }
69: return $result;
70: }
71:
72: sub end_rankresponse {
73: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
74: my $result;
75: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
76: &Apache::response::end_response;
77: pop @Apache::lonxml::namespace;
78: &Apache::lonxml::deregister('Apache::rankresponse',
79: ('foilgroup','foil','conceptgroup'));
80: return $result;
81: }
82:
83: %Apache::response::foilgroup=();
84: sub start_foilgroup {
85: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
86: my $result;
87: %Apache::response::foilgroup=();
88: $Apache::rankresponse::conceptgroup=0;
89: &Apache::response::setrandomnumber();
90: return $result;
91: }
92:
93: sub end_foilgroup {
94: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
95: my $result;
1.3 sakharuk 96: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.1 albertel 97: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
98: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
99: $safeeval,'-2');
100: my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
101: if (!defined($tol)) { $tol=0; }
1.3 sakharuk 102: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 103: $result=&displayfoils($target,$max,$randomize,$tol);
104: } elsif ($target eq 'answer' ) {
105: $result=&displayanswers($max,$randomize,$tol);
106: } elsif ( $target eq 'grade') {
107: &grade_response($max,$randomize,$tol);
108: }
109: }
110: return $result;
111: }
112:
113: sub get_correct_order {
114: my ($tol,@foils) =@_;
115: my @correctorder;
116: my @value_names;
117: foreach my $name (@foils) {
118: my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
119: push(@value_names,\@pair);
120: }
121: @value_names =
122: sort {
123: if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] cmp $b->[0]);}
124: return 0;
125: } @value_names;
126: my @value_names_tmp=@value_names;
127: my $firstpair=shift(@value_names_tmp);
128: my $order=1;
129: my %order;
130: my $count=1;
131: my $lastvalue=$firstpair->[0];
132: $order{$firstpair->[1]}=$order;
133: foreach my $pair (@value_names_tmp) {
134: $count++;
135: if (abs($pair->[0]-$lastvalue) > $tol ) {
136: $order=$count;
137: }
138: $order{$pair->[1]}=$order;
139: $lastvalue=$pair->[0];
140: }
141: foreach my $name (@foils) {
142: push(@correctorder,$order{$name});
143: }
144: &Apache::lonhomework::showhash('b' => \@value_names);
145: &Apache::lonhomework::showhash('b' => \@correctorder);
146: return @correctorder;
147: }
148:
149: sub displayanswers {
150: my ($max,$randomize,$tol,@opt)=@_;
151: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
152: my @names = @{ $Apache::response::foilgroup{'names'} };
153: my @whichfoils = &whichfoils($max,$randomize);
154: my $result=&Apache::response::answer_header('rankresponse');
155: my @correctorder=&get_correct_order($tol,@whichfoils);
156: foreach my $order (@correctorder) {
157: $result.=&Apache::response::answer_part('rankresponse',$order);
158: }
159: $result.=&Apache::response::answer_footer('rankresponse');
160: return $result;
161: }
162:
163: sub check_response_order {
164: my (%responsehash)=@_;
165: my @order=sort(values(%responsehash));
166: my $lastvalue=0;
167: my $expected=1;
168: my $malformed=0;
169: foreach my $current (@order) {
170: &Apache::lonxml::debug("$lastvalue $expected $malformed");
171: if (!($current == $lastvalue || $current == $expected)) {
172: $malformed=1;
173: }
174: $expected++;
175: $lastvalue=$current;
176: }
177: return $malformed;
178: }
179:
180: sub grade_response {
181: my ($max,$randomize,$tol)=@_;
182: my (@whichfoils)=&whichfoils($max,$randomize);
183: if (!defined($ENV{'form.submitted'})) { return; }
184: my %responsehash;
185: my %grade;
186: my ($temp,$right,$wrong,$ignored)=(0,0,0,0);
187: my @correctorder=&get_correct_order($tol,@whichfoils);
188: foreach my $name (@whichfoils) {
189: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
190: $responsehash{$name}=$response;
191: my $value=shift(@correctorder);
192: if ( $response =~ /[^\s]/) {
193: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
194: if ($value eq $response) {
195: $grade{$name}='1'; $right++;
196: } else {
197: $grade{$name}='0'; $wrong++;
198: }
199: } else {
200: $ignored++;
201: }
202: $temp++;
203: }
204: my $malformed=&check_response_order(%responsehash);
205: my $part=$Apache::inputtags::part;
206: my $id = $Apache::inputtags::response['-1'];
207: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
208: my $gradestr =&Apache::lonnet::hash2str(%grade);
209: my %previous=&Apache::response::check_for_previous($responsestr,
210: $part,$id);
211: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
212: my $ad;
213: if ($malformed) {
214: $ad='MISORDERED_RANK';
215: } elsif ($wrong==0 && $ignored==0) {
216: $ad='EXACT_ANS';
217: } elsif ($wrong==0 && $right==0) {
218: #nothing submitted
219: } else {
220: if ($ignored==0) {
221: $ad='INCORRECT';
222: } else {
223: $ad='MISSING_ANSWER';
224: }
225: }
226: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
227: $responsestr;
228: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
229: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
230: &Apache::response::handle_previous(\%previous,$ad);
231: }
232:
233: sub displayfoils {
234: my ($target,$max,$randomize,$tol)=@_;
235: my $result;
1.4 sakharuk 236: my @alphabet=('A'..'Z');
1.1 albertel 237: my (@whichfoils)=&whichfoils($max,$randomize);
238: my $part=$Apache::inputtags::part;
239: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
240: my $status=$Apache::inputtags::status[-1];
241: my @whichopt=(1..($#whichfoils+1));
242: my @correctorder=&get_correct_order($tol,@whichfoils);
243: if (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) {
244: foreach my $name (@whichfoils) {
245: my $text=$Apache::response::foilgroup{$name.'.text'};
246: my $value=shift(@correctorder);
247: $result.='<br />'.$value.':'.$text;
248: }
249: } else {
250: my $i = 0;
251: my $temp=0;
252: my $id=$Apache::inputtags::response[-1];
253: my $part=$Apache::inputtags::part;
254: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.5 sakharuk 255: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
256: my @alp = splice @alphabet, 0, $#whichopt + 1;
1.1 albertel 257: foreach my $name (@whichfoils) {
258: my $lastopt=$lastresponse{$name};
1.3 sakharuk 259: my $optionlist='';
260: if ($target ne 'tex') {$optionlist="<option></option>\n";}
1.1 albertel 261: my $option;
262: foreach $option (@whichopt) {
263: if ($option eq $lastopt) {
1.3 sakharuk 264: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1 albertel 265: } else {
1.3 sakharuk 266: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1 albertel 267: }
268: }
1.3 sakharuk 269: if ($target ne 'tex') {
270: $optionlist='<select name="HWVAL_'.
271: $Apache::inputtags::response[-1].':'.$temp.'">'.
272: $optionlist."</select>\n";
273: } else {
274: $optionlist=' '.$temp.' '.$optionlist.' ';
275: }
1.1 albertel 276: my $text=$Apache::response::foilgroup{$name.'.text'};
1.3 sakharuk 277: if ($target ne 'tex') {
278: $result.='<br />'.$optionlist.$text."\n";
1.4 sakharuk 279: if ($Apache::lonhomework::type eq 'exam') {
1.5 sakharuk 280: $result.=&Apache::optionresponse::webbubbles(\@alp,\@whichopt);
1.4 sakharuk 281: }
282: } else {
283: if ($Apache::lonhomework::type eq 'exam') {
1.9 ! sakharuk 284: $result.='\vskip 0 mm '.$text.' \vskip -3 mm '."\n";
! 285: $result.='\vskip -5 mm\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&Apache::optionresponse::bubbles(\@alp,\@whichopt).'}\end{enumerate} \vskip -5 mm \strut ';
1.4 sakharuk 286: } else {
1.5 sakharuk 287: $result.=' \\\\ \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
1.4 sakharuk 288: }
289: }
1.1 albertel 290: $temp++;
291: }
292: }
1.5 sakharuk 293: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
1.1 albertel 294: return $result;
295: }
296:
297: sub getfoilcounts {
298: my ($max)=@_;
299: # +1 since instructors will count from 1
300: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
301: if (&Apache::response::showallfoils()) { $max=$count; }
302: if ($count>$max) { $count=$max }
303: &Apache::lonxml::debug("Count is $count from $max");
304: return $count;
305: }
306:
307: sub whichfoils {
308: my ($max,$randomize)=@_;
309: $max = &getfoilcounts($max);
310: # &Apache::lonxml::debug("man $max randomize $randomize");
311: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
312: my @names = @{ $Apache::response::foilgroup{'names'} };
313: my @whichopt =();
314: my (%top,@toplist,%bottom,@bottomlist);
315: if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
316: my $current=0;
317: foreach my $name (@names) {
318: $current++;
319: if ($Apache::response::foilgroup{"$name.location"} eq 'top') {
320: $top{$name}=$current;
321: } elsif ($Apache::response::foilgroup{"$name.location"} eq
322: 'bottom') {
323: $bottom{$name}=$current;
324: }
325: }
326: }
327: while ((($#whichopt+1) < $max) && ($#names > -1)) {
328: # &Apache::lonxml::debug("Have $#whichopt max is $max");
329: my $aopt;
330: if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
331: $aopt=0;
332: } else {
333: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
334: }
335: # &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
336: $aopt=splice(@names,$aopt,1);
337: # &Apache::lonxml::debug("Picked $aopt");
338: if ($top{$aopt}) {
339: $toplist[$top{$aopt}]=$aopt;
340: } elsif ($bottom{$aopt}) {
341: $bottomlist[$bottom{$aopt}]=$aopt;
342: } else {
343: push (@whichopt,$aopt);
344: }
345: }
346: # &Apache::lonxml::debug("Grr, ".$#whichopt.":".$#toplist.':'.$#bottomlist);
347: for (my $i=0;$i<=$#toplist;$i++) {
348: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
349: }
350: for (my $i=0;$i<=$#bottomlist;$i++) {
351: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
352: }
353: &Apache::lonxml::debug("Grr, ".$#whichopt.":".$#toplist.':'.$#bottomlist);
354: return @whichopt;
355: }
356:
357: sub start_conceptgroup {
358: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
359: $Apache::rankresponse::conceptgroup=1;
360: %Apache::response::conceptgroup=();
361: my $result;
362: if ($target eq 'edit') {
363: $result.=&Apache::edit::tag_start($target,$token,
364: "Concept Grouped Foils");
365: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
366: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
367: }
368: if ($target eq 'modified') {
369: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
370: $safeeval,'concept');
371: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
372: }
373: return $result;
374: }
375:
376: sub end_conceptgroup {
377: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
378: $Apache::rankresponse::conceptgroup=0;
379: my $result='';
1.8 sakharuk 380: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.1 albertel 381: #if not there aren't any foils to display and thus no question
382: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
383: my @names = @{ $Apache::response::conceptgroup{'names'} };
384: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
385: my $name=$names[$pick];
386: push @{ $Apache::response::foilgroup{'names'} }, $name;
387: $Apache::response::foilgroup{"$name.value"} =
388: $Apache::response::conceptgroup{"$name.value"};
389: $Apache::response::foilgroup{"$name.text"} =
390: $Apache::response::conceptgroup{"$name.text"};
391: $Apache::response::foilgroup{"$name.location"} =
392: $Apache::response::conceptgroup{"$name.location"};
393: my $concept = &Apache::lonxml::get_param('concept',$parstack,
394: $safeeval);
395: $Apache::response::foilgroup{"$name.concept"} = $concept;
396: &Apache::lonxml::debug("Selecting $name in $concept");
1.8 sakharuk 397: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 398: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
399: push(@{ $Apache::hint::rank{"$part_id.concepts"} },
400: $concept);
401: $Apache::hint::rank{"$part_id.concept.$concept"}=
402: $Apache::response::conceptgroup{'names'};
403: }
404: }
405: } elsif ($target eq 'edit') {
406: $result=&Apache::edit::end_table();
407: }
408: return $result;
409: }
410:
411: sub insert_conceptgroup {
412: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
413: return $result;
414: }
415:
416: sub start_foil {
417: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
418: my $result='';
1.3 sakharuk 419: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 420: &Apache::lonxml::startredirection;
421: } elsif ($target eq 'edit') {
422: $result=&Apache::edit::tag_start($target,$token,"Foil");
423: my $level='-2';
424: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
425: $result.=&Apache::edit::text_arg('Name:','name',$token);
426: $result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
427: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
428: $safeeval,'-3');
429: if ($randomize ne 'no') {
430: $result.=&Apache::edit::select_arg('Location:','location',
431: ['random','top','bottom'],$token);
432: }
433: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
434: } elsif ($target eq 'modified') {
435: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
436: $safeeval,'value',
437: 'name','location');
438: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
439: }
440: return $result;
441: }
442:
443: sub end_foil {
444: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
445: my $text ='';
446: my $result = '';
1.3 sakharuk 447: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 448: $text=&Apache::lonxml::endredirection;
449: }
1.3 sakharuk 450: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.1 albertel 451: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
452: if ($value ne 'unused') {
453: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
454: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
455: &Apache::lonxml::debug("Got a name of :$name:");
456: if (!$name) { $name=$Apache::lonxml::curdepth; }
457: &Apache::lonxml::debug("Using a name of :$name:");
458: if ( $Apache::rankresponse::conceptgroup
459: && !&Apache::response::showallfoils() ) {
460: push @{ $Apache::response::conceptgroup{'names'} }, $name;
461: $Apache::response::conceptgroup{"$name.value"} = $value;
1.9 ! sakharuk 462: if ($target eq 'tex') {
! 463: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
! 464: } else {
! 465: $Apache::response::conceptgroup{"$name.text"} = $text;
! 466: }
1.1 albertel 467: $Apache::response::conceptgroup{"$name.location"} = $location;
468: } else {
469: push @{ $Apache::response::foilgroup{'names'} }, $name;
470: $Apache::response::foilgroup{"$name.value"} = $value;
1.9 ! sakharuk 471: if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam' and $flag_for_exam_printing) {
! 472: $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
! 473: $flag_for_exam_printing = 0;
! 474: } else {
! 475: if ($target eq 'tex') {
! 476: $Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
! 477: } else {
! 478: $Apache::response::foilgroup{"$name.text"} = $text;
! 479: }
! 480: }
1.1 albertel 481: $Apache::response::foilgroup{"$name.location"} = $location;
482: }
483: }
484: }
485: if ($target eq 'edit') {
486: $result.= &Apache::edit::tag_end($target,$token,'');
487: }
488: return $result;
489: }
490:
491: sub insert_foil {
492: return '
493: <foil name="" value="unused">
494: <startouttext />
495: <endouttext />
496: </foil>';
497: }
498: 1;
499: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>