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,$tagstack,$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.=&Apache::edit::start_table($token)."<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: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
31: &Apache::response::end_response;
32: pop @Apache::lonxml::namespace;
33: my $result;
34: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
35: return $result;
36: }
37:
38: sub insert_optionresponse {
39: return '
40: <optionresponse max="10">
41: <foilgroup options=\"\">
42: </foilgroup>
43: </optionresponse>';
44: }
45:
46: %Apache::response::foilgroup={};
47: sub start_foilgroup {
48: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
49:
50: my $result='';
51: %Apache::response::foilgroup={};
52: $Apache::optionresponse::conceptgroup=0;
53: &Apache::response::setrandomnumber();
54: if ($target eq 'edit') {
55: my $optionlist="<option></option>\n";
56: my $option;
57: my @opt;
58: eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
59: my $count=0;
60: foreach $option (@opt) {
61: $optionlist.="<option value=\"$count\">$option</option>\n";
62: }
63: my $insertlist=&Apache::edit::insertlist($target,$token);
64: $result.=&Apache::edit::start_table($token);
65: $result.= (<<ENDTABLE);
66: <tr><td>Select Options</td>
67: <td>
68: Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
69: </td>
70: <td>Delete an Option:
71: <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
72: </td>
73: </tr>
74: <tr><td colspan="3">$insertlist<br />
75: ENDTABLE
76: }
77: if ($target eq 'modified') {
78: my @options;
79: my $optchanged=0;
80: eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
81: if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
82: my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
83: splice(@options,$delopt,1);
84: $optchanged=1;
85: }
86: if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
87: my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
88: if ($options[0]) {
89: push(@options,$newopt);
90: } else {
91: $options[0]=$newopt;
92: }
93: $optchanged=1;
94: }
95: if ($optchanged) {
96: $result = "<foilgroup options=\"(";
97: foreach my $option (@options) {
98: $option=~s/\'/\\\'/;
99: &Apache::lonxml::debug("adding option :$option:");
100: $result .="'".$option."',";
101: }
102: chop $result;
103: $result.=')">';
104: } # else nothing changed so just use the default mechanism
105: }
106: return $result;
107: }
108:
109: sub end_foilgroup {
110: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
111:
112: my $result;
113: if ($target eq 'grade' || $target eq 'web') {
114: my $name;
115: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
116: if ($count>$max) { $count=$max }
117: &Apache::lonxml::debug("Count is $count from $max");
118: my @opt;
119: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
120: &Apache::lonxml::debug("Options are $#opt");
121: if ($target eq 'web') {
122: $result=&displayfoils($count,@opt);
123: } elsif ( $target eq 'grade') {
124: if ( defined $ENV{'form.submitted'}) {
125: my @whichopt = &whichfoils($count);
126: my $temp=1;my $name;
127: my $allresponse;
128: my $right=0;
129: my $wrong=0;
130: my $ignored=0;
131: foreach $name (@whichopt) {
132: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
133: $allresponse.="$response:";
134: if ( $response =~ /[^\s]/) {
135: &Apache::lonxml::debug("submitted a $response<br />\n");
136: my $value=$Apache::response::foilgroup{$name.'.value'};
137: if ($value eq $response) {$right++;} else {$wrong++;}
138: } else {
139: $ignored++;
140: }
141: $temp++;
142: }
143: my $id = $Apache::inputtags::response['-1'];
144: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
145: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
146: if ($wrong==0 && $ignored==0) {
147: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
148: } else {
149: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
150: }
151: }
152: }
153: }
154: if ($target eq 'edit') {
155: $result.=&Apache::edit::end_table();
156: }
157: return $result;
158: }
159:
160: sub getfoilcounts {
161: my ($parstack,$safeeval)=@_;
162: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
163: # +1 since instructors will count from 1
164: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
165: return ($count,$max);
166: }
167:
168: sub whichfoils {
169: my ($max)=@_;
170: my @names = @{ $Apache::response::foilgroup{'names'} };
171: my @whichopt =();
172: while ((($#whichopt+1) < $max) && ($#names > -1)) {
173: &Apache::lonxml::debug("Have $#whichopt max is $max");
174: my $aopt=int(rand($#names+1));
175: &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
176: $aopt=splice(@names,$aopt,1);
177: &Apache::lonxml::debug("Picked $aopt");
178: push (@whichopt,$aopt);
179: }
180: return @whichopt;
181: }
182:
183: sub displayfoils {
184: my ($max,@opt)=@_;
185: my @names = @{ $Apache::response::foilgroup{'names'} };
186: my @truelist;
187: my @falselist;
188: my $result;
189: my $name;
190: my @whichopt = &whichfoils($max);
191: my $optionlist="<option></option>\n";
192: my $option;
193: foreach $option (@opt) {
194: $optionlist.="<option>$option</option>\n";
195: }
196: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
197: foreach $name (@whichopt) {
198: $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
199: ":".$Apache::response::foilgroup{$name.'.text'}."\n";
200: }
201: } else {
202: my $temp=1;
203: foreach $name (@whichopt) {
204: $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
205: .$optionlist
206: ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
207: $temp++;
208: }
209: }
210: return $result."<br />";
211: }
212:
213:
214: sub start_conceptgroup {
215: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
216: $Apache::optionresponse::conceptgroup=1;
217: %Apache::response::conceptgroup={};
218: my $result;
219: if ($target eq 'edit') {
220: my $insertlist=&Apache::edit::insertlist($target,$token);
221: $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
222: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
223: $result.="Concept: <input name=\"$Apache::lonxml::curdepth.concept\" type=\"text\" value=\"$concept\" /></td></tr><tr><td colspan=\"3\">";
224: }
225: return $result;
226: }
227:
228: sub end_conceptgroup {
229: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
230: $Apache::optionresponse::conceptgroup=0;
231: my $result='';
232: if ($target eq 'web' || $target eq 'grade') {
233: my @names = @{ $Apache::response::conceptgroup{'names'} };
234: my $pick=int rand $#names+1;
235: my $name=$names[$pick];
236: push @{ $Apache::response::foilgroup{'names'} }, $name;
237: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
238: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
239: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
240: $Apache::response::foilgroup{"$name.concept"} = $concept;
241: &Apache::lonxml::debug("Selecting $name in $concept");
242: }
243: if ($target eq 'edit') {
244: $result=&Apache::edit::end_table();
245: }
246: return $result;
247: }
248:
249: sub start_foil {
250: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
251: my $result='';
252: if ($target eq 'web') { &Apache::lonxml::startredirection; }
253: if ($target eq 'edit') {
254: $result=&Apache::edit::tag_start($target,$token,"Foil");
255: my $level='-2';
256: if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
257: my @opt;
258: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
259: my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval);
260: my $optionlist="<option>unused</option>\n";
261: foreach my $option (@opt) {
262: if ( $value eq $option ) {
263: $optionlist.="<option selected=\"on\">$option</option>\n";
264: } else {
265: $optionlist.="<option>$option</option>\n";
266: }
267: }
268: my $name=&Apache::lonxml::get_param('name',$parstack,$safeeval);
269: $result.="Name: <input name=\"$Apache::lonxml::curdepth.name\" type=\"text\" value=\"$name\" />".
270: "Correct Option:
271: <select name=\"$Apache::lonxml::curdepth.value\">
272: $optionlist
273: </select></td></tr><tr><td colspan=\"3\">";
274: }
275: if ($target eq 'modified') {
276: my $constructtag=0;
277: foreach my $arg ('value','name') {
278: my $value=&Apache::lonxml::get_param($arg,$parstack,$safeeval);
279: my $newvalue=$ENV{"form.$Apache::lonxml::curdepth.$arg"};
280: &Apache::lonxml::debug(" for:$arg: cur is :$value: new is :$newvalue:");
281: if ($value ne $newvalue) {
282: $token->[2]->{$arg}=$newvalue;
283: $constructtag=1;
284: }
285: }
286: if ($constructtag) {
287: $result = '<'.$token->[1];
288: while (my ($key,$val)= each(%{$token->[2]})) {
289: &Apache::lonxml::debug("setting :$key: to :$val:");
290: $result.=' '.$key.'="'.$val.'"';
291: }
292: $result.=">";
293: }
294: }
295: return $result;
296: }
297:
298: sub end_foil {
299: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
300: my $text ='';
301: my $result = '';
302: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
303: if ($target eq 'web' || $target eq 'grade') {
304: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
305: if ($value ne 'unused') {
306: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
307: if (!$name) { $name=$Apache::lonxml::curdepth; }
308: if ( $Apache::optionresponse::conceptgroup ) {
309: push @{ $Apache::response::conceptgroup{'names'} }, $name;
310: $Apache::response::conceptgroup{"$name.value"} = $value;
311: $Apache::response::conceptgroup{"$name.text"} = $text;
312: } else {
313: push @{ $Apache::response::foilgroup{'names'} }, $name;
314: $Apache::response::foilgroup{"$name.value"} = $value;
315: $Apache::response::foilgroup{"$name.text"} = $text;
316: }
317: }
318: }
319: if ($target eq 'edit') {
320: $result.= &Apache::edit::tag_end($target,$token,'');
321: }
322: return $result;
323: }
324:
325: sub insert_foil {
326: return '
327: <foil name="" value="unused">
328: <startouttext />
329: <endouttext />
330: </foil>';
331: }
332: 1;
333: __END__
334:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>