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