File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.86: download - view: text, annotated - select for diffs
Thu Dec 15 01:21:28 2011 UTC (12 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
- Bug 2802.
  - Separate decision on leniency in grading from logic on whether additional
    new submission is possible for partially correct problem parts.
  - new parameter: retrypartial set to "yes" if part is to be answerable
    when partially correct.
  - custompartial.problem template incorporates retrypartial parameter in
    place of lenient parameter.
  - points awarded > 1 no longer considered 'partially correct'.
  - &grading_is_nonlenient() moved from inputtags.pm back to optionresponse.pm

    1: # The LearningOnline Network with CAPA
    2: # Full matching style response
    3: #
    4: # $Id: matchresponse.pm,v 1.86 2011/12/15 01:21:28 raeburn Exp $
    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();
   33: use Apache::optionresponse();
   34: use Apache::lonlocal;
   35: use Apache::lonnet;
   36: use Apache::lonxml;
   37: 
   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=();
   52:     undef(%Apache::response::foilnames);
   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>'
   58:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
   59: 	   .&Apache::edit::deletelist($target,$token)
   60:            .'</span></td>'
   61:            .'<td>&nbsp;'.&Apache::edit::end_row()
   62: 	   .&Apache::edit::start_spanning_row();
   63: 	
   64: 	$result.=
   65: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
   66: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
   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); }
   74:     } elsif ($target eq 'analyze') {
   75: 	my $part_id="$Apache::inputtags::part.$id";
   76:         $Apache::lonhomework::analyze{"$part_id.type"} = 'matchresponse';
   77: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   78: 	push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
   79: 	      1);
   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'));
   92:     undef(%Apache::response::foilnames);
   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=();
  101:     %Apache::matchresponse::itemtable=();
  102: 
  103:     if ($target eq 'edit') {
  104: 	$result=&Apache::edit::tag_start($target,$token);
  105: 	$result.=&Apache::edit::select_arg('Randomize Order:','randomize',
  106: 					   ['yes','no'],$token).'&nbsp;'x 3;
  107: 	$result.=&Apache::edit::select_arg('Items Display Location:',
  108: 					   'location',
  109: 					   ['top','bottom','left','right'],
  110: 					   $token).'&nbsp;'x 3;
  111: 	$result.=&Apache::edit::select_arg('Items Display Direction:',
  112: 					   'direction',
  113: 					   ['vertical','horizontal'],
  114: 					   $token);
  115: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  116:     } elsif ($target eq 'modified') {
  117: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  118: 						     $safeeval,'randomize',
  119: 						     'location','direction');
  120: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  121:     } elsif ($target eq 'web' or $target eq 'tex') {
  122: 	$Apache::matchresponse::itemtable{'location'}=
  123: 	    &Apache::lonxml::get_param('location',$parstack,$safeeval);
  124: 	$Apache::matchresponse::TeXitemgroupwidth=&Apache::lonxml::get_param('TeXitemgroupwidth',$parstack,$safeeval,undef,0);
  125:     }
  126:     return $result;
  127: }
  128: 
  129: sub end_itemgroup {
  130:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  131:     my $result;
  132: 
  133:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
  134:     if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') {
  135:         if(!@{ $Apache::response::itemgroup{'names'} }) { return; }
  136:     } else {
  137:         return;
  138:     }
  139:     my @names=@{ $Apache::response::itemgroup{'names'} };
  140:     my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
  141:     if ($randomize ne 'no' ) {
  142: 	@names=&Apache::response::whichorder($#names+1,$randomize,0,
  143: 					 \%Apache::response::itemgroup);
  144:     }
  145:     if ($target eq 'analyze') {
  146: 	my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  147: 	push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
  148:     }
  149:     my %letter_name_map;
  150:     my %name_letter_map;
  151:     my @alphabet=('A'..'Z');
  152:     my $i=0;
  153:     foreach my $name (@names) {
  154: 	$letter_name_map{$alphabet[$i]}=$name;
  155: 	$name_letter_map{$name}=$alphabet[$i];
  156: 	$i++;
  157:     }
  158:     $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
  159:     $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
  160:     my $direction=&Apache::lonxml::get_param('direction',$parstack,$safeeval);
  161:     if ($target eq 'web') {
  162: 	
  163: 	my $table='<br /><table>'; # extra space to match what latex does.
  164: 	my $i=0;
  165: 	if ($direction eq 'horizontal') { $table .='<tr>';}
  166: 	foreach my $name (@names) {
  167: 	    if ($direction ne 'horizontal') { $table.='<tr>'; }
  168: 	    $table.='<td>'.$alphabet[$i].'</td><td>'.
  169: 		$Apache::response::itemgroup{$name.'.text'}.'</td>';
  170: 	    if ($direction ne 'horizontal') { $table.='</tr>'; }
  171: 	    $i++;
  172: 	}
  173: 	if ($direction eq 'horizontal') { $table .='</tr>';}
  174: 	$table.='</table>';
  175: 	$Apache::matchresponse::itemtable{'display'}=$table;
  176: 	$Apache::lonxml::post_evaluate=0;
  177:     } elsif ($target eq 'tex') {
  178: 	my $table=' \begin{description}\setlength{\leftmargin}{2em}\setlength{\labelwidth}{1em}\setlength{\itemsep}{0.5pt plus1pt minus2pt}\setlength{\listparindent}{0em} ';
  179: 	my $i=0;
  180:         my ($numrows,$bubbles_per_row);
  181:         if ($Apache::lonhomework::type eq 'exam') {
  182:             ($numrows,$bubbles_per_row) = 
  183:                 &Apache::optionresponse::getnumrows(scalar(@names)); 
  184:         } else {
  185:             $numrows = 1;
  186:         }
  187: 	foreach my $name (@names) {
  188: 	    # $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
  189:             my $item;
  190:             if (($numrows > 1) && ($bubbles_per_row > 0)) {
  191:                 my $num = 1+int($i/$bubbles_per_row);
  192:                 my $idx = int($i % $bubbles_per_row);
  193:                 if ($idx == 0) {
  194:                     if ($num == 1) {
  195:                         $table .= '\item[\footnotesize {'.&mt('(first line)').'}]';
  196:                     } else {
  197:                         $table .= '\item[\footnotesize {'.&mt('(next line)').'}]';
  198:                     }
  199:                 }
  200:                 $item = $alphabet[$idx];
  201:             } else {
  202:                 $item = $alphabet[$i];
  203:             }
  204: 	    $table.='\item['.$item.'] '.
  205: 		$Apache::response::itemgroup{$name.'.text'};
  206: 	    $i++;
  207: 	}
  208: 	$table.=' \end{description}  \strut ';
  209: 	if ($Apache::lonhomework::type eq 'exam')  {$table.='\vskip -13 mm \strut ';}
  210: 	$Apache::matchresponse::itemtable{'display'}=$table;
  211: 	$Apache::lonxml::post_evaluate=0;
  212:     }
  213:     return $result;
  214: }
  215: 
  216: sub start_item {
  217:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  218:     my $result='';
  219:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  220: 	&Apache::lonxml::startredirection;
  221:     } elsif ($target eq 'edit') {
  222: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  223: 						 $safeeval,'-2');
  224: 	$result=&Apache::edit::tag_start($target,$token,"Item");
  225: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  226: 	if ($randomize ne 'no') {
  227: 	    $result.='&nbsp;'x 3 .
  228:                      &Apache::edit::select_arg('Location:','location',
  229: 					       ['random','top','bottom'],
  230: 					       $token);
  231: 	}
  232: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  233:     } elsif ($target eq 'modified') {
  234: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  235: 						     $safeeval,'name',
  236: 						     'location');
  237: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  238:     }
  239:     return $result;
  240: }
  241: 
  242: sub end_item {
  243:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  244:     my $text ='';
  245:     my $result = '';
  246:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  247: 	$text=&Apache::lonxml::endredirection;
  248:     }
  249:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  250: 	$target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
  251: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  252: 	my $location=&Apache::lonxml::get_param('location',$parstack,
  253: 						$safeeval);
  254: 	&Apache::lonxml::debug("Got a name of :$name:");
  255: 	if ($name eq "") { $name=$Apache::lonxml::curdepth; }
  256: 	&Apache::lonxml::debug("Using a name of :$name:");
  257: 	push @{ $Apache::response::itemgroup{'names'} }, $name;
  258: 	$Apache::response::itemgroup{"$name.text"} = $text;
  259: 	$Apache::response::itemgroup{"$name.location"} = $location;
  260:     }
  261:     if ($target eq 'edit') {
  262: 	$result.= &Apache::edit::tag_end($target,$token,'');
  263:     }
  264:     return $result;
  265: }
  266: 
  267: sub insert_item {
  268:     return '
  269: <item name="">
  270: <startouttext />
  271: <endouttext />
  272: </item>';
  273: }
  274: 
  275: %Apache::response::foilgroup=();
  276: sub start_foilgroup {
  277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  278:     my $result;
  279:     %Apache::response::foilgroup=();
  280:     $Apache::matchresponse::conceptgroup=0;
  281:     &Apache::response::pushrandomnumber(undef,$target);
  282:     if ($target eq 'edit') {
  283: 	$result.=&Apache::edit::start_table($token)
  284: 	    .'<tr><td>'.&mt('Collection Of Foils').'</td>'
  285:             .'<td><span class="LC_nobreak">'.&mt('Delete?')
  286: 	    .&Apache::edit::deletelist($target,$token)
  287: 	    .'</span></td>'
  288:             .'<td>&nbsp;'.&Apache::edit::end_row()
  289:             .&Apache::edit::start_spanning_row()."\n";
  290:     }
  291:     return $result;
  292: }
  293: 
  294: sub end_foilgroup {
  295:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  296:     my $result;
  297:     my $part        = $Apache::inputtags::part;
  298:     my $response_id = $Apache::inputtags::response[-1];
  299: 
  300:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
  301: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  302: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  303: 						   $safeeval,'-2');
  304: 	if ($target eq 'web' || $target eq 'tex') {
  305: 	    $result=&displayfoils($target,$max,$randomize);
  306: 	    $Apache::lonxml::post_evaluate=0;
  307: 	} elsif ($target eq 'answer' ) {
  308: 	    $result=&displayanswers($max,$randomize);
  309: 	} elsif ( $target eq 'grade') {
  310: 	    &grade_response($max,$randomize);
  311: 	} elsif ( $target eq 'analyze') {
  312: 	    my @shown=&whichfoils($max,$randomize);
  313: 	    &Apache::response::analyze_store_foilgroup(\@shown,
  314: 						 ['text','value','location']);
  315: 	    #FIXME need to store options in some way
  316: 	}
  317:         my ($numrows,$bubbles_per_row);
  318:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  319:             my $numitems;
  320:             if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') {
  321:                 $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
  322:                 ($numrows,$bubbles_per_row) =
  323:                     &Apache::optionresponse::getnumrows($numitems);
  324:             }
  325:         }
  326:         if ($numrows < 1) {
  327:             $numrows = 1;
  328:         }
  329:         my $increment = &getfoilcounts($max) * $numrows;
  330: 	&Apache::lonxml::increment_counter($increment,"$part.$response_id");
  331: 	if ($target eq 'analyze') {
  332: 	    &Apache::lonhomework::set_bubble_lines();
  333: 	}
  334: 
  335:     } elsif ($target eq 'edit') {
  336: 	$result=&Apache::edit::end_table();
  337:     }
  338:     &Apache::response::poprandomnumber();
  339:     return $result;
  340: }
  341: 
  342: sub whichfoils {
  343:     my ($max,$randomize)=@_;
  344:     return &Apache::response::whichorder(&getfoilcounts($max),
  345: 					 $randomize,
  346: 					 &Apache::response::showallfoils(),
  347: 					 \%Apache::response::foilgroup);
  348: }
  349: 
  350: sub displayanswers {
  351:     my ($max,$randomize,@opt)=@_;
  352:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  353:         if (!@{ $Apache::response::foilgroup{'names'} }) { return; }
  354:     } else {
  355:         return;
  356:     }
  357:     my @names = @{ $Apache::response::foilgroup{'names'} };
  358:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
  359: 						   $randomize,
  360: 					  &Apache::response::showallfoils(),
  361: 					 \%Apache::response::foilgroup);
  362:     my %name_letter_map;
  363:     if (ref($Apache::response::itemgroup{'name_letter_map'}) eq 'HASH') {
  364:         if (%{$Apache::response::itemgroup{'name_letter_map'}}) {
  365: 	    %name_letter_map=
  366: 	        %{ $Apache::response::itemgroup{'name_letter_map'} };
  367:         }
  368:     }
  369:     my $result;
  370:     if ($Apache::lonhomework::type eq 'exam') {
  371: 	my $i=0;
  372: 	foreach my $name (@whichfoils) {
  373: 	    $result.=&Apache::response::answer_header('matchresponse',$i++);
  374: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
  375: 	    my $letter=$name_letter_map{$value_name};
  376: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
  377: 	    $result.=&Apache::response::answer_footer('matchresponse');
  378: 	}
  379:     } else {
  380: 	$result=&Apache::response::answer_header('matchresponse');
  381: 	foreach my $name (@whichfoils) {
  382: 	    my $value_name=$Apache::response::foilgroup{$name.'.value'};
  383: 	    my $letter=$name_letter_map{$value_name};
  384: 	    $result.=&Apache::response::answer_part('matchresponse',$letter);
  385: 	}
  386: 	$result.=&Apache::response::answer_footer('matchresponse');
  387:     }
  388:     return $result;
  389: }
  390: 
  391: 
  392: sub grade_response {
  393:     my ($max,$randomize)=@_;
  394:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  395: 						   $randomize,
  396: 				            &Apache::response::showallfoils(),
  397: 				           \%Apache::response::foilgroup);
  398:     if (!&Apache::response::submitted()) { return; }
  399:     my %responsehash;
  400:     my %grade;
  401:     my %answerhash;
  402:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  403:     my %letter_name_map;
  404:     if (ref($Apache::response::itemgroup{'letter_name_map'}) eq 'HASH') {
  405:         if (%{$Apache::response::itemgroup{'letter_name_map'}}) {
  406: 	    %letter_name_map=
  407: 	        %{ $Apache::response::itemgroup{'letter_name_map'} };
  408:         }
  409:     }
  410:     my @items;
  411:     my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
  412:     my ($numrows,$bubbles_per_row);
  413:     if ($Apache::lonhomework::scantronmode) {
  414:         my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
  415:         ($numrows,$bubbles_per_row) =
  416:             &Apache::optionresponse::getnumrows($numitems);
  417:     }
  418:     if ($numrows < 1) {
  419:         $numrows = 1;
  420:     }
  421:     my @alphabet=('A'..'Z');
  422:     my %nums_from_letters;
  423:     for (my $i=0; $i<@alphabet; $i++) {
  424:         $nums_from_letters{$alphabet[$i]} = $i;
  425:     }
  426:     foreach my $name (@whichfoils) {
  427:         my $response;
  428:         if ($numrows > 1) {
  429:             my $num = $temp;
  430:             my $totalnum;
  431:             for (my $i=0; $i<$numrows; $i++) {
  432:                 my $item = &Apache::response::getresponse($num,'letter');
  433:                 if ($item =~ /^\w$/) {
  434:                     $totalnum = $i*$bubbles_per_row + $nums_from_letters{$item};
  435:                 }
  436:                 $num ++;
  437:             }
  438:             if ($totalnum =~ /^\d+$/) {
  439:                 $response = $alphabet[$totalnum];
  440:             }
  441:             $temp += $numrows;
  442:         } else {
  443: 	    $response = &Apache::response::getresponse($temp,'letter');
  444:             $temp ++;
  445:         }
  446: 	push(@items,$response);
  447: 	my $responsename = $letter_name_map{$response};
  448: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  449:         if ($Apache::lonhomework::type eq 'randomizetry') {
  450:             $answerhash{$name} = $value;
  451:         }
  452: 	if ( $response =~ /[^\s]/) {
  453: 	    $responsehash{$name}=$responsename;
  454: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
  455: 	    if ($value eq $responsename) {
  456: 		$grade{$name}='1'; $right++;
  457: 	    } else {
  458: 		$grade{$name}='0'; $wrong++;
  459: 	    }
  460: 	} else {
  461: 	    $ignored++;
  462: 	}
  463:     }
  464:     my $part=$Apache::inputtags::part;
  465:     my $nonlenient=&Apache::optionresponse::grading_is_nonlenient($part);
  466:     my $id = $Apache::inputtags::response['-1'];
  467:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  468:     my $itemstr    =&Apache::lonnet::array2str(@items);
  469:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  470:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  471:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
  472:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  473: 	$responsestr;
  474:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
  475: 	$itemstr;
  476:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
  477: 	$gradestr;
  478:     if ($Apache::lonhomework::type eq 'randomizetry') {
  479:         $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
  480:     }
  481:     if (($Apache::lonhomework::type eq 'survey') ||
  482:         ($Apache::lonhomework::type eq 'surveycred') ||
  483:         ($Apache::lonhomework::type eq 'anonsurvey') ||
  484:         ($Apache::lonhomework::type eq 'anonsurveycred')) {
  485: 	if ($ignored == 0) {
  486: 	    my $ad;
  487:             if ($Apache::lonhomework::type eq 'anonsurvey') {
  488:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  489:             } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
  490:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  491:             } elsif ($Apache::lonhomework::type eq 'surveycred') {
  492:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  493:             } else {
  494:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  495:             }
  496: 	    &Apache::response::handle_previous(\%previous,$ad);
  497: 	} elsif ($wrong==0 && $right==0) {
  498: 	} else {
  499: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  500: 	    &Apache::response::handle_previous(\%previous,$ad);
  501: 	}
  502:     } elsif ($nonlenient) {
  503:         my $ad;
  504:         if ($wrong==0 && $ignored==0) {
  505:             $ad='EXACT_ANS';
  506:         } elsif ($wrong==0 && $right==0) {
  507:             #nothing submitted
  508:         } else {
  509:             if ($ignored==0) {
  510:                 $ad='INCORRECT';
  511:             } else {
  512:                 $ad='MISSING_ANSWER';
  513:             }
  514:         }
  515:         $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  516:         &Apache::response::handle_previous(\%previous,$ad);
  517:     } else {
  518:         my $ad;
  519:         if ($wrong==0 && $right==0) {
  520:             #nothing submitted only assign a score if we
  521:             #need to override a previous grade
  522:             if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  523:                 $ad='ASSIGNED_SCORE';
  524:             }
  525:         } else {
  526:             $ad='ASSIGNED_SCORE';
  527:         }
  528:         $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  529:         $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  530:             $right/(scalar(@whichfoils));
  531:         $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  532:             scalar(@whichfoils);
  533:     }
  534: }
  535: 
  536: sub format_prior_answer {
  537:     my ($mode,$answer,$other_data) = @_;
  538:     my %answer      =&Apache::lonnet::str2hash($answer);
  539:     my $foil_order  =$other_data->[0];
  540:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
  541:     my @items       =&Apache::lonnet::str2array($other_data->[2]);
  542:     my $output;
  543:     
  544:     foreach my $name (@{ $foil_order }) {
  545: 	my $item=shift(@items);
  546: 	$output .= '<tr><td>'.$item.'</td></tr>';
  547:     }
  548:     return if (!defined($output));
  549: 
  550:     $output = '<table class="LC_prior_match">'.$output.'</table>';
  551:     return $output;
  552: }
  553: 
  554: sub itemdisplay {
  555:     my ($location)=@_;
  556:     if ($location eq 'top' &&
  557: 	!defined($Apache::matchresponse::itemtable{'location'})) {
  558: 	return $Apache::matchresponse::itemtable{'display'};
  559:     }
  560:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
  561: 	return $Apache::matchresponse::itemtable{'display'};
  562:     }
  563:     return undef;
  564: }
  565: 
  566: sub displayfoils {
  567:     my ($target,$max,$randomize)=@_;
  568:     my ($tabsize, $lefttabsize, $righttabsize);
  569: 
  570:     my $result;
  571:     my $question;
  572:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  573: 						   $randomize,
  574: 				            &Apache::response::showallfoils(),
  575: 				           \%Apache::response::foilgroup);
  576:     my $part=$Apache::inputtags::part;
  577:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  578:     my %letter_name_map;
  579:     if (ref($Apache::response::itemgroup{'letter_name_map'}) eq 'HASH') {
  580:         if (%{$Apache::response::itemgroup{'letter_name_map'}}) {
  581: 	    %letter_name_map=
  582: 	        %{ $Apache::response::itemgroup{'letter_name_map'} };
  583:         }
  584:     }
  585:     my %name_letter_map;
  586:     if (ref($Apache::response::itemgroup{'name_letter_map'}) eq 'HASH') {
  587:         if (%{$Apache::response::itemgroup{'name_letter_map'}}) {
  588: 	    %name_letter_map=
  589: 	        %{ $Apache::response::itemgroup{'name_letter_map'} };
  590:         }
  591:     }
  592:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  593: 	foreach my $name (@whichfoils) {
  594: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  595: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  596: 	    my $letter=$name_letter_map{$value};
  597: 	    if ($target eq 'tex') {
  598: 		$question.=' \\\\ '.$letter.':'.$text;
  599: 	    } else {
  600: 		$question.='<br /><b>'.$letter.':</b> '.$text;
  601: 	    }
  602: 	}
  603:     } else {
  604: 	my $i = 0;
  605: 	my $temp=1;
  606: 	my $id=$Apache::inputtags::response[-1];
  607: 	my $part=$Apache::inputtags::part;
  608:         my ($lastresponse,$newvariation);
  609:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  610:             ($Apache::lonhomework::type eq 'randomizetry')) &&
  611:             ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  612:             if ($env{'form.'.$part.'.rndseed'} ne
  613:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  614:                 $newvariation = 1;
  615:             }
  616:         }
  617:         unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred') && (defined($env{'form.grade_symb'}))) || $newvariation) {
  618: 	    $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  619:         }
  620: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
  621: 	my @alphabet=('A'..'Z');
  622: 	my @used_letters=sort(keys(%letter_name_map));
  623: 	my $internal_counter=$Apache::lonxml::counter;
  624:         my ($numrows,$bubbles_per_row);
  625:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  626:             ($numrows,$bubbles_per_row) = 
  627:                 &Apache::optionresponse::getnumrows(scalar(@used_letters));
  628:         } else {
  629:             $numrows = 1;
  630:         }
  631: 	foreach my $name (@whichfoils) {
  632: 	    my $lastopt=$lastresponse{$name};
  633: 	    my $last_letter=$name_letter_map{$lastopt};
  634: 	    my $optionlist = '';
  635: 	    if ($target ne 'tex') {
  636: 		$optionlist="<option></option>\n";
  637: 	    } else {
  638: 		if ($Apache::lonhomework::type ne 'exam') {
  639:                     if($env{'form.pdfFormFields'} eq 'yes'
  640:                        && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
  641:                         my $fieldname = $env{'request.symb'} . '&part_'. $Apache::inputtags::part .'&matchresponse'. '&HWVAL_' . $Apache::inputtags::response['-1'] . ':' . $temp . '&submit_' . $Apache::inputtags::part . '&';
  642:                         $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
  643:                     } else {
  644: 
  645:                         $optionlist='\framebox[10 mm][s]{\tiny\strut}';
  646:                     }
  647:                 }
  648: 	    }
  649: 	    my $option;
  650: 	    foreach $option (@used_letters) {
  651: 		if ($option eq $last_letter) {
  652: 		    if ($target ne 'tex') {
  653:                         $optionlist.="<option selected=\"selected\">$option</option>\n";
  654:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  655:                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
  656:                              && $Apache::lonhomework::type ne 'exam') {
  657:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  658:                     }
  659: 		} else {
  660: 		    if ($target ne 'tex') {
  661:                         $optionlist.="<option>$option</option>\n";
  662:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  663:                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
  664:                              && $Apache::lonhomework::type ne 'exam') {
  665:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  666:                     }
  667: 		}
  668: 	    }
  669: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  670: 	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  671: 		  $part.'\');" name="HWVAL_'.
  672: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  673: 		        $optionlist."</select>\n";
  674: 	    }
  675: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  676: 	    #
  677:             #  Factor out the tex computations of the left/right 1/2 minipage
  678: 	    #  widths for left or right positioning.  This allows us
  679: 	    #  to, if necessary constrain the bubble widths:
  680: 	    #
  681: 	    if ($target eq 'tex'   &&
  682: 		(&itemdisplay('left') || &itemdisplay('right'))) {
  683: 		$tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
  684: 		($lefttabsize,$righttabsize)=(0,0);
  685: 		if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
  686: 		    $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
  687: 		    $lefttabsize=$tabsize*$1/100;	
  688: 		    $righttabsize=0.95*($tabsize-$lefttabsize);
  689: 		} else {
  690: 		    $tabsize=~/(\d+\.?\d*)/;
  691: 		    $lefttabsize=$1/2.1;
  692: 		    $righttabsize=0.95*($1-$lefttabsize);
  693: 		}
  694: 	    }
  695: 	    if ($target ne 'tex') {
  696: 		if ($Apache::lonhomework::type ne 'exam') {
  697: 		    $question.="<br />\n".$optionlist.$text;
  698: 		} else {
  699: 		    $question.="<br />\n".$text;
  700: 		}
  701: 		if ($Apache::lonhomework::type eq 'exam') {
  702: 		    my @blank;
  703: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
  704: 		}
  705: 	    } else {		
  706: 		# Note that if left or right positioned, we must
  707: 		# confine the bubbles to righttabsize:
  708: 		#
  709: 		if ($Apache::lonhomework::type eq 'exam') {
  710: 		    $question.=' '.$optionlist.$text."\n";
  711: 		    my @emptyItems = ();
  712: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
  713: 		    $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
  714: 			.'\item \hskip -3mm ';
  715:                     if ($numrows == 1) {
  716:                         $question .= '\textbf{'.$internal_counter.'}';
  717:                     } else {
  718:                         my $linetext;
  719:                         for (my $i=0; $i<$numrows; $i++) {
  720:                             $linetext .= $internal_counter+$i.', ';
  721:                         }
  722:                         $linetext =~ s/,\s$//;
  723:                         $question .= '\small {\textbf{'.$linetext.'}} '.
  724:                                      '\hskip 2 mm {\footnotesize '.
  725:                                      &mt('(Bubble once in [_1] lines)',$numrows).
  726:                                      '} \vskip 3 mm';
  727:                     }
  728:                     my $max_width;
  729: 		    if (&itemdisplay('left') || &itemdisplay('right')) {
  730:                         $max_width = $righttabsize;
  731:                     }
  732: 		    $question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems,'',$max_width,$numrows,$bubbles_per_row,$internal_counter);
  733: 		    $question .= '\end{list} \vskip -8 mm \strut ';
  734: 		    $internal_counter += $numrows;
  735: 	        } else {
  736:                     if($env{'form.pdfFormFields'} eq 'yes' 
  737:                             && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
  738:                         $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
  739:                     } else {
  740:                         $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
  741:                     }
  742: 		}
  743:             } 
  744: 	    $temp++;
  745: 	}
  746:     }
  747:     if ($result=&itemdisplay('top')) {
  748: 	$result.=$question;
  749:     } elsif ($result=&itemdisplay('bottom')) {
  750: 	$result=$question.$result;
  751:     } elsif ($result=&itemdisplay('right')) {
  752: 	if ($target ne 'tex') {
  753: 	    #remove the first <br />
  754: 	    $question=~s|<br />||;
  755: 	    $result='<table><tr><td valign="top">'.$question.
  756: 		'</td><td valign="top">'.$result.'</td></tr></table>';
  757: 	} else {
  758: 	    $lefttabsize.=' mm ';
  759: 	    $righttabsize.=' mm ';
  760: 	    $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}';
  761: 	}
  762:     } elsif ($result=&itemdisplay('left')) {
  763: 	if ($target ne 'tex') {
  764: 	    #remove the first <br />
  765: 	    $question=~s|<br />||;
  766: 	    $result='<table><tr><td valign="top">'.$result.
  767: 		'</td><td valign="top">'.$question.'</td></tr></table>';
  768: 	} else {
  769: 	    $lefttabsize.=' mm ';
  770: 	    $righttabsize.=' mm ';
  771: 	    $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}';
  772: 	}
  773:     }
  774:     if ($target eq 'web') {
  775:         my $questiontype;
  776:         if ($Apache::lonhomework::type eq 'randomizetry') {
  777:             $questiontype = $Apache::lonhomework::type;
  778:         }
  779: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  780: 						  [\@whichfoils,
  781: 						   'submissiongrading',
  782: 						   'submissionitems'],
  783:                                                   $questiontype);
  784:     }
  785:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
  786:     return $result;
  787: }
  788: 
  789: sub getfoilcounts {
  790:     my ($max)=@_;
  791:     # +1 since instructors will count from 1
  792:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  793:     if (&Apache::response::showallfoils()) { $max=$count; }
  794:     if ($count>$max) { $count=$max } 
  795:     &Apache::lonxml::debug("Count is $count from $max");
  796:     return $count;
  797: }
  798: 
  799: 
  800: sub start_conceptgroup {
  801:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  802:     $Apache::matchresponse::conceptgroup=1;
  803:     %Apache::response::conceptgroup=();
  804:     my $result;
  805:     if ($target eq 'edit') {
  806: 	$result.=&Apache::edit::tag_start($target,$token,
  807: 					  "Concept Grouped Foils");
  808: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  809: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  810:     }
  811:     if ($target eq 'modified') {
  812: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  813: 						     $safeeval,'concept');
  814: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  815:     }
  816:     return $result;
  817: }
  818: 
  819: sub end_conceptgroup {
  820:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  821:     $Apache::matchresponse::conceptgroup=0;
  822:     my $result='';
  823:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  824: 	$target eq 'tex' || $target eq 'analyze') {
  825: 	&Apache::response::pick_foil_for_concept($target,
  826: 						 ['value','text','location'],
  827: 						 \%Apache::hint::match,
  828: 						 $parstack,$safeeval);
  829:     } elsif ($target eq 'edit') {
  830: 	$result=&Apache::edit::end_table();
  831:     }
  832:     return $result;
  833: }
  834: 
  835: sub insert_conceptgroup {
  836:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  837:     return $result;
  838: }
  839: 
  840: sub start_foil {
  841:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  842:     my $result='';
  843:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  844: 	&Apache::lonxml::startredirection;
  845: 	if ($target eq 'analyze') {
  846: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  847: 	}
  848:     } elsif ($target eq 'edit') {
  849: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  850: 	my $level='-2';
  851: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  852: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  853: 	my @names;
  854: 	if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') {
  855:             if (@{ $Apache::response::itemgroup{'names'} }) {
  856: 	        @names=@{ $Apache::response::itemgroup{'names'} };
  857:             }
  858: 	}
  859: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
  860: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  861: 						 $safeeval,'-3');
  862: 	if ($randomize ne 'no') {
  863: 	    $result.=&Apache::edit::select_arg('Location:','location',
  864: 					     ['random','top','bottom'],$token);
  865: 	}
  866: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  867:     } elsif ($target eq 'modified') {
  868: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  869: 						     $safeeval,'value',
  870: 						     'name','location');
  871: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  872:     }
  873:     return $result;
  874: }
  875: 
  876: sub end_foil {
  877:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  878:     my $text ='';
  879:     my $result = '';
  880:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  881: 	$text=&Apache::lonxml::endredirection;
  882:     }
  883: 
  884:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  885: 	$target eq 'tex' || $target eq 'analyze') {
  886: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  887: 	    $text='\vskip 5mm $\triangleright$ '.$text;
  888: 	}
  889: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  890: 	if ($value ne 'unused') {
  891: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  892: 	    &Apache::lonxml::debug("Got a name of :$name:");
  893: 	    if (!$name) {
  894: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  895: 		$name=$Apache::lonxml::curdepth;
  896: 	    }
  897: 	    &Apache::lonxml::debug("Using a name of :$name:");
  898: 	    if (defined($Apache::response::foilnames{$name})) {
  899: 		&Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
  900: 	    }
  901: 	    $Apache::response::foilnames{$name}++;
  902: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  903: 						     $safeeval);
  904: 	    if ( $Apache::matchresponse::conceptgroup
  905: 		 && !&Apache::response::showallfoils() ) {
  906: 
  907: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  908: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  909: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  910: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  911: 	    } else {
  912: 
  913: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  914: 		$Apache::response::foilgroup{"$name.value"} = $value;
  915: 		$Apache::response::foilgroup{"$name.text"} = $text;
  916: 		$Apache::response::foilgroup{"$name.location"} = $location;
  917: 	    }
  918: 	}
  919:     }
  920:     if ($target eq 'edit') {
  921: 	$result.= &Apache::edit::tag_end($target,$token,'');
  922:     }
  923:     return $result;
  924: }
  925: 
  926: sub insert_foil {
  927:     return '
  928: <foil name="" value="unused">
  929: <startouttext />
  930: <endouttext />
  931: </foil>';
  932: }
  933: 
  934: 1;
  935: __END__

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