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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Full matching style response
                      3: #
1.65    ! foxr        4: # $Id: matchresponse.pm,v 1.64 2007/04/17 23:25:09 albertel 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.28      albertel  264:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  265: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    266: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    267: 						   $safeeval,'-2');
1.5       sakharuk  268: 	if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  269: 	    $result=&displayfoils($target,$max,$randomize);
1.60      albertel  270: 	    $Apache::lonxml::post_evaluate=0;
1.1       albertel  271: 	} elsif ($target eq 'answer' ) {
                    272: 	    $result=&displayanswers($max,$randomize);
                    273: 	} elsif ( $target eq 'grade') {
                    274: 	    &grade_response($max,$randomize);
1.28      albertel  275: 	} elsif ( $target eq 'analyze') {
                    276: 	    my @shown=&whichfoils($max,$randomize);
                    277: 	    &Apache::response::analyze_store_foilgroup(\@shown,
1.29      albertel  278: 						 ['text','value','location']);
1.28      albertel  279: 	    #FIXME need to store options in some way
1.1       albertel  280: 	}
1.20      sakharuk  281: 	&Apache::lonxml::increment_counter(&getfoilcounts($max));
1.1       albertel  282:     } elsif ($target eq 'edit') {
                    283: 	$result=&Apache::edit::end_table();
                    284:     }
1.41      albertel  285:     &Apache::response::poprandomnumber();
1.1       albertel  286:     return $result;
1.29      albertel  287: }
                    288: 
                    289: sub whichfoils {
                    290:     my ($max,$randomize)=@_;
                    291:     return &Apache::response::whichorder(&getfoilcounts($max),
                    292: 					 $randomize,
                    293: 					 &Apache::response::showallfoils(),
                    294: 					 \%Apache::response::foilgroup);
1.1       albertel  295: }
                    296: 
                    297: sub displayanswers {
                    298:     my ($max,$randomize,@opt)=@_;
                    299:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                    300:     my @names = @{ $Apache::response::foilgroup{'names'} };
1.18      albertel  301:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
                    302: 						   $randomize,
                    303: 					  &Apache::response::showallfoils(),
                    304: 					 \%Apache::response::foilgroup);
1.1       albertel  305:     my %name_letter_map;
                    306:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    307: 	%name_letter_map=
                    308: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    309:     }
1.56      albertel  310:     my $result;
                    311:     if ($Apache::lonhomework::type eq 'exam') {
                    312: 	my $i=0;
                    313: 	foreach my $name (@whichfoils) {
                    314: 	    $result.=&Apache::response::answer_header('matchresponse',$i++);
                    315: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
                    316: 	    my $letter=$name_letter_map{$value_name};
                    317: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
                    318: 	    $result.=&Apache::response::answer_footer('matchresponse');
                    319: 	}
                    320:     } else {
                    321: 	$result=&Apache::response::answer_header('matchresponse');
                    322: 	foreach my $name (@whichfoils) {
                    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: 	}
                    327: 	$result.=&Apache::response::answer_footer('matchresponse');
1.1       albertel  328:     }
                    329:     return $result;
                    330: }
                    331: 
                    332: 
                    333: sub grade_response {
                    334:     my ($max,$randomize)=@_;
1.18      albertel  335:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
                    336: 						   $randomize,
                    337: 				            &Apache::response::showallfoils(),
                    338: 				           \%Apache::response::foilgroup);
1.50      albertel  339:     if (!&Apache::response::submitted()) { return; }
1.1       albertel  340:     my %responsehash;
                    341:     my %grade;
1.33      albertel  342:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1       albertel  343:     my %letter_name_map;
                    344:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    345: 	%letter_name_map=
                    346: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    347:     }
1.30      albertel  348:     my @items;
1.1       albertel  349:     foreach my $name (@whichfoils) {
1.34      albertel  350: 	my $response = &Apache::response::getresponse($temp,'letter');
1.30      albertel  351: 	push(@items,$response);
1.1       albertel  352: 	my $responsename = $letter_name_map{$response};
                    353: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    354: 	if ( $response =~ /[^\s]/) {
1.31      albertel  355: 	    $responsehash{$name}=$responsename;
1.33      albertel  356: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
1.1       albertel  357: 	    if ($value eq $responsename) {
                    358: 		$grade{$name}='1'; $right++;
                    359: 	    } else {
                    360: 		$grade{$name}='0'; $wrong++;
                    361: 	    }
                    362: 	} else {
                    363: 	    $ignored++;
                    364: 	}
                    365: 	$temp++;
                    366:     }
                    367:     my $part=$Apache::inputtags::part;
                    368:     my $id = $Apache::inputtags::response['-1'];
                    369:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.30      albertel  370:     my $itemstr    =&Apache::lonnet::array2str(@items);
1.1       albertel  371:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.30      albertel  372:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1       albertel  373:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
                    374:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    375: 	$responsestr;
1.30      albertel  376:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
                    377: 	$itemstr;
1.1       albertel  378:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
                    379: 	$gradestr;
1.47      albertel  380:     if ($Apache::lonhomework::type eq 'survey') {
                    381: 	if ($ignored == 0) {
                    382: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    383: 	    &Apache::response::handle_previous(\%previous,$ad);
                    384: 	} elsif ($wrong==0 && $right==0) {
                    385: 	} else {
                    386: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
                    387: 	    &Apache::response::handle_previous(\%previous,$ad);
                    388: 	}
                    389:     } elsif (!$Apache::lonhomework::scantronmode) {
1.34      albertel  390: 	my $ad;
                    391: 	if ($wrong==0 && $ignored==0) {
                    392: 	    $ad='EXACT_ANS';
                    393: 	} elsif ($wrong==0 && $right==0) {
                    394: 	    #nothing submitted
                    395: 	} else {
                    396: 	    if ($ignored==0) {
                    397: 		$ad='INCORRECT';
                    398: 	    } else {
                    399: 		$ad='MISSING_ANSWER';
                    400: 	    }
                    401: 	}
                    402: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    403: 	&Apache::response::handle_previous(\%previous,$ad);
                    404:     } else {
                    405: 	my $ad;
                    406: 	if ($wrong==0 && $right==0) {
                    407: 	    #nothing submitted
                    408: 	} else {
                    409: 	    $ad='ASSIGNED_SCORE';
                    410: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    411: 		$ad;
                    412: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
1.35      albertel  413: 		$right/(scalar(@whichfoils));
1.34      albertel  414: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
1.35      albertel  415: 		scalar(@whichfoils);
1.34      albertel  416: 	}
                    417:     }
1.1       albertel  418: }
                    419: 
1.64      albertel  420: sub format_prior_answer {
                    421:     my ($mode,$answer,$other_data) = @_;
                    422:     my %answer      =&Apache::lonnet::str2hash($answer);
                    423:     my $foil_order  =$other_data->[0];
                    424:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
                    425:     my @items       =&Apache::lonnet::str2array($other_data->[2]);
                    426:     my $output;
                    427:     
                    428:     foreach my $name (@{ $foil_order }) {
                    429: 	my $item=shift(@items);
                    430: 	$output .= '<tr><td>'.$item.'</td></tr>';
                    431:     }
                    432:     return if (!defined($output));
                    433: 
                    434:     $output = '<table class="LC_prior_match">'.$output.'</table>';
                    435:     return $output;
                    436: }
                    437: 
1.4       albertel  438: sub itemdisplay {
                    439:     my ($location)=@_;
                    440:     if ($location eq 'top' &&
                    441: 	!defined($Apache::matchresponse::itemtable{'location'})) {
                    442: 	return $Apache::matchresponse::itemtable{'display'};
                    443:     }
                    444:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
                    445: 	return $Apache::matchresponse::itemtable{'display'};
                    446:     }
                    447:     return undef;
                    448: }
1.64      albertel  449: 
1.1       albertel  450: sub displayfoils {
                    451:     my ($target,$max,$randomize)=@_;
1.62      foxr      452:     my ($tabsize, $lefttabsize, $righttabsize);
                    453: 
1.1       albertel  454:     my $result;
1.4       albertel  455:     my $question;
1.18      albertel  456:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
                    457: 						   $randomize,
                    458: 				            &Apache::response::showallfoils(),
                    459: 				           \%Apache::response::foilgroup);
1.1       albertel  460:     my $part=$Apache::inputtags::part;
                    461:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    462:     my %letter_name_map;
                    463:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    464: 	%letter_name_map=
                    465: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    466:     }
                    467:     my %name_letter_map;
                    468:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    469: 	%name_letter_map=
                    470: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    471:     }
1.25      albertel  472:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1       albertel  473: 	foreach my $name (@whichfoils) {
                    474: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
                    475: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    476: 	    my $letter=$name_letter_map{$value};
1.5       sakharuk  477: 	    if ($target eq 'tex') {
                    478: 		$question.=' \\\\ '.$letter.':'.$text;
                    479: 	    } else {
1.53      albertel  480: 		$question.='<br /><b>'.$letter.':</b> '.$text;
1.5       sakharuk  481: 	    }
1.1       albertel  482: 	}
                    483:     } else {
                    484: 	my $i = 0;
1.36      albertel  485: 	my $temp=1;
1.1       albertel  486: 	my $id=$Apache::inputtags::response[-1];
                    487: 	my $part=$Apache::inputtags::part;
                    488: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.9       sakharuk  489: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
                    490: 	my @alphabet=('A'..'Z');
1.15      albertel  491: 	my @used_letters=sort(keys(%letter_name_map));
1.20      sakharuk  492: 	my $internal_counter=$Apache::lonxml::counter;
1.1       albertel  493: 	foreach my $name (@whichfoils) {
                    494: 	    my $lastopt=$lastresponse{$name};
                    495: 	    my $last_letter=$name_letter_map{$lastopt};
1.5       sakharuk  496: 	    my $optionlist = '';
1.7       sakharuk  497: 	    if ($target ne 'tex') {
                    498: 		$optionlist="<option></option>\n";
                    499: 	    } else {
                    500: 		if ($Apache::lonhomework::type ne 'exam') {
                    501: 		    $optionlist='\framebox[5 mm][s]{\tiny\strut}';
                    502: 		}
                    503: 	    }
1.1       albertel  504: 	    my $option;
1.15      albertel  505: 	    foreach $option (@used_letters) {
1.1       albertel  506: 		if ($option eq $last_letter) {
1.6       sakharuk  507: 		    if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1       albertel  508: 		} else {
1.6       sakharuk  509: 		    if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1       albertel  510: 		}
                    511: 	    }
1.19      sakharuk  512: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.61      albertel  513: 	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
                    514: 		  $part.'\');" name="HWVAL_'.
1.5       sakharuk  515: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
                    516: 		        $optionlist."</select>\n";
                    517: 	    }
1.1       albertel  518: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
1.62      foxr      519: 	    #
                    520:             #  Factor out the tex computations of the left/right 1/2 minipage
                    521: 	    #  widths for left or right positioning.  This allows us
                    522: 	    #  to, if necessary constrain the bubble widths:
                    523: 	    #
                    524: 	    if ($target eq 'tex'   &&
                    525: 		(&itemdisplay('left') || &itemdisplay('right'))) {
                    526: 		$tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
                    527: 		($lefttabsize,$righttabsize)=(0,0);
                    528: 		if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
                    529: 		    $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
                    530: 		    $lefttabsize=$tabsize*$1/100;	
                    531: 		    $righttabsize=0.95*($tabsize-$lefttabsize);
                    532: 		} else {
                    533: 		    $tabsize=~/(\d+\.?\d*)/;
                    534: 		    $lefttabsize=$1/2.1;
                    535: 		    $righttabsize=0.95*($1-$lefttabsize);
                    536: 		}
                    537: 	    }
1.5       sakharuk  538: 	    if ($target ne 'tex') {
1.19      sakharuk  539: 		if ($Apache::lonhomework::type ne 'exam') {
1.45      albertel  540: 		    $question.="<br />\n".$optionlist.$text;
1.19      sakharuk  541: 		} else {
1.45      albertel  542: 		    $question.="<br />\n".$text;
1.19      sakharuk  543: 		}
1.6       sakharuk  544: 		if ($Apache::lonhomework::type eq 'exam') {
1.42      albertel  545: 		    my @blank;
                    546: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
1.6       sakharuk  547: 		}
                    548: 	    } else {		
1.62      foxr      549: 		# Note that if left or right positioned, we must
                    550: 		# confine the bubbles to righttabsize:
                    551: 		#
1.6       sakharuk  552: 		if ($Apache::lonhomework::type eq 'exam') {
                    553: 		    $question.=' '.$optionlist.$text."\n";
1.13      sakharuk  554: 		    my @emptyItems = ();
1.16      albertel  555: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.62      foxr      556: 		    $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
                    557: 			.'\item \hskip -3mm \textbf{'.$internal_counter.'}';
                    558: 		    if (&itemdisplay('left') || &itemdisplay('right')) {
                    559: 			$question .= &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems, "", $righttabsize);
                    560: 		    }
                    561: 		    else {
                    562: 			$question .= &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems);
                    563: 		    }
                    564: 		    $question .= '\end{list} \vskip -8 mm \strut ';
1.20      sakharuk  565: 		    $internal_counter++;
1.6       sakharuk  566: 	        } else {
1.22      sakharuk  567: 		    $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
1.6       sakharuk  568: 		}
1.5       sakharuk  569:             } 
1.1       albertel  570: 	    $temp++;
                    571: 	}
1.4       albertel  572:     }
                    573:     if ($result=&itemdisplay('top')) {
                    574: 	$result.=$question;
                    575:     } elsif ($result=&itemdisplay('bottom')) {
                    576: 	$result=$question.$result;
                    577:     } elsif ($result=&itemdisplay('right')) {
1.32      sakharuk  578: 	if ($target ne 'tex') {
1.45      albertel  579: 	    #remove the first <br />
                    580: 	    $question=~s|<br />||;
                    581: 	    $result='<table><tr><td valign="top">'.$question.
                    582: 		'</td><td valign="top">'.$result.'</td></tr></table>';
1.32      sakharuk  583: 	} else {
1.43      sakharuk  584: 	    $lefttabsize.=' mm ';
                    585: 	    $righttabsize.=' mm ';
                    586: 	    $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  587: 	}
1.4       albertel  588:     } elsif ($result=&itemdisplay('left')) {
1.32      sakharuk  589: 	if ($target ne 'tex') {
1.45      albertel  590: 	    #remove the first <br />
                    591: 	    $question=~s|<br />||;
                    592: 	    $result='<table><tr><td valign="top">'.$result.
                    593: 		'</td><td valign="top">'.$question.'</td></tr></table>';
1.32      sakharuk  594: 	} else {
1.43      sakharuk  595: 	    $lefttabsize.=' mm ';
                    596: 	    $righttabsize.=' mm ';
                    597: 	    $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  598: 	}
1.1       albertel  599:     }
1.64      albertel  600:     if ($target eq 'web') {
                    601: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
                    602: 						  [\@whichfoils,
                    603: 						   'submissiongrading',
                    604: 						   'submissionitems']);
                    605:     }
1.5       sakharuk  606:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1       albertel  607:     return $result;
                    608: }
                    609: 
                    610: sub getfoilcounts {
                    611:     my ($max)=@_;
                    612:     # +1 since instructors will count from 1
                    613:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
                    614:     if (&Apache::response::showallfoils()) { $max=$count; }
                    615:     if ($count>$max) { $count=$max } 
                    616:     &Apache::lonxml::debug("Count is $count from $max");
                    617:     return $count;
                    618: }
                    619: 
                    620: 
                    621: sub start_conceptgroup {
                    622:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    623:     $Apache::matchresponse::conceptgroup=1;
                    624:     %Apache::response::conceptgroup=();
                    625:     my $result;
                    626:     if ($target eq 'edit') {
                    627: 	$result.=&Apache::edit::tag_start($target,$token,
                    628: 					  "Concept Grouped Foils");
                    629: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    630: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    631:     }
                    632:     if ($target eq 'modified') {
                    633: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    634: 						     $safeeval,'concept');
                    635: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    636:     }
                    637:     return $result;
                    638: }
                    639: 
                    640: sub end_conceptgroup {
                    641:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    642:     $Apache::matchresponse::conceptgroup=0;
                    643:     my $result='';
1.28      albertel  644:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    645: 	$target eq 'tex' || $target eq 'analyze') {
                    646: 	&Apache::response::pick_foil_for_concept($target,
                    647: 						 ['value','text','location'],
                    648: 						 \%Apache::hint::match,
                    649: 						 $parstack,$safeeval);
1.1       albertel  650:     } elsif ($target eq 'edit') {
                    651: 	$result=&Apache::edit::end_table();
                    652:     }
                    653:     return $result;
                    654: }
                    655: 
                    656: sub insert_conceptgroup {
                    657:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    658:     return $result;
                    659: }
                    660: 
                    661: sub start_foil {
                    662:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    663:     my $result='';
1.28      albertel  664:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  665: 	&Apache::lonxml::startredirection;
1.46      albertel  666: 	if ($target eq 'analyze') {
                    667: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    668: 	}
1.1       albertel  669:     } elsif ($target eq 'edit') {
                    670: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
                    671: 	my $level='-2';
                    672: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
                    673: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    674: 	my @names;
                    675: 	if (defined(@{ $Apache::response::itemgroup{'names'} })) {
                    676: 	    @names=@{ $Apache::response::itemgroup{'names'} };
                    677: 	}
                    678: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
                    679: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    680: 						 $safeeval,'-3');
                    681: 	if ($randomize ne 'no') {
                    682: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    683: 					     ['random','top','bottom'],$token);
                    684: 	}
                    685: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    686:     } elsif ($target eq 'modified') {
                    687: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    688: 						     $safeeval,'value',
                    689: 						     'name','location');
                    690: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    691:     }
                    692:     return $result;
                    693: }
                    694: 
                    695: sub end_foil {
                    696:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    697:     my $text ='';
                    698:     my $result = '';
1.28      albertel  699:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  700: 	$text=&Apache::lonxml::endredirection;
                    701:     }
1.48      albertel  702: 
1.28      albertel  703:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    704: 	$target eq 'tex' || $target eq 'analyze') {
1.48      albertel  705: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    706: 	    $text='\vskip 5mm $\triangleright$ '.$text;
                    707: 	}
1.1       albertel  708: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    709: 	if ($value ne 'unused') {
                    710: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    711: 	    &Apache::lonxml::debug("Got a name of :$name:");
1.49      albertel  712: 	    if (!$name) {
1.54      albertel  713: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.49      albertel  714: 		$name=$Apache::lonxml::curdepth;
                    715: 	    }
1.1       albertel  716: 	    &Apache::lonxml::debug("Using a name of :$name:");
1.37      albertel  717: 	    if (defined($Apache::response::foilnames{$name})) {
                    718: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    719: 	    }
1.38      albertel  720: 	    $Apache::response::foilnames{$name}++;
1.37      albertel  721: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    722: 						     $safeeval);
1.1       albertel  723: 	    if ( $Apache::matchresponse::conceptgroup
                    724: 		 && !&Apache::response::showallfoils() ) {
                    725: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    726: 		$Apache::response::conceptgroup{"$name.value"} = $value;
1.48      albertel  727: 		$Apache::response::conceptgroup{"$name.text"} = $text;
1.1       albertel  728: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    729: 	    } else {
                    730: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    731: 		$Apache::response::foilgroup{"$name.value"} = $value;
1.48      albertel  732: 		$Apache::response::foilgroup{"$name.text"} = $text;
1.1       albertel  733: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    734: 	    }
                    735: 	}
                    736:     }
                    737:     if ($target eq 'edit') {
                    738: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    739:     }
                    740:     return $result;
                    741: }
                    742: 
                    743: sub insert_foil {
                    744:     return '
                    745: <foil name="" value="unused">
                    746: <startouttext />
                    747: <endouttext />
                    748: </foil>';
                    749: }
                    750: 1;
                    751: __END__

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