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