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