File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.46: download - view: text, annotated - select for diffs
Thu Oct 7 20:28:30 2004 UTC (19 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- bug#2730, answeriscomputed attribute now exists for match,rank,radiobutton,string,formula,numercial responses

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

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