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