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