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