1: # LearningOnline Network with CAPA
2: # option list style responses
3: #
4: # $Id: optionresponse.pm,v 1.75 2003/04/21 22:09:47 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: %Apache::hint::option=();
46: if ($target eq 'edit') {
47: $result.=&Apache::edit::start_table($token).
48: "<tr><td>Multiple Option Response Question</td><td>Delete:".
49: &Apache::edit::deletelist($target,$token)
50: ."</td><td> ".
51: &Apache::edit::end_row().
52: &Apache::edit::start_spanning_row().
53: "\n";
54: $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
55: $token,'4').
56: &Apache::edit::select_arg('Randomize Foil Order','randomize',
57: ['yes','no'],$token).
58: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
59: } elsif ($target eq 'modified') {
60: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
61: $safeeval,'max','randomize');
62: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
63: } elsif ($target eq 'meta') {
64: $result=&Apache::response::meta_package_write('optionresponse');
65: } elsif ($target eq 'analyze') {
66: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
67: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
68: }
69: return $result;
70: }
71:
72: sub end_optionresponse {
73: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
74: &Apache::response::end_response;
75: pop @Apache::lonxml::namespace;
76: &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
77: my $result;
78: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
79: return $result;
80: }
81:
82: %Apache::response::foilgroup=();
83: sub start_foilgroup {
84: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
85:
86: my $result='';
87: %Apache::response::foilgroup=();
88: $Apache::optionresponse::conceptgroup=0;
89: &Apache::response::setrandomnumber();
90: if ($target eq 'edit') {
91: my $optionlist="<option></option>\n";
92: my $option;
93: my @opt;
94: eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
95: my $count=1;
96: foreach $option (@opt) {
97: $optionlist.="<option value=\"$count\">$option</option>\n";
98: $count++;
99: }
100: my $insertlist=&Apache::edit::insertlist($target,$token);
101: $result.=&Apache::edit::start_table($token);
102: $result.= (<<ENDTABLE);
103: <tr><td>Select Options</td>
104: <td>
105: Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
106: </td>
107: <td>Delete an Option:
108: <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
109: ENDTABLE
110: $result.= &Apache::edit::end_row();
111: $result.= &Apache::edit::start_spanning_row();
112: $result.= $insertlist.'<br />';
113: }
114: if ($target eq 'modified') {
115: my @options;
116: my $optchanged=0;
117: eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
118: if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
119: my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
120: &Apache::lonxml::debug("Deleting :$delopt:");
121: splice(@options,$delopt-1,1);
122: $optchanged=1;
123: }
124: if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
125: my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
126: if ($options[0]) {
127: push(@options,$newopt);
128: } else {
129: $options[0]=$newopt;
130: }
131: $optchanged=1;
132: }
133: if ($optchanged) {
134: $result = "<foilgroup options=\"(";
135: foreach my $option (@options) {
136: $option=~s/\'/\\\'/g;
137: &Apache::lonxml::debug("adding option :$option:");
138: $result .="'".$option."',";
139: }
140: chop $result;
141: $result.=')">';
142: } # else nothing changed so just use the default mechanism
143: }
144: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
145: $result .= ' \begin{enumerate} ';
146: }
147: return $result;
148: }
149:
150: sub end_foilgroup {
151: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
152:
153: my $result;
154: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
155: my $name;
156: my @opt;
157: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
158: &Apache::lonxml::debug("Options are $#opt");
159: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
160: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
161: $safeeval,'-2');
162: if ($target eq 'web' || $target eq 'tex') {
163: $result.=&displayfoils($target,$max,$randomize,@opt);
164: } elsif ( $target eq 'answer') {
165: $result.=&displayanswers($max,$randomize,@opt);
166: } elsif ( $target eq 'analyze') {
167: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
168: foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
169: if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
170: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
171: $Apache::lonhomework::analyze{"$part_id.foil.value.$name"} =
172: $Apache::response::foilgroup{"$name.value"};
173: $Apache::lonhomework::analyze{"$part_id.foil.text.$name"} =
174: $Apache::response::foilgroup{"$name.text"};
175: }
176: push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
177: push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} },&whichfoils($max,$randomize));
178: } elsif ( $target eq 'grade') {
179: if ( defined $ENV{'form.submitted'}) {
180: my @whichopt = &whichfoils($max,$randomize);
181: my $temp=1;my $name;
182: my %responsehash;
183: my %grade;
184: my $right=0;
185: my $wrong=0;
186: my $ignored=0;
187: foreach $name (@whichopt) {
188: my $response=&Apache::response::getresponse($temp);
189: if ($ENV{'form.submitted'} eq 'scantron') {
190: $response = $opt[$response];
191: }
192: $responsehash{$name}=$response;
193: if ( $response =~ /[^\s]/) {
194: my $value=$Apache::response::foilgroup{$name.'.value'};
195: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
196: if ($value eq $response) {
197: $grade{$name}='1'; $right++;
198: } else {
199: $grade{$name}='0'; $wrong++;
200: }
201: } else {
202: $ignored++;
203: }
204: $temp++;
205: }
206: my $part=$Apache::inputtags::part;
207: my $id = $Apache::inputtags::response['-1'];
208: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
209: my $gradestr =&Apache::lonnet::hash2str(%grade);
210: my %previous=&Apache::response::check_for_previous($responsestr,
211: $part,$id);
212: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
213: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
214: $responsestr;
215: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
216:
217: if (!$Apache::lonhomework::scantronmode) {
218: my $ad;
219: if ($wrong==0 && $ignored==0) {
220: $ad='EXACT_ANS';
221: } elsif ($wrong==0 && $right==0) {
222: #nothing submitted
223: } else {
224: if ($ignored==0) {
225: $ad='INCORRECT';
226: } else {
227: $ad='MISSING_ANSWER';
228: }
229: }
230: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
231: &Apache::response::handle_previous(\%previous,$ad);
232: } else {
233: my $ad;
234: if ($wrong==0 && $right==0) {
235: #nothing submitted
236: } else {
237: $ad='ASSIGNED_SCORE';
238: }
239: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
240: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
241: $right/(scalar(@whichopt));
242: }
243: }
244: }
245: &Apache::lonxml::increment_counter(&getfoilcounts($max));
246: } elsif ($target eq 'edit') {
247: $result.=&Apache::edit::end_table();
248: }
249: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
250: $result .= '\end{enumerate}';
251: }
252: return $result;
253: }
254:
255: sub getfoilcounts {
256: my ($max)=@_;
257: # +1 since instructors will count from 1
258: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
259: if (&Apache::response::showallfoils()) { $max=$count; }
260: if ($count>$max) { $count=$max }
261: &Apache::lonxml::debug("Count is $count from $max");
262: return $count;
263: }
264:
265: sub whichfoils {
266: my ($max,$randomize)=@_;
267: $max = &getfoilcounts($max);
268: &Apache::lonxml::debug("randomize $randomize");
269: if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
270: my @names = @{ $Apache::response::foilgroup{'names'} };
271: my @whichopt =();
272: my (%top,@toplist,%bottom,@bottomlist);
273: if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
274: my $current=0;
275: foreach my $name (@names) {
276: $current++;
277: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
278: $top{$name}=$current;
279: } elsif ($Apache::response::foilgroup{$name.'.location'} eq
280: 'bottom') {
281: $bottom{$name}=$current;
282: }
283: }
284: }
285: while ((($#whichopt+1) < $max) && ($#names > -1)) {
286: &Apache::lonxml::debug("Have $#whichopt max is $max");
287: my $aopt;
288: if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
289: $aopt=0;
290: } else {
291: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
292: }
293: &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
294: $aopt=splice(@names,$aopt,1);
295: &Apache::lonxml::debug("Picked $aopt");
296: if ($top{$aopt}) {
297: $toplist[$top{$aopt}]=$aopt;
298: } elsif ($bottom{$aopt}) {
299: $bottomlist[$bottom{$aopt}]=$aopt;
300: } else {
301: push (@whichopt,$aopt);
302: }
303: }
304: for (my $i=0;$i<=$#toplist;$i++) {
305: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
306: }
307: for (my $i=0;$i<=$#bottomlist;$i++) {
308: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
309: }
310:
311: return @whichopt;
312: }
313:
314: sub displayanswers {
315: my ($max,$randomize,@opt)=@_;
316: if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
317: my @names = @{ $Apache::response::foilgroup{'names'} };
318: my @whichopt = &whichfoils($max,$randomize);
319: my $result=&Apache::response::answer_header('optionresponse');
320: foreach my $name (@whichopt) {
321: $result.=&Apache::response::answer_part('optionresponse',
322: $Apache::response::foilgroup{$name.'.value'})
323: }
324: $result.=&Apache::response::answer_footer('optionresponse');
325: return $result;
326: }
327:
328: sub displayfoils {
329: my ($target,$max,$randomize,@opt)=@_;
330: if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
331: my @names = @{ $Apache::response::foilgroup{'names'} };
332: my @truelist;
333: my @falselist;
334: my $result;
335: my $name;
336: my $displayoptionintex=0;
337: my @alphabet = ('A'..'Z');
338: my @whichopt = &whichfoils($max,$randomize);
339: my $part=$Apache::inputtags::part;
340: my $id=$Apache::inputtags::response[-1];
341: my $break;
342: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
343: my $status=$Apache::inputtags::status[-1];
344: if (
345: ($target ne 'tex') &&
346: (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) ) {
347: foreach $name (@whichopt) {
348: my $text=$Apache::response::foilgroup{$name.'.text'};
349: if ($text!~/^\s*$/) {
350: if ($target eq 'tex') {
351: $break='\vskip 0 mm ';
352: } elsif ($target eq 'web') {
353: $break='<br />';
354: }
355: }
356: $result.=$break;
357: if ($target eq 'web') {
358: my $value=$Apache::response::foilgroup{$name.'.value'};
359: if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
360: if ($text=~/^\s*$/) {
361: $text=$value.$text;
362: } else {
363: $text=$value.': '.$text;
364: }
365: }
366: $result.=$text."\n";
367: } elsif ($target eq 'tex') {
368: $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
369: $result .='\item \textit{'.$Apache::response::foilgroup{$name.'.value'}.'}'.
370: ":".$Apache::response::foilgroup{$name.'.text'}."\n";
371: }
372: if ($Apache::lonhomework::type eq 'exam') {
373: if ($target ne 'tex') {
374: $result.=&webbubbles(\@opt,\@alphabet);
375: } else {
376: $result.=&bubbles(\@alphabet,\@opt);
377: }
378: }
379: }
380: } else {
381: my $temp=1;
382: my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
383: foreach $name (@whichopt) {
384: my $text=$Apache::response::foilgroup{$name.'.text'};
385: if ($text!~/^\s*$/) {
386: if ($target eq 'tex') {
387: $break='\vskip 0 mm ';
388: } elsif ($target eq 'web') {
389: $break='<br />';
390: }
391: }
392: my $lastopt=$lastresponse{$name};
393: my $optionlist="<option></option>\n";
394: my $option;
395: foreach $option (@opt) {
396: if ($option eq $lastopt) {
397: $optionlist.="<option selected=\"on\">$option</option>\n";
398: } else {
399: $optionlist.="<option>$option</option>\n";
400: }
401: }
402: if ($target ne 'tex') {
403: if ($Apache::lonhomework::type ne 'exam') {
404: $optionlist='<select name="HWVAL_'.
405: $Apache::inputtags::response['-1'].':'.$temp.'">'.
406: $optionlist."</select>\n";
407: }
408: my $text=$Apache::response::foilgroup{$name.'.text'};
409: if (!($text=~s|<drawoptionlist\s*/>|$optionlist|) && $Apache::lonhomework::type ne 'exam') {
410: $text=$optionlist.$text;
411: }
412: $result.=$break.$text."\n";
413: if ($Apache::lonhomework::type eq 'exam') {
414: $result.=&webbubbles(\@opt,\@alphabet,$temp);
415: }
416: $temp++;
417: } else {
418: my $texoptionlist = &optionlist_correction($optionlist);
419: if ($displayoptionintex == 0) {
420: if ($Apache::lonhomework::type eq 'exam') {$texoptionlist='';} #if exam we do not need to show optionlist
421: if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
422: if ($Apache::lonhomework::type eq 'exam') {
423: $Apache::response::foilgroup{$name.'.text'}=~s/\\item/\\vskip 2 mm/;
424: }
425: if ($Apache::response::foilgroup{$name.'.text'}=~/<drawoptionlist\s*\/>/) {
426: $Apache::response::foilgroup{$name.'.text'}=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
427: }
428: $result.= $texoptionlist.$Apache::response::foilgroup{$name.'.text'};
429: } else {
430: if ($Apache::lonhomework::type eq 'exam') {
431: $result.= $texoptionlist.' '.$Apache::response::foilgroup{$name.'.text'};
432: } else {
433: $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
434: }
435: }
436: if ($Apache::lonhomework::type eq 'exam') {
437: $result.='\vskip -2 mm\parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&bubbles(\@alphabet,\@opt).'}\end{enumerate}} \vskip -9 mm \strut ';
438: }
439: $displayoptionintex=1;
440: } else {
441: if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
442: if ($Apache::lonhomework::type eq 'exam') {
443: $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
444: }
445: if ($Apache::response::foilgroup{$name.'.text'}=~/<drawoptionlist\s*\/>/) {
446: $Apache::response::foilgroup{$name.'.text'}=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
447: }
448: $result.= $Apache::response::foilgroup{$name.'.text'};
449: } else {
450: if ($Apache::lonhomework::type eq 'exam') {
451: $result.= ' '.$Apache::response::foilgroup{$name.'.text'};
452: } else {
453: $result.= '\item '.$Apache::response::foilgroup{$name.'.text'};
454: }
455: }
456: if ($Apache::lonhomework::type eq 'exam') {
457: $result.='\vskip -2 mm \parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&bubbles(\@alphabet,\@opt).'}\end{enumerate}} \vskip -9 mm \strut ';
458: }
459: }
460: }
461: }
462: }
463: if ($target ne 'tex') {
464: return $result.$break;
465: } else {
466: return $result;
467: }
468: }
469:
470:
471: sub optionlist_correction {
472:
473: my $texoptionlist = shift;
474: $texoptionlist =~ s/<option><\/option>/\\item \[\] Possible answers are:/;
475: $texoptionlist =~ s/<option>/\{\\bf /g;
476: $texoptionlist =~ s/<option selected="on">/\{\\bf /g;
477: $texoptionlist =~ s/<\/option>/\},/g;
478: $texoptionlist =~ s/,$/\./g;
479: $texoptionlist =~ s/>/\$>\$/g;
480: $texoptionlist =~ s/</\$<\$/g;
481: $texoptionlist =~ s/=/\$=\$/g;
482: $texoptionlist =~ s/\^(\d+)/<m>\$$1\$<\/m>/g;
483: return $texoptionlist;
484: }
485:
486:
487: sub webbubbles {
488:
489: my ($ropt,$ralphabet,$temp)=@_;
490: my @opt=@$ropt;
491: my @alphabet=@$ralphabet;
492: my $result='';
493: my $number_of_bubbles = $#opt + 1;
494: $result.= '<table border="1"><tr>';
495: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
496: $result.='<td><input type="radio" name="HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp.
497: '" value='.$opt[$ind].'>'.$alphabet[$ind].': '.$opt[$ind].'</td>';
498: }
499: $result.='</tr></table>';
500: return $result;
501: }
502:
503:
504: sub bubbles {
505:
506: my ($ralphabit,$ropt) = @_;
507: my @alphabet = @$ralphabit;
508: my @opt = @$ropt;
509: my ($result,$head,$line) =('','','');
510: my $number_of_bubbles = $#opt + 1;
511: my $current_length = 0;
512: for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
513: $current_length += (length($opt[$ind])+length($alphabet[$ind])+2)*2;
514: if ($current_length<0.9*$ENV{'form.textwidth'} and $ind!=$number_of_bubbles) {
515: $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
516: $head.='lr';
517: } else {
518: $line=~s/\&\s*$//;
519: $result.='\vskip -5 mm\begin{tabular}{'.$head.'}\\\\'.$line.'\\\\\end{tabular}';
520: $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
521: $head ='lr';
522: $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
523: }
524:
525: }
526: return $result;
527: }
528:
529:
530: sub start_conceptgroup {
531: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
532: $Apache::optionresponse::conceptgroup=1;
533: %Apache::response::conceptgroup=();
534: my $result;
535: if ($target eq 'edit') {
536: $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
537: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
538: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
539: }
540: if ($target eq 'modified') {
541: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
542: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
543: }
544: return $result;
545: }
546:
547: sub end_conceptgroup {
548: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
549: $Apache::optionresponse::conceptgroup=0;
550: my $result='';
551: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
552: || $target eq 'tex' || $target eq 'analyze') {
553: #if not there aren't any foils to display and thus no question
554: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
555: my @names = @{ $Apache::response::conceptgroup{'names'} };
556: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
557: my $name=$names[$pick];
558: push @{ $Apache::response::foilgroup{'names'} }, $name;
559: $Apache::response::foilgroup{"$name.value"} =
560: $Apache::response::conceptgroup{"$name.value"};
561: $Apache::response::foilgroup{"$name.text"} =
562: $Apache::response::conceptgroup{"$name.text"};
563: $Apache::response::foilgroup{"$name.location"} =
564: $Apache::response::conceptgroup{"$name.location"};
565: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
566: $Apache::response::foilgroup{"$name.concept"} = $concept;
567: &Apache::lonxml::debug("Selecting $name in $concept");
568: if ($target eq 'analyze') {
569: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
570: push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
571: $concept);
572: $Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
573: $Apache::response::conceptgroup{'names'};
574: foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
575: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
576: $name);
577: $Apache::lonhomework::analyze{"$part_id.foil.value.$name"} =
578: $Apache::response::conceptgroup{"$name.value"};
579: $Apache::lonhomework::analyze{"$part_id.foil.text.$name"} =
580: $Apache::response::conceptgroup{"$name.text"};
581: $Apache::lonhomework::analyze{"$part_id.foil.location.$name"} =
582: $Apache::response::conceptgroup{"$name.location"};
583: }
584: } elsif ($target eq 'web') {
585: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
586: push(@{ $Apache::hint::option{"$part_id.concepts"} },
587: $concept);
588: $Apache::hint::option{"$part_id.concept.$concept"}=
589: $Apache::response::conceptgroup{'names'};
590: }
591: }
592: } elsif ($target eq 'edit') {
593: $result=&Apache::edit::end_table();
594: }
595: return $result;
596: }
597:
598: sub insert_conceptgroup {
599: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
600: return $result;
601: }
602:
603: sub start_foil {
604: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
605: my $result='';
606: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
607: &Apache::lonxml::startredirection;
608: } elsif ($target eq 'edit') {
609: $result=&Apache::edit::tag_start($target,$token,"Foil");
610: my $level='-2';
611: if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
612: my @opt;
613: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
614: $result.=&Apache::edit::text_arg('Name:','name',$token);
615: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
616: ['unused',(@opt)],$token,'15');
617: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
618: $safeeval,'-3');
619: if ($randomize ne 'no') {
620: $result.=&Apache::edit::select_arg('Location:','location',
621: ['random','top','bottom'],$token);
622: }
623: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
624: } elsif ($target eq 'modified') {
625: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
626: 'value','name','location');
627: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
628: }
629: return $result;
630: }
631:
632: sub end_foil {
633: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
634: my $text ='';
635: my $result = '';
636: if ($target eq 'web' || $target eq 'tex') {
637: $text=&Apache::lonxml::endredirection;
638: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') { $text = '\vspace*{-2 mm}\item '.$text; }
639: }
640: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
641: || $target eq 'tex' || $target eq 'analyze') {
642: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
643: if ($value ne 'unused') {
644: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
645: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
646: &Apache::lonxml::debug("Got a name of :$name:");
647: if (!$name) { $name=$Apache::lonxml::curdepth; }
648: &Apache::lonxml::debug("Using a name of :$name:");
649: if ( $Apache::optionresponse::conceptgroup
650: && !&Apache::response::showallfoils() ) {
651: push @{ $Apache::response::conceptgroup{'names'} }, $name;
652: $Apache::response::conceptgroup{"$name.value"} = $value;
653: if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
654: $Apache::response::conceptgroup{"$name.text"} = '\vskip 4 mm $\triangleright$ '.$text;
655: } else {
656: if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
657: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
658: } else {
659: $Apache::response::conceptgroup{"$name.text"} = $text;
660: }
661: }
662: $Apache::response::conceptgroup{"$name.location"} = $location;
663: } else {
664: push @{ $Apache::response::foilgroup{'names'} }, $name;
665: $Apache::response::foilgroup{"$name.value"} = $value;
666: if ($Apache::lonhomework::type eq 'exam') {
667: $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
668: } else {
669: if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
670: $Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
671: } else {
672: $Apache::response::foilgroup{"$name.text"} = $text;
673: }
674: }
675: $Apache::response::foilgroup{"$name.location"} = $location;
676: }
677: }
678: }
679: if ($target eq 'edit') {
680: $result.= &Apache::edit::tag_end($target,$token,'');
681: }
682: return $result;
683: }
684:
685: sub insert_foil {
686: return '
687: <foil name="" value="unused">
688: <startouttext />
689: <endouttext />
690: </foil>';
691: }
692: 1;
693: __END__
694:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>