File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.70: download - view: text, annotated - select for diffs
Fri Aug 8 16:37:13 2008 UTC (15 years, 8 months ago) by bisitz
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_1, version_2_7_0, HEAD
Localization:
- Added missing &mt() call to "Foils without names" warning
- Optimized &mt() call for "Foil name ... appears more than once" warning
- Added some other missing &mt() calls

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

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