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

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

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