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

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

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