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