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

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

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