File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.65: download - view: text, annotated - select for diffs
Wed Aug 29 10:07:42 2007 UTC (16 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Make anaylze target produce a bubble_lines entry that describes how many
lines on an answer bubble sheet the problem requires for its answer.

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

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