1: # The LearningOnline Network with CAPA
2: # rank style response
3: #
4: # $Id: rankresponse.pm,v 1.1 2003/01/19 08:13:04 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # 2/21 Guy
29:
30: package Apache::rankresponse;
31: use strict;
32: use HTML::Entities();
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=();
47: if ($target eq 'meta') {
48: $result=&Apache::response::meta_package_write('rankresponse');
49: } elsif ($target eq 'edit' ) {
50: $result.=&Apache::edit::start_table($token).
51: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
52: &Apache::edit::deletelist($target,$token)
53: ."</td><td> ".&Apache::edit::end_row()
54: .&Apache::edit::start_spanning_row();
55:
56: $result.=
57: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
58: &Apache::edit::select_arg('Randomize Foil Order','randomize',
59: ['yes','no'],$token).
60: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
61: } elsif ($target eq 'modified') {
62: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
63: $safeeval,'max',
64: 'randomize');
65: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
66: }
67: return $result;
68: }
69:
70: sub end_rankresponse {
71: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
72: my $result;
73: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
74: &Apache::response::end_response;
75: pop @Apache::lonxml::namespace;
76: &Apache::lonxml::deregister('Apache::rankresponse',
77: ('foilgroup','foil','conceptgroup'));
78: return $result;
79: }
80:
81: %Apache::response::foilgroup=();
82: sub start_foilgroup {
83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $result;
85: %Apache::response::foilgroup=();
86: $Apache::rankresponse::conceptgroup=0;
87: &Apache::response::setrandomnumber();
88: return $result;
89: }
90:
91: sub end_foilgroup {
92: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
93: my $result;
94: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
95: my $style = $Apache::lonhomework::type;
96: my $name;
97: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
98: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
99: $safeeval,'-2');
100: my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
101: if (!defined($tol)) { $tol=0; }
102: if ($target eq 'web') {
103: $result=&displayfoils($target,$max,$randomize,$tol);
104: } elsif ($target eq 'answer' ) {
105: $result=&displayanswers($max,$randomize,$tol);
106: } elsif ( $target eq 'grade') {
107: &grade_response($max,$randomize,$tol);
108: }
109: }
110: return $result;
111: }
112:
113: sub get_correct_order {
114: my ($tol,@foils) =@_;
115: my @correctorder;
116: my @value_names;
117: foreach my $name (@foils) {
118: my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
119: push(@value_names,\@pair);
120: }
121: @value_names =
122: sort {
123: if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] cmp $b->[0]);}
124: return 0;
125: } @value_names;
126: my @value_names_tmp=@value_names;
127: my $firstpair=shift(@value_names_tmp);
128: my $order=1;
129: my %order;
130: my $count=1;
131: my $lastvalue=$firstpair->[0];
132: $order{$firstpair->[1]}=$order;
133: foreach my $pair (@value_names_tmp) {
134: $count++;
135: if (abs($pair->[0]-$lastvalue) > $tol ) {
136: $order=$count;
137: }
138: $order{$pair->[1]}=$order;
139: $lastvalue=$pair->[0];
140: }
141: foreach my $name (@foils) {
142: push(@correctorder,$order{$name});
143: }
144: &Apache::lonhomework::showhash('b' => \@value_names);
145: &Apache::lonhomework::showhash('b' => \@correctorder);
146: return @correctorder;
147: }
148:
149: sub displayanswers {
150: my ($max,$randomize,$tol,@opt)=@_;
151: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
152: my @names = @{ $Apache::response::foilgroup{'names'} };
153: my @whichfoils = &whichfoils($max,$randomize);
154: my $result=&Apache::response::answer_header('rankresponse');
155: my @correctorder=&get_correct_order($tol,@whichfoils);
156: foreach my $order (@correctorder) {
157: $result.=&Apache::response::answer_part('rankresponse',$order);
158: }
159: $result.=&Apache::response::answer_footer('rankresponse');
160: return $result;
161: }
162:
163: sub check_response_order {
164: my (%responsehash)=@_;
165: my @order=sort(values(%responsehash));
166: my $lastvalue=0;
167: my $expected=1;
168: my $malformed=0;
169: foreach my $current (@order) {
170: &Apache::lonxml::debug("$lastvalue $expected $malformed");
171: if (!($current == $lastvalue || $current == $expected)) {
172: $malformed=1;
173: }
174: $expected++;
175: $lastvalue=$current;
176: }
177: return $malformed;
178: }
179:
180: sub grade_response {
181: my ($max,$randomize,$tol)=@_;
182: my (@whichfoils)=&whichfoils($max,$randomize);
183: if (!defined($ENV{'form.submitted'})) { return; }
184: my %responsehash;
185: my %grade;
186: my ($temp,$right,$wrong,$ignored)=(0,0,0,0);
187: my @correctorder=&get_correct_order($tol,@whichfoils);
188: foreach my $name (@whichfoils) {
189: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
190: $responsehash{$name}=$response;
191: my $value=shift(@correctorder);
192: if ( $response =~ /[^\s]/) {
193: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
194: if ($value eq $response) {
195: $grade{$name}='1'; $right++;
196: } else {
197: $grade{$name}='0'; $wrong++;
198: }
199: } else {
200: $ignored++;
201: }
202: $temp++;
203: }
204: my $malformed=&check_response_order(%responsehash);
205: my $part=$Apache::inputtags::part;
206: my $id = $Apache::inputtags::response['-1'];
207: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
208: my $gradestr =&Apache::lonnet::hash2str(%grade);
209: my %previous=&Apache::response::check_for_previous($responsestr,
210: $part,$id);
211: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
212: my $ad;
213: if ($malformed) {
214: $ad='MISORDERED_RANK';
215: } elsif ($wrong==0 && $ignored==0) {
216: $ad='EXACT_ANS';
217: } elsif ($wrong==0 && $right==0) {
218: #nothing submitted
219: } else {
220: if ($ignored==0) {
221: $ad='INCORRECT';
222: } else {
223: $ad='MISSING_ANSWER';
224: }
225: }
226: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
227: $responsestr;
228: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
229: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
230: &Apache::response::handle_previous(\%previous,$ad);
231: }
232:
233: sub displayfoils {
234: my ($target,$max,$randomize,$tol)=@_;
235: my $result;
236: my (@whichfoils)=&whichfoils($max,$randomize);
237: my $part=$Apache::inputtags::part;
238: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
239: my $status=$Apache::inputtags::status[-1];
240: my @whichopt=(1..($#whichfoils+1));
241: my @correctorder=&get_correct_order($tol,@whichfoils);
242: if (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) {
243: foreach my $name (@whichfoils) {
244: my $text=$Apache::response::foilgroup{$name.'.text'};
245: my $value=shift(@correctorder);
246: $result.='<br />'.$value.':'.$text;
247: }
248: } else {
249: my $i = 0;
250: my $temp=0;
251: my $id=$Apache::inputtags::response[-1];
252: my $part=$Apache::inputtags::part;
253: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
254: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
255: foreach my $name (@whichfoils) {
256: my $lastopt=$lastresponse{$name};
257: my $optionlist="<option></option>\n";
258: my $option;
259: foreach $option (@whichopt) {
260: if ($option eq $lastopt) {
261: $optionlist.="<option selected=\"on\">$option</option>\n";
262: } else {
263: $optionlist.="<option>$option</option>\n";
264: }
265: }
266: $optionlist='<select name="HWVAL_'.
267: $Apache::inputtags::response[-1].':'.$temp.'">'.
268: $optionlist."</select>\n";
269: my $text=$Apache::response::foilgroup{$name.'.text'};
270: $result.='<br />'.$optionlist.$text."\n";
271: $temp++;
272: }
273: }
274: $result.="<br />";
275: return $result;
276: }
277:
278: sub getfoilcounts {
279: my ($max)=@_;
280: # +1 since instructors will count from 1
281: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
282: if (&Apache::response::showallfoils()) { $max=$count; }
283: if ($count>$max) { $count=$max }
284: &Apache::lonxml::debug("Count is $count from $max");
285: return $count;
286: }
287:
288: sub whichfoils {
289: my ($max,$randomize)=@_;
290: $max = &getfoilcounts($max);
291: # &Apache::lonxml::debug("man $max randomize $randomize");
292: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
293: my @names = @{ $Apache::response::foilgroup{'names'} };
294: my @whichopt =();
295: my (%top,@toplist,%bottom,@bottomlist);
296: if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
297: my $current=0;
298: foreach my $name (@names) {
299: $current++;
300: if ($Apache::response::foilgroup{"$name.location"} eq 'top') {
301: $top{$name}=$current;
302: } elsif ($Apache::response::foilgroup{"$name.location"} eq
303: 'bottom') {
304: $bottom{$name}=$current;
305: }
306: }
307: }
308: while ((($#whichopt+1) < $max) && ($#names > -1)) {
309: # &Apache::lonxml::debug("Have $#whichopt max is $max");
310: my $aopt;
311: if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
312: $aopt=0;
313: } else {
314: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
315: }
316: # &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
317: $aopt=splice(@names,$aopt,1);
318: # &Apache::lonxml::debug("Picked $aopt");
319: if ($top{$aopt}) {
320: $toplist[$top{$aopt}]=$aopt;
321: } elsif ($bottom{$aopt}) {
322: $bottomlist[$bottom{$aopt}]=$aopt;
323: } else {
324: push (@whichopt,$aopt);
325: }
326: }
327: # &Apache::lonxml::debug("Grr, ".$#whichopt.":".$#toplist.':'.$#bottomlist);
328: for (my $i=0;$i<=$#toplist;$i++) {
329: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
330: }
331: for (my $i=0;$i<=$#bottomlist;$i++) {
332: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
333: }
334: &Apache::lonxml::debug("Grr, ".$#whichopt.":".$#toplist.':'.$#bottomlist);
335: return @whichopt;
336: }
337:
338: sub start_conceptgroup {
339: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
340: $Apache::rankresponse::conceptgroup=1;
341: %Apache::response::conceptgroup=();
342: my $result;
343: if ($target eq 'edit') {
344: $result.=&Apache::edit::tag_start($target,$token,
345: "Concept Grouped Foils");
346: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
347: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
348: }
349: if ($target eq 'modified') {
350: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
351: $safeeval,'concept');
352: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
353: }
354: return $result;
355: }
356:
357: sub end_conceptgroup {
358: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
359: $Apache::rankresponse::conceptgroup=0;
360: my $result='';
361: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
362: #if not there aren't any foils to display and thus no question
363: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
364: my @names = @{ $Apache::response::conceptgroup{'names'} };
365: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
366: my $name=$names[$pick];
367: push @{ $Apache::response::foilgroup{'names'} }, $name;
368: $Apache::response::foilgroup{"$name.value"} =
369: $Apache::response::conceptgroup{"$name.value"};
370: $Apache::response::foilgroup{"$name.text"} =
371: $Apache::response::conceptgroup{"$name.text"};
372: $Apache::response::foilgroup{"$name.location"} =
373: $Apache::response::conceptgroup{"$name.location"};
374: my $concept = &Apache::lonxml::get_param('concept',$parstack,
375: $safeeval);
376: $Apache::response::foilgroup{"$name.concept"} = $concept;
377: &Apache::lonxml::debug("Selecting $name in $concept");
378: if ($target eq 'web') {
379: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
380: push(@{ $Apache::hint::rank{"$part_id.concepts"} },
381: $concept);
382: $Apache::hint::rank{"$part_id.concept.$concept"}=
383: $Apache::response::conceptgroup{'names'};
384: }
385: }
386: } elsif ($target eq 'edit') {
387: $result=&Apache::edit::end_table();
388: }
389: return $result;
390: }
391:
392: sub insert_conceptgroup {
393: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
394: return $result;
395: }
396:
397: sub start_foil {
398: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
399: my $result='';
400: if ($target eq 'web' ) {
401: &Apache::lonxml::startredirection;
402: } elsif ($target eq 'edit') {
403: $result=&Apache::edit::tag_start($target,$token,"Foil");
404: my $level='-2';
405: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
406: $result.=&Apache::edit::text_arg('Name:','name',$token);
407: $result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
408: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
409: $safeeval,'-3');
410: if ($randomize ne 'no') {
411: $result.=&Apache::edit::select_arg('Location:','location',
412: ['random','top','bottom'],$token);
413: }
414: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
415: } elsif ($target eq 'modified') {
416: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
417: $safeeval,'value',
418: 'name','location');
419: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
420: }
421: return $result;
422: }
423:
424: sub end_foil {
425: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
426: my $text ='';
427: my $result = '';
428: if ($target eq 'web') {
429: $text=&Apache::lonxml::endredirection;
430: }
431: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
432: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
433: if ($value ne 'unused') {
434: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
435: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
436: &Apache::lonxml::debug("Got a name of :$name:");
437: if (!$name) { $name=$Apache::lonxml::curdepth; }
438: &Apache::lonxml::debug("Using a name of :$name:");
439: if ( $Apache::rankresponse::conceptgroup
440: && !&Apache::response::showallfoils() ) {
441: push @{ $Apache::response::conceptgroup{'names'} }, $name;
442: $Apache::response::conceptgroup{"$name.value"} = $value;
443: $Apache::response::conceptgroup{"$name.text"} = $text;
444: $Apache::response::conceptgroup{"$name.location"} = $location;
445: } else {
446: push @{ $Apache::response::foilgroup{'names'} }, $name;
447: $Apache::response::foilgroup{"$name.value"} = $value;
448: $Apache::response::foilgroup{"$name.text"} = $text;
449: $Apache::response::foilgroup{"$name.location"} = $location;
450: }
451: }
452: }
453: if ($target eq 'edit') {
454: $result.= &Apache::edit::tag_end($target,$token,'');
455: }
456: return $result;
457: }
458:
459: sub insert_foil {
460: return '
461: <foil name="" value="unused">
462: <startouttext />
463: <endouttext />
464: </foil>';
465: }
466: 1;
467: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>