Annotation of loncom/homework/matchresponse.pm, revision 1.85

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

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