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