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

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

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