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

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

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