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