File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.59: download - view: text, annotated - select for diffs
Tue Apr 4 22:30:52 2006 UTC (18 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
Remove double $$ =>$ mapping .

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

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