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