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