1: # The LearningOnline Network with CAPA
2: # option list style responses
3: # 2/21 Guy
4: package Apache::optionresponse;
5: use strict;
6: use Apache::response;
7:
8: sub BEGIN {
9: &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
10: }
11:
12: sub start_optionresponse {
13: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
14: my $result='';
15: #when in a radiobutton response use these
16: &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
17: push (@Apache::lonxml::namespace,'optionresponse');
18: my $id = &Apache::response::start_response($parstack,$safeeval);
19: if ($target eq 'edit') {
20: $result.="<table width=\"100%\" border=\"2\"><tr><td>Multiple Option Response Question</td>
21: <td>Delete:".
22: &Apache::edit::deletelist($target,$token)
23: ."</td></tr><tr><td colspan=\"3\">\n";
24: }
25:
26: return $result;
27: }
28:
29: sub end_optionresponse {
30: &Apache::response::end_response;
31: pop @Apache::lonxml::namespace;
32: return '';
33: }
34:
35: sub insert_optionresponse {
36: return '
37: <optionresponse max="10">
38: <foilgroup options=\"\">
39: </foilgroup>
40: </optionresponse>';
41: }
42:
43: %Apache::response::foilgroup={};
44: sub start_foilgroup {
45: %Apache::response::foilgroup={};
46: $Apache::optionresponse::conceptgroup=0;
47: &Apache::response::setrandomnumber();
48: return '';
49: }
50:
51: sub end_foilgroup {
52: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
53:
54: my $result;
55: if ($target ne 'meta') {
56: my $name;
57: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
58: if ($count>$max) { $count=$max }
59: &Apache::lonxml::debug("Count is $count from $max");
60: my $args ='';
61: if ( $#$parstack > 0 ) { $args=$$parstack['-1']; }
62: my @opt;
63: eval '@opt ='.&Apache::run::run("{$args;".'return $options}',$safeeval);
64: if ($target eq 'web') {
65: $result=&displayfoils($count,@opt);
66: } elsif ( $target eq 'grade') {
67: if ( defined $ENV{'form.submitted'}) {
68: my @whichopt = &whichfoils($count);
69: my $temp=1;my $name;
70: my $allresponse;
71: my $right=0;
72: my $wrong=0;
73: my $ignored=0;
74: foreach $name (@whichopt) {
75: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
76: $allresponse.="$response:";
77: if ( $response =~ /[^\s]/) {
78: &Apache::lonxml::debug("submitted a $response<br />\n");
79: my $value=$Apache::response::foilgroup{$name.'.value'};
80: if ($value eq $response) {$right++;} else {$wrong++;}
81: } else {
82: $ignored++;
83: }
84: $temp++;
85: }
86: my $id = $Apache::inputtags::response['-1'];
87: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
88: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
89: if ($wrong==0 && $ignored==0) {
90: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
91: } else {
92: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
93: }
94: }
95: }
96: }
97: return $result;
98: }
99:
100: sub getfoilcounts {
101: my ($parstack,$safeeval)=@_;
102: my $rrargs ='';
103: if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
104: my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
105: # +1 since instructors will count from 1
106: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
107: return ($count,$max);
108: }
109:
110: sub whichfoils {
111: my ($max)=@_;
112: my @names = @{ $Apache::response::foilgroup{'names'} };
113: my @whichopt =();
114: while ((($#whichopt+1) < $max) && ($#names > -1)) {
115: &Apache::lonxml::debug("Have $#whichopt max is $max");
116: my $aopt=int(rand($#names+1));
117: &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
118: $aopt=splice(@names,$aopt,1);
119: &Apache::lonxml::debug("Picked $aopt");
120: push (@whichopt,$aopt);
121: }
122: return @whichopt;
123: }
124:
125: sub displayfoils {
126: my ($max,@opt)=@_;
127: my @names = @{ $Apache::response::foilgroup{'names'} };
128: my @truelist;
129: my @falselist;
130: my $result;
131: my $name;
132: my @whichopt = &whichfoils($max);
133: my $optionlist="<option></option>\n";
134: my $option;
135: foreach $option (@opt) {
136: $optionlist.="<option>$option</option>\n";
137: }
138: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
139: foreach $name (@whichopt) {
140: $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
141: ":".$Apache::response::foilgroup{$name.'.text'}."\n";
142: }
143: } else {
144: my $temp=1;
145: foreach $name (@whichopt) {
146: $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
147: .$optionlist
148: ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
149: $temp++;
150: }
151: }
152: return $result."<br />";
153: }
154:
155:
156: sub start_conceptgroup {
157: $Apache::optionresponse::conceptgroup=1;
158: %Apache::response::conceptgroup={};
159: return '';
160: }
161:
162: sub end_conceptgroup {
163: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
164: $Apache::optionresponse::conceptgroup=0;
165: if ($target eq 'web' || $target eq 'grade') {
166: my @names = @{ $Apache::response::conceptgroup{'names'} };
167: my $pick=int rand $#names+1;
168: my $name=$names[$pick];
169: push @{ $Apache::response::foilgroup{'names'} }, $name;
170: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
171: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
172: my $args;
173: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
174: my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
175: $Apache::response::foilgroup{"$name.concept"} = $concept;
176: &Apache::lonxml::debug("Selecting $name in $concept");
177: }
178: return '';
179: }
180:
181: sub start_foil {
182: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
183: if ($target eq 'web') { &Apache::lonxml::startredirection; }
184: return '';
185: }
186:
187: sub end_foil {
188: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
189: my $text ='';
190: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
191: if ($target eq 'web' || $target eq 'grade') {
192: my $args ='';
193: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
194: my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
195: if ($value ne 'unused') {
196: my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
197: if ( $Apache::optionresponse::conceptgroup ) {
198: push @{ $Apache::response::conceptgroup{'names'} }, $name;
199: $Apache::response::conceptgroup{"$name.value"} = $value;
200: $Apache::response::conceptgroup{"$name.text"} = $text;
201: } else {
202: push @{ $Apache::response::foilgroup{'names'} }, $name;
203: $Apache::response::foilgroup{"$name.value"} = $value;
204: $Apache::response::foilgroup{"$name.text"} = $text;
205: }
206: }
207: }
208: if ($target eq 'edit') {
209: my $args ='';
210: if ( $#$parstack > 1 ) { $args=$$parstack['-2']; }
211: my $options=&Apache::run::run("{$args;".'return $options}',$safeeval);
212: if (!$options && $#$parstack > 2 ) { $args=$$parstack['-2']; }
213: my @opt;
214: #eval '@opt ='.
215: }
216: return '';
217: }
218:
219: sub insert_foil {
220: return '
221: <foil name="" value="">
222: </foil>';
223: }
224: 1;
225: __END__
226:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>