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