File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.56: download - view: text, annotated - select for diffs
Tue Dec 6 10:13:46 2005 UTC (18 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- fixup soe exam printouts also now have correct bubble listed

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

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