File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.82: download - view: text, annotated - select for diffs
Tue Sep 13 21:42:58 2011 UTC (12 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Additional (eighteenth field) BubblesPerRow in Bubblesheet Format file.
  - specify bubbles available for each question row (default is 10).
- Support case where question (or subquestion for optionresponse,
    matchresponse, or rankresponse) requires more bubbles than in one line.
  - Students prompted to bubble once in multiple lines.
  - Numbering recognizes multiple lines per question.
  - Also supports: imageresponse, stringresponse, formularesponse where
    students leave link(s) blank if weight assigned is greater than
    bubbles per line -- multiple lines also supported in this case.

For example:
- 15-bubble MSU Art History exams now supported on standard 10-bubble
    bubblesheets used at MSU.

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

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