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