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

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

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