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

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

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