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