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

1.46      sakharuk    1: # LearningOnline Network with CAPA
1.22      albertel    2: # option list style responses
1.27      albertel    3: #
1.153   ! bisitz      4: # $Id: optionresponse.pm,v 1.152 2008/09/11 17:38:14 onken 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.1       albertel   28: package Apache::optionresponse;
                     29: use strict;
1.103     albertel   30: use Apache::response();
                     31: use Apache::lonlocal;
1.125     albertel   32: use Apache::lonnet;
1.1       albertel   33: 
1.31      harris41   34: BEGIN {
1.1       albertel   35:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
                     36: }
                     37: 
                     38: sub start_optionresponse {
1.22      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     40:   my $result='';
1.29      albertel   41:   #when in a option response use these
1.112     sakharuk   42:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
1.22      albertel   43:   push (@Apache::lonxml::namespace,'optionresponse');
                     44:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.57      albertel   45:   %Apache::hint::option=();
1.103     albertel   46:   undef(%Apache::response::foilnames);
1.22      albertel   47:   if ($target eq 'edit') {
1.153   ! bisitz     48:     $result.=&Apache::edit::start_table($token)
        !            49:        .'<tr><td><span class="LC_nobreak">'.&mt('Multiple Option Response Question').'</span> '
        !            50:        .&Apache::loncommon::help_open_topic('Option_Response_Problems')."</td>"
        !            51:        .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
        !            52:        .&Apache::edit::deletelist($target,$token)
        !            53:        .'</span></td>'
        !            54:        ."<td>&nbsp;"
        !            55:        .&Apache::edit::end_row()
        !            56:        .&Apache::edit::start_spanning_row()
        !            57:        ."\n";
1.48      albertel   58:     $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
                     59: 				     $token,'4').
1.153   ! bisitz     60:         &Apache::edit::select_arg('Randomize Foil Order:','randomize',
1.48      albertel   61: 				  ['yes','no'],$token).
1.108     albertel   62:         &Apache::edit::select_arg(&mt('Display of options when printed'),'TeXlayout',
                     63: 				  [['horizontal',&mt('Normal list')],
                     64: 				   ['vertical',&mt('Listed in a vertical column')]],$token).
1.48      albertel   65:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.34      albertel   66:   } elsif ($target eq 'modified') {
1.23      albertel   67:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.108     albertel   68: 						 $safeeval,'max','randomize',
                     69: 						 'TeXlayout');
1.22      albertel   70:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.34      albertel   71:   } elsif ($target eq 'meta') {
1.23      albertel   72:     $result=&Apache::response::meta_package_write('optionresponse');
1.34      albertel   73:   } elsif ($target eq 'analyze') {
1.93      albertel   74:     my $part_id="$Apache::inputtags::part.$id";
1.149     raeburn    75:     $Apache::lonhomework::analyze{"$part_id.type"} = 'optionresponse';
1.34      albertel   76:     push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.148     raeburn    77:     push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} }, 1);
1.145     foxr       78: 
1.22      albertel   79:   }
                     80:   return $result;
1.1       albertel   81: }
                     82: 
                     83: sub end_optionresponse {
1.22      albertel   84:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     85:   &Apache::response::end_response;
                     86:   pop @Apache::lonxml::namespace;
1.112     sakharuk   87:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
1.22      albertel   88:   my $result;
                     89:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.103     albertel   90:   undef(%Apache::response::foilnames);
1.22      albertel   91:   return $result;
1.1       albertel   92: }
                     93: 
1.44      albertel   94: %Apache::response::foilgroup=();
1.1       albertel   95: sub start_foilgroup {
1.22      albertel   96:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     97: 
                     98:   my $result='';
1.44      albertel   99:   %Apache::response::foilgroup=();
1.22      albertel  100:   $Apache::optionresponse::conceptgroup=0;
1.110     albertel  101:   &Apache::response::pushrandomnumber();
1.22      albertel  102:   if ($target eq 'edit') {
                    103:     my $optionlist="<option></option>\n";
                    104:     my $option;
                    105:     my @opt;
                    106:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
                    107:     my $count=1;
                    108:     foreach $option (@opt) {
                    109:       $optionlist.="<option value=\"$count\">$option</option>\n";
                    110:       $count++;
                    111:     }
                    112:     my $insertlist=&Apache::edit::insertlist($target,$token);
                    113:     $result.=&Apache::edit::start_table($token);
                    114:     $result.= (<<ENDTABLE);
                    115:       <tr><td>Select Options</td>
1.13      albertel  116:         <td>
                    117: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
                    118:         </td>
                    119:         <td>Delete an Option:
                    120: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
                    121: ENDTABLE
1.132     albertel  122:     $result.= &Apache::edit::checked_arg('Print options:','texoptions',
                    123: 					 [ ['nochoice','Don\'t show option list'] ]
                    124: 					 ,$token);
1.30      matthew   125:     $result.= &Apache::edit::end_row();
                    126:     $result.= &Apache::edit::start_spanning_row();
                    127:     $result.= $insertlist.'<br />';
1.22      albertel  128:   }
                    129:   if ($target eq 'modified') {
                    130:     my @options;
                    131:     my $optchanged=0;
                    132:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.125     albertel  133:     if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
                    134:       my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
1.22      albertel  135:       &Apache::lonxml::debug("Deleting :$delopt:");
                    136:       splice(@options,$delopt-1,1);
                    137:       $optchanged=1;
                    138:     }
1.125     albertel  139:     if ($env{"form.$Apache::lonxml::curdepth.options"}) {
                    140:       my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
1.22      albertel  141:       if ($options[0]) {
                    142: 	push(@options,$newopt);
                    143:       } else {
                    144: 	$options[0]=$newopt;
                    145:       }
                    146:       $optchanged=1;
                    147:     }
1.132     albertel  148:     my $rebuildtag = &Apache::edit::get_new_args($token,$parstack,$safeeval,
                    149: 						 ('texoptions'));
                    150:     if ($optchanged || $rebuildtag ) {
1.22      albertel  151:       $result = "<foilgroup options=\"(";
                    152:       foreach my $option (@options) {
1.41      albertel  153: 	$option=~s/\'/\\\'/g;
1.22      albertel  154: 	&Apache::lonxml::debug("adding option :$option:");
                    155: 	$result .="'".$option."',";
                    156:       }
                    157:       chop $result;
1.132     albertel  158:       $result.=')" ';
                    159:       $result .= 'texoptions="'.$token->[2]{'texoptions'}.'" ';
                    160:       $result .= '>';
1.22      albertel  161:     } # else nothing changed so just use the default mechanism
                    162:   }
1.68      sakharuk  163:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.152     onken     164:     if($env{'form.pdfFormFields'} ne 'yes') {
1.109     albertel  165:       $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}
                    166:                    \begin{enumerate} ';
1.152     onken     167:     } else {
                    168:       $result .= "\\\\";
                    169:     }
1.42      sakharuk  170:   }
1.22      albertel  171:   return $result;
1.1       albertel  172: }
                    173: 
                    174: sub end_foilgroup {
1.22      albertel  175:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    176:   
                    177:   my $result;
1.94      albertel  178:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    179:       $target eq 'tex' || $target eq 'analyze') {
1.81      sakharuk  180:     my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
1.22      albertel  181:     my $name;
                    182:     my @opt;
                    183:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.126     albertel  184:     &Apache::lonxml::debug("options:".join(':',@opt));
                    185: 
1.108     albertel  186:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
                    187: 					     -2,0);
1.81      sakharuk  188:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
1.22      albertel  189:     &Apache::lonxml::debug("Options are $#opt");
1.48      albertel  190:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    191:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    192: 					       $safeeval,'-2');
1.32      sakharuk  193:     if ($target eq 'web' || $target eq 'tex') {
1.107     sakharuk  194:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,@opt);
1.137     albertel  195:       $Apache::lonxml::post_evaluate=0;
1.25      albertel  196:     } elsif ( $target eq 'answer') {
1.48      albertel  197:       $result.=&displayanswers($max,$randomize,@opt);
1.136     foxr      198:       
1.34      albertel  199:     } elsif ( $target eq 'analyze') {
1.93      albertel  200: 	my @shown = &whichfoils($max,$randomize);
1.94      albertel  201: 	&Apache::response::analyze_store_foilgroup(\@shown,
                    202: 						  ['text','value','location']);
1.93      albertel  203: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    204: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.22      albertel  205:     } elsif ( $target eq 'grade') {
1.124     albertel  206:       if ( &Apache::response::submitted()) {
1.48      albertel  207: 	my @whichopt = &whichfoils($max,$randomize);
1.22      albertel  208: 	my $temp=1;my $name;
1.26      albertel  209: 	my %responsehash;
1.57      albertel  210: 	my %grade;
1.22      albertel  211: 	my $right=0;
                    212: 	my $wrong=0;
                    213: 	my $ignored=0;
                    214: 	foreach $name (@whichopt) {
1.74      albertel  215: 	  my $response=&Apache::response::getresponse($temp);
1.125     albertel  216: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
1.74      albertel  217: 	      $response = $opt[$response];
                    218: 	  }
1.22      albertel  219: 	  if ( $response =~ /[^\s]/) {
1.96      albertel  220: 	    $responsehash{$name}=$response;
1.22      albertel  221: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
1.34      albertel  222: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57      albertel  223: 	    if ($value eq $response) {
                    224: 		$grade{$name}='1'; $right++;
                    225: 	    } else {
                    226: 		$grade{$name}='0'; $wrong++;
                    227: 	    }
1.22      albertel  228: 	  } else {
                    229: 	    $ignored++;
                    230: 	  }
                    231: 	  $temp++;
                    232: 	}
1.28      albertel  233: 	my $part=$Apache::inputtags::part;
1.22      albertel  234: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  235: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57      albertel  236: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.28      albertel  237: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    238: 							   $part,$id);
1.22      albertel  239: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.75      albertel  240: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    241: 	    $responsestr;
                    242: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
                    243: 	
1.121     albertel  244: 	if ($Apache::lonhomework::type eq 'survey') {
                    245: 	    if ($ignored == 0) {
                    246: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    247: 		&Apache::response::handle_previous(\%previous,$ad);
                    248: 	    } elsif ($wrong==0 && $right==0) {
                    249: 	    } else {
                    250: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
                    251: 		&Apache::response::handle_previous(\%previous,$ad);
                    252: 	    }
                    253: 	} elsif (!$Apache::lonhomework::scantronmode) {
1.75      albertel  254: 	    my $ad;
                    255: 	    if ($wrong==0 && $ignored==0) {
                    256: 		$ad='EXACT_ANS';
                    257: 	    } elsif ($wrong==0 && $right==0) {
                    258: 		#nothing submitted
                    259: 	    } else {
                    260: 		if ($ignored==0) {
                    261: 		    $ad='INCORRECT';
                    262: 		} else {
                    263: 		    $ad='MISSING_ANSWER';
                    264: 		}
                    265: 	    }
                    266: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    267: 	    &Apache::response::handle_previous(\%previous,$ad);
1.22      albertel  268: 	} else {
1.75      albertel  269: 	    my $ad;
                    270: 	    if ($wrong==0 && $right==0) {
1.120     albertel  271: 		#nothing submitted only assign a score if we 
                    272: 		#need to override a previous grade
                    273: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
                    274: 		    $ad='ASSIGNED_SCORE';
                    275: 		}
1.75      albertel  276: 	    } else {
                    277: 		$ad='ASSIGNED_SCORE';
                    278: 	    }
                    279: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    280: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
                    281: 		$right/(scalar(@whichopt));
1.76      albertel  282: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
                    283: 		scalar(@whichopt);
1.1       albertel  284: 	}
1.22      albertel  285:       }
1.1       albertel  286:     }
1.147     foxr      287:     my $part_id     = $Apache::inputtags::part;
                    288:     my $response_id = $Apache::inputtags::response[-1];
1.146     foxr      289:     &Apache::lonxml::increment_counter(&getfoilcounts($max),
1.147     foxr      290: 				       "$part_id.$response_id");
1.146     foxr      291:     if ($target eq 'analyze') {
                    292: 	&Apache::lonhomework::set_bubble_lines();
                    293:     }
                    294: 
1.25      albertel  295:   } elsif ($target eq 'edit') {
1.22      albertel  296:     $result.=&Apache::edit::end_table();
1.74      albertel  297:   }
                    298:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.152     onken     299:     if($env{'form.pdfFormFields'} ne 'yes') {
1.74      albertel  300:       $result .= '\end{enumerate}';
1.152     onken     301:     } else {
                    302:       $result .= "\\\\";
                    303:     }
1.74      albertel  304:   }
1.110     albertel  305:   &Apache::response::poprandomnumber();
1.22      albertel  306:   return $result;
1.1       albertel  307: }
                    308: 
                    309: sub getfoilcounts {
1.48      albertel  310:   my ($max)=@_;
1.22      albertel  311:   # +1 since instructors will count from 1
                    312:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39      albertel  313:   if (&Apache::response::showallfoils()) { $max=$count; }
1.48      albertel  314:   if ($count>$max) { $count=$max } 
                    315:   &Apache::lonxml::debug("Count is $count from $max");
                    316:   return $count;
1.1       albertel  317: }
                    318: 
                    319: sub whichfoils {
1.77      albertel  320:     my ($max,$randomize)=@_;
                    321:     return &Apache::response::whichorder($max,$randomize,
                    322: 					 &Apache::response::showallfoils(),
                    323: 					 \%Apache::response::foilgroup);
1.1       albertel  324: }
                    325: 
1.25      albertel  326: sub displayanswers {
1.130     albertel  327:     my ($max,$randomize,@opt)=@_;
                    328:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
                    329:     my @names = @{ $Apache::response::foilgroup{'names'} };
                    330:     my @whichopt = &whichfoils($max,$randomize);
                    331:     my $result;
                    332:     if ($Apache::lonhomework::type eq 'exam') {
                    333: 	my $i = 0;
                    334: 	my %opt = map { ($_,$i++) } @opt;
                    335: 	
                    336: 	$i = 0;
                    337: 	my @alphabet = ('A'..'Z');
                    338: 	foreach my $name (@whichopt) {
                    339: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
                    340: 	    $result.=&Apache::response::answer_part('optionresponse',
                    341: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
                    342: 	    $result.=&Apache::response::answer_part('optionresponse',
                    343: 						    $Apache::response::foilgroup{$name.'.value'});
                    344: 	    $result.=&Apache::response::answer_footer('optionresponse');
                    345: 	}
                    346:     } else {
                    347: 	$result=&Apache::response::answer_header('optionresponse');
                    348: 	foreach my $name (@whichopt) {
                    349: 	    $result.=&Apache::response::answer_part('optionresponse',
                    350: 						    $Apache::response::foilgroup{$name.'.value'});
                    351: 	}
                    352: 	$result.=&Apache::response::answer_footer('optionresponse');
                    353:     }
                    354:     return $result;
1.25      albertel  355: }
                    356: 
1.106     albertel  357: sub check_for_invalid {
                    358:     my ($names,$options) = @_;
                    359:     my %bad_names;
                    360:     foreach my $name (@{ $names }) {
                    361: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    362: 	my $found=0;
                    363: 	foreach my $option (@{ $options }) {
                    364: 	    if ($value eq $option) { $found=1; }
                    365: 	}
                    366: 	if (!$found) { $bad_names{$name}=$value; }
                    367:     }
                    368:     if (%bad_names) {
                    369: 	my $error=&mt('The question can not be gotten correct, '.
                    370: 	    'the following foils in the &lt;optionresponse&gt; '.
                    371: 	    'have invalid correct options').' <br /><tt>'.
                    372: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
                    373: 	    "</tt>";
                    374: 	&Apache::lonxml::error($error);
                    375:     }
                    376: }
                    377: 
1.1       albertel  378: sub displayfoils {
1.107     sakharuk  379:   my ($target,$max,$randomize,$TeXlayout,@opt)=@_;
1.67      albertel  380:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
1.22      albertel  381:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    382:   my @truelist;
                    383:   my @falselist;
1.79      sakharuk  384:   my $result;  
1.22      albertel  385:   my $name;
1.101     albertel  386:   my $displayoptionintex=1;
1.72      sakharuk  387:   my @alphabet = ('A'..'Z');
1.48      albertel  388:   my @whichopt = &whichfoils($max,$randomize);
1.106     albertel  389:   &check_for_invalid(\@whichopt,\@opt);
1.28      albertel  390:   my $part=$Apache::inputtags::part;
                    391:   my $id=$Apache::inputtags::response[-1];
1.62      albertel  392:   my $break;
1.65      albertel  393:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
1.86      albertel  394:   if ( ($target ne 'tex') &&
                    395:        &Apache::response::show_answer() ) {
1.90      albertel  396:     my $temp=1;
1.22      albertel  397:     foreach $name (@whichopt) {
1.62      albertel  398: 	my $text=$Apache::response::foilgroup{$name.'.text'};
1.90      albertel  399: 	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
                    400: 	my $lastopt=$lastresponse{$name};
1.101     albertel  401: 	if ($text!~/^\s*$/) { $break='<br />'; }
1.62      albertel  402: 	$result.=$break;
1.113     sakharuk  403: 	if ($target eq 'web') {
1.62      albertel  404: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    405: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
                    406: 		if ($text=~/^\s*$/) {
                    407: 		    $text=$value.$text;
                    408: 		} else {
1.127     albertel  409: 		    $text='<b>'.$value.':</b> '.$text;
1.62      albertel  410: 		}
1.95      albertel  411: 	    } else {
                    412: 		$text='&#149;'.$text;
1.62      albertel  413: 	    }
                    414: 	    $result.=$text."\n";
1.112     sakharuk  415: 	}
1.58      sakharuk  416:       if ($Apache::lonhomework::type eq 'exam') {
1.90      albertel  417: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.58      sakharuk  418:       }
1.90      albertel  419:       $temp++;
1.22      albertel  420:     }
                    421:   } else {
                    422:     my $temp=1;
1.28      albertel  423:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.79      sakharuk  424:     my $internal_counter=$Apache::lonxml::counter;
1.22      albertel  425:     foreach $name (@whichopt) {
1.62      albertel  426:       my $text=$Apache::response::foilgroup{$name.'.text'};
                    427:       if ($text!~/^\s*$/) {
                    428: 	  if ($target eq 'tex') {
                    429: 	      $break='\vskip 0 mm ';
                    430: 	  } elsif ($target eq 'web') {
                    431: 	      $break='<br />';
                    432: 	  }
                    433:       }
1.28      albertel  434:       my $lastopt=$lastresponse{$name};
                    435:       my $optionlist="<option></option>\n";
1.152     onken     436: 
                    437:       if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes') {
                    438:           my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
                    439:           $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
                    440:       }
                    441: 
1.126     albertel  442:       foreach my $option (@opt) {
                    443: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
1.152     onken     444:           if ($option eq $lastopt) {
                    445:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
                    446:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
                    447:               } else {
                    448:                   $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
                    449:               }
                    450:           } else {
                    451:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
                    452:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
                    453:               } else {
                    454:                   $optionlist.="<option value='".$escopt."'>$option</option>\n";
                    455:               }
                    456:           }
1.28      albertel  457:       }
1.32      sakharuk  458:       if ($target ne 'tex') {
1.72      sakharuk  459: 	  if ($Apache::lonhomework::type ne 'exam') {
1.139     albertel  460: 	      $optionlist='<select onchange="javascript:setSubmittedPart(\''.
                    461: 		  $part.'\');" name="HWVAL_'.
1.101     albertel  462: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
1.59      albertel  463: 		  $optionlist."</select>\n";
1.101     albertel  464: 	  } else {
                    465: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
1.72      sakharuk  466: 	  }
1.101     albertel  467: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
                    468: 	      if ($Apache::lonhomework::type ne 'exam') {
                    469: 		  $text='&#149;'.$text;
                    470: 	      }
1.95      albertel  471: 	  } else {
1.101     albertel  472: 	      if ($Apache::lonhomework::type ne 'exam') {
                    473: 		  $text=$optionlist.$text;
                    474: 	      }
1.59      albertel  475: 	  }
1.62      albertel  476: 	  $result.=$break.$text."\n";
1.49      sakharuk  477: 	  if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  478: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.49      sakharuk  479: 	  }
1.32      sakharuk  480: 	  $temp++;
                    481:       } else {
1.101     albertel  482: 	  my $texoptionlist='';
                    483: 	  if ($displayoptionintex &&
                    484: 	      $Apache::lonhomework::type ne 'exam') {
1.107     sakharuk  485: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
1.101     albertel  486: 	  }
                    487: 	  if ($text=~/<drawoptionlist\s*\/>/) {
1.136     foxr      488: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
1.101     albertel  489: 	  }
                    490: 
                    491: 	  if ($text=~m/\\item /) {
1.50      sakharuk  492: 	      if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  493: 	          $text=~s/\\item/\\vskip 2 mm/;
1.152     onken     494: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
                    495:                   $result.= $texoptionlist.$text;
                    496:               }
                    497:           } else {
1.101     albertel  498: 	      if ($Apache::lonhomework::type eq 'exam') {
                    499: 		  $result.= $texoptionlist.'  '.$text;
1.152     onken     500: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
                    501: 		  if ($text=~/\S/) {
                    502:                       $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
                    503:                   } else {
                    504:                       $result.= $texoptionlist;
                    505:                   }
1.50      sakharuk  506: 	      }
1.32      sakharuk  507: 	  }
1.101     albertel  508: 	  if ($Apache::lonhomework::type eq 'exam') {
                    509: 	      $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
                    510: 		  $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
                    511: 		  '\end{enumerate} \vskip -8 mm \strut ';
                    512: 	      $internal_counter++;
                    513: 	  }
1.152     onken     514:           if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
                    515:               $text =~ s/.*indent(.*)$/$1/;
                    516:               $text = $1;
                    517:               $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
                    518:               $temp++;
                    519:           }
1.101     albertel  520: 	  $displayoptionintex=0;
1.62      albertel  521:       }
1.1       albertel  522:     }
1.62      albertel  523:   }
1.138     albertel  524: 
                    525:   if ($target eq 'web') {
1.143     albertel  526:       &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,
                    527: 						[\@whichopt,
                    528: 						 'submissiongrading']);
1.138     albertel  529:   }
                    530: 
1.32      sakharuk  531:   if ($target ne 'tex') {
1.62      albertel  532:       return $result.$break;
                    533:   } else {
1.32      sakharuk  534:       return $result;
1.22      albertel  535:   }
1.50      sakharuk  536: }
                    537: 
                    538: 
                    539: sub optionlist_correction {
1.107     sakharuk  540:     my ($TeXlayout,@options) = @_;
1.150     bisitz    541:     my $texoptionlist='\\item [] '.&mt('Choices:').' ';
1.107     sakharuk  542:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
1.101     albertel  543:     if (scalar(@options) > 0) {
                    544: 	foreach my $option (@options) {
                    545: 	    $texoptionlist.='{\bf '.
1.135     albertel  546: 		&Apache::lonxml::latex_special_symbols($option).
1.107     sakharuk  547: 		'}';
                    548: 	    if ($TeXlayout eq 'vertical') {
                    549: 		$texoptionlist.=' \vskip 0 mm ',
                    550: 	    } else {
1.115     sakharuk  551: 		$texoptionlist.=', ';
1.107     sakharuk  552: 	    }
1.101     albertel  553: 	}
1.116     albertel  554: 	$texoptionlist=~s/, $//;
1.107     sakharuk  555: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
1.80      sakharuk  556:     } else {
1.107     sakharuk  557: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
1.80      sakharuk  558:     }
1.50      sakharuk  559:     return $texoptionlist;
1.58      sakharuk  560: }
                    561: 
                    562: 
                    563: sub webbubbles {
1.90      albertel  564:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
1.58      sakharuk  565:     my @opt=@$ropt; 
                    566:     my @alphabet=@$ralphabet;
                    567:     my $result='';
1.90      albertel  568:     my $number_of_bubbles = $#opt + 1;
                    569:     $result.= '<table border="1"><tr>';
                    570:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
                    571: 	my $checked='';
                    572: 	if ($lastopt eq $opt[$ind]) {
                    573: 	    $checked=' checked="on" ';
1.58      sakharuk  574: 	}
1.90      albertel  575: 	$result.='<td><input type="radio" name="HWVAL_'.
                    576: 	    $Apache::inputtags::response['-1'].':'.$temp.
1.111     albertel  577: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
                    578: 	if ($alphabet[$ind]) {
                    579: 	    $result.=$alphabet[$ind].': ';
                    580: 	}
                    581: 	$result.=$opt[$ind].'</td>';
1.90      albertel  582:     }
                    583:     $result.='</tr></table>';
                    584:     return $result;
1.50      sakharuk  585: }
                    586: 
                    587: 
                    588: sub bubbles {
1.140     foxr      589:     my ($ralphabet,$ropt,$response, $max_width) = @_;
1.119     albertel  590:     my @alphabet = @$ralphabet;
1.50      sakharuk  591:     my @opt = @$ropt;
1.51      sakharuk  592:     my ($result,$head,$line) =('','','');
1.50      sakharuk  593:     my $number_of_bubbles = $#opt + 1;
1.51      sakharuk  594:     my $current_length = 0;
1.100     sakharuk  595:     my $textwidth;
1.141     albertel  596:     if (defined($max_width)) {
1.140     foxr      597: 	$textwidth=$max_width;
1.141     albertel  598: 	&Apache::lonxml::debug("Max width passed in: $max_width");
                    599:     } elsif ($env{'form.textwidth'} ne '') {
1.125     albertel  600: 	$env{'form.textwidth'}=~/(\d+)/;
1.100     sakharuk  601: 	$textwidth=$1;
1.141     albertel  602: 	&Apache::lonxml::debug("Max width from form: $textwidth");
1.100     sakharuk  603:     } else {
1.125     albertel  604: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.102     sakharuk  605: 	$textwidth=$1.'.'.$2;
1.141     albertel  606: 	&Apache::lonxml::debug("Max width defaults? $textwidth");
1.100     sakharuk  607:     }
1.141     albertel  608:     &Apache::lonxml::debug("Final maxwidth: $textwidth");
1.51      sakharuk  609:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
1.99      sakharuk  610: 	my $leftmargin;
1.101     albertel  611: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
1.119     albertel  612: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
1.99      sakharuk  613: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
1.140     foxr      614: 
1.102     sakharuk  615: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
1.99      sakharuk  616: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
1.51      sakharuk  617: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
                    618: 	    $head.='lr';
                    619: 	} else {
                    620: 	    $line=~s/\&\s*$//;
1.131     albertel  621: 	    $result.='\vskip -2 mm\noindent\setlength{\tabcolsep}{2 mm}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
1.51      sakharuk  622: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
                    623: 	    $head ='lr';
                    624: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
                    625: 	}
                    626: 
1.50      sakharuk  627:     }
                    628:     return $result;
1.1       albertel  629: }
                    630: 
1.22      albertel  631: 
1.2       albertel  632: sub start_conceptgroup {
1.22      albertel  633:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    634:   $Apache::optionresponse::conceptgroup=1;
1.44      albertel  635:   %Apache::response::conceptgroup=();
1.22      albertel  636:   my $result;
                    637:   if ($target eq 'edit') {
                    638:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30      matthew   639:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    640:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  641:   }
                    642:   if ($target eq 'modified') {
                    643:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    644:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    645:   }
                    646:   return $result;
1.2       albertel  647: }
                    648: 
                    649: sub end_conceptgroup {
1.22      albertel  650:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    651:   $Apache::optionresponse::conceptgroup=0;
                    652:   my $result='';
1.93      albertel  653:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    654:       $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  655:     #if not there aren't any foils to display and thus no question
1.93      albertel  656:       &Apache::response::pick_foil_for_concept($target,
                    657: 					       ['value','text','location'],
                    658: 					       \%Apache::hint::option,
                    659: 					       $parstack,$safeeval);
1.34      albertel  660:   } elsif ($target eq 'edit') {
1.22      albertel  661:     $result=&Apache::edit::end_table();
                    662:   }
                    663:   return $result;
1.2       albertel  664: }
                    665: 
1.16      albertel  666: sub insert_conceptgroup {
1.22      albertel  667:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    668:   return $result;
1.16      albertel  669: }
                    670: 
1.1       albertel  671: sub start_foil {
1.22      albertel  672:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    673:   my $result='';
1.34      albertel  674:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.105     albertel  675:       &Apache::lonxml::startredirection;
                    676:       if ($target eq 'analyze') {
                    677: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    678:       }
1.25      albertel  679:   } elsif ($target eq 'edit') {
1.22      albertel  680:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    681:     my $level='-2';
                    682:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
                    683:     my @opt;
                    684:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
                    685:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48      albertel  686:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
                    687: 					       ['unused',(@opt)],$token,'15');
                    688:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    689: 					     $safeeval,'-3');
                    690:     if ($randomize ne 'no') {
                    691: 	$result.=&Apache::edit::select_arg('Location:','location',
                    692: 					   ['random','top','bottom'],$token);
                    693:     }
1.30      matthew   694:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  695:   } elsif ($target eq 'modified') {
1.48      albertel  696:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    697: 						 'value','name','location');
1.22      albertel  698:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  699:   } 
1.22      albertel  700:   return $result;
1.1       albertel  701: }
                    702: 
                    703: sub end_foil {
1.22      albertel  704:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    705:   my $text ='';
                    706:   my $result = '';
1.88      albertel  707:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
1.48      albertel  708:       $text=&Apache::lonxml::endredirection;
1.114     sakharuk  709:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
1.107     sakharuk  710:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
1.32      sakharuk  711:   }
                    712:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
1.34      albertel  713:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  714:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.122     albertel  715:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    716: 	$text='\vskip 5mm $\triangleright$ '.$text;
                    717:     }
1.22      albertel  718:     if ($value ne 'unused') {
                    719:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    720:       &Apache::lonxml::debug("Got a name of :$name:");
1.129     albertel  721:       if ($name eq "") {
1.151     bisitz    722: 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.123     albertel  723: 	  $name=$Apache::lonxml::curdepth;
                    724:       }
1.22      albertel  725:       &Apache::lonxml::debug("Using a name of :$name:");
1.103     albertel  726:       if (defined($Apache::response::foilnames{$name})) {
1.151     bisitz    727: 	  &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  728:       }
1.104     albertel  729:       $Apache::response::foilnames{$name}++;
1.103     albertel  730:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.39      albertel  731:       if ( $Apache::optionresponse::conceptgroup
                    732: 	   && !&Apache::response::showallfoils() ) {
1.22      albertel  733: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    734: 	$Apache::response::conceptgroup{"$name.value"} = $value;
1.122     albertel  735: 	$Apache::response::conceptgroup{"$name.text"} = $text;
1.48      albertel  736: 	$Apache::response::conceptgroup{"$name.location"} = $location;
1.22      albertel  737:       } else {
                    738: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48      albertel  739: 	$Apache::response::foilgroup{"$name.value"} = $value;
1.122     albertel  740: 	$Apache::response::foilgroup{"$name.text"} = $text;
1.48      albertel  741: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.22      albertel  742:       }
                    743:     }
                    744:   }
                    745:   if ($target eq 'edit') {
                    746:     $result.= &Apache::edit::tag_end($target,$token,'');
                    747:   }
                    748:   return $result;
1.1       albertel  749: }
                    750: 
1.112     sakharuk  751: sub start_drawoptionlist {
                    752:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.144     albertel  753:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
1.117     albertel  754: 	return $token->[4];
                    755:     }
1.112     sakharuk  756: }
                    757: 
                    758: sub end_drawoptionlist {
                    759:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.144     albertel  760:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
1.117     albertel  761: 	return $token->[2];
                    762:     }
1.112     sakharuk  763: }
                    764: 
1.7       albertel  765: sub insert_foil {
1.144     albertel  766:     return '
1.15      albertel  767: <foil name="" value="unused">
1.14      albertel  768: <startouttext />
                    769: <endouttext />
1.7       albertel  770: </foil>';
                    771: }
1.144     albertel  772: 
                    773: sub insert_drawoptionlist {
                    774:     return '<drawoptionlist />';
                    775: }
1.1       albertel  776: 1;
                    777: __END__
                    778:  

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