Annotation of loncom/homework/optionresponse.pm, revision 1.189

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>