File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.81: download - view: text, annotated - select for diffs
Sun Dec 19 00:54:20 2010 UTC (13 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_0_RC2, version_2_10_0, HEAD
- New Question Type - randomizetry
  - Only set dropdown lists to selections last try when displaying options
    for new try, if rndseed for last try is the same as for current try.

    1: # The LearningOnline Network with CAPA
    2: # Full matching style response
    3: #
    4: # $Id: matchresponse.pm,v 1.81 2010/12/19 00:54:20 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: 	foreach my $name (@names) {
  177: 	    # $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
  178: 	    $table.='\item['.$alphabet[$i].'] '.
  179: 		$Apache::response::itemgroup{$name.'.text'};
  180: 	    $i++;
  181: 	}
  182: 	$table.=' \end{description}  \strut ';
  183: 	if ($Apache::lonhomework::type eq 'exam')  {$table.='\vskip -13 mm \strut ';}
  184: 	$Apache::matchresponse::itemtable{'display'}=$table;
  185: 	$Apache::lonxml::post_evaluate=0;
  186:     }
  187:     return $result;
  188: }
  189: 
  190: sub start_item {
  191:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  192:     my $result='';
  193:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  194: 	&Apache::lonxml::startredirection;
  195:     } elsif ($target eq 'edit') {
  196: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  197: 						 $safeeval,'-2');
  198: 	$result=&Apache::edit::tag_start($target,$token,"Item");
  199: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  200: 	if ($randomize ne 'no') {
  201: 	    $result.='&nbsp;'x 3 .
  202:                      &Apache::edit::select_arg('Location:','location',
  203: 					       ['random','top','bottom'],
  204: 					       $token);
  205: 	}
  206: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  207:     } elsif ($target eq 'modified') {
  208: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  209: 						     $safeeval,'name',
  210: 						     'location');
  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 = '';
  220:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  221: 	$text=&Apache::lonxml::endredirection;
  222:     }
  223:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  224: 	$target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
  225: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  226: 	my $location=&Apache::lonxml::get_param('location',$parstack,
  227: 						$safeeval);
  228: 	&Apache::lonxml::debug("Got a name of :$name:");
  229: 	if ($name eq "") { $name=$Apache::lonxml::curdepth; }
  230: 	&Apache::lonxml::debug("Using a name of :$name:");
  231: 	push @{ $Apache::response::itemgroup{'names'} }, $name;
  232: 	$Apache::response::itemgroup{"$name.text"} = $text;
  233: 	$Apache::response::itemgroup{"$name.location"} = $location;
  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;
  255:     &Apache::response::pushrandomnumber(undef,$target);
  256:     if ($target eq 'edit') {
  257: 	$result.=&Apache::edit::start_table($token)
  258: 	    .'<tr><td>'.&mt('Collection Of Foils').'</td>'
  259:             .'<td><span class="LC_nobreak">'.&mt('Delete?')
  260: 	    .&Apache::edit::deletelist($target,$token)
  261: 	    .'</span></td>'
  262:             .'<td>&nbsp;'.&Apache::edit::end_row()
  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;
  271:     my $part        = $Apache::inputtags::part;
  272:     my $response_id = $Apache::inputtags::response[-1];
  273: 
  274:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
  275: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  276: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  277: 						   $safeeval,'-2');
  278: 	if ($target eq 'web' || $target eq 'tex') {
  279: 	    $result=&displayfoils($target,$max,$randomize);
  280: 	    $Apache::lonxml::post_evaluate=0;
  281: 	} elsif ($target eq 'answer' ) {
  282: 	    $result=&displayanswers($max,$randomize);
  283: 	} elsif ( $target eq 'grade') {
  284: 	    &grade_response($max,$randomize);
  285: 	} elsif ( $target eq 'analyze') {
  286: 	    my @shown=&whichfoils($max,$randomize);
  287: 	    &Apache::response::analyze_store_foilgroup(\@shown,
  288: 						 ['text','value','location']);
  289: 	    #FIXME need to store options in some way
  290: 	}
  291: 	&Apache::lonxml::increment_counter(&getfoilcounts($max), 
  292: 					   "$part.$response_id");
  293: 	if ($target eq 'analyze') {
  294: 	    &Apache::lonhomework::set_bubble_lines();
  295: 	}
  296: 
  297:     } elsif ($target eq 'edit') {
  298: 	$result=&Apache::edit::end_table();
  299:     }
  300:     &Apache::response::poprandomnumber();
  301:     return $result;
  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);
  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'} };
  316:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
  317: 						   $randomize,
  318: 					  &Apache::response::showallfoils(),
  319: 					 \%Apache::response::foilgroup);
  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:     }
  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');
  343:     }
  344:     return $result;
  345: }
  346: 
  347: 
  348: sub grade_response {
  349:     my ($max,$randomize)=@_;
  350:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  351: 						   $randomize,
  352: 				            &Apache::response::showallfoils(),
  353: 				           \%Apache::response::foilgroup);
  354:     if (!&Apache::response::submitted()) { return; }
  355:     my %responsehash;
  356:     my %grade;
  357:     my %answerhash;
  358:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  359:     my %letter_name_map;
  360:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
  361: 	%letter_name_map=
  362: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
  363:     }
  364:     my @items;
  365:     foreach my $name (@whichfoils) {
  366: 	my $response = &Apache::response::getresponse($temp,'letter');
  367: 	push(@items,$response);
  368: 	my $responsename = $letter_name_map{$response};
  369: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  370:         if ($Apache::lonhomework::type eq 'randomizetry') {
  371:             $answerhash{$name} = $value;
  372:         }
  373: 	if ( $response =~ /[^\s]/) {
  374: 	    $responsehash{$name}=$responsename;
  375: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
  376: 	    if ($value eq $responsename) {
  377: 		$grade{$name}='1'; $right++;
  378: 	    } else {
  379: 		$grade{$name}='0'; $wrong++;
  380: 	    }
  381: 	} else {
  382: 	    $ignored++;
  383: 	}
  384: 	$temp++;
  385:     }
  386:     my $part=$Apache::inputtags::part;
  387:     my $id = $Apache::inputtags::response['-1'];
  388:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  389:     my $itemstr    =&Apache::lonnet::array2str(@items);
  390:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  391:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  392:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
  393:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  394: 	$responsestr;
  395:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
  396: 	$itemstr;
  397:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
  398: 	$gradestr;
  399:     if ($Apache::lonhomework::type eq 'randomizetry') {
  400:         $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
  401:     }
  402:     if (($Apache::lonhomework::type eq 'survey') ||
  403:         ($Apache::lonhomework::type eq 'surveycred') ||
  404:         ($Apache::lonhomework::type eq 'anonsurvey') ||
  405:         ($Apache::lonhomework::type eq 'anonsurveycred')) {
  406: 	if ($ignored == 0) {
  407: 	    my $ad;
  408:             if ($Apache::lonhomework::type eq 'anonsurvey') {
  409:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  410:             } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
  411:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  412:             } elsif ($Apache::lonhomework::type eq 'surveycred') {
  413:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  414:             } else {
  415:                 $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  416:             }
  417: 	    &Apache::response::handle_previous(\%previous,$ad);
  418: 	} elsif ($wrong==0 && $right==0) {
  419: 	} else {
  420: 	    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  421: 	    &Apache::response::handle_previous(\%previous,$ad);
  422: 	}
  423:     } elsif (!$Apache::lonhomework::scantronmode) {
  424: 	my $ad;
  425: 	if ($wrong==0 && $ignored==0) {
  426: 	    $ad='EXACT_ANS';
  427: 	} elsif ($wrong==0 && $right==0) {
  428: 	    #nothing submitted
  429: 	} else {
  430: 	    if ($ignored==0) {
  431: 		$ad='INCORRECT';
  432: 	    } else {
  433: 		$ad='MISSING_ANSWER';
  434: 	    }
  435: 	}
  436: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  437: 	&Apache::response::handle_previous(\%previous,$ad);
  438:     } else {
  439: 	my $ad;
  440: 	if ($wrong==0 && $right==0) {
  441: 	    #nothing submitted
  442: 	} else {
  443: 	    $ad='ASSIGNED_SCORE';
  444: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  445: 		$ad;
  446: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  447: 		$right/(scalar(@whichfoils));
  448: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  449: 		scalar(@whichfoils);
  450: 	}
  451:     }
  452: }
  453: 
  454: sub format_prior_answer {
  455:     my ($mode,$answer,$other_data) = @_;
  456:     my %answer      =&Apache::lonnet::str2hash($answer);
  457:     my $foil_order  =$other_data->[0];
  458:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
  459:     my @items       =&Apache::lonnet::str2array($other_data->[2]);
  460:     my $output;
  461:     
  462:     foreach my $name (@{ $foil_order }) {
  463: 	my $item=shift(@items);
  464: 	$output .= '<tr><td>'.$item.'</td></tr>';
  465:     }
  466:     return if (!defined($output));
  467: 
  468:     $output = '<table class="LC_prior_match">'.$output.'</table>';
  469:     return $output;
  470: }
  471: 
  472: sub itemdisplay {
  473:     my ($location)=@_;
  474:     if ($location eq 'top' &&
  475: 	!defined($Apache::matchresponse::itemtable{'location'})) {
  476: 	return $Apache::matchresponse::itemtable{'display'};
  477:     }
  478:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
  479: 	return $Apache::matchresponse::itemtable{'display'};
  480:     }
  481:     return undef;
  482: }
  483: 
  484: sub displayfoils {
  485:     my ($target,$max,$randomize)=@_;
  486:     my ($tabsize, $lefttabsize, $righttabsize);
  487: 
  488:     my $result;
  489:     my $question;
  490:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
  491: 						   $randomize,
  492: 				            &Apache::response::showallfoils(),
  493: 				           \%Apache::response::foilgroup);
  494:     my $part=$Apache::inputtags::part;
  495:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  496:     my %letter_name_map;
  497:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
  498: 	%letter_name_map=
  499: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
  500:     }
  501:     my %name_letter_map;
  502:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
  503: 	%name_letter_map=
  504: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
  505:     }
  506:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  507: 	foreach my $name (@whichfoils) {
  508: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  509: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  510: 	    my $letter=$name_letter_map{$value};
  511: 	    if ($target eq 'tex') {
  512: 		$question.=' \\\\ '.$letter.':'.$text;
  513: 	    } else {
  514: 		$question.='<br /><b>'.$letter.':</b> '.$text;
  515: 	    }
  516: 	}
  517:     } else {
  518: 	my $i = 0;
  519: 	my $temp=1;
  520: 	my $id=$Apache::inputtags::response[-1];
  521: 	my $part=$Apache::inputtags::part;
  522:         my ($lastresponse,$newvariation);
  523:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  524:             ($Apache::lonhomework::type eq 'randomizetry')) &&
  525:             ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  526:             if ($env{'form.'.$part.'.rndseed'} ne
  527:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  528:                 $newvariation = 1;
  529:             }
  530:         }
  531:         unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred') && (defined($env{'form.grade_symb'}))) || $newvariation) {
  532: 	    $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  533:         }
  534: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
  535: 	my @alphabet=('A'..'Z');
  536: 	my @used_letters=sort(keys(%letter_name_map));
  537: 	my $internal_counter=$Apache::lonxml::counter;
  538: 	foreach my $name (@whichfoils) {
  539: 	    my $lastopt=$lastresponse{$name};
  540: 	    my $last_letter=$name_letter_map{$lastopt};
  541: 	    my $optionlist = '';
  542: 	    if ($target ne 'tex') {
  543: 		$optionlist="<option></option>\n";
  544: 	    } else {
  545: 		if ($Apache::lonhomework::type ne 'exam') {
  546:                     if($env{'form.pdfFormFields'} eq 'yes'
  547:                        && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
  548:                         my $fieldname = $env{'request.symb'} . '&part_'. $Apache::inputtags::part .'&matchresponse'. '&HWVAL_' . $Apache::inputtags::response['-1'] . ':' . $temp . '&submit_' . $Apache::inputtags::part . '&';
  549:                         $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
  550:                     } else {
  551: 
  552:                         $optionlist='\framebox[10 mm][s]{\tiny\strut}';
  553:                     }
  554:                 }
  555: 	    }
  556: 	    my $option;
  557: 	    foreach $option (@used_letters) {
  558: 		if ($option eq $last_letter) {
  559: 		    if ($target ne 'tex') {
  560:                         $optionlist.="<option selected=\"selected\">$option</option>\n";
  561:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  562:                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
  563:                              && $Apache::lonhomework::type ne 'exam') {
  564:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  565:                     }
  566: 		} else {
  567: 		    if ($target ne 'tex') {
  568:                         $optionlist.="<option>$option</option>\n";
  569:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  570:                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
  571:                              && $Apache::lonhomework::type ne 'exam') {
  572:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  573:                     }
  574: 		}
  575: 	    }
  576: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  577: 	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  578: 		  $part.'\');" name="HWVAL_'.
  579: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  580: 		        $optionlist."</select>\n";
  581: 	    }
  582: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  583: 	    #
  584:             #  Factor out the tex computations of the left/right 1/2 minipage
  585: 	    #  widths for left or right positioning.  This allows us
  586: 	    #  to, if necessary constrain the bubble widths:
  587: 	    #
  588: 	    if ($target eq 'tex'   &&
  589: 		(&itemdisplay('left') || &itemdisplay('right'))) {
  590: 		$tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
  591: 		($lefttabsize,$righttabsize)=(0,0);
  592: 		if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
  593: 		    $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
  594: 		    $lefttabsize=$tabsize*$1/100;	
  595: 		    $righttabsize=0.95*($tabsize-$lefttabsize);
  596: 		} else {
  597: 		    $tabsize=~/(\d+\.?\d*)/;
  598: 		    $lefttabsize=$1/2.1;
  599: 		    $righttabsize=0.95*($1-$lefttabsize);
  600: 		}
  601: 	    }
  602: 	    if ($target ne 'tex') {
  603: 		if ($Apache::lonhomework::type ne 'exam') {
  604: 		    $question.="<br />\n".$optionlist.$text;
  605: 		} else {
  606: 		    $question.="<br />\n".$text;
  607: 		}
  608: 		if ($Apache::lonhomework::type eq 'exam') {
  609: 		    my @blank;
  610: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
  611: 		}
  612: 	    } else {		
  613: 		# Note that if left or right positioned, we must
  614: 		# confine the bubbles to righttabsize:
  615: 		#
  616: 		if ($Apache::lonhomework::type eq 'exam') {
  617: 		    $question.=' '.$optionlist.$text."\n";
  618: 		    my @emptyItems = ();
  619: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
  620: 		    $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
  621: 			.'\item \hskip -3mm \textbf{'.$internal_counter.'}';
  622: 		    if (&itemdisplay('left') || &itemdisplay('right')) {
  623: 			$question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems, "", $righttabsize);
  624: 		    }
  625: 		    else {
  626: 			$question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems);
  627: 		    }
  628: 		    $question .= '\end{list} \vskip -8 mm \strut ';
  629: 		    $internal_counter++;
  630: 	        } else {
  631:                     if($env{'form.pdfFormFields'} eq 'yes' 
  632:                             && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
  633:                         $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
  634:                     } else {
  635:                         $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
  636:                     }
  637: 		}
  638:             } 
  639: 	    $temp++;
  640: 	}
  641:     }
  642:     if ($result=&itemdisplay('top')) {
  643: 	$result.=$question;
  644:     } elsif ($result=&itemdisplay('bottom')) {
  645: 	$result=$question.$result;
  646:     } elsif ($result=&itemdisplay('right')) {
  647: 	if ($target ne 'tex') {
  648: 	    #remove the first <br />
  649: 	    $question=~s|<br />||;
  650: 	    $result='<table><tr><td valign="top">'.$question.
  651: 		'</td><td valign="top">'.$result.'</td></tr></table>';
  652: 	} else {
  653: 	    $lefttabsize.=' mm ';
  654: 	    $righttabsize.=' mm ';
  655: 	    $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}';
  656: 	}
  657:     } elsif ($result=&itemdisplay('left')) {
  658: 	if ($target ne 'tex') {
  659: 	    #remove the first <br />
  660: 	    $question=~s|<br />||;
  661: 	    $result='<table><tr><td valign="top">'.$result.
  662: 		'</td><td valign="top">'.$question.'</td></tr></table>';
  663: 	} else {
  664: 	    $lefttabsize.=' mm ';
  665: 	    $righttabsize.=' mm ';
  666: 	    $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}';
  667: 	}
  668:     }
  669:     if ($target eq 'web') {
  670:         my $questiontype;
  671:         if ($Apache::lonhomework::type eq 'randomizetry') {
  672:             $questiontype = $Apache::lonhomework::type;
  673:         }
  674: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  675: 						  [\@whichfoils,
  676: 						   'submissiongrading',
  677: 						   'submissionitems'],
  678:                                                   $questiontype);
  679:     }
  680:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
  681:     return $result;
  682: }
  683: 
  684: sub getfoilcounts {
  685:     my ($max)=@_;
  686:     # +1 since instructors will count from 1
  687:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  688:     if (&Apache::response::showallfoils()) { $max=$count; }
  689:     if ($count>$max) { $count=$max } 
  690:     &Apache::lonxml::debug("Count is $count from $max");
  691:     return $count;
  692: }
  693: 
  694: 
  695: sub start_conceptgroup {
  696:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  697:     $Apache::matchresponse::conceptgroup=1;
  698:     %Apache::response::conceptgroup=();
  699:     my $result;
  700:     if ($target eq 'edit') {
  701: 	$result.=&Apache::edit::tag_start($target,$token,
  702: 					  "Concept Grouped Foils");
  703: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  704: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  705:     }
  706:     if ($target eq 'modified') {
  707: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  708: 						     $safeeval,'concept');
  709: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  710:     }
  711:     return $result;
  712: }
  713: 
  714: sub end_conceptgroup {
  715:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  716:     $Apache::matchresponse::conceptgroup=0;
  717:     my $result='';
  718:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  719: 	$target eq 'tex' || $target eq 'analyze') {
  720: 	&Apache::response::pick_foil_for_concept($target,
  721: 						 ['value','text','location'],
  722: 						 \%Apache::hint::match,
  723: 						 $parstack,$safeeval);
  724:     } elsif ($target eq 'edit') {
  725: 	$result=&Apache::edit::end_table();
  726:     }
  727:     return $result;
  728: }
  729: 
  730: sub insert_conceptgroup {
  731:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  732:     return $result;
  733: }
  734: 
  735: sub start_foil {
  736:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  737:     my $result='';
  738:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  739: 	&Apache::lonxml::startredirection;
  740: 	if ($target eq 'analyze') {
  741: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  742: 	}
  743:     } elsif ($target eq 'edit') {
  744: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  745: 	my $level='-2';
  746: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  747: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  748: 	my @names;
  749: 	if (defined(@{ $Apache::response::itemgroup{'names'} })) {
  750: 	    @names=@{ $Apache::response::itemgroup{'names'} };
  751: 	}
  752: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
  753: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  754: 						 $safeeval,'-3');
  755: 	if ($randomize ne 'no') {
  756: 	    $result.=&Apache::edit::select_arg('Location:','location',
  757: 					     ['random','top','bottom'],$token);
  758: 	}
  759: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  760:     } elsif ($target eq 'modified') {
  761: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  762: 						     $safeeval,'value',
  763: 						     'name','location');
  764: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  765:     }
  766:     return $result;
  767: }
  768: 
  769: sub end_foil {
  770:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  771:     my $text ='';
  772:     my $result = '';
  773:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  774: 	$text=&Apache::lonxml::endredirection;
  775:     }
  776: 
  777:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  778: 	$target eq 'tex' || $target eq 'analyze') {
  779: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  780: 	    $text='\vskip 5mm $\triangleright$ '.$text;
  781: 	}
  782: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  783: 	if ($value ne 'unused') {
  784: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  785: 	    &Apache::lonxml::debug("Got a name of :$name:");
  786: 	    if (!$name) {
  787: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  788: 		$name=$Apache::lonxml::curdepth;
  789: 	    }
  790: 	    &Apache::lonxml::debug("Using a name of :$name:");
  791: 	    if (defined($Apache::response::foilnames{$name})) {
  792: 		&Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
  793: 	    }
  794: 	    $Apache::response::foilnames{$name}++;
  795: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  796: 						     $safeeval);
  797: 	    if ( $Apache::matchresponse::conceptgroup
  798: 		 && !&Apache::response::showallfoils() ) {
  799: 
  800: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  801: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  802: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  803: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  804: 	    } else {
  805: 
  806: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  807: 		$Apache::response::foilgroup{"$name.value"} = $value;
  808: 		$Apache::response::foilgroup{"$name.text"} = $text;
  809: 		$Apache::response::foilgroup{"$name.location"} = $location;
  810: 	    }
  811: 	}
  812:     }
  813:     if ($target eq 'edit') {
  814: 	$result.= &Apache::edit::tag_end($target,$token,'');
  815:     }
  816:     return $result;
  817: }
  818: 
  819: sub insert_foil {
  820:     return '
  821: <foil name="" value="unused">
  822: <startouttext />
  823: <endouttext />
  824: </foil>';
  825: }
  826: 1;
  827: __END__

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