File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.71: download - view: text, annotated - select for diffs
Wed Sep 10 10:11:14 2008 UTC (15 years, 7 months ago) by onken
Branches: MAIN
CVS tags: HEAD
Adds Matchresponse to PDF-Forms

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

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