Annotation of loncom/homework/optionresponse.pm, revision 1.195
1.46 sakharuk 1: # LearningOnline Network with CAPA
1.22 albertel 2: # option list style responses
1.27 albertel 3: #
1.195 ! raeburn 4: # $Id: optionresponse.pm,v 1.194 2014/11/27 17:37:09 raeburn 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.192 bisitz 67: &Apache::edit::select_arg('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.173 raeburn 106: &Apache::response::pushrandomnumber(undef,$target);
1.22 albertel 107: if ($target eq 'edit') {
108: my $optionlist="<option></option>\n";
109: my $option;
110: my @opt;
1.188 foxr 111: my @raw_options = &Apache::lonxml::get_param('options', $parstack, $safeeval, 0, 0, 1);
112:
113:
114: eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval, 0, 0, 1);
115:
1.22 albertel 116: my $count=1;
117: foreach $option (@opt) {
118: $optionlist.="<option value=\"$count\">$option</option>\n";
119: $count++;
120: }
121: my $insertlist=&Apache::edit::insertlist($target,$token);
122: $result.=&Apache::edit::start_table($token);
1.193 bisitz 123: my %lt = &Apache::lonlocal::texthash(
124: sel => 'Select Options',
125: add => 'Add new Option:',
126: del => 'Delete an Option:',
1.195 ! raeburn 127: maxcheck => 'Enter maximum number students may check (e.g., 3)',
! 128: mincheck => 'Enter minimum number students need to check (e.g., 1)',
1.193 bisitz 129: );
1.22 albertel 130: $result.= (<<ENDTABLE);
1.193 bisitz 131: <tr><td>$lt{'sel'}</td>
1.13 albertel 132: <td>
1.193 bisitz 133: $lt{'add'} <input type="text" name="$Apache::lonxml::curdepth.options" />
1.13 albertel 134: </td>
1.193 bisitz 135: <td>$lt{'del'}
1.13 albertel 136: <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
1.162 www 137: </td>
1.13 albertel 138: ENDTABLE
1.162 www 139: $result.= '<td>'.&Apache::edit::checked_arg('Print options:','texoptions',
1.193 bisitz 140: [ ['nochoice',"Don't show option list"] ]
1.162 www 141: ,$token).'</td>';
142: $result.= '<td><label>'.&mt('Two-option checkboxes for:').
143: '<select name="checkboxvalue_'.$Apache::lonxml::curdepth.'">';
144: foreach $option (('',@opt)) {
145: $result.='<option value="'.$option.'"';
146: if ($option eq &Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval)) {
147: $result.=' selected="selected"';
148: }
149: $result.='>'.$option.'</option>';
150: }
1.194 raeburn 151: $result.='</select></label> ';
1.195 ! raeburn 152: my %checknumtxt = (
! 153: maxcheck => 'Max. boxes permitted to check',
! 154: mincheck => 'Min. boxes required to check',
! 155: );
! 156: my (%onclicknum,%checknum);
! 157: foreach my $item ('maxcheck','mincheck') {
! 158: $onclicknum{$item} = 'onclick="'."updateNumber('$item','$Apache::lonxml::curdepth','check','$lt{$item}');".'"';
! 159: $checknum{$item} = &Apache::lonxml::get_param($item,$parstack,$safeeval);
! 160: }
1.172 www 161: $result.=&Apache::edit::checked_arg('Checkbox options:','checkboxoptions',
1.193 bisitz 162: [ ['nochoice',"Don't show option list"] ]
1.194 raeburn 163: ,$token).' '.
164: &Apache::edit::checked_arg('','noprompt',
165: [ ['nochoice','Omit "Select all that are ... "' ] ]
1.195 ! raeburn 166: ,$token);
! 167: foreach my $item ('maxcheck','mincheck') {
! 168: my $currnum = $checknum{$item};
! 169: unless ($currnum =~ /^\d+$/) {
! 170: $currnum = 'nochoice';
! 171: }
! 172: $result.= ' <span class="LC_nobreak">'.
! 173: &Apache::edit::checked_arg('',$item,[ [$currnum,$checknumtxt{$item}] ],
! 174: $token,$onclicknum{$item},1).' '.
! 175: '<span id="'.$item.'_numtext_'.$Apache::lonxml::curdepth.'">';
! 176: if ($checknum{$item} ne '') {
! 177: $result .= ': <a href="javascript:updateNumber('."'$item','$Apache::lonxml::curdepth','link','$lt{$item}'".')">'.
! 178: $checknum{$item}.'</a>';
! 179: }
! 180: $result .= '</span></span>';
! 181: }
! 182: $result.= '</td>';
1.30 matthew 183: $result.= &Apache::edit::end_row();
184: $result.= &Apache::edit::start_spanning_row();
185: $result.= $insertlist.'<br />';
1.22 albertel 186: }
187: if ($target eq 'modified') {
188: my @options;
189: my $optchanged=0;
1.188 foxr 190:
191:
192: eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval, 0, 0, 1);
193:
1.125 albertel 194: if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
195: my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
1.22 albertel 196: &Apache::lonxml::debug("Deleting :$delopt:");
197: splice(@options,$delopt-1,1);
198: $optchanged=1;
199: }
1.180 raeburn 200: if ($env{"form.$Apache::lonxml::curdepth.options"} ne '') {
1.125 albertel 201: my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
1.180 raeburn 202: push(@options,$newopt);
1.22 albertel 203: $optchanged=1;
204: }
1.132 albertel 205: my $rebuildtag = &Apache::edit::get_new_args($token,$parstack,$safeeval,
1.194 raeburn 206: 'texoptions','checkboxvalue','checkboxoptions',
1.195 ! raeburn 207: 'noprompt','maxcheck','mincheck');
! 208: if ($rebuildtag) {
! 209: my $maxcheck = $token->[2]->{'maxcheck'};
! 210: my $mincheck = $token->[2]->{'mincheck'};
! 211: if ($mincheck =~ /D/) {
! 212: $token->[2]->{'mincheck'}='';
! 213: }
! 214: if ($maxcheck =~ /D/) {
! 215: $token->[2]->{'maxcheck'}='';
! 216: }
! 217: if (($mincheck ne '') && ($maxcheck ne '')) {
! 218: if ($mincheck > $maxcheck) {
! 219: $token->[2]->{'mincheck'}=$maxcheck;
! 220: }
! 221: }
! 222: }
1.132 albertel 223: if ($optchanged || $rebuildtag ) {
1.22 albertel 224: $result = "<foilgroup options=\"(";
225: foreach my $option (@options) {
1.41 albertel 226: $option=~s/\'/\\\'/g;
1.22 albertel 227: &Apache::lonxml::debug("adding option :$option:");
228: $result .="'".$option."',";
229: }
230: chop $result;
1.132 albertel 231: $result.=')" ';
232: $result .= 'texoptions="'.$token->[2]{'texoptions'}.'" ';
1.172 www 233: $result .= 'checkboxoptions="'.$token->[2]{'checkboxoptions'}.'" ';
1.194 raeburn 234: $result .= 'checkboxvalue="'.$token->[2]{'checkboxvalue'}.'" ';
1.195 ! raeburn 235: $result .= 'noprompt="'.$token->[2]{'noprompt'}.'" ';
! 236: $result .= 'maxcheck="'.$token->[2]{'maxcheck'}.'" ';
! 237: $result .= 'mincheck="'.$token->[2]{'mincheck'}.'"';
1.132 albertel 238: $result .= '>';
1.22 albertel 239: } # else nothing changed so just use the default mechanism
240: }
1.68 sakharuk 241: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.152 onken 242: if($env{'form.pdfFormFields'} ne 'yes') {
1.167 raeburn 243: $result .= ' \begin{itemize} ';
1.152 onken 244: } else {
245: $result .= "\\\\";
246: }
1.42 sakharuk 247: }
1.22 albertel 248: return $result;
1.1 albertel 249: }
250:
251: sub end_foilgroup {
1.22 albertel 252: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
253:
254: my $result;
1.94 albertel 255: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
256: $target eq 'tex' || $target eq 'analyze') {
1.81 sakharuk 257: my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
1.22 albertel 258: my $name;
259: my @opt;
260: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.126 albertel 261: &Apache::lonxml::debug("options:".join(':',@opt));
262:
1.108 albertel 263: my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
264: -2,0);
1.161 www 265: my $checkboxvalue=&Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval);
1.172 www 266: my $checkboxchoices=(&Apache::lonxml::get_param('checkboxoptions',$parstack,$safeeval) ne 'nochoice');
1.195 ! raeburn 267: my $maxcheck=&Apache::lonxml::get_param('maxcheck', $parstack, $safeeval);
! 268: $maxcheck =~ s/\D+//g;
! 269: my $mincheck=&Apache::lonxml::get_param('mincheck', $parstack, $safeeval);
! 270: $mincheck =~ s/\D+//g;
1.194 raeburn 271: my $noprompt=&Apache::lonxml::get_param('noprompt', $parstack, $safeeval);
1.186 foxr 272:
1.81 sakharuk 273: if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
1.22 albertel 274: &Apache::lonxml::debug("Options are $#opt");
1.48 albertel 275: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
276: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
277: $safeeval,'-2');
1.32 sakharuk 278: if ($target eq 'web' || $target eq 'tex') {
1.195 ! raeburn 279: $result.=&displayfoils($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,$tex_option_switch, $noprompt,$maxcheck,$mincheck, @opt);
1.137 albertel 280: $Apache::lonxml::post_evaluate=0;
1.25 albertel 281: } elsif ( $target eq 'answer') {
1.48 albertel 282: $result.=&displayanswers($max,$randomize,@opt);
1.136 foxr 283:
1.34 albertel 284: } elsif ( $target eq 'analyze') {
1.93 albertel 285: my @shown = &whichfoils($max,$randomize);
1.94 albertel 286: &Apache::response::analyze_store_foilgroup(\@shown,
287: ['text','value','location']);
1.93 albertel 288: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
289: push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.22 albertel 290: } elsif ( $target eq 'grade') {
1.175 www 291:
1.124 albertel 292: if ( &Apache::response::submitted()) {
1.175 www 293:
1.48 albertel 294: my @whichopt = &whichfoils($max,$randomize);
1.22 albertel 295: my $temp=1;my $name;
1.26 albertel 296: my %responsehash;
1.57 albertel 297: my %grade;
1.22 albertel 298: my $right=0;
299: my $wrong=0;
300: my $ignored=0;
1.195 ! raeburn 301: my $checked=0;
1.177 raeburn 302: my ($numrows,$bubbles_per_row);
303: if ($Apache::lonhomework::scantronmode) {
304: my $numitems = scalar(@opt);
305: ($numrows,$bubbles_per_row) =
306: &Apache::optionresponse::getnumrows($numitems);
307: }
308: if ($numrows < 1) {
309: $numrows = 1;
310: }
1.195 ! raeburn 311: my $checkboxopt;
! 312: if ($checkboxvalue) {
! 313: $checkboxopt = &check_box_opt($target,$checkboxvalue,@opt);
! 314: if (($checkboxopt) && ($Apache::lonhomework::type !~ /survey/)) {
! 315: my $corrnumcheck = 0;
! 316: if (($maxcheck ne '') || ($mincheck ne '')) {
! 317: foreach $name (@whichopt) {
! 318: if ($Apache::response::foilgroup{$name.'.value'} eq $checkboxopt) {
! 319: $corrnumcheck ++;
! 320: }
! 321: }
! 322: if (($maxcheck ne '') && ($corrnumcheck > $maxcheck)) {
! 323: $maxcheck = $corrnumcheck;
! 324: }
! 325: if (($mincheck ne '') && ($corrnumcheck < $mincheck)) {
! 326: $mincheck = $corrnumcheck;
! 327: }
! 328: }
! 329: }
! 330: }
1.22 albertel 331: foreach $name (@whichopt) {
1.177 raeburn 332: my $response;
333: if ($env{'form.submitted'} eq 'scantron') {
334: if ($numrows > 1) {
335: my $num = $temp;
336: my $totalnum;
337: for (my $i=0; $i<$numrows; $i++) {
338: my $item = &Apache::response::getresponse($num);
339: if ($item =~ /^\d+$/) {
340: $totalnum = $i*$bubbles_per_row + $item;
341: }
342: $num ++;
343: }
344: if ($totalnum =~ /^\d+$/) {
345: $response = $opt[$totalnum];
346: }
347: $temp += $numrows;
348: } else {
1.178 raeburn 349: $response=&Apache::response::getresponse($temp);
1.177 raeburn 350: if ($response=~/\S/) {
351: $response = $opt[$response];
352: }
353: $temp ++;
354: }
355: } else {
1.178 raeburn 356: $response=&Apache::response::getresponse($temp);
1.177 raeburn 357: $temp ++;
358: }
1.22 albertel 359: if ( $response =~ /[^\s]/) {
1.96 albertel 360: $responsehash{$name}=$response;
1.22 albertel 361: my $value=$Apache::response::foilgroup{$name.'.value'};
1.34 albertel 362: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57 albertel 363: if ($value eq $response) {
364: $grade{$name}='1'; $right++;
365: } else {
366: $grade{$name}='0'; $wrong++;
367: }
1.195 ! raeburn 368: if ($checkboxopt) {
! 369: if ($response eq $checkboxopt) {
! 370: $checked++;
! 371: }
! 372: }
1.22 albertel 373: } else {
374: $ignored++;
375: }
376: }
1.28 albertel 377: my $part=$Apache::inputtags::part;
1.184 raeburn 378: my $nonlenient=&grading_is_nonlenient($part);
1.22 albertel 379: my $id = $Apache::inputtags::response['-1'];
1.28 albertel 380: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57 albertel 381: my $gradestr =&Apache::lonnet::hash2str(%grade);
1.28 albertel 382: my %previous=&Apache::response::check_for_previous($responsestr,
383: $part,$id);
1.22 albertel 384: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.75 albertel 385: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
386: $responsestr;
387: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
1.173 raeburn 388: if ($Apache::lonhomework::type eq 'randomizetry') {
389: $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
390: }
1.195 ! raeburn 391: my $skipaward;
! 392: if ($checkboxvalue) {
! 393: if ((($maxcheck ne '') && ($checked > $maxcheck)) ||
! 394: (($mincheck ne '') && ($checked < $mincheck))) {
! 395: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='WRONG_NUMBOXESCHECKED';
! 396: &Apache::response::handle_previous(\%previous,$ad);
! 397: $skipaward = 1;
! 398: }
! 399: }
! 400: unless ($skipaward) {
! 401: if (($Apache::lonhomework::type eq 'survey') ||
! 402: ($Apache::lonhomework::type eq 'surveycred') ||
! 403: ($Apache::lonhomework::type eq 'anonsurvey') ||
! 404: ($Apache::lonhomework::type eq 'anonsurveycred')) {
! 405: if ($ignored == 0) {
! 406: my $ad;
! 407: if ($Apache::lonhomework::type eq 'anonsurveycred') {
! 408: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
! 409: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
! 410: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
! 411: } elsif ($Apache::lonhomework::type eq 'surveycred') {
! 412: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
! 413: } else {
! 414: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
! 415: }
! 416: &Apache::response::handle_previous(\%previous,$ad);
! 417: } elsif ($wrong==0 && $right==0) {
! 418: } else {
! 419: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
! 420: &Apache::response::handle_previous(\%previous,$ad);
! 421: }
! 422: } elsif ($nonlenient) {
1.174 www 423: #
424: # Non-lenient mode. All right or all wrong
425: #
1.195 ! raeburn 426: my $ad;
! 427: if ($wrong==0 && $ignored==0) {
! 428: $ad='EXACT_ANS';
! 429: } elsif ($wrong==0 && $right==0) {
! 430: #nothing submitted
! 431: } else {
! 432: if ($ignored==0) {
! 433: $ad='INCORRECT';
! 434: } else {
! 435: $ad='MISSING_ANSWER';
! 436: }
! 437: }
! 438: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
! 439: &Apache::response::handle_previous(\%previous,$ad);
! 440: } else {
1.174 www 441: #
442: # This is lenient mode
443: #
1.195 ! raeburn 444: my $ad;
! 445: if ($wrong==0 && $right==0) {
! 446: #nothing submitted only assign a score if we
! 447: #need to override a previous grade
! 448: if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
! 449: $ad='ASSIGNED_SCORE';
! 450: }
! 451: } else {
1.120 albertel 452: $ad='ASSIGNED_SCORE';
1.195 ! raeburn 453: }
! 454: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
! 455: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
! 456: $right/(scalar(@whichopt));
! 457: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
! 458: scalar(@whichopt);
! 459: }
! 460: }
1.22 albertel 461: }
1.1 albertel 462: }
1.147 foxr 463: my $part_id = $Apache::inputtags::part;
464: my $response_id = $Apache::inputtags::response[-1];
1.177 raeburn 465: my ($numrows,$bubbles_per_row);
466: if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
467: ($numrows,$bubbles_per_row) =
468: &Apache::optionresponse::getnumrows(scalar(@opt));
469: }
470: if ($numrows < 1) {
471: $numrows = 1;
472: }
473: my $increment = &getfoilcounts($max) * $numrows;
474: &Apache::lonxml::increment_counter($increment,"$part_id.$response_id");
1.146 foxr 475: if ($target eq 'analyze') {
476: &Apache::lonhomework::set_bubble_lines();
477: }
478:
1.25 albertel 479: } elsif ($target eq 'edit') {
1.22 albertel 480: $result.=&Apache::edit::end_table();
1.74 albertel 481: }
482: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.152 onken 483: if($env{'form.pdfFormFields'} ne 'yes') {
1.167 raeburn 484: $result .= '\end{itemize}';
1.152 onken 485: } else {
486: $result .= "\\\\";
487: }
1.74 albertel 488: }
1.110 albertel 489: &Apache::response::poprandomnumber();
1.22 albertel 490: return $result;
1.1 albertel 491: }
492:
1.184 raeburn 493: sub grading_is_nonlenient {
494: my ($part) = @_;
495: # Web mode: we are non-lenient unless told otherwise
496: my $defaultparm = 'off';
497: my $nonlenient = 0;
498: # Grading a bubblesheet exam: we are grading lenient unless told otherwise
499: if ($Apache::lonhomework::scantronmode) {
500: $defaultparm = 'on';
501: $nonlenient = 1;
502: }
503: my $lenientparm =
504: &Apache::response::get_response_param($part,'lenient',$defaultparm);
505: if ($lenientparm=~/^0|off|no$/i) {
506: $nonlenient = 1;
507: } elsif ($lenientparm=~/^1|on|yes$/i) {
508: $nonlenient = 0;
509: }
510: return $nonlenient;
511: }
512:
1.1 albertel 513: sub getfoilcounts {
1.48 albertel 514: my ($max)=@_;
1.22 albertel 515: # +1 since instructors will count from 1
516: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39 albertel 517: if (&Apache::response::showallfoils()) { $max=$count; }
1.48 albertel 518: if ($count>$max) { $count=$max }
519: &Apache::lonxml::debug("Count is $count from $max");
520: return $count;
1.1 albertel 521: }
522:
523: sub whichfoils {
1.77 albertel 524: my ($max,$randomize)=@_;
525: return &Apache::response::whichorder($max,$randomize,
526: &Apache::response::showallfoils(),
527: \%Apache::response::foilgroup);
1.1 albertel 528: }
529:
1.25 albertel 530: sub displayanswers {
1.130 albertel 531: my ($max,$randomize,@opt)=@_;
1.187 raeburn 532: my @names;
533: if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
534: @names = @{ $Apache::response::foilgroup{'names'} };
535: }
536: return if (!@names);
1.130 albertel 537: my @whichopt = &whichfoils($max,$randomize);
538: my $result;
539: if ($Apache::lonhomework::type eq 'exam') {
540: my $i = 0;
541: my %opt = map { ($_,$i++) } @opt;
542:
543: $i = 0;
544: my @alphabet = ('A'..'Z');
545: foreach my $name (@whichopt) {
546: $result.=&Apache::response::answer_header('optionresponse',$i++);
547: $result.=&Apache::response::answer_part('optionresponse',
548: $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
549: $result.=&Apache::response::answer_part('optionresponse',
550: $Apache::response::foilgroup{$name.'.value'});
551: $result.=&Apache::response::answer_footer('optionresponse');
552: }
553: } else {
554: $result=&Apache::response::answer_header('optionresponse');
555: foreach my $name (@whichopt) {
556: $result.=&Apache::response::answer_part('optionresponse',
557: $Apache::response::foilgroup{$name.'.value'});
558: }
559: $result.=&Apache::response::answer_footer('optionresponse');
560: }
561: return $result;
1.25 albertel 562: }
563:
1.160 www 564: sub check_box_opt {
1.161 www 565: my ($target,$checkboxvalue,@opt)=@_;
566: # Check if we are in checkbox mode: checkboxvalue specified, on web, only two options.
567: # If so, return "checked" value
568: if ($#opt!=1) { return ''; }
1.195 ! raeburn 569: unless (($target eq 'web') || ($target eq 'grade')) { return ''; }
1.161 www 570: return $checkboxvalue;
1.160 www 571: }
572:
1.106 albertel 573: sub check_for_invalid {
574: my ($names,$options) = @_;
575: my %bad_names;
576: foreach my $name (@{ $names }) {
577: my $value=$Apache::response::foilgroup{$name.'.value'};
578: my $found=0;
579: foreach my $option (@{ $options }) {
580: if ($value eq $option) { $found=1; }
581: }
582: if (!$found) { $bad_names{$name}=$value; }
583: }
584: if (%bad_names) {
585: my $error=&mt('The question can not be gotten correct, '.
586: 'the following foils in the <optionresponse> '.
587: 'have invalid correct options').' <br /><tt>'.
588: join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
589: "</tt>";
590: &Apache::lonxml::error($error);
591: }
592: }
593:
1.1 albertel 594: sub displayfoils {
1.182 raeburn 595: my ($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,
1.195 ! raeburn 596: $tex_option_switch, $no_tfprompt, $maxcheck, $mincheck, @opt)=@_;
1.189 raeburn 597: my @names;
598: if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
599: @names = @{ $Apache::response::foilgroup{'names'} };
600: }
601: unless (@names > 0) { return;}
1.22 albertel 602: my @truelist;
603: my @falselist;
1.79 sakharuk 604: my $result;
1.22 albertel 605: my $name;
1.101 albertel 606: my $displayoptionintex=1;
1.72 sakharuk 607: my @alphabet = ('A'..'Z');
1.48 albertel 608: my @whichopt = &whichfoils($max,$randomize);
1.182 raeburn 609: unless (($target eq 'tex') && ($tex_option_switch eq 'nochoice')) {
610: &check_for_invalid(\@whichopt,\@opt);
611: }
1.28 albertel 612: my $part=$Apache::inputtags::part;
613: my $id=$Apache::inputtags::response[-1];
1.62 albertel 614: my $break;
1.86 albertel 615: if ( ($target ne 'tex') &&
616: &Apache::response::show_answer() ) {
1.90 albertel 617: my $temp=1;
1.22 albertel 618: foreach $name (@whichopt) {
1.62 albertel 619: my $text=$Apache::response::foilgroup{$name.'.text'};
1.163 raeburn 620: my $lastresp;
1.190 raeburn 621: if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
622: (($env{'form.grade_username'} eq $env{'user.name'}) &&
623: ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
1.163 raeburn 624: $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
1.190 raeburn 625: } else {
626: unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
627: ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
628: $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
629: }
1.163 raeburn 630: }
631: my %lastresponse=&Apache::lonnet::str2hash($lastresp);
1.90 albertel 632: my $lastopt=$lastresponse{$name};
1.101 albertel 633: if ($text!~/^\s*$/) { $break='<br />'; }
1.62 albertel 634: $result.=$break;
1.113 sakharuk 635: if ($target eq 'web') {
1.62 albertel 636: my $value=$Apache::response::foilgroup{$name.'.value'};
637: if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
638: if ($text=~/^\s*$/) {
639: $text=$value.$text;
640: } else {
1.127 albertel 641: $text='<b>'.$value.':</b> '.$text;
1.62 albertel 642: }
1.95 albertel 643: } else {
1.170 raeburn 644: if (@whichopt > 1) {
645: $text='•'.$text;
646: }
1.62 albertel 647: }
648: $result.=$text."\n";
1.112 sakharuk 649: }
1.58 sakharuk 650: if ($Apache::lonhomework::type eq 'exam') {
1.90 albertel 651: $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.58 sakharuk 652: }
1.90 albertel 653: $temp++;
1.22 albertel 654: }
655: } else {
656: my $temp=1;
1.173 raeburn 657: my %lastresponse;
658: my $newvariation;
659: if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
660: ($Apache::lonhomework::type eq 'randomizetry')) &&
661: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
662: if ($env{'form.'.$part.'.rndseed'} ne
663: $Apache::lonhomework::history{"resource.$part.rndseed"}) {
664: $newvariation = 1;
665: }
666: }
667: unless ($newvariation) {
1.190 raeburn 668: if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
669: (($env{'form.grade_username'} eq $env{'user.name'}) &&
670: ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
671: %lastresponse =
672: &Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
673: } else {
674: unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
675: ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
676: %lastresponse =
677: &Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
678: }
679: }
1.173 raeburn 680: }
1.79 sakharuk 681: my $internal_counter=$Apache::lonxml::counter;
1.195 ! raeburn 682: my $checkboxopt;
! 683: if ($target eq 'web') {
! 684: $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
! 685: }
1.186 foxr 686: if ($checkboxopt && (!$no_tfprompt)) {
1.172 www 687: $result.='<br />'.
688: ($checkboxchoices?&mt('Choices: ').'<b>'.$opt[0].','.$opt[1].'</b>. ':'').
1.191 bisitz 689: &mt('Select all that are [_1].','<b>'.$checkboxopt.'</b>');
1.160 www 690: }
1.22 albertel 691: foreach $name (@whichopt) {
1.62 albertel 692: my $text=$Apache::response::foilgroup{$name.'.text'};
693: if ($text!~/^\s*$/) {
694: if ($target eq 'tex') {
695: $break='\vskip 0 mm ';
696: } elsif ($target eq 'web') {
697: $break='<br />';
698: }
699: }
1.28 albertel 700: my $lastopt=$lastresponse{$name};
701: my $optionlist="<option></option>\n";
1.152 onken 702:
1.158 onken 703: if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes'
704: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.152 onken 705: my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
706: $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
707: }
708:
1.126 albertel 709: foreach my $option (@opt) {
710: my $escopt=&HTML::Entities::encode($option,'\'"&<>');
1.152 onken 711: if ($option eq $lastopt) {
1.158 onken 712: if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.164 onken 713: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
714: && $Apache::lonhomework::type ne 'exam') {
1.152 onken 715: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
716: } else {
717: $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
718: }
719: } else {
1.158 onken 720: if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.164 onken 721: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
722: && $Apache::lonhomework::type ne 'exam') {
1.152 onken 723: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
724: } else {
725: $optionlist.="<option value='".$escopt."'>$option</option>\n";
726: }
727: }
1.28 albertel 728: }
1.32 sakharuk 729: if ($target ne 'tex') {
1.72 sakharuk 730: if ($Apache::lonhomework::type ne 'exam') {
1.159 www 731: # we are on the web, this is not an exam, and the problem can be answered
732: if ($checkboxopt) {
733: # generate checkboxes
734: my $fieldname=$Apache::inputtags::response['-1'].':'.$temp;
735: my $altopt=$opt[0];
736: if ($opt[0] eq $checkboxopt) {
737: $altopt=$opt[1];
738: }
739: my $defopt=$lastopt;
740: unless ($defopt) { $defopt=$altopt; }
741: my $escdefopt=&HTML::Entities::encode($defopt,'\'"&<>');
742: my $esccheckboxopt=&HTML::Entities::encode($checkboxopt,'\'"&<>');
743: my $escaltopt=&HTML::Entities::encode($altopt,'\'"&<>');
744: # checkboxopt is how the box is labelled
745: # altopt is the alternative option
746: # lastopt is what the user submitted before
747: # defopt is what the field is going to start out with: either previous choice or altopt
748: # fieldname is this input field's name after HWVAL_
749: $optionlist='<input type="hidden" name="HWVAL_'.$fieldname.'" value="'.$escdefopt.'" />'.
1.160 www 750: '<input type="checkbox" name="HWCHK_'.$fieldname.'" onclick="javascript:if (this.form.elements[\'HWCHK_'.
751: $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 752: } else {
753: # classic selection list
754: $optionlist='<select onchange="javascript:setSubmittedPart(\''.
1.139 albertel 755: $part.'\');" name="HWVAL_'.
1.101 albertel 756: $Apache::inputtags::response['-1'].':'.$temp.'">'.
1.59 albertel 757: $optionlist."</select>\n";
1.159 www 758: }
1.101 albertel 759: } else {
760: $optionlist='<u>'.(' 'x10).'</u>';
1.72 sakharuk 761: }
1.101 albertel 762: if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
763: if ($Apache::lonhomework::type ne 'exam') {
1.170 raeburn 764: if (@whichopt > 1) {
765: $text='•'.$text;
766: }
1.101 albertel 767: }
1.95 albertel 768: } else {
1.101 albertel 769: if ($Apache::lonhomework::type ne 'exam') {
770: $text=$optionlist.$text;
771: }
1.59 albertel 772: }
1.62 albertel 773: $result.=$break.$text."\n";
1.49 sakharuk 774: if ($Apache::lonhomework::type eq 'exam') {
1.101 albertel 775: $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.49 sakharuk 776: }
1.32 sakharuk 777: $temp++;
778: } else {
1.101 albertel 779: my $texoptionlist='';
780: if ($displayoptionintex &&
781: $Apache::lonhomework::type ne 'exam') {
1.107 sakharuk 782: $texoptionlist = &optionlist_correction($TeXlayout,@opt);
1.101 albertel 783: }
784: if ($text=~/<drawoptionlist\s*\/>/) {
1.136 foxr 785: $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
1.101 albertel 786: }
787:
788: if ($text=~m/\\item /) {
1.50 sakharuk 789: if ($Apache::lonhomework::type eq 'exam') {
1.101 albertel 790: $text=~s/\\item/\\vskip 2 mm/;
1.152 onken 791: } elsif ($env{'form.pdfFormFields'} ne 'yes') {
792: $result.= $texoptionlist.$text;
793: }
794: } else {
1.101 albertel 795: if ($Apache::lonhomework::type eq 'exam') {
796: $result.= $texoptionlist.' '.$text;
1.152 onken 797: } elsif ($env{'form.pdfFormFields'} ne 'yes') {
798: if ($text=~/\S/) {
799: $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
800: } else {
801: $result.= $texoptionlist;
802: }
1.50 sakharuk 803: }
1.32 sakharuk 804: }
1.101 albertel 805: if ($Apache::lonhomework::type eq 'exam') {
1.165 foxr 806: $result.='\vskip -1 mm\noindent';
1.177 raeburn 807: my ($numrows,$bubbles_per_row) = &getnumrows(scalar(@opt));
808: if ($numrows == 1) {
809: $result .= '\textbf{'.$internal_counter.'}. \vskip -3mm';
810: } else {
811: my $linetext;
812: for (my $i=0; $i<$numrows; $i++) {
813: $linetext .= $internal_counter+$i.', ';
814: }
815: $linetext =~ s/,\s$//;
816: $result .= '\small {\textbf{'.$linetext.'}} '.
817: '\hskip 2 mm {\footnotesize '.
818: &mt('(Bubble once in [_1] lines)',$numrows).
819: '} \vskip 1 mm';
820: }
821: $result.= &bubbles(\@alphabet,\@opt,undef,undef,$numrows,
822: $bubbles_per_row,$internal_counter).
823: ' \strut ';
824: $internal_counter += $numrows;
1.101 albertel 825: }
1.158 onken 826: if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.164 onken 827: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
828: && $Apache::lonhomework::type ne 'exam') {
1.158 onken 829: $text =~ s/\\item//m;
1.152 onken 830: $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
831: $temp++;
832: }
1.101 albertel 833: $displayoptionintex=0;
1.62 albertel 834: }
1.1 albertel 835: }
1.62 albertel 836: }
1.138 albertel 837:
838: if ($target eq 'web') {
1.173 raeburn 839: my $data = [\@whichopt,'submissiongrading'];
840: my $questiontype;
841: if ($Apache::lonhomework::type eq 'randomizetry') {
842: $questiontype = $Apache::lonhomework::type,
843: }
844: &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,$data,$questiontype);
1.138 albertel 845: }
846:
1.32 sakharuk 847: if ($target ne 'tex') {
1.62 albertel 848: return $result.$break;
849: } else {
1.32 sakharuk 850: return $result;
1.22 albertel 851: }
1.50 sakharuk 852: }
853:
854:
855: sub optionlist_correction {
1.107 sakharuk 856: my ($TeXlayout,@options) = @_;
1.150 bisitz 857: my $texoptionlist='\\item [] '.&mt('Choices:').' ';
1.107 sakharuk 858: if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
1.101 albertel 859: if (scalar(@options) > 0) {
860: foreach my $option (@options) {
861: $texoptionlist.='{\bf '.
1.135 albertel 862: &Apache::lonxml::latex_special_symbols($option).
1.107 sakharuk 863: '}';
864: if ($TeXlayout eq 'vertical') {
865: $texoptionlist.=' \vskip 0 mm ',
866: } else {
1.115 sakharuk 867: $texoptionlist.=', ';
1.107 sakharuk 868: }
1.101 albertel 869: }
1.116 albertel 870: $texoptionlist=~s/, $//;
1.107 sakharuk 871: if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
1.80 sakharuk 872: } else {
1.107 sakharuk 873: if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
1.80 sakharuk 874: }
1.50 sakharuk 875: return $texoptionlist;
1.58 sakharuk 876: }
877:
878:
879: sub webbubbles {
1.90 albertel 880: my ($ropt,$ralphabet,$temp,$lastopt)=@_;
1.58 sakharuk 881: my @opt=@$ropt;
882: my @alphabet=@$ralphabet;
883: my $result='';
1.90 albertel 884: my $number_of_bubbles = $#opt + 1;
885: $result.= '<table border="1"><tr>';
886: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
887: my $checked='';
888: if ($lastopt eq $opt[$ind]) {
889: $checked=' checked="on" ';
1.58 sakharuk 890: }
1.90 albertel 891: $result.='<td><input type="radio" name="HWVAL_'.
892: $Apache::inputtags::response['-1'].':'.$temp.
1.111 albertel 893: '" value="'.$opt[$ind].'" '.$checked.' />';
894: if ($alphabet[$ind]) {
895: $result.=$alphabet[$ind].': ';
896: }
897: $result.=$opt[$ind].'</td>';
1.90 albertel 898: }
899: $result.='</tr></table>';
900: return $result;
1.50 sakharuk 901: }
902:
903:
904: sub bubbles {
1.177 raeburn 905: my ($ralphabet,$ropt,$response,$max_width,$numrows,$bubbles_per_row,
906: $internal_counter) = @_;
1.119 albertel 907: my @alphabet = @$ralphabet;
1.50 sakharuk 908: my @opt = @$ropt;
1.51 sakharuk 909: my ($result,$head,$line) =('','','');
1.50 sakharuk 910: my $number_of_bubbles = $#opt + 1;
1.51 sakharuk 911: my $current_length = 0;
1.100 sakharuk 912: my $textwidth;
1.141 albertel 913: if (defined($max_width)) {
1.140 foxr 914: $textwidth=$max_width;
1.141 albertel 915: &Apache::lonxml::debug("Max width passed in: $max_width");
916: } elsif ($env{'form.textwidth'} ne '') {
1.125 albertel 917: $env{'form.textwidth'}=~/(\d+)/;
1.100 sakharuk 918: $textwidth=$1;
1.141 albertel 919: &Apache::lonxml::debug("Max width from form: $textwidth");
1.100 sakharuk 920: } else {
1.125 albertel 921: $env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.102 sakharuk 922: $textwidth=$1.'.'.$2;
1.141 albertel 923: &Apache::lonxml::debug("Max width defaults? $textwidth");
1.100 sakharuk 924: }
1.141 albertel 925: &Apache::lonxml::debug("Final maxwidth: $textwidth");
1.51 sakharuk 926: for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
1.177 raeburn 927: my $item;
928: if ($numrows > 1) {
929: my $num = $internal_counter+int($ind/$bubbles_per_row);
930: my $idx = int($ind % $bubbles_per_row);
931: $item = $num.$alphabet[$idx];
932: } else {
933: $item = $alphabet[$ind];
934: }
1.99 sakharuk 935: my $leftmargin;
1.101 albertel 936: $opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
1.119 albertel 937: if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
1.99 sakharuk 938: if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
1.140 foxr 939:
1.185 raeburn 940: $current_length += (length($opt[$ind])+length($item)+5)*2;
1.177 raeburn 941:
1.99 sakharuk 942: if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
1.177 raeburn 943:
944: $line.='\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
1.51 sakharuk 945: $head.='lr';
946: } else {
947: $line=~s/\&\s*$//;
1.171 raeburn 948: $result.='\vskip -1 mm\noindent\setlength{\tabcolsep}{2 mm}\renewcommand{\arraystretch}{1.25}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
1.177 raeburn 949: $line = '\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
1.51 sakharuk 950: $head ='lr';
1.177 raeburn 951: $current_length = (length($opt[$ind])+length($item))*2;
1.51 sakharuk 952: }
953:
1.50 sakharuk 954: }
955: return $result;
1.1 albertel 956: }
957:
1.2 albertel 958: sub start_conceptgroup {
1.22 albertel 959: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
960: $Apache::optionresponse::conceptgroup=1;
1.44 albertel 961: %Apache::response::conceptgroup=();
1.22 albertel 962: my $result;
963: if ($target eq 'edit') {
964: $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30 matthew 965: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
966: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22 albertel 967: }
968: if ($target eq 'modified') {
969: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
970: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
971: }
972: return $result;
1.2 albertel 973: }
974:
975: sub end_conceptgroup {
1.22 albertel 976: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
977: $Apache::optionresponse::conceptgroup=0;
978: my $result='';
1.93 albertel 979: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
980: $target eq 'tex' || $target eq 'analyze') {
1.22 albertel 981: #if not there aren't any foils to display and thus no question
1.93 albertel 982: &Apache::response::pick_foil_for_concept($target,
983: ['value','text','location'],
984: \%Apache::hint::option,
985: $parstack,$safeeval);
1.34 albertel 986: } elsif ($target eq 'edit') {
1.22 albertel 987: $result=&Apache::edit::end_table();
988: }
989: return $result;
1.2 albertel 990: }
991:
1.16 albertel 992: sub insert_conceptgroup {
1.22 albertel 993: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
994: return $result;
1.16 albertel 995: }
996:
1.1 albertel 997: sub start_foil {
1.22 albertel 998: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
999: my $result='';
1.34 albertel 1000: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.105 albertel 1001: &Apache::lonxml::startredirection;
1002: if ($target eq 'analyze') {
1003: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
1004: }
1.25 albertel 1005: } elsif ($target eq 'edit') {
1.22 albertel 1006: $result=&Apache::edit::tag_start($target,$token,"Foil");
1007: my $level='-2';
1008: if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
1009: my @opt;
1010: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
1011: $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48 albertel 1012: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
1013: ['unused',(@opt)],$token,'15');
1014: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
1015: $safeeval,'-3');
1016: if ($randomize ne 'no') {
1017: $result.=&Apache::edit::select_arg('Location:','location',
1018: ['random','top','bottom'],$token);
1019: }
1.30 matthew 1020: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25 albertel 1021: } elsif ($target eq 'modified') {
1.48 albertel 1022: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1023: 'value','name','location');
1.22 albertel 1024: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32 sakharuk 1025: }
1.22 albertel 1026: return $result;
1.1 albertel 1027: }
1028:
1029: sub end_foil {
1.22 albertel 1030: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1031: my $text ='';
1032: my $result = '';
1.88 albertel 1033: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.48 albertel 1034: $text=&Apache::lonxml::endredirection;
1.114 sakharuk 1035: if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
1.107 sakharuk 1036: if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;}
1.32 sakharuk 1037: }
1038: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
1.34 albertel 1039: || $target eq 'tex' || $target eq 'analyze') {
1.22 albertel 1040: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.122 albertel 1041: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
1042: $text='\vskip 5mm $\triangleright$ '.$text;
1043: }
1.22 albertel 1044: if ($value ne 'unused') {
1045: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1046: &Apache::lonxml::debug("Got a name of :$name:");
1.129 albertel 1047: if ($name eq "") {
1.151 bisitz 1048: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.123 albertel 1049: $name=$Apache::lonxml::curdepth;
1050: }
1.22 albertel 1051: &Apache::lonxml::debug("Using a name of :$name:");
1.103 albertel 1052: if (defined($Apache::response::foilnames{$name})) {
1.151 bisitz 1053: &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 1054: }
1.104 albertel 1055: $Apache::response::foilnames{$name}++;
1.103 albertel 1056: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.39 albertel 1057: if ( $Apache::optionresponse::conceptgroup
1058: && !&Apache::response::showallfoils() ) {
1.22 albertel 1059: push @{ $Apache::response::conceptgroup{'names'} }, $name;
1060: $Apache::response::conceptgroup{"$name.value"} = $value;
1.122 albertel 1061: $Apache::response::conceptgroup{"$name.text"} = $text;
1.48 albertel 1062: $Apache::response::conceptgroup{"$name.location"} = $location;
1.22 albertel 1063: } else {
1064: push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48 albertel 1065: $Apache::response::foilgroup{"$name.value"} = $value;
1.122 albertel 1066: $Apache::response::foilgroup{"$name.text"} = $text;
1.48 albertel 1067: $Apache::response::foilgroup{"$name.location"} = $location;
1.22 albertel 1068: }
1069: }
1070: }
1071: if ($target eq 'edit') {
1072: $result.= &Apache::edit::tag_end($target,$token,'');
1073: }
1074: return $result;
1.1 albertel 1075: }
1076:
1.112 sakharuk 1077: sub start_drawoptionlist {
1078: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.144 albertel 1079: if ($target !~ /^(?:meta|answer|modified|edit)$/) {
1.117 albertel 1080: return $token->[4];
1081: }
1.112 sakharuk 1082: }
1083:
1084: sub end_drawoptionlist {
1085: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.144 albertel 1086: if ($target !~ /^(?:meta|answer|modified|edit)$/) {
1.117 albertel 1087: return $token->[2];
1088: }
1.112 sakharuk 1089: }
1090:
1.7 albertel 1091: sub insert_foil {
1.144 albertel 1092: return '
1.15 albertel 1093: <foil name="" value="unused">
1.14 albertel 1094: <startouttext />
1095: <endouttext />
1.7 albertel 1096: </foil>';
1097: }
1.144 albertel 1098:
1099: sub insert_drawoptionlist {
1100: return '<drawoptionlist />';
1101: }
1.177 raeburn 1102:
1103: sub getnumrows {
1104: my ($numitems) = @_;
1105: my $bubbles_per_row;
1106: my $default_numbubbles = 10;
1107: if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
1108: ($env{'form.bubbles_per_row'} > 0)) {
1109: $bubbles_per_row = $env{'form.bubbles_per_row'};
1110: } else {
1111: $bubbles_per_row = $default_numbubbles;
1112: }
1113: my $numrows = int ($numitems/$bubbles_per_row);
1114: if (($numitems % $bubbles_per_row) != 0) {
1115: $numrows ++;
1116: }
1117: return ($numrows,$bubbles_per_row);
1118: }
1119:
1.1 albertel 1120: 1;
1121: __END__
1122:
1.156 jms 1123: =head1 NAME
1124:
1125: Apache::optionresponse.pm;
1126:
1127: =head1 SYNOPSIS
1128:
1129: Handles tags associated with showing a list of
1130: options.
1131:
1132: This is part of the LearningOnline Network with CAPA project
1133: described at http://www.lon-capa.org.
1134:
1135: =head1 HANDLER SUBROUTINE
1136:
1137: start_optionresponse()
1138:
1139: =head1 OTHER SUBROUTINES
1140:
1141: =over
1142:
1143: =item end_optionresponse()
1144:
1145: =item start_foilgroup()
1146:
1147: =item end_foilgroup()
1148:
1149: =item getfoilcounts()
1150:
1151: =item displayanswers()
1152:
1153: =item check_for_invalid()
1154:
1155: =item displayfoils()
1156:
1157: =item optionlist_correction()
1158:
1159: =item webbubbles()
1160:
1161: =item bubbles()
1162:
1163: =item start_conceptgroup()
1164:
1165: =item end_conceptgroup()
1166:
1167: =item insert_conceptgroup()
1168:
1169: =item start_foil()
1170:
1171: =item end_foil()
1172:
1173: =item start_drawoptionlist()
1174:
1175: =item end_drawoptionlist()
1176:
1177: =item insert_foil()
1178:
1179: =item insert_drawoptionlist()
1180:
1181: =back
1182:
1.157 raeburn 1183: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>