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