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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Full matching style response
                      3: #
1.94    ! raeburn     4: # $Id: matchresponse.pm,v 1.93 2016/01/22 22:42:51 damieng 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.92      golterma   58: 	    '<tr><td>'.&Apache::loncommon::insert_folding_button().&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: 	
1.93      damieng   174: 	my $table='<table class="LC_itemgroup">'; # 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.92      golterma  322: 	    .'<tr><td>'.&Apache::loncommon::insert_folding_button()
                    323:             .&mt('Collection Of Foils').'</td>'
1.72      bisitz    324:             .'<td><span class="LC_nobreak">'.&mt('Delete?')
1.1       albertel  325: 	    .&Apache::edit::deletelist($target,$token)
1.72      bisitz    326: 	    .'</span></td>'
1.73      raeburn   327:             .'<td>&nbsp;'.&Apache::edit::end_row()
1.1       albertel  328:             .&Apache::edit::start_spanning_row()."\n";
                    329:     }
                    330:     return $result;
                    331: }
                    332: 
                    333: sub end_foilgroup {
                    334:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    335:     my $result;
1.67      foxr      336:     my $part        = $Apache::inputtags::part;
1.68      raeburn   337:     my $response_id = $Apache::inputtags::response[-1];
1.67      foxr      338: 
1.28      albertel  339:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  340: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    341: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    342: 						   $safeeval,'-2');
1.5       sakharuk  343: 	if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  344: 	    $result=&displayfoils($target,$max,$randomize);
1.60      albertel  345: 	    $Apache::lonxml::post_evaluate=0;
1.1       albertel  346: 	} elsif ($target eq 'answer' ) {
                    347: 	    $result=&displayanswers($max,$randomize);
                    348: 	} elsif ( $target eq 'grade') {
                    349: 	    &grade_response($max,$randomize);
1.28      albertel  350: 	} elsif ( $target eq 'analyze') {
                    351: 	    my @shown=&whichfoils($max,$randomize);
                    352: 	    &Apache::response::analyze_store_foilgroup(\@shown,
1.29      albertel  353: 						 ['text','value','location']);
1.28      albertel  354: 	    #FIXME need to store options in some way
1.1       albertel  355: 	}
1.82      raeburn   356:         my ($numrows,$bubbles_per_row);
                    357:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
                    358:             my $numitems;
                    359:             if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') {
                    360:                 $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
                    361:                 ($numrows,$bubbles_per_row) =
                    362:                     &Apache::optionresponse::getnumrows($numitems);
                    363:             }
                    364:         }
                    365:         if ($numrows < 1) {
                    366:             $numrows = 1;
                    367:         }
                    368:         my $increment = &getfoilcounts($max) * $numrows;
                    369: 	&Apache::lonxml::increment_counter($increment,"$part.$response_id");
1.66      foxr      370: 	if ($target eq 'analyze') {
                    371: 	    &Apache::lonhomework::set_bubble_lines();
                    372: 	}
                    373: 
1.1       albertel  374:     } elsif ($target eq 'edit') {
                    375: 	$result=&Apache::edit::end_table();
                    376:     }
1.41      albertel  377:     &Apache::response::poprandomnumber();
1.1       albertel  378:     return $result;
1.29      albertel  379: }
                    380: 
                    381: sub whichfoils {
                    382:     my ($max,$randomize)=@_;
                    383:     return &Apache::response::whichorder(&getfoilcounts($max),
                    384: 					 $randomize,
                    385: 					 &Apache::response::showallfoils(),
                    386: 					 \%Apache::response::foilgroup);
1.1       albertel  387: }
                    388: 
                    389: sub displayanswers {
                    390:     my ($max,$randomize,@opt)=@_;
1.84      raeburn   391:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
                    392:         if (!@{ $Apache::response::foilgroup{'names'} }) { return; }
                    393:     } else {
                    394:         return;
                    395:     }
1.1       albertel  396:     my @names = @{ $Apache::response::foilgroup{'names'} };
1.18      albertel  397:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
                    398: 						   $randomize,
                    399: 					  &Apache::response::showallfoils(),
                    400: 					 \%Apache::response::foilgroup);
1.1       albertel  401:     my %name_letter_map;
1.84      raeburn   402:     if (ref($Apache::response::itemgroup{'name_letter_map'}) eq 'HASH') {
                    403:         if (%{$Apache::response::itemgroup{'name_letter_map'}}) {
                    404: 	    %name_letter_map=
                    405: 	        %{ $Apache::response::itemgroup{'name_letter_map'} };
                    406:         }
1.1       albertel  407:     }
1.56      albertel  408:     my $result;
                    409:     if ($Apache::lonhomework::type eq 'exam') {
                    410: 	my $i=0;
                    411: 	foreach my $name (@whichfoils) {
                    412: 	    $result.=&Apache::response::answer_header('matchresponse',$i++);
                    413: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
                    414: 	    my $letter=$name_letter_map{$value_name};
                    415: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
                    416: 	    $result.=&Apache::response::answer_footer('matchresponse');
                    417: 	}
                    418:     } else {
                    419: 	$result=&Apache::response::answer_header('matchresponse');
                    420: 	foreach my $name (@whichfoils) {
                    421: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
                    422: 	    my $letter=$name_letter_map{$value_name};
                    423: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
                    424: 	}
                    425: 	$result.=&Apache::response::answer_footer('matchresponse');
1.1       albertel  426:     }
                    427:     return $result;
                    428: }
                    429: 
                    430: 
                    431: sub grade_response {
                    432:     my ($max,$randomize)=@_;
1.18      albertel  433:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
                    434: 						   $randomize,
                    435: 				            &Apache::response::showallfoils(),
                    436: 				           \%Apache::response::foilgroup);
1.50      albertel  437:     if (!&Apache::response::submitted()) { return; }
1.1       albertel  438:     my %responsehash;
                    439:     my %grade;
1.81      raeburn   440:     my %answerhash;
1.33      albertel  441:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1       albertel  442:     my %letter_name_map;
1.84      raeburn   443:     if (ref($Apache::response::itemgroup{'letter_name_map'}) eq 'HASH') {
                    444:         if (%{$Apache::response::itemgroup{'letter_name_map'}}) {
                    445: 	    %letter_name_map=
                    446: 	        %{ $Apache::response::itemgroup{'letter_name_map'} };
                    447:         }
1.1       albertel  448:     }
1.30      albertel  449:     my @items;
1.82      raeburn   450:     my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
                    451:     my ($numrows,$bubbles_per_row);
                    452:     if ($Apache::lonhomework::scantronmode) {
                    453:         my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
                    454:         ($numrows,$bubbles_per_row) =
                    455:             &Apache::optionresponse::getnumrows($numitems);
                    456:     }
                    457:     if ($numrows < 1) {
                    458:         $numrows = 1;
                    459:     }
                    460:     my @alphabet=('A'..'Z');
                    461:     my %nums_from_letters;
                    462:     for (my $i=0; $i<@alphabet; $i++) {
                    463:         $nums_from_letters{$alphabet[$i]} = $i;
                    464:     }
1.1       albertel  465:     foreach my $name (@whichfoils) {
1.82      raeburn   466:         my $response;
                    467:         if ($numrows > 1) {
                    468:             my $num = $temp;
                    469:             my $totalnum;
                    470:             for (my $i=0; $i<$numrows; $i++) {
                    471:                 my $item = &Apache::response::getresponse($num,'letter');
                    472:                 if ($item =~ /^\w$/) {
                    473:                     $totalnum = $i*$bubbles_per_row + $nums_from_letters{$item};
                    474:                 }
                    475:                 $num ++;
                    476:             }
                    477:             if ($totalnum =~ /^\d+$/) {
                    478:                 $response = $alphabet[$totalnum];
                    479:             }
                    480:             $temp += $numrows;
                    481:         } else {
                    482: 	    $response = &Apache::response::getresponse($temp,'letter');
                    483:             $temp ++;
                    484:         }
1.30      albertel  485: 	push(@items,$response);
1.1       albertel  486: 	my $responsename = $letter_name_map{$response};
                    487: 	my $value=$Apache::response::foilgroup{$name.'.value'};
1.81      raeburn   488:         if ($Apache::lonhomework::type eq 'randomizetry') {
                    489:             $answerhash{$name} = $value;
                    490:         }
1.1       albertel  491: 	if ( $response =~ /[^\s]/) {
1.31      albertel  492: 	    $responsehash{$name}=$responsename;
1.33      albertel  493: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
1.1       albertel  494: 	    if ($value eq $responsename) {
                    495: 		$grade{$name}='1'; $right++;
                    496: 	    } else {
                    497: 		$grade{$name}='0'; $wrong++;
                    498: 	    }
                    499: 	} else {
                    500: 	    $ignored++;
                    501: 	}
                    502:     }
                    503:     my $part=$Apache::inputtags::part;
1.86      raeburn   504:     my $nonlenient=&Apache::optionresponse::grading_is_nonlenient($part);
1.1       albertel  505:     my $id = $Apache::inputtags::response['-1'];
                    506:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.30      albertel  507:     my $itemstr    =&Apache::lonnet::array2str(@items);
1.1       albertel  508:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.30      albertel  509:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1       albertel  510:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
                    511:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    512: 	$responsestr;
1.30      albertel  513:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
                    514: 	$itemstr;
1.1       albertel  515:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
                    516: 	$gradestr;
1.81      raeburn   517:     if ($Apache::lonhomework::type eq 'randomizetry') {
                    518:         $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
                    519:     }
1.79      raeburn   520:     if (($Apache::lonhomework::type eq 'survey') ||
                    521:         ($Apache::lonhomework::type eq 'surveycred') ||
                    522:         ($Apache::lonhomework::type eq 'anonsurvey') ||
                    523:         ($Apache::lonhomework::type eq 'anonsurveycred')) {
1.47      albertel  524: 	if ($ignored == 0) {
1.79      raeburn   525: 	    my $ad;
                    526:             if ($Apache::lonhomework::type eq 'anonsurvey') {
                    527:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
                    528:             } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    529:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
                    530:             } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    531:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
                    532:             } else {
                    533:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    534:             }
1.47      albertel  535: 	    &Apache::response::handle_previous(\%previous,$ad);
                    536: 	} elsif ($wrong==0 && $right==0) {
                    537: 	} else {
                    538: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
                    539: 	    &Apache::response::handle_previous(\%previous,$ad);
                    540: 	}
1.83      raeburn   541:     } elsif ($nonlenient) {
                    542:         my $ad;
                    543:         if ($wrong==0 && $ignored==0) {
                    544:             $ad='EXACT_ANS';
                    545:         } elsif ($wrong==0 && $right==0) {
                    546:             #nothing submitted
                    547:         } else {
                    548:             if ($ignored==0) {
                    549:                 $ad='INCORRECT';
                    550:             } else {
                    551:                 $ad='MISSING_ANSWER';
                    552:             }
                    553:         }
                    554:         $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    555:         &Apache::response::handle_previous(\%previous,$ad);
1.34      albertel  556:     } else {
1.83      raeburn   557:         my $ad;
                    558:         if ($wrong==0 && $right==0) {
                    559:             #nothing submitted only assign a score if we
                    560:             #need to override a previous grade
                    561:             if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
                    562:                 $ad='ASSIGNED_SCORE';
                    563:             }
                    564:         } else {
                    565:             $ad='ASSIGNED_SCORE';
                    566:         }
                    567:         $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    568:         $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
                    569:             $right/(scalar(@whichfoils));
                    570:         $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
                    571:             scalar(@whichfoils);
1.34      albertel  572:     }
1.1       albertel  573: }
                    574: 
1.64      albertel  575: sub format_prior_answer {
                    576:     my ($mode,$answer,$other_data) = @_;
                    577:     my %answer      =&Apache::lonnet::str2hash($answer);
                    578:     my $foil_order  =$other_data->[0];
                    579:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
                    580:     my @items       =&Apache::lonnet::str2array($other_data->[2]);
                    581:     my $output;
                    582:     
                    583:     foreach my $name (@{ $foil_order }) {
1.90      bisitz    584: 	my $item = &HTML::Entities::encode(shift(@items),'<>&"');
1.91      bisitz    585:         if ($item eq '') {
                    586:             $item = '&nbsp;';
                    587:         }
1.64      albertel  588: 	$output .= '<tr><td>'.$item.'</td></tr>';
                    589:     }
                    590:     return if (!defined($output));
                    591: 
                    592:     $output = '<table class="LC_prior_match">'.$output.'</table>';
                    593:     return $output;
                    594: }
                    595: 
1.4       albertel  596: sub itemdisplay {
                    597:     my ($location)=@_;
                    598:     if ($location eq 'top' &&
                    599: 	!defined($Apache::matchresponse::itemtable{'location'})) {
                    600: 	return $Apache::matchresponse::itemtable{'display'};
                    601:     }
                    602:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
                    603: 	return $Apache::matchresponse::itemtable{'display'};
                    604:     }
                    605:     return undef;
                    606: }
1.64      albertel  607: 
1.1       albertel  608: sub displayfoils {
                    609:     my ($target,$max,$randomize)=@_;
1.62      foxr      610:     my ($tabsize, $lefttabsize, $righttabsize);
                    611: 
1.1       albertel  612:     my $result;
1.4       albertel  613:     my $question;
1.18      albertel  614:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
                    615: 						   $randomize,
                    616: 				            &Apache::response::showallfoils(),
                    617: 				           \%Apache::response::foilgroup);
1.1       albertel  618:     my $part=$Apache::inputtags::part;
                    619:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    620:     my %letter_name_map;
1.84      raeburn   621:     if (ref($Apache::response::itemgroup{'letter_name_map'}) eq 'HASH') {
                    622:         if (%{$Apache::response::itemgroup{'letter_name_map'}}) {
                    623: 	    %letter_name_map=
                    624: 	        %{ $Apache::response::itemgroup{'letter_name_map'} };
                    625:         }
1.1       albertel  626:     }
                    627:     my %name_letter_map;
1.84      raeburn   628:     if (ref($Apache::response::itemgroup{'name_letter_map'}) eq 'HASH') {
                    629:         if (%{$Apache::response::itemgroup{'name_letter_map'}}) {
                    630: 	    %name_letter_map=
                    631: 	        %{ $Apache::response::itemgroup{'name_letter_map'} };
                    632:         }
1.1       albertel  633:     }
1.25      albertel  634:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1       albertel  635: 	foreach my $name (@whichfoils) {
                    636: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
                    637: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    638: 	    my $letter=$name_letter_map{$value};
1.5       sakharuk  639: 	    if ($target eq 'tex') {
                    640: 		$question.=' \\\\ '.$letter.':'.$text;
                    641: 	    } else {
1.53      albertel  642: 		$question.='<br /><b>'.$letter.':</b> '.$text;
1.5       sakharuk  643: 	    }
1.1       albertel  644: 	}
                    645:     } else {
                    646: 	my $i = 0;
1.36      albertel  647: 	my $temp=1;
1.1       albertel  648: 	my $id=$Apache::inputtags::response[-1];
                    649: 	my $part=$Apache::inputtags::part;
1.81      raeburn   650:         my ($lastresponse,$newvariation);
                    651:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
                    652:             ($Apache::lonhomework::type eq 'randomizetry')) &&
                    653:             ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
                    654:             if ($env{'form.'.$part.'.rndseed'} ne
                    655:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
                    656:                 $newvariation = 1;
                    657:             }
                    658:         }
1.89      raeburn   659:         unless ($newvariation) {
                    660:             if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    661:                 (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    662:                  ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    663:                 $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    664:             } else {
                    665:                 unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
                    666:                         ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
                    667:                     $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    668:                 }
                    669:             }
1.79      raeburn   670:         }
1.89      raeburn   671: 
1.9       sakharuk  672: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
                    673: 	my @alphabet=('A'..'Z');
1.15      albertel  674: 	my @used_letters=sort(keys(%letter_name_map));
1.20      sakharuk  675: 	my $internal_counter=$Apache::lonxml::counter;
1.82      raeburn   676:         my ($numrows,$bubbles_per_row);
                    677:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
                    678:             ($numrows,$bubbles_per_row) = 
                    679:                 &Apache::optionresponse::getnumrows(scalar(@used_letters));
                    680:         } else {
                    681:             $numrows = 1;
                    682:         }
1.1       albertel  683: 	foreach my $name (@whichfoils) {
                    684: 	    my $lastopt=$lastresponse{$name};
                    685: 	    my $last_letter=$name_letter_map{$lastopt};
1.5       sakharuk  686: 	    my $optionlist = '';
1.7       sakharuk  687: 	    if ($target ne 'tex') {
1.94    ! raeburn   688: 		$optionlist="<option value=\"\"></option>\n";
1.7       sakharuk  689: 	    } else {
                    690: 		if ($Apache::lonhomework::type ne 'exam') {
1.76      onken     691:                     if($env{'form.pdfFormFields'} eq 'yes'
                    692:                        && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
1.75      raeburn   693:                         my $fieldname = $env{'request.symb'} . '&part_'. $Apache::inputtags::part .'&matchresponse'. '&HWVAL_' . $Apache::inputtags::response['-1'] . ':' . $temp . '&submit_' . $Apache::inputtags::part . '&';
                    694:                         $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
                    695:                     } else {
1.77      foxr      696: 
1.75      raeburn   697:                         $optionlist='\framebox[10 mm][s]{\tiny\strut}';
                    698:                     }
1.74      raeburn   699:                 }
1.7       sakharuk  700: 	    }
1.1       albertel  701: 	    my $option;
1.15      albertel  702: 	    foreach $option (@used_letters) {
1.1       albertel  703: 		if ($option eq $last_letter) {
1.71      onken     704: 		    if ($target ne 'tex') {
1.94    ! raeburn   705:                         $optionlist.="<option value=\"$option\" selected=\"selected\">$option</option>\n";
1.76      onken     706:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.80      onken     707:                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
                    708:                              && $Apache::lonhomework::type ne 'exam') {
1.75      raeburn   709:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
1.71      onken     710:                     }
1.1       albertel  711: 		} else {
1.71      onken     712: 		    if ($target ne 'tex') {
1.94    ! raeburn   713:                         $optionlist.="<option value=\"$option\">$option</option>\n";
1.76      onken     714:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.80      onken     715:                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
                    716:                              && $Apache::lonhomework::type ne 'exam') {
1.75      raeburn   717:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
1.71      onken     718:                     }
1.1       albertel  719: 		}
                    720: 	    }
1.19      sakharuk  721: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.61      albertel  722: 	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
                    723: 		  $part.'\');" name="HWVAL_'.
1.5       sakharuk  724: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
                    725: 		        $optionlist."</select>\n";
                    726: 	    }
1.1       albertel  727: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
1.62      foxr      728: 	    #
                    729:             #  Factor out the tex computations of the left/right 1/2 minipage
                    730: 	    #  widths for left or right positioning.  This allows us
                    731: 	    #  to, if necessary constrain the bubble widths:
                    732: 	    #
                    733: 	    if ($target eq 'tex'   &&
                    734: 		(&itemdisplay('left') || &itemdisplay('right'))) {
                    735: 		$tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
                    736: 		($lefttabsize,$righttabsize)=(0,0);
                    737: 		if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
                    738: 		    $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
                    739: 		    $lefttabsize=$tabsize*$1/100;	
                    740: 		    $righttabsize=0.95*($tabsize-$lefttabsize);
                    741: 		} else {
                    742: 		    $tabsize=~/(\d+\.?\d*)/;
                    743: 		    $lefttabsize=$1/2.1;
                    744: 		    $righttabsize=0.95*($1-$lefttabsize);
                    745: 		}
                    746: 	    }
1.5       sakharuk  747: 	    if ($target ne 'tex') {
1.19      sakharuk  748: 		if ($Apache::lonhomework::type ne 'exam') {
1.45      albertel  749: 		    $question.="<br />\n".$optionlist.$text;
1.19      sakharuk  750: 		} else {
1.45      albertel  751: 		    $question.="<br />\n".$text;
1.19      sakharuk  752: 		}
1.6       sakharuk  753: 		if ($Apache::lonhomework::type eq 'exam') {
1.42      albertel  754: 		    my @blank;
                    755: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
1.6       sakharuk  756: 		}
                    757: 	    } else {		
1.62      foxr      758: 		# Note that if left or right positioned, we must
                    759: 		# confine the bubbles to righttabsize:
                    760: 		#
1.80      onken     761: 		if ($Apache::lonhomework::type eq 'exam') {
1.6       sakharuk  762: 		    $question.=' '.$optionlist.$text."\n";
1.13      sakharuk  763: 		    my @emptyItems = ();
1.16      albertel  764: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.62      foxr      765: 		    $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
1.82      raeburn   766: 			.'\item \hskip -3mm ';
                    767:                     if ($numrows == 1) {
                    768:                         $question .= '\textbf{'.$internal_counter.'}';
                    769:                     } else {
                    770:                         my $linetext;
                    771:                         for (my $i=0; $i<$numrows; $i++) {
                    772:                             $linetext .= $internal_counter+$i.', ';
                    773:                         }
                    774:                         $linetext =~ s/,\s$//;
                    775:                         $question .= '\small {\textbf{'.$linetext.'}} '.
                    776:                                      '\hskip 2 mm {\footnotesize '.
                    777:                                      &mt('(Bubble once in [_1] lines)',$numrows).
                    778:                                      '} \vskip 3 mm';
                    779:                     }
                    780:                     my $max_width;
1.62      foxr      781: 		    if (&itemdisplay('left') || &itemdisplay('right')) {
1.82      raeburn   782:                         $max_width = $righttabsize;
                    783:                     }
                    784: 		    $question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems,'',$max_width,$numrows,$bubbles_per_row,$internal_counter);
1.62      foxr      785: 		    $question .= '\end{list} \vskip -8 mm \strut ';
1.82      raeburn   786: 		    $internal_counter += $numrows;
1.6       sakharuk  787: 	        } else {
1.76      onken     788:                     if($env{'form.pdfFormFields'} eq 'yes' 
                    789:                             && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
1.75      raeburn   790:                         $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
                    791:                     } else {
                    792:                         $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
                    793:                     }
1.6       sakharuk  794: 		}
1.5       sakharuk  795:             } 
1.1       albertel  796: 	    $temp++;
                    797: 	}
1.4       albertel  798:     }
                    799:     if ($result=&itemdisplay('top')) {
                    800: 	$result.=$question;
                    801:     } elsif ($result=&itemdisplay('bottom')) {
                    802: 	$result=$question.$result;
                    803:     } elsif ($result=&itemdisplay('right')) {
1.32      sakharuk  804: 	if ($target ne 'tex') {
1.45      albertel  805: 	    #remove the first <br />
                    806: 	    $question=~s|<br />||;
                    807: 	    $result='<table><tr><td valign="top">'.$question.
                    808: 		'</td><td valign="top">'.$result.'</td></tr></table>';
1.32      sakharuk  809: 	} else {
1.43      sakharuk  810: 	    $lefttabsize.=' mm ';
                    811: 	    $righttabsize.=' mm ';
                    812: 	    $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  813: 	}
1.4       albertel  814:     } elsif ($result=&itemdisplay('left')) {
1.32      sakharuk  815: 	if ($target ne 'tex') {
1.45      albertel  816: 	    #remove the first <br />
                    817: 	    $question=~s|<br />||;
                    818: 	    $result='<table><tr><td valign="top">'.$result.
                    819: 		'</td><td valign="top">'.$question.'</td></tr></table>';
1.32      sakharuk  820: 	} else {
1.43      sakharuk  821: 	    $lefttabsize.=' mm ';
                    822: 	    $righttabsize.=' mm ';
                    823: 	    $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  824: 	}
1.1       albertel  825:     }
1.64      albertel  826:     if ($target eq 'web') {
1.81      raeburn   827:         my $questiontype;
                    828:         if ($Apache::lonhomework::type eq 'randomizetry') {
                    829:             $questiontype = $Apache::lonhomework::type;
                    830:         }
1.64      albertel  831: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
                    832: 						  [\@whichfoils,
                    833: 						   'submissiongrading',
1.81      raeburn   834: 						   'submissionitems'],
                    835:                                                   $questiontype);
1.64      albertel  836:     }
1.5       sakharuk  837:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1       albertel  838:     return $result;
                    839: }
                    840: 
                    841: sub getfoilcounts {
                    842:     my ($max)=@_;
                    843:     # +1 since instructors will count from 1
                    844:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
                    845:     if (&Apache::response::showallfoils()) { $max=$count; }
                    846:     if ($count>$max) { $count=$max } 
                    847:     &Apache::lonxml::debug("Count is $count from $max");
                    848:     return $count;
                    849: }
                    850: 
                    851: 
                    852: sub start_conceptgroup {
                    853:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    854:     $Apache::matchresponse::conceptgroup=1;
                    855:     %Apache::response::conceptgroup=();
                    856:     my $result;
                    857:     if ($target eq 'edit') {
                    858: 	$result.=&Apache::edit::tag_start($target,$token,
                    859: 					  "Concept Grouped Foils");
                    860: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    861: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    862:     }
                    863:     if ($target eq 'modified') {
                    864: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    865: 						     $safeeval,'concept');
                    866: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    867:     }
                    868:     return $result;
                    869: }
                    870: 
                    871: sub end_conceptgroup {
                    872:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    873:     $Apache::matchresponse::conceptgroup=0;
                    874:     my $result='';
1.28      albertel  875:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    876: 	$target eq 'tex' || $target eq 'analyze') {
                    877: 	&Apache::response::pick_foil_for_concept($target,
                    878: 						 ['value','text','location'],
                    879: 						 \%Apache::hint::match,
                    880: 						 $parstack,$safeeval);
1.1       albertel  881:     } elsif ($target eq 'edit') {
                    882: 	$result=&Apache::edit::end_table();
                    883:     }
                    884:     return $result;
                    885: }
                    886: 
                    887: sub insert_conceptgroup {
                    888:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    889:     return $result;
                    890: }
                    891: 
                    892: sub start_foil {
                    893:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    894:     my $result='';
1.28      albertel  895:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  896: 	&Apache::lonxml::startredirection;
1.46      albertel  897: 	if ($target eq 'analyze') {
                    898: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    899: 	}
1.1       albertel  900:     } elsif ($target eq 'edit') {
                    901: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
                    902: 	my $level='-2';
                    903: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
                    904: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    905: 	my @names;
1.84      raeburn   906: 	if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') {
                    907:             if (@{ $Apache::response::itemgroup{'names'} }) {
                    908: 	        @names=@{ $Apache::response::itemgroup{'names'} };
                    909:             }
1.1       albertel  910: 	}
                    911: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
                    912: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    913: 						 $safeeval,'-3');
                    914: 	if ($randomize ne 'no') {
                    915: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    916: 					     ['random','top','bottom'],$token);
                    917: 	}
                    918: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    919:     } elsif ($target eq 'modified') {
                    920: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    921: 						     $safeeval,'value',
                    922: 						     'name','location');
                    923: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    924:     }
                    925:     return $result;
                    926: }
                    927: 
                    928: sub end_foil {
                    929:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    930:     my $text ='';
                    931:     my $result = '';
1.28      albertel  932:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  933: 	$text=&Apache::lonxml::endredirection;
                    934:     }
1.48      albertel  935: 
1.28      albertel  936:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    937: 	$target eq 'tex' || $target eq 'analyze') {
1.48      albertel  938: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    939: 	    $text='\vskip 5mm $\triangleright$ '.$text;
                    940: 	}
1.1       albertel  941: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    942: 	if ($value ne 'unused') {
                    943: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    944: 	    &Apache::lonxml::debug("Got a name of :$name:");
1.49      albertel  945: 	    if (!$name) {
1.70      bisitz    946: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.49      albertel  947: 		$name=$Apache::lonxml::curdepth;
                    948: 	    }
1.1       albertel  949: 	    &Apache::lonxml::debug("Using a name of :$name:");
1.37      albertel  950: 	    if (defined($Apache::response::foilnames{$name})) {
1.70      bisitz    951: 		&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  952: 	    }
1.38      albertel  953: 	    $Apache::response::foilnames{$name}++;
1.37      albertel  954: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    955: 						     $safeeval);
1.1       albertel  956: 	    if ( $Apache::matchresponse::conceptgroup
                    957: 		 && !&Apache::response::showallfoils() ) {
1.77      foxr      958: 
1.1       albertel  959: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    960: 		$Apache::response::conceptgroup{"$name.value"} = $value;
1.48      albertel  961: 		$Apache::response::conceptgroup{"$name.text"} = $text;
1.1       albertel  962: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    963: 	    } else {
1.77      foxr      964: 
1.1       albertel  965: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    966: 		$Apache::response::foilgroup{"$name.value"} = $value;
1.48      albertel  967: 		$Apache::response::foilgroup{"$name.text"} = $text;
1.1       albertel  968: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    969: 	    }
                    970: 	}
                    971:     }
                    972:     if ($target eq 'edit') {
                    973: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    974:     }
                    975:     return $result;
                    976: }
                    977: 
                    978: sub insert_foil {
                    979:     return '
                    980: <foil name="" value="unused">
                    981: <startouttext />
                    982: <endouttext />
                    983: </foil>';
                    984: }
1.82      raeburn   985: 
1.1       albertel  986: 1;
                    987: __END__

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