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