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

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

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