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

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

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