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