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

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

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