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

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

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