File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.66: download - view: text, annotated - select for diffs
Mon Oct 8 09:22:50 2007 UTC (16 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
bz4074 - Use lonxml subs to maintain the number of bubble lines required
for responses to these types of problems.

    1: # The LearningOnline Network with CAPA
    2: # Full matching style response
    3: #
    4: # $Id: matchresponse.pm,v 1.66 2007/10/08 09:22:50 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: 					   $Apache::inputtags::part);
  283: 	if ($target eq 'analyze') {
  284: 	    &Apache::lonhomework::set_bubble_lines();
  285: 	}
  286: 
  287:     } elsif ($target eq 'edit') {
  288: 	$result=&Apache::edit::end_table();
  289:     }
  290:     &Apache::response::poprandomnumber();
  291:     return $result;
  292: }
  293: 
  294: sub whichfoils {
  295:     my ($max,$randomize)=@_;
  296:     return &Apache::response::whichorder(&getfoilcounts($max),
  297: 					 $randomize,
  298: 					 &Apache::response::showallfoils(),
  299: 					 \%Apache::response::foilgroup);
  300: }
  301: 
  302: sub displayanswers {
  303:     my ($max,$randomize,@opt)=@_;
  304:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  305:     my @names = @{ $Apache::response::foilgroup{'names'} };
  306:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
  307: 						   $randomize,
  308: 					  &Apache::response::showallfoils(),
  309: 					 \%Apache::response::foilgroup);
  310:     my %name_letter_map;
  311:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
  312: 	%name_letter_map=
  313: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
  314:     }
  315:     my $result;
  316:     if ($Apache::lonhomework::type eq 'exam') {
  317: 	my $i=0;
  318: 	foreach my $name (@whichfoils) {
  319: 	    $result.=&Apache::response::answer_header('matchresponse',$i++);
  320: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
  321: 	    my $letter=$name_letter_map{$value_name};
  322: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
  323: 	    $result.=&Apache::response::answer_footer('matchresponse');
  324: 	}
  325:     } else {
  326: 	$result=&Apache::response::answer_header('matchresponse');
  327: 	foreach my $name (@whichfoils) {
  328: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
  329: 	    my $letter=$name_letter_map{$value_name};
  330: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
  331: 	}
  332: 	$result.=&Apache::response::answer_footer('matchresponse');
  333:     }
  334:     return $result;
  335: }
  336: 
  337: 
  338: sub grade_response {
  339:     my ($max,$randomize)=@_;
  340:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  341: 						   $randomize,
  342: 				            &Apache::response::showallfoils(),
  343: 				           \%Apache::response::foilgroup);
  344:     if (!&Apache::response::submitted()) { return; }
  345:     my %responsehash;
  346:     my %grade;
  347:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  348:     my %letter_name_map;
  349:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
  350: 	%letter_name_map=
  351: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
  352:     }
  353:     my @items;
  354:     foreach my $name (@whichfoils) {
  355: 	my $response = &Apache::response::getresponse($temp,'letter');
  356: 	push(@items,$response);
  357: 	my $responsename = $letter_name_map{$response};
  358: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  359: 	if ( $response =~ /[^\s]/) {
  360: 	    $responsehash{$name}=$responsename;
  361: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
  362: 	    if ($value eq $responsename) {
  363: 		$grade{$name}='1'; $right++;
  364: 	    } else {
  365: 		$grade{$name}='0'; $wrong++;
  366: 	    }
  367: 	} else {
  368: 	    $ignored++;
  369: 	}
  370: 	$temp++;
  371:     }
  372:     my $part=$Apache::inputtags::part;
  373:     my $id = $Apache::inputtags::response['-1'];
  374:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  375:     my $itemstr    =&Apache::lonnet::array2str(@items);
  376:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  377:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  378:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
  379:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  380: 	$responsestr;
  381:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
  382: 	$itemstr;
  383:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
  384: 	$gradestr;
  385:     if ($Apache::lonhomework::type eq 'survey') {
  386: 	if ($ignored == 0) {
  387: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  388: 	    &Apache::response::handle_previous(\%previous,$ad);
  389: 	} elsif ($wrong==0 && $right==0) {
  390: 	} else {
  391: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  392: 	    &Apache::response::handle_previous(\%previous,$ad);
  393: 	}
  394:     } elsif (!$Apache::lonhomework::scantronmode) {
  395: 	my $ad;
  396: 	if ($wrong==0 && $ignored==0) {
  397: 	    $ad='EXACT_ANS';
  398: 	} elsif ($wrong==0 && $right==0) {
  399: 	    #nothing submitted
  400: 	} else {
  401: 	    if ($ignored==0) {
  402: 		$ad='INCORRECT';
  403: 	    } else {
  404: 		$ad='MISSING_ANSWER';
  405: 	    }
  406: 	}
  407: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  408: 	&Apache::response::handle_previous(\%previous,$ad);
  409:     } else {
  410: 	my $ad;
  411: 	if ($wrong==0 && $right==0) {
  412: 	    #nothing submitted
  413: 	} else {
  414: 	    $ad='ASSIGNED_SCORE';
  415: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  416: 		$ad;
  417: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  418: 		$right/(scalar(@whichfoils));
  419: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  420: 		scalar(@whichfoils);
  421: 	}
  422:     }
  423: }
  424: 
  425: sub format_prior_answer {
  426:     my ($mode,$answer,$other_data) = @_;
  427:     my %answer      =&Apache::lonnet::str2hash($answer);
  428:     my $foil_order  =$other_data->[0];
  429:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
  430:     my @items       =&Apache::lonnet::str2array($other_data->[2]);
  431:     my $output;
  432:     
  433:     foreach my $name (@{ $foil_order }) {
  434: 	my $item=shift(@items);
  435: 	$output .= '<tr><td>'.$item.'</td></tr>';
  436:     }
  437:     return if (!defined($output));
  438: 
  439:     $output = '<table class="LC_prior_match">'.$output.'</table>';
  440:     return $output;
  441: }
  442: 
  443: sub itemdisplay {
  444:     my ($location)=@_;
  445:     if ($location eq 'top' &&
  446: 	!defined($Apache::matchresponse::itemtable{'location'})) {
  447: 	return $Apache::matchresponse::itemtable{'display'};
  448:     }
  449:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
  450: 	return $Apache::matchresponse::itemtable{'display'};
  451:     }
  452:     return undef;
  453: }
  454: 
  455: sub displayfoils {
  456:     my ($target,$max,$randomize)=@_;
  457:     my ($tabsize, $lefttabsize, $righttabsize);
  458: 
  459:     my $result;
  460:     my $question;
  461:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  462: 						   $randomize,
  463: 				            &Apache::response::showallfoils(),
  464: 				           \%Apache::response::foilgroup);
  465:     my $part=$Apache::inputtags::part;
  466:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  467:     my %letter_name_map;
  468:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
  469: 	%letter_name_map=
  470: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
  471:     }
  472:     my %name_letter_map;
  473:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
  474: 	%name_letter_map=
  475: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
  476:     }
  477:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  478: 	foreach my $name (@whichfoils) {
  479: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  480: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  481: 	    my $letter=$name_letter_map{$value};
  482: 	    if ($target eq 'tex') {
  483: 		$question.=' \\\\ '.$letter.':'.$text;
  484: 	    } else {
  485: 		$question.='<br /><b>'.$letter.':</b> '.$text;
  486: 	    }
  487: 	}
  488:     } else {
  489: 	my $i = 0;
  490: 	my $temp=1;
  491: 	my $id=$Apache::inputtags::response[-1];
  492: 	my $part=$Apache::inputtags::part;
  493: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  494: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
  495: 	my @alphabet=('A'..'Z');
  496: 	my @used_letters=sort(keys(%letter_name_map));
  497: 	my $internal_counter=$Apache::lonxml::counter;
  498: 	foreach my $name (@whichfoils) {
  499: 	    my $lastopt=$lastresponse{$name};
  500: 	    my $last_letter=$name_letter_map{$lastopt};
  501: 	    my $optionlist = '';
  502: 	    if ($target ne 'tex') {
  503: 		$optionlist="<option></option>\n";
  504: 	    } else {
  505: 		if ($Apache::lonhomework::type ne 'exam') {
  506: 		    $optionlist='\framebox[5 mm][s]{\tiny\strut}';
  507: 		}
  508: 	    }
  509: 	    my $option;
  510: 	    foreach $option (@used_letters) {
  511: 		if ($option eq $last_letter) {
  512: 		    if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
  513: 		} else {
  514: 		    if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
  515: 		}
  516: 	    }
  517: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  518: 	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  519: 		  $part.'\');" name="HWVAL_'.
  520: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  521: 		        $optionlist."</select>\n";
  522: 	    }
  523: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  524: 	    #
  525:             #  Factor out the tex computations of the left/right 1/2 minipage
  526: 	    #  widths for left or right positioning.  This allows us
  527: 	    #  to, if necessary constrain the bubble widths:
  528: 	    #
  529: 	    if ($target eq 'tex'   &&
  530: 		(&itemdisplay('left') || &itemdisplay('right'))) {
  531: 		$tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
  532: 		($lefttabsize,$righttabsize)=(0,0);
  533: 		if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
  534: 		    $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
  535: 		    $lefttabsize=$tabsize*$1/100;	
  536: 		    $righttabsize=0.95*($tabsize-$lefttabsize);
  537: 		} else {
  538: 		    $tabsize=~/(\d+\.?\d*)/;
  539: 		    $lefttabsize=$1/2.1;
  540: 		    $righttabsize=0.95*($1-$lefttabsize);
  541: 		}
  542: 	    }
  543: 	    if ($target ne 'tex') {
  544: 		if ($Apache::lonhomework::type ne 'exam') {
  545: 		    $question.="<br />\n".$optionlist.$text;
  546: 		} else {
  547: 		    $question.="<br />\n".$text;
  548: 		}
  549: 		if ($Apache::lonhomework::type eq 'exam') {
  550: 		    my @blank;
  551: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
  552: 		}
  553: 	    } else {		
  554: 		# Note that if left or right positioned, we must
  555: 		# confine the bubbles to righttabsize:
  556: 		#
  557: 		if ($Apache::lonhomework::type eq 'exam') {
  558: 		    $question.=' '.$optionlist.$text."\n";
  559: 		    my @emptyItems = ();
  560: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
  561: 		    $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
  562: 			.'\item \hskip -3mm \textbf{'.$internal_counter.'}';
  563: 		    if (&itemdisplay('left') || &itemdisplay('right')) {
  564: 			$question .= &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems, "", $righttabsize);
  565: 		    }
  566: 		    else {
  567: 			$question .= &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems);
  568: 		    }
  569: 		    $question .= '\end{list} \vskip -8 mm \strut ';
  570: 		    $internal_counter++;
  571: 	        } else {
  572: 		    $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
  573: 		}
  574:             } 
  575: 	    $temp++;
  576: 	}
  577:     }
  578:     if ($result=&itemdisplay('top')) {
  579: 	$result.=$question;
  580:     } elsif ($result=&itemdisplay('bottom')) {
  581: 	$result=$question.$result;
  582:     } elsif ($result=&itemdisplay('right')) {
  583: 	if ($target ne 'tex') {
  584: 	    #remove the first <br />
  585: 	    $question=~s|<br />||;
  586: 	    $result='<table><tr><td valign="top">'.$question.
  587: 		'</td><td valign="top">'.$result.'</td></tr></table>';
  588: 	} else {
  589: 	    $lefttabsize.=' mm ';
  590: 	    $righttabsize.=' mm ';
  591: 	    $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}';
  592: 	}
  593:     } elsif ($result=&itemdisplay('left')) {
  594: 	if ($target ne 'tex') {
  595: 	    #remove the first <br />
  596: 	    $question=~s|<br />||;
  597: 	    $result='<table><tr><td valign="top">'.$result.
  598: 		'</td><td valign="top">'.$question.'</td></tr></table>';
  599: 	} else {
  600: 	    $lefttabsize.=' mm ';
  601: 	    $righttabsize.=' mm ';
  602: 	    $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}';
  603: 	}
  604:     }
  605:     if ($target eq 'web') {
  606: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  607: 						  [\@whichfoils,
  608: 						   'submissiongrading',
  609: 						   'submissionitems']);
  610:     }
  611:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
  612:     return $result;
  613: }
  614: 
  615: sub getfoilcounts {
  616:     my ($max)=@_;
  617:     # +1 since instructors will count from 1
  618:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  619:     if (&Apache::response::showallfoils()) { $max=$count; }
  620:     if ($count>$max) { $count=$max } 
  621:     &Apache::lonxml::debug("Count is $count from $max");
  622:     return $count;
  623: }
  624: 
  625: 
  626: sub start_conceptgroup {
  627:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  628:     $Apache::matchresponse::conceptgroup=1;
  629:     %Apache::response::conceptgroup=();
  630:     my $result;
  631:     if ($target eq 'edit') {
  632: 	$result.=&Apache::edit::tag_start($target,$token,
  633: 					  "Concept Grouped Foils");
  634: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  635: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  636:     }
  637:     if ($target eq 'modified') {
  638: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  639: 						     $safeeval,'concept');
  640: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  641:     }
  642:     return $result;
  643: }
  644: 
  645: sub end_conceptgroup {
  646:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  647:     $Apache::matchresponse::conceptgroup=0;
  648:     my $result='';
  649:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  650: 	$target eq 'tex' || $target eq 'analyze') {
  651: 	&Apache::response::pick_foil_for_concept($target,
  652: 						 ['value','text','location'],
  653: 						 \%Apache::hint::match,
  654: 						 $parstack,$safeeval);
  655:     } elsif ($target eq 'edit') {
  656: 	$result=&Apache::edit::end_table();
  657:     }
  658:     return $result;
  659: }
  660: 
  661: sub insert_conceptgroup {
  662:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  663:     return $result;
  664: }
  665: 
  666: sub start_foil {
  667:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  668:     my $result='';
  669:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  670: 	&Apache::lonxml::startredirection;
  671: 	if ($target eq 'analyze') {
  672: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  673: 	}
  674:     } elsif ($target eq 'edit') {
  675: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  676: 	my $level='-2';
  677: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  678: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  679: 	my @names;
  680: 	if (defined(@{ $Apache::response::itemgroup{'names'} })) {
  681: 	    @names=@{ $Apache::response::itemgroup{'names'} };
  682: 	}
  683: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
  684: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  685: 						 $safeeval,'-3');
  686: 	if ($randomize ne 'no') {
  687: 	    $result.=&Apache::edit::select_arg('Location:','location',
  688: 					     ['random','top','bottom'],$token);
  689: 	}
  690: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  691:     } elsif ($target eq 'modified') {
  692: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  693: 						     $safeeval,'value',
  694: 						     'name','location');
  695: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  696:     }
  697:     return $result;
  698: }
  699: 
  700: sub end_foil {
  701:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  702:     my $text ='';
  703:     my $result = '';
  704:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  705: 	$text=&Apache::lonxml::endredirection;
  706:     }
  707: 
  708:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  709: 	$target eq 'tex' || $target eq 'analyze') {
  710: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  711: 	    $text='\vskip 5mm $\triangleright$ '.$text;
  712: 	}
  713: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  714: 	if ($value ne 'unused') {
  715: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  716: 	    &Apache::lonxml::debug("Got a name of :$name:");
  717: 	    if (!$name) {
  718: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
  719: 		$name=$Apache::lonxml::curdepth;
  720: 	    }
  721: 	    &Apache::lonxml::debug("Using a name of :$name:");
  722: 	    if (defined($Apache::response::foilnames{$name})) {
  723: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  724: 	    }
  725: 	    $Apache::response::foilnames{$name}++;
  726: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  727: 						     $safeeval);
  728: 	    if ( $Apache::matchresponse::conceptgroup
  729: 		 && !&Apache::response::showallfoils() ) {
  730: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  731: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  732: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  733: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  734: 	    } else {
  735: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  736: 		$Apache::response::foilgroup{"$name.value"} = $value;
  737: 		$Apache::response::foilgroup{"$name.text"} = $text;
  738: 		$Apache::response::foilgroup{"$name.location"} = $location;
  739: 	    }
  740: 	}
  741:     }
  742:     if ($target eq 'edit') {
  743: 	$result.= &Apache::edit::tag_end($target,$token,'');
  744:     }
  745:     return $result;
  746: }
  747: 
  748: sub insert_foil {
  749:     return '
  750: <foil name="" value="unused">
  751: <startouttext />
  752: <endouttext />
  753: </foil>';
  754: }
  755: 1;
  756: __END__

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