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