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