File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.57: download - view: text, annotated - select for diffs
Fri Mar 31 11:10:24 2006 UTC (18 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
Bug 3535: Do some tricky stuff with tables in boxes so that we can
          eliminate the whilespace that normally preceds a description list.

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

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