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

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

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