File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.72: download - view: text, annotated - select for diffs
Fri Oct 24 16:22:54 2008 UTC (15 years, 6 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Localization, wording, nobreaks for Colorful Editor
- Added missing &mt() calls at several places (more to be done)
- Same wording "Delete?" for all Delete options
- Same wording style, e.g. added ":" after text and before optionlist
- Added nobreak style for some elements which need to stay together, e.g. help_open_topic in loncommon.pm
- Added/updated some related German translations

    1: # The LearningOnline Network with CAPA
    2: # Full matching style response
    3: #
    4: # $Id: matchresponse.pm,v 1.72 2008/10/24 16:22:54 bisitz Exp $
    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: #
   28: 
   29: package Apache::matchresponse;
   30: use strict;
   31: use HTML::Entities();
   32: use Math::Random();
   33: use Apache::optionresponse();
   34: use Apache::lonlocal;
   35: use Apache::lonnet;
   36: use Apache::lonxml;
   37: 
   38: BEGIN {
   39:     &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
   40: }
   41: 
   42: sub start_matchresponse {
   43:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   44:     my $result;
   45:     #when in a matchresponse use these
   46:     &Apache::lonxml::register('Apache::matchresponse',
   47: 			      ('foilgroup','foil','conceptgroup','item',
   48: 			       'itemgroup'));
   49:     push (@Apache::lonxml::namespace,'matchresponse');
   50:     my $id = &Apache::response::start_response($parstack,$safeeval);
   51:     %Apache::hint::match=();
   52:     undef(%Apache::response::foilnames);
   53:     if ($target eq 'meta') {
   54: 	$result=&Apache::response::meta_package_write('matchresponse');
   55:     } elsif ($target eq 'edit' ) {
   56: 	$result.=&Apache::edit::start_table($token).
   57: 	    '<tr><td>'.&Apache::lonxml::description($token)."</td>"
   58:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
   59: 	   .&Apache::edit::deletelist($target,$token)
   60:            .'</span></td>'
   61:            ."<td>&nbsp".&Apache::edit::end_row()
   62: 	   .&Apache::edit::start_spanning_row();
   63: 	
   64: 	$result.=
   65: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
   66: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
   67: 				      ['yes','no'],$token).
   68: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
   69:     } elsif ($target eq 'modified') {
   70: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   71: 						     $safeeval,'max',
   72: 						     'randomize');
   73: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   74:     } elsif ($target eq 'analyze') {
   75: 	my $part_id="$Apache::inputtags::part.$id";
   76:         $Apache::lonhomework::analyze{"$part_id.type"} = 'matchresponse';
   77: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   78: 	push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
   79: 	      1);
   80:     }
   81:     return $result;
   82: }
   83: 
   84: sub end_matchresponse {
   85:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   86:     my $result;
   87:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   88:     &Apache::response::end_response;
   89:     pop @Apache::lonxml::namespace;
   90:     &Apache::lonxml::deregister('Apache::matchresponse',
   91: 				('foilgroup','foil','conceptgroup'));
   92:     undef(%Apache::response::foilnames);
   93:     return $result;
   94: }
   95: 
   96: %Apache::response::itemgroup=();
   97: sub start_itemgroup {
   98:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   99:     my $result;
  100:     %Apache::response::itemgroup=();
  101:     %Apache::matchresponse::itemtable=();
  102: 
  103:     if ($target eq 'edit') {
  104: 	$result=&Apache::edit::tag_start($target,$token);
  105: 	$result.=&Apache::edit::select_arg('Randomize Order:','randomize',
  106: 					   ['yes','no'],$token);
  107: 	$result.=&Apache::edit::select_arg('Items Display Location:',
  108: 					   'location',
  109: 					   ['top','bottom','left','right'],
  110: 					   $token);
  111: 	$result.=&Apache::edit::select_arg('Items Display Directection:',
  112: 					   'direction',
  113: 					   ['vertical','horizontal'],
  114: 					   $token);
  115: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  116:     } elsif ($target eq 'modified') {
  117: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  118: 						     $safeeval,'randomize',
  119: 						     'location','direction');
  120: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  121:     } elsif ($target eq 'web' or $target eq 'tex') {
  122: 	$Apache::matchresponse::itemtable{'location'}=
  123: 	    &Apache::lonxml::get_param('location',$parstack,$safeeval);
  124: 	$Apache::matchresponse::TeXitemgroupwidth=&Apache::lonxml::get_param('TeXitemgroupwidth',$parstack,$safeeval,undef,0);
  125:     }
  126:     return $result;
  127: }
  128: 
  129: sub end_itemgroup {
  130:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  131:     my $result;
  132: 
  133:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
  134:     if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
  135:     my @names=@{ $Apache::response::itemgroup{'names'} };
  136:     my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
  137:     if ($randomize ne 'no' ) {
  138: 	@names=&Apache::response::whichorder($#names+1,$randomize,0,
  139: 					 \%Apache::response::itemgroup);
  140:     }
  141:     if ($target eq 'analyze') {
  142: 	my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  143: 	push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
  144:     }
  145:     my %letter_name_map;
  146:     my %name_letter_map;
  147:     my @alphabet=('A'..'Z');
  148:     my $i=0;
  149:     foreach my $name (@names) {
  150: 	$letter_name_map{$alphabet[$i]}=$name;
  151: 	$name_letter_map{$name}=$alphabet[$i];
  152: 	$i++;
  153:     }
  154:     $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
  155:     $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
  156:     my $direction=&Apache::lonxml::get_param('direction',$parstack,$safeeval);
  157:     if ($target eq 'web') {
  158: 	
  159: 	my $table='<br /><table>'; # extra space to match what latex does.
  160: 	my $i=0;
  161: 	if ($direction eq 'horizontal') { $table .='<tr>';}
  162: 	foreach my $name (@names) {
  163: 	    if ($direction ne 'horizontal') { $table.='<tr>'; }
  164: 	    $table.='<td>'.$alphabet[$i].'</td><td>'.
  165: 		$Apache::response::itemgroup{$name.'.text'}.'</td>';
  166: 	    if ($direction ne 'horizontal') { $table.='</tr>'; }
  167: 	    $i++;
  168: 	}
  169: 	if ($direction eq 'horizontal') { $table .='</tr>';}
  170: 	$table.='</table>';
  171: 	$Apache::matchresponse::itemtable{'display'}=$table;
  172: 	$Apache::lonxml::post_evaluate=0;
  173:     } elsif ($target eq 'tex') {
  174: 	my $table=' \begin{description}\setlength{\leftmargin}{2em}\setlength{\labelwidth}{1em}\setlength{\itemsep}{0.5pt plus1pt minus2pt}\setlength{\listparindent}{0em} ';
  175: 	my $i=0;
  176: 	foreach my $name (@names) {
  177: 	    # $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
  178: 	    $table.='\item['.$alphabet[$i].'] '.
  179: 		$Apache::response::itemgroup{$name.'.text'};
  180: 	    $i++;
  181: 	}
  182: 	$table.=' \end{description}  \strut ';
  183: 	if ($Apache::lonhomework::type eq 'exam') {$table.='\vskip -13 mm \strut ';}
  184: 	$Apache::matchresponse::itemtable{'display'}=$table;
  185: 	$Apache::lonxml::post_evaluate=0;
  186:     }
  187:     return $result;
  188: }
  189: 
  190: sub start_item {
  191:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  192:     my $result='';
  193:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  194: 	&Apache::lonxml::startredirection;
  195:     } elsif ($target eq 'edit') {
  196: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  197: 						 $safeeval,'-2');
  198: 	$result=&Apache::edit::tag_start($target,$token,"Item");
  199: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  200: 	if ($randomize ne 'no') {
  201: 	    $result.=&Apache::edit::select_arg('Location:','location',
  202: 					       ['random','top','bottom'],
  203: 					       $token);
  204: 	}
  205: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  206:     } elsif ($target eq 'modified') {
  207: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  208: 						     $safeeval,'name',
  209: 						     'location');
  210: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  211:     }
  212:     return $result;
  213: }
  214: 
  215: sub end_item {
  216:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  217:     my $text ='';
  218:     my $result = '';
  219:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  220: 	$text=&Apache::lonxml::endredirection;
  221:     }
  222:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  223: 	$target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
  224: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  225: 	my $location=&Apache::lonxml::get_param('location',$parstack,
  226: 						$safeeval);
  227: 	&Apache::lonxml::debug("Got a name of :$name:");
  228: 	if ($name eq "") { $name=$Apache::lonxml::curdepth; }
  229: 	&Apache::lonxml::debug("Using a name of :$name:");
  230: 	push @{ $Apache::response::itemgroup{'names'} }, $name;
  231: 	$Apache::response::itemgroup{"$name.text"} = $text;
  232: 	$Apache::response::itemgroup{"$name.location"} = $location;
  233:     }
  234:     if ($target eq 'edit') {
  235: 	$result.= &Apache::edit::tag_end($target,$token,'');
  236:     }
  237:     return $result;
  238: }
  239: 
  240: sub insert_item {
  241:     return '
  242: <item name="">
  243: <startouttext />
  244: <endouttext />
  245: </item>';
  246: }
  247: 
  248: %Apache::response::foilgroup=();
  249: sub start_foilgroup {
  250:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  251:     my $result;
  252:     %Apache::response::foilgroup=();
  253:     $Apache::matchresponse::conceptgroup=0;
  254:     &Apache::response::pushrandomnumber();
  255:     if ($target eq 'edit') {
  256: 	$result.=&Apache::edit::start_table($token)
  257: 	    ."<tr><td>".&mt('Collection Of Foils')."</td>"
  258:             .'<td><span class="LC_nobreak">'.&mt('Delete?')
  259: 	    .&Apache::edit::deletelist($target,$token)
  260: 	    .'</span></td>'
  261:             ."<td>&nbsp;".&Apache::edit::end_row()
  262:             .&Apache::edit::start_spanning_row()."\n";
  263:     }
  264:     return $result;
  265: }
  266: 
  267: sub end_foilgroup {
  268:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  269:     my $result;
  270:     my $part        = $Apache::inputtags::part;
  271:     my $response_id = $Apache::inputtags::response[-1];
  272: 
  273:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
  274: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  275: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  276: 						   $safeeval,'-2');
  277: 	if ($target eq 'web' || $target eq 'tex') {
  278: 	    $result=&displayfoils($target,$max,$randomize);
  279: 	    $Apache::lonxml::post_evaluate=0;
  280: 	} elsif ($target eq 'answer' ) {
  281: 	    $result=&displayanswers($max,$randomize);
  282: 	} elsif ( $target eq 'grade') {
  283: 	    &grade_response($max,$randomize);
  284: 	} elsif ( $target eq 'analyze') {
  285: 	    my @shown=&whichfoils($max,$randomize);
  286: 	    &Apache::response::analyze_store_foilgroup(\@shown,
  287: 						 ['text','value','location']);
  288: 	    #FIXME need to store options in some way
  289: 	}
  290: 	&Apache::lonxml::increment_counter(&getfoilcounts($max), 
  291: 					   "$part.$response_id");
  292: 	if ($target eq 'analyze') {
  293: 	    &Apache::lonhomework::set_bubble_lines();
  294: 	}
  295: 
  296:     } elsif ($target eq 'edit') {
  297: 	$result=&Apache::edit::end_table();
  298:     }
  299:     &Apache::response::poprandomnumber();
  300:     return $result;
  301: }
  302: 
  303: sub whichfoils {
  304:     my ($max,$randomize)=@_;
  305:     return &Apache::response::whichorder(&getfoilcounts($max),
  306: 					 $randomize,
  307: 					 &Apache::response::showallfoils(),
  308: 					 \%Apache::response::foilgroup);
  309: }
  310: 
  311: sub displayanswers {
  312:     my ($max,$randomize,@opt)=@_;
  313:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  314:     my @names = @{ $Apache::response::foilgroup{'names'} };
  315:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
  316: 						   $randomize,
  317: 					  &Apache::response::showallfoils(),
  318: 					 \%Apache::response::foilgroup);
  319:     my %name_letter_map;
  320:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
  321: 	%name_letter_map=
  322: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
  323:     }
  324:     my $result;
  325:     if ($Apache::lonhomework::type eq 'exam') {
  326: 	my $i=0;
  327: 	foreach my $name (@whichfoils) {
  328: 	    $result.=&Apache::response::answer_header('matchresponse',$i++);
  329: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
  330: 	    my $letter=$name_letter_map{$value_name};
  331: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
  332: 	    $result.=&Apache::response::answer_footer('matchresponse');
  333: 	}
  334:     } else {
  335: 	$result=&Apache::response::answer_header('matchresponse');
  336: 	foreach my $name (@whichfoils) {
  337: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
  338: 	    my $letter=$name_letter_map{$value_name};
  339: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
  340: 	}
  341: 	$result.=&Apache::response::answer_footer('matchresponse');
  342:     }
  343:     return $result;
  344: }
  345: 
  346: 
  347: sub grade_response {
  348:     my ($max,$randomize)=@_;
  349:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  350: 						   $randomize,
  351: 				            &Apache::response::showallfoils(),
  352: 				           \%Apache::response::foilgroup);
  353:     if (!&Apache::response::submitted()) { return; }
  354:     my %responsehash;
  355:     my %grade;
  356:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  357:     my %letter_name_map;
  358:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
  359: 	%letter_name_map=
  360: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
  361:     }
  362:     my @items;
  363:     foreach my $name (@whichfoils) {
  364: 	my $response = &Apache::response::getresponse($temp,'letter');
  365: 	push(@items,$response);
  366: 	my $responsename = $letter_name_map{$response};
  367: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  368: 	if ( $response =~ /[^\s]/) {
  369: 	    $responsehash{$name}=$responsename;
  370: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
  371: 	    if ($value eq $responsename) {
  372: 		$grade{$name}='1'; $right++;
  373: 	    } else {
  374: 		$grade{$name}='0'; $wrong++;
  375: 	    }
  376: 	} else {
  377: 	    $ignored++;
  378: 	}
  379: 	$temp++;
  380:     }
  381:     my $part=$Apache::inputtags::part;
  382:     my $id = $Apache::inputtags::response['-1'];
  383:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  384:     my $itemstr    =&Apache::lonnet::array2str(@items);
  385:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  386:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  387:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
  388:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  389: 	$responsestr;
  390:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
  391: 	$itemstr;
  392:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
  393: 	$gradestr;
  394:     if ($Apache::lonhomework::type eq 'survey') {
  395: 	if ($ignored == 0) {
  396: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  397: 	    &Apache::response::handle_previous(\%previous,$ad);
  398: 	} elsif ($wrong==0 && $right==0) {
  399: 	} else {
  400: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  401: 	    &Apache::response::handle_previous(\%previous,$ad);
  402: 	}
  403:     } elsif (!$Apache::lonhomework::scantronmode) {
  404: 	my $ad;
  405: 	if ($wrong==0 && $ignored==0) {
  406: 	    $ad='EXACT_ANS';
  407: 	} elsif ($wrong==0 && $right==0) {
  408: 	    #nothing submitted
  409: 	} else {
  410: 	    if ($ignored==0) {
  411: 		$ad='INCORRECT';
  412: 	    } else {
  413: 		$ad='MISSING_ANSWER';
  414: 	    }
  415: 	}
  416: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  417: 	&Apache::response::handle_previous(\%previous,$ad);
  418:     } else {
  419: 	my $ad;
  420: 	if ($wrong==0 && $right==0) {
  421: 	    #nothing submitted
  422: 	} else {
  423: 	    $ad='ASSIGNED_SCORE';
  424: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  425: 		$ad;
  426: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  427: 		$right/(scalar(@whichfoils));
  428: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  429: 		scalar(@whichfoils);
  430: 	}
  431:     }
  432: }
  433: 
  434: sub format_prior_answer {
  435:     my ($mode,$answer,$other_data) = @_;
  436:     my %answer      =&Apache::lonnet::str2hash($answer);
  437:     my $foil_order  =$other_data->[0];
  438:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
  439:     my @items       =&Apache::lonnet::str2array($other_data->[2]);
  440:     my $output;
  441:     
  442:     foreach my $name (@{ $foil_order }) {
  443: 	my $item=shift(@items);
  444: 	$output .= '<tr><td>'.$item.'</td></tr>';
  445:     }
  446:     return if (!defined($output));
  447: 
  448:     $output = '<table class="LC_prior_match">'.$output.'</table>';
  449:     return $output;
  450: }
  451: 
  452: sub itemdisplay {
  453:     my ($location)=@_;
  454:     if ($location eq 'top' &&
  455: 	!defined($Apache::matchresponse::itemtable{'location'})) {
  456: 	return $Apache::matchresponse::itemtable{'display'};
  457:     }
  458:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
  459: 	return $Apache::matchresponse::itemtable{'display'};
  460:     }
  461:     return undef;
  462: }
  463: 
  464: sub displayfoils {
  465:     my ($target,$max,$randomize)=@_;
  466:     my ($tabsize, $lefttabsize, $righttabsize);
  467: 
  468:     my $result;
  469:     my $question;
  470:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  471: 						   $randomize,
  472: 				            &Apache::response::showallfoils(),
  473: 				           \%Apache::response::foilgroup);
  474:     my $part=$Apache::inputtags::part;
  475:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  476:     my %letter_name_map;
  477:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
  478: 	%letter_name_map=
  479: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
  480:     }
  481:     my %name_letter_map;
  482:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
  483: 	%name_letter_map=
  484: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
  485:     }
  486:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  487: 	foreach my $name (@whichfoils) {
  488: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  489: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  490: 	    my $letter=$name_letter_map{$value};
  491: 	    if ($target eq 'tex') {
  492: 		$question.=' \\\\ '.$letter.':'.$text;
  493: 	    } else {
  494: 		$question.='<br /><b>'.$letter.':</b> '.$text;
  495: 	    }
  496: 	}
  497:     } else {
  498: 	my $i = 0;
  499: 	my $temp=1;
  500: 	my $id=$Apache::inputtags::response[-1];
  501: 	my $part=$Apache::inputtags::part;
  502: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  503: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
  504: 	my @alphabet=('A'..'Z');
  505: 	my @used_letters=sort(keys(%letter_name_map));
  506: 	my $internal_counter=$Apache::lonxml::counter;
  507: 	foreach my $name (@whichfoils) {
  508: 	    my $lastopt=$lastresponse{$name};
  509: 	    my $last_letter=$name_letter_map{$lastopt};
  510: 	    my $optionlist = '';
  511: 	    if ($target ne 'tex') {
  512: 		$optionlist="<option></option>\n";
  513: 	    } else {
  514: 		if ($Apache::lonhomework::type ne 'exam') {
  515:                     if($env{'form.pdfFormFields'} eq 'yes') {
  516:                         my $fieldname = $env{'request.symb'} . '&part_'. $Apache::inputtags::part .'&matchresponse'. '&HWVAL_' . $Apache::inputtags::response['-1'] . ':' . $temp . '&submit_' . $Apache::inputtags::part . '&';
  517:                         $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
  518:                     } else {
  519:                         $optionlist='\framebox[10 mm][s]{\tiny\strut}';
  520:                     }
  521:                  }
  522: 	    }
  523: 	    my $option;
  524: 	    foreach $option (@used_letters) {
  525: 		if ($option eq $last_letter) {
  526: 		    if ($target ne 'tex') {
  527:                         $optionlist.="<option selected=\"on\">$option</option>\n";
  528:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  529:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  530:                     }
  531: 		} else {
  532: 		    if ($target ne 'tex') {
  533:                         $optionlist.="<option>$option</option>\n";
  534:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  535:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  536:                     }
  537: 		}
  538: 	    }
  539: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  540: 	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  541: 		  $part.'\');" name="HWVAL_'.
  542: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  543: 		        $optionlist."</select>\n";
  544: 	    }
  545: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  546: 	    #
  547:             #  Factor out the tex computations of the left/right 1/2 minipage
  548: 	    #  widths for left or right positioning.  This allows us
  549: 	    #  to, if necessary constrain the bubble widths:
  550: 	    #
  551: 	    if ($target eq 'tex'   &&
  552: 		(&itemdisplay('left') || &itemdisplay('right'))) {
  553: 		$tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
  554: 		($lefttabsize,$righttabsize)=(0,0);
  555: 		if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
  556: 		    $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
  557: 		    $lefttabsize=$tabsize*$1/100;	
  558: 		    $righttabsize=0.95*($tabsize-$lefttabsize);
  559: 		} else {
  560: 		    $tabsize=~/(\d+\.?\d*)/;
  561: 		    $lefttabsize=$1/2.1;
  562: 		    $righttabsize=0.95*($1-$lefttabsize);
  563: 		}
  564: 	    }
  565: 	    if ($target ne 'tex') {
  566: 		if ($Apache::lonhomework::type ne 'exam') {
  567: 		    $question.="<br />\n".$optionlist.$text;
  568: 		} else {
  569: 		    $question.="<br />\n".$text;
  570: 		}
  571: 		if ($Apache::lonhomework::type eq 'exam') {
  572: 		    my @blank;
  573: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
  574: 		}
  575: 	    } else {		
  576: 		# Note that if left or right positioned, we must
  577: 		# confine the bubbles to righttabsize:
  578: 		#
  579: 		if ($Apache::lonhomework::type eq 'exam' && $env{'form.pdfFormFields'} ne 'yes') {
  580: 		    $question.=' '.$optionlist.$text."\n";
  581: 		    my @emptyItems = ();
  582: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
  583: 		    $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
  584: 			.'\item \hskip -3mm \textbf{'.$internal_counter.'}';
  585: 		    if (&itemdisplay('left') || &itemdisplay('right')) {
  586: 			$question .= &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems, "", $righttabsize);
  587: 		    }
  588: 		    else {
  589: 			$question .= &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems);
  590: 		    }
  591: 		    $question .= '\end{list} \vskip -8 mm \strut ';
  592: 		    $internal_counter++;
  593: 	        } else {
  594:                     if($env{'form.pdfFormFields'} eq 'yes') {
  595:                         $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
  596:                     } else {
  597:                         $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
  598:                     }
  599: 		}
  600:             } 
  601: 	    $temp++;
  602: 	}
  603:     }
  604:     if ($result=&itemdisplay('top')) {
  605: 	$result.=$question;
  606:     } elsif ($result=&itemdisplay('bottom')) {
  607: 	$result=$question.$result;
  608:     } elsif ($result=&itemdisplay('right')) {
  609: 	if ($target ne 'tex') {
  610: 	    #remove the first <br />
  611: 	    $question=~s|<br />||;
  612: 	    $result='<table><tr><td valign="top">'.$question.
  613: 		'</td><td valign="top">'.$result.'</td></tr></table>';
  614: 	} else {
  615: 	    $lefttabsize.=' mm ';
  616: 	    $righttabsize.=' mm ';
  617: 	    $result='\setlength{\tabcolsep}{1 mm}\begin{tabular}{p{'.$righttabsize.'}p{'.$lefttabsize.'}}\begin{minipage}{'.$righttabsize.'}'.$question.'\end{minipage}&\begin{minipage}{'.$lefttabsize.'}'.$result.'\end{minipage}\end{tabular}';
  618: 	}
  619:     } elsif ($result=&itemdisplay('left')) {
  620: 	if ($target ne 'tex') {
  621: 	    #remove the first <br />
  622: 	    $question=~s|<br />||;
  623: 	    $result='<table><tr><td valign="top">'.$result.
  624: 		'</td><td valign="top">'.$question.'</td></tr></table>';
  625: 	} else {
  626: 	    $lefttabsize.=' mm ';
  627: 	    $righttabsize.=' mm ';
  628: 	    $result='\setlength{\tabcolsep}{1 mm}\begin{tabular}{p{'.$lefttabsize.'}p{'.$righttabsize.'}}\begin{minipage}{'.$lefttabsize.'}'.$result.'\end{minipage}&\begin{minipage}{'.$righttabsize.'}'.$question.'\end{minipage}\end{tabular}';
  629: 	}
  630:     }
  631:     if ($target eq 'web') {
  632: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  633: 						  [\@whichfoils,
  634: 						   'submissiongrading',
  635: 						   'submissionitems']);
  636:     }
  637:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
  638:     return $result;
  639: }
  640: 
  641: sub getfoilcounts {
  642:     my ($max)=@_;
  643:     # +1 since instructors will count from 1
  644:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  645:     if (&Apache::response::showallfoils()) { $max=$count; }
  646:     if ($count>$max) { $count=$max } 
  647:     &Apache::lonxml::debug("Count is $count from $max");
  648:     return $count;
  649: }
  650: 
  651: 
  652: sub start_conceptgroup {
  653:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  654:     $Apache::matchresponse::conceptgroup=1;
  655:     %Apache::response::conceptgroup=();
  656:     my $result;
  657:     if ($target eq 'edit') {
  658: 	$result.=&Apache::edit::tag_start($target,$token,
  659: 					  "Concept Grouped Foils");
  660: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  661: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  662:     }
  663:     if ($target eq 'modified') {
  664: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  665: 						     $safeeval,'concept');
  666: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  667:     }
  668:     return $result;
  669: }
  670: 
  671: sub end_conceptgroup {
  672:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  673:     $Apache::matchresponse::conceptgroup=0;
  674:     my $result='';
  675:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  676: 	$target eq 'tex' || $target eq 'analyze') {
  677: 	&Apache::response::pick_foil_for_concept($target,
  678: 						 ['value','text','location'],
  679: 						 \%Apache::hint::match,
  680: 						 $parstack,$safeeval);
  681:     } elsif ($target eq 'edit') {
  682: 	$result=&Apache::edit::end_table();
  683:     }
  684:     return $result;
  685: }
  686: 
  687: sub insert_conceptgroup {
  688:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  689:     return $result;
  690: }
  691: 
  692: sub start_foil {
  693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  694:     my $result='';
  695:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  696: 	&Apache::lonxml::startredirection;
  697: 	if ($target eq 'analyze') {
  698: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  699: 	}
  700:     } elsif ($target eq 'edit') {
  701: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  702: 	my $level='-2';
  703: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  704: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  705: 	my @names;
  706: 	if (defined(@{ $Apache::response::itemgroup{'names'} })) {
  707: 	    @names=@{ $Apache::response::itemgroup{'names'} };
  708: 	}
  709: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
  710: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  711: 						 $safeeval,'-3');
  712: 	if ($randomize ne 'no') {
  713: 	    $result.=&Apache::edit::select_arg('Location:','location',
  714: 					     ['random','top','bottom'],$token);
  715: 	}
  716: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  717:     } elsif ($target eq 'modified') {
  718: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  719: 						     $safeeval,'value',
  720: 						     'name','location');
  721: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  722:     }
  723:     return $result;
  724: }
  725: 
  726: sub end_foil {
  727:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  728:     my $text ='';
  729:     my $result = '';
  730:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  731: 	$text=&Apache::lonxml::endredirection;
  732:     }
  733: 
  734:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  735: 	$target eq 'tex' || $target eq 'analyze') {
  736: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  737: 	    $text='\vskip 5mm $\triangleright$ '.$text;
  738: 	}
  739: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  740: 	if ($value ne 'unused') {
  741: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  742: 	    &Apache::lonxml::debug("Got a name of :$name:");
  743: 	    if (!$name) {
  744: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  745: 		$name=$Apache::lonxml::curdepth;
  746: 	    }
  747: 	    &Apache::lonxml::debug("Using a name of :$name:");
  748: 	    if (defined($Apache::response::foilnames{$name})) {
  749: 		&Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
  750: 	    }
  751: 	    $Apache::response::foilnames{$name}++;
  752: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  753: 						     $safeeval);
  754: 	    if ( $Apache::matchresponse::conceptgroup
  755: 		 && !&Apache::response::showallfoils() ) {
  756: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  757: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  758: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  759: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  760: 	    } else {
  761: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  762: 		$Apache::response::foilgroup{"$name.value"} = $value;
  763: 		$Apache::response::foilgroup{"$name.text"} = $text;
  764: 		$Apache::response::foilgroup{"$name.location"} = $location;
  765: 	    }
  766: 	}
  767:     }
  768:     if ($target eq 'edit') {
  769: 	$result.= &Apache::edit::tag_end($target,$token,'');
  770:     }
  771:     return $result;
  772: }
  773: 
  774: sub insert_foil {
  775:     return '
  776: <foil name="" value="unused">
  777: <startouttext />
  778: <endouttext />
  779: </foil>';
  780: }
  781: 1;
  782: __END__

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