File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.51: download - view: text, annotated - select for diffs
Sat Feb 12 01:13:56 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- implementing direction for the item list for <mr> for the web, LaTeXx's tables piss me off so printing isnt working yet

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

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