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.="<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: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
46:
47: my $result='';
48: %Apache::response::foilgroup={};
49: $Apache::optionresponse::conceptgroup=0;
50: &Apache::response::setrandomnumber();
51: if ($target eq 'edit') {
52: my $optionlist="<option></option>\n";
53: my $option;
54: my @opt;
55: eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
56:
57: foreach $option (@opt) {
58: $optionlist.="<option>$option</option>\n";
59: }
60: my $insertlist=&Apache::edit::insertlist($target,$token);
61: $result.= (<<ENDTABLE);
62: <table width="100%" border="2">
63: <tr><td>Select Options</td>
64: <td>
65: Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
66: </td>
67: <td>Delete an Option:
68: <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
69: </td>
70: </tr>
71: <tr><td colspan="3">$insertlist<br />
72: ENDTABLE
73: }
74: if ($target eq 'modified') {
75: my @options;
76: eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
77: if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
78: my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
79: $newopt =~ s/\'/\\\'/g;
80: if ($options[0]) {
81: push(@options,$ENV{"form.$Apache::lonxml::curdepth.options"});
82: } else {
83: $options[0]=$ENV{"form.$Apache::lonxml::curdepth.options"};
84: }
85: $result = "<foilgroup options=\"(";
86: foreach my $option (@options) {
87: &Apache::lonxml::debug("adding option :$option:");
88: $result .="'".$option."',";
89: }
90: chop $result;
91: $result.=')">';
92: }
93: }
94: return $result;
95: }
96:
97: sub end_foilgroup {
98: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
99:
100: my $result;
101: if ($target ne 'meta') {
102: my $name;
103: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
104: if ($count>$max) { $count=$max }
105: &Apache::lonxml::debug("Count is $count from $max");
106: my @opt;
107: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
108: &Apache::lonxml::debug("Options are $#opt");
109: if ($target eq 'web') {
110: $result=&displayfoils($count,@opt);
111: } elsif ( $target eq 'grade') {
112: if ( defined $ENV{'form.submitted'}) {
113: my @whichopt = &whichfoils($count);
114: my $temp=1;my $name;
115: my $allresponse;
116: my $right=0;
117: my $wrong=0;
118: my $ignored=0;
119: foreach $name (@whichopt) {
120: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
121: $allresponse.="$response:";
122: if ( $response =~ /[^\s]/) {
123: &Apache::lonxml::debug("submitted a $response<br />\n");
124: my $value=$Apache::response::foilgroup{$name.'.value'};
125: if ($value eq $response) {$right++;} else {$wrong++;}
126: } else {
127: $ignored++;
128: }
129: $temp++;
130: }
131: my $id = $Apache::inputtags::response['-1'];
132: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
133: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
134: if ($wrong==0 && $ignored==0) {
135: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
136: } else {
137: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
138: }
139: }
140: }
141: }
142: return $result;
143: }
144:
145: sub getfoilcounts {
146: my ($parstack,$safeeval)=@_;
147: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
148: # +1 since instructors will count from 1
149: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
150: return ($count,$max);
151: }
152:
153: sub whichfoils {
154: my ($max)=@_;
155: my @names = @{ $Apache::response::foilgroup{'names'} };
156: my @whichopt =();
157: while ((($#whichopt+1) < $max) && ($#names > -1)) {
158: &Apache::lonxml::debug("Have $#whichopt max is $max");
159: my $aopt=int(rand($#names+1));
160: &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
161: $aopt=splice(@names,$aopt,1);
162: &Apache::lonxml::debug("Picked $aopt");
163: push (@whichopt,$aopt);
164: }
165: return @whichopt;
166: }
167:
168: sub displayfoils {
169: my ($max,@opt)=@_;
170: my @names = @{ $Apache::response::foilgroup{'names'} };
171: my @truelist;
172: my @falselist;
173: my $result;
174: my $name;
175: my @whichopt = &whichfoils($max);
176: my $optionlist="<option></option>\n";
177: my $option;
178: foreach $option (@opt) {
179: $optionlist.="<option>$option</option>\n";
180: }
181: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
182: foreach $name (@whichopt) {
183: $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
184: ":".$Apache::response::foilgroup{$name.'.text'}."\n";
185: }
186: } else {
187: my $temp=1;
188: foreach $name (@whichopt) {
189: $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
190: .$optionlist
191: ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
192: $temp++;
193: }
194: }
195: return $result."<br />";
196: }
197:
198:
199: sub start_conceptgroup {
200: $Apache::optionresponse::conceptgroup=1;
201: %Apache::response::conceptgroup={};
202: return '';
203: }
204:
205: sub end_conceptgroup {
206: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
207: $Apache::optionresponse::conceptgroup=0;
208: if ($target eq 'web' || $target eq 'grade') {
209: my @names = @{ $Apache::response::conceptgroup{'names'} };
210: my $pick=int rand $#names+1;
211: my $name=$names[$pick];
212: push @{ $Apache::response::foilgroup{'names'} }, $name;
213: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
214: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
215: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
216: $Apache::response::foilgroup{"$name.concept"} = $concept;
217: &Apache::lonxml::debug("Selecting $name in $concept");
218: }
219: return '';
220: }
221:
222: sub start_foil {
223: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
224: my $result='';
225: if ($target eq 'web') { &Apache::lonxml::startredirection; }
226: if ($target eq 'edit') {
227: $result=&Apache::edit::tag_start($target,$token,"Foil");
228: my $options= &Apache::lonxml::get_param('options',$parstack,$safeeval);
229: my @opt;
230: eval '@opt ='.$options;
231: }
232: return $result;
233: }
234:
235: sub end_foil {
236: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
237: my $text ='';
238: my $result = '';
239: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
240: if ($target eq 'web' || $target eq 'grade') {
241: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
242: if ($value ne 'unused') {
243: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
244: if (!$name) { $name=$Apache::lonxml::curdepth; }
245: if ( $Apache::optionresponse::conceptgroup ) {
246: push @{ $Apache::response::conceptgroup{'names'} }, $name;
247: $Apache::response::conceptgroup{"$name.value"} = $value;
248: $Apache::response::conceptgroup{"$name.text"} = $text;
249: } else {
250: push @{ $Apache::response::foilgroup{'names'} }, $name;
251: $Apache::response::foilgroup{"$name.value"} = $value;
252: $Apache::response::foilgroup{"$name.text"} = $text;
253: }
254: }
255: }
256: if ($target eq 'edit') {
257: $result.= &Apache::edit::tag_end($target,$token,'');
258: }
259: return $result;
260: }
261:
262: sub insert_foil {
263: return '
264: <foil name="" value="">
265: <starttextarea />
266: <endtextarea />
267: </foil>';
268: }
269: 1;
270: __END__
271:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>