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