Annotation of loncom/homework/rankresponse.pm, revision 1.37
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # rank style response
3: #
1.37 ! albertel 4: # $Id: rankresponse.pm,v 1.36 2004/08/25 20:00:31 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:
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.1 albertel 70: }
71: return $result;
72: }
73:
74: sub end_rankresponse {
75: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
76: my $result;
77: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
78: &Apache::response::end_response;
79: pop @Apache::lonxml::namespace;
80: &Apache::lonxml::deregister('Apache::rankresponse',
81: ('foilgroup','foil','conceptgroup'));
1.30 albertel 82: undef(%Apache::response::foilnames);
1.1 albertel 83: return $result;
84: }
85:
86: %Apache::response::foilgroup=();
87: sub start_foilgroup {
88: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
89: my $result;
90: %Apache::response::foilgroup=();
91: $Apache::rankresponse::conceptgroup=0;
1.34 albertel 92: &Apache::response::pushrandomnumber();
1.1 albertel 93: return $result;
94: }
95:
96: sub end_foilgroup {
97: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
98: my $result;
1.26 albertel 99: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
100: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 101: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
102: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
103: $safeeval,'-2');
104: my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
105: if (!defined($tol)) { $tol=0; }
1.3 sakharuk 106: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 107: $result=&displayfoils($target,$max,$randomize,$tol);
108: } elsif ($target eq 'answer' ) {
109: $result=&displayanswers($max,$randomize,$tol);
110: } elsif ( $target eq 'grade') {
111: &grade_response($max,$randomize,$tol);
1.25 albertel 112: } elsif ( $target eq 'analyze') {
113: my @shown = &whichfoils($max,$randomize);
114: &Apache::response::analyze_store_foilgroup(\@shown,
1.26 albertel 115: ['text','value','location']);
1.1 albertel 116: }
1.24 sakharuk 117: &Apache::lonxml::increment_counter(&getfoilcounts($max));
1.14 albertel 118: } elsif ($target eq 'edit') {
119: $result=&Apache::edit::end_table();
1.1 albertel 120: }
1.34 albertel 121: &Apache::response::poprandomnumber();
1.1 albertel 122: return $result;
123: }
124:
125: sub get_correct_order {
126: my ($tol,@foils) =@_;
127: my @correctorder;
128: my @value_names;
129: foreach my $name (@foils) {
130: my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
131: push(@value_names,\@pair);
132: }
133: @value_names =
134: sort {
1.17 albertel 135: if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] <=> $b->[0]);}
1.1 albertel 136: return 0;
137: } @value_names;
138: my @value_names_tmp=@value_names;
139: my $firstpair=shift(@value_names_tmp);
140: my $order=1;
141: my %order;
142: my $count=1;
143: my $lastvalue=$firstpair->[0];
144: $order{$firstpair->[1]}=$order;
145: foreach my $pair (@value_names_tmp) {
146: $count++;
147: if (abs($pair->[0]-$lastvalue) > $tol ) {
148: $order=$count;
149: }
150: $order{$pair->[1]}=$order;
151: $lastvalue=$pair->[0];
152: }
153: foreach my $name (@foils) {
154: push(@correctorder,$order{$name});
155: }
156: &Apache::lonhomework::showhash('b' => \@value_names);
157: &Apache::lonhomework::showhash('b' => \@correctorder);
158: return @correctorder;
159: }
160:
161: sub displayanswers {
162: my ($max,$randomize,$tol,@opt)=@_;
163: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
164: my @names = @{ $Apache::response::foilgroup{'names'} };
165: my @whichfoils = &whichfoils($max,$randomize);
166: my $result=&Apache::response::answer_header('rankresponse');
167: my @correctorder=&get_correct_order($tol,@whichfoils);
168: foreach my $order (@correctorder) {
169: $result.=&Apache::response::answer_part('rankresponse',$order);
170: }
171: $result.=&Apache::response::answer_footer('rankresponse');
172: return $result;
173: }
174:
175: sub check_response_order {
176: my (%responsehash)=@_;
177: my @order=sort(values(%responsehash));
178: my $lastvalue=0;
179: my $expected=1;
180: my $malformed=0;
181: foreach my $current (@order) {
182: &Apache::lonxml::debug("$lastvalue $expected $malformed");
183: if (!($current == $lastvalue || $current == $expected)) {
184: $malformed=1;
185: }
186: $expected++;
187: $lastvalue=$current;
188: }
189: return $malformed;
190: }
191:
192: sub grade_response {
193: my ($max,$randomize,$tol)=@_;
194: my (@whichfoils)=&whichfoils($max,$randomize);
195: if (!defined($ENV{'form.submitted'})) { return; }
196: my %responsehash;
197: my %grade;
1.28 albertel 198: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1 albertel 199: my @correctorder=&get_correct_order($tol,@whichfoils);
200: foreach my $name (@whichfoils) {
1.37 ! albertel 201: # get response returns a=>0, when we want a=>1
! 202: my $response = &Apache::response::getresponse($temp)+1;
1.1 albertel 203: my $value=shift(@correctorder);
204: if ( $response =~ /[^\s]/) {
1.27 albertel 205: $responsehash{$name}=$response;
1.1 albertel 206: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
207: if ($value eq $response) {
208: $grade{$name}='1'; $right++;
209: } else {
210: $grade{$name}='0'; $wrong++;
211: }
212: } else {
213: $ignored++;
214: }
215: $temp++;
216: }
217: my $malformed=&check_response_order(%responsehash);
218: my $part=$Apache::inputtags::part;
219: my $id = $Apache::inputtags::response['-1'];
220: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
221: my $gradestr =&Apache::lonnet::hash2str(%grade);
222: my %previous=&Apache::response::check_for_previous($responsestr,
223: $part,$id);
224: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
225: my $ad;
226: if ($malformed) {
227: $ad='MISORDERED_RANK';
228: } elsif ($wrong==0 && $ignored==0) {
229: $ad='EXACT_ANS';
230: } elsif ($wrong==0 && $right==0) {
231: #nothing submitted
232: } else {
233: if ($ignored==0) {
234: $ad='INCORRECT';
235: } else {
236: $ad='MISSING_ANSWER';
237: }
238: }
239: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
240: $responsestr;
241: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
242: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
243: &Apache::response::handle_previous(\%previous,$ad);
244: }
245:
246: sub displayfoils {
247: my ($target,$max,$randomize,$tol)=@_;
248: my $result;
1.4 sakharuk 249: my @alphabet=('A'..'Z');
1.1 albertel 250: my (@whichfoils)=&whichfoils($max,$randomize);
251: my $part=$Apache::inputtags::part;
252: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
253: my @whichopt=(1..($#whichfoils+1));
254: my @correctorder=&get_correct_order($tol,@whichfoils);
1.20 sakharuk 255: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 256: foreach my $name (@whichfoils) {
257: my $text=$Apache::response::foilgroup{$name.'.text'};
258: my $value=shift(@correctorder);
1.19 sakharuk 259: if ($target eq 'web') {$result.='<br />';} else {$result.=' \strut\\\\\strut ';}
260: $result.=$value.':'.$text;
1.1 albertel 261: }
262: } else {
263: my $i = 0;
1.29 albertel 264: my $temp=1;
1.1 albertel 265: my $id=$Apache::inputtags::response[-1];
266: my $part=$Apache::inputtags::part;
267: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.5 sakharuk 268: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
269: my @alp = splice @alphabet, 0, $#whichopt + 1;
1.15 sakharuk 270: my $internal_counter=$Apache::lonxml::counter;
1.1 albertel 271: foreach my $name (@whichfoils) {
272: my $lastopt=$lastresponse{$name};
1.3 sakharuk 273: my $optionlist='';
274: if ($target ne 'tex') {$optionlist="<option></option>\n";}
1.1 albertel 275: my $option;
276: foreach $option (@whichopt) {
277: if ($option eq $lastopt) {
1.3 sakharuk 278: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1 albertel 279: } else {
1.3 sakharuk 280: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1 albertel 281: }
282: }
1.12 sakharuk 283: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.3 sakharuk 284: $optionlist='<select name="HWVAL_'.
285: $Apache::inputtags::response[-1].':'.$temp.'">'.
286: $optionlist."</select>\n";
287: } else {
288: $optionlist=' '.$temp.' '.$optionlist.' ';
289: }
1.1 albertel 290: my $text=$Apache::response::foilgroup{$name.'.text'};
1.3 sakharuk 291: if ($target ne 'tex') {
1.12 sakharuk 292: if ($Apache::lonhomework::type ne 'exam') {
293: $result.='<br />'.$optionlist.$text."\n";
294: } else {
295: $result.='<br />'.$text."\n";
296: }
1.4 sakharuk 297: if ($Apache::lonhomework::type eq 'exam') {
1.22 albertel 298: my @values=(1..scalar(@whichopt));
299: $result.=&Apache::optionresponse::webbubbles(\@values,\@whichopt,$temp,$lastopt);
1.4 sakharuk 300: }
301: } else {
302: if ($Apache::lonhomework::type eq 'exam') {
1.33 sakharuk 303: $result.='\vskip 0 mm '.$text.' \vskip 0 mm '."\n";
1.36 albertel 304: $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 305: $internal_counter++;
1.4 sakharuk 306: } else {
1.16 sakharuk 307: $result.=' \vskip 0mm \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
1.4 sakharuk 308: }
309: }
1.1 albertel 310: $temp++;
311: }
312: }
1.5 sakharuk 313: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
1.1 albertel 314: return $result;
315: }
316:
317: sub getfoilcounts {
318: my ($max)=@_;
319: # +1 since instructors will count from 1
320: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
321: if (&Apache::response::showallfoils()) { $max=$count; }
322: if ($count>$max) { $count=$max }
323: &Apache::lonxml::debug("Count is $count from $max");
324: return $count;
325: }
326:
327: sub whichfoils {
328: my ($max,$randomize)=@_;
1.13 albertel 329: return &Apache::response::whichorder($max,$randomize,
330: &Apache::response::showallfoils(),
331: \%Apache::response::foilgroup);
1.1 albertel 332: }
333:
334: sub start_conceptgroup {
335: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
336: $Apache::rankresponse::conceptgroup=1;
337: %Apache::response::conceptgroup=();
338: my $result;
339: if ($target eq 'edit') {
340: $result.=&Apache::edit::tag_start($target,$token,
341: "Concept Grouped Foils");
342: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
343: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
344: }
345: if ($target eq 'modified') {
346: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
347: $safeeval,'concept');
348: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
349: }
350: return $result;
351: }
352:
353: sub end_conceptgroup {
354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
355: $Apache::rankresponse::conceptgroup=0;
356: my $result='';
1.25 albertel 357: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
358: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 359: #if not there aren't any foils to display and thus no question
1.25 albertel 360: &Apache::response::pick_foil_for_concept($target,
361: ['value','text','location'],
362: \%Apache::hint::rank,
363: $parstack,$safeeval);
1.1 albertel 364: } elsif ($target eq 'edit') {
365: $result=&Apache::edit::end_table();
366: }
367: return $result;
368: }
369:
370: sub insert_conceptgroup {
371: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
372: return $result;
373: }
374:
375: sub start_foil {
376: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
377: my $result='';
1.25 albertel 378: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 379: &Apache::lonxml::startredirection;
380: } elsif ($target eq 'edit') {
381: $result=&Apache::edit::tag_start($target,$token,"Foil");
382: my $level='-2';
383: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
384: $result.=&Apache::edit::text_arg('Name:','name',$token);
385: $result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
386: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
387: $safeeval,'-3');
388: if ($randomize ne 'no') {
389: $result.=&Apache::edit::select_arg('Location:','location',
390: ['random','top','bottom'],$token);
391: }
392: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
393: } elsif ($target eq 'modified') {
394: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
395: $safeeval,'value',
396: 'name','location');
397: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
398: }
399: return $result;
400: }
401:
402: sub end_foil {
403: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
404: my $text ='';
405: my $result = '';
1.25 albertel 406: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 407: $text=&Apache::lonxml::endredirection;
408: }
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: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
412: if ($value ne 'unused') {
413: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
414: &Apache::lonxml::debug("Got a name of :$name:");
415: if (!$name) { $name=$Apache::lonxml::curdepth; }
416: &Apache::lonxml::debug("Using a name of :$name:");
1.30 albertel 417: if (defined($Apache::response::foilnames{$name})) {
418: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
419: }
1.31 albertel 420: $Apache::response::foilnames{$name}++;
1.30 albertel 421: my $location =&Apache::lonxml::get_param('location',$parstack,
422: $safeeval);
1.1 albertel 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>