File:  [LON-CAPA] / loncom / homework / response.pm
Revision 1.87: download - view: text, annotated - select for diffs
Mon Oct 27 19:27:09 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixing up analyze mode for radiobutton/option/rank/match/image to all use the smae code

    1: # The LearningOnline Network with CAPA
    2: # various response type definitons response definition
    3: #
    4: # $Id: response.pm,v 1.87 2003/10/27 19:27:09 albertel 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::response;
   30: use strict;
   31: 
   32: BEGIN {
   33:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
   34: }
   35: 
   36: sub start_response {
   37:     my ($parstack,$safeeval)=@_;
   38:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
   39:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
   40:     if ($#Apache::inputtags::import > -1) {
   41: 	&Apache::lonxml::debug("Turning :$id: into");
   42: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
   43: 	&Apache::lonxml::debug("New  :$id:");
   44:     }
   45:     push (@Apache::inputtags::response,$id);
   46:     push (@Apache::inputtags::responselist,$id);
   47:     @Apache::inputtags::inputlist=();
   48:     return $id;
   49: }
   50: 
   51: sub end_response {
   52:     #pop @Apache::inputtags::response;
   53:     @Apache::inputtags::inputlist=();
   54:     return '';
   55: }
   56: 
   57: sub start_hintresponse {
   58:     my ($parstack,$safeeval)=@_;
   59:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
   60:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
   61:     push (@Apache::inputtags::response,$id);
   62:     push (@Apache::inputtags::responselist,$id);
   63:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
   64:     return $id;
   65: }
   66: 
   67: sub end_hintresponse {
   68:     pop @Apache::inputtags::response;
   69:     if (defined($Apache::inputtags::paramstack[-1])) {
   70: 	%Apache::inputtags::params=
   71: 	    @{ pop(@Apache::inputtags::paramstack) };
   72:     }
   73:     return '';
   74: }
   75: 
   76: # used by response to set the non-safe space random number generator to something
   77: # that is stable and unique based on the part number and response number
   78: sub setrandomnumber {
   79:     my $rndseed;
   80:     if ($ENV{'request.state'} eq "construct") {
   81: 	$rndseed=$ENV{'form.rndseed'};
   82: 	if (!$rndseed) { $rndseed=time; }
   83:     } else {
   84: 	$rndseed=&Apache::lonnet::rndseed();
   85:     }
   86:     &Apache::lonxml::debug("randseed $rndseed");
   87:     #  $rndseed=unpack("%32i",$rndseed);
   88:     my $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
   89:     if (defined($Apache::inputtags::response['-1'])) {
   90:        $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
   91:     }
   92:     if ($rndseed =~/,/) {
   93: 	{
   94: 	    use integer;
   95: 	    my ($num1,$num2)=split(/,/,$rndseed);
   96: 	    $num1+=$rndmod;
   97: 	    $num2+=$rndmod;
   98: 	    $rndseed="$num1,$num2";
   99: 	}
  100:     } else {
  101: 	$rndseed+=$rndmod;
  102:     }
  103:     &Apache::lonnet::setup_random_from_rndseed($rndseed);
  104:     &Apache::lonxml::debug("randseed $rndseed");
  105:     return '';
  106: }
  107: 
  108: sub meta_parameter_write {
  109:     my ($name,$type,$default,$display)=@_;
  110:     my $partref=$Apache::inputtags::part;
  111:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
  112:     if (defined($Apache::inputtags::response[-1])) {
  113: 	$result.=            ' id="'.$Apache::inputtags::response[-1].'"';
  114: 	$partref.='_'.$Apache::inputtags::response[-1];
  115:     }
  116:     $result.=            ' name="'.$name.'"'.
  117:                          ' type="'.$type.'"'.
  118: ($default?' default="'.$default.'"':'').
  119: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
  120:              .'></parameter>'
  121:              ."\n";
  122:     return $result;
  123: }
  124: 
  125: sub meta_package_write {
  126:     my $name=shift;
  127:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
  128:     if(defined($Apache::inputtags::response[-1])) {
  129: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
  130:     }
  131:     $result.=' package="'.$name.'"></parameter>'."\n";
  132:     return $result;
  133: }
  134: 
  135: sub meta_stores_write {
  136:     my ($name,$type,$display)=@_;
  137:     my $partref=$Apache::inputtags::part;
  138:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
  139:     if (defined($Apache::inputtags::response[-1])) {
  140: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
  141: 	$partref.='_'.$Apache::inputtags::response[-1];
  142:     }	
  143:     $result.=          ' name="'.$name.'"'.
  144:                        ' type="'.$type.'"'.
  145: 	            ' display="'.$display.' [Part: '.$partref.']"'.
  146: 		      "></stores>\n";
  147: }
  148: 
  149: sub mandatory_part_meta {
  150: #
  151: # Autogenerate metadata for mandatory
  152: # input (from RAT or lonparmset) and 
  153: # output (to lonspreadsheet)
  154: # of each part
  155: #
  156:     return
  157: #    &meta_parameter_write('opendate','date_start','',
  158: #                          'Opening Date').
  159: #    &meta_parameter_write('duedate','date_end','',
  160: #                          'Due Date').
  161: #    &meta_parameter_write('answerdate','date_start','',
  162: #                          'Show Answer Date').
  163: #    &meta_parameter_write('weight','int_zeropos','',
  164: #                          'Available Points').
  165: #    &meta_parameter_write('maxtries','int_pos','',
  166: #                          'Maximum Number of Tries').
  167: 	&meta_package_write('part').
  168:         &meta_stores_write('solved','string',
  169: 			   'Problem Status').
  170:         &meta_stores_write('tries','int_zeropos',
  171: 			   'Number of Attempts').
  172:         &meta_stores_write('awarded','float',
  173: 			   'Partial Credit Factor');
  174: #
  175: # Note: responseid-specific data 'submission' and 'awarddetail'
  176: # not available to spreadsheet -> skip here
  177: #
  178: }
  179: 
  180: sub meta_part_order {
  181:     if (@Apache::inputtags::partlist) {
  182: 	my @parts=@Apache::inputtags::partlist;
  183: 	shift(@parts);
  184: 	return '<partorder>'.join(',',@parts).'</partorder>';
  185:     } else {
  186: 	return '<partorder>0</partorder>';
  187:     }
  188: }
  189: 
  190: sub check_for_previous {
  191:     my ($curresponse,$partid,$id) = @_;
  192:     my %previous;
  193:     $previous{'used'} = 0;
  194:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
  195: 	if ($key =~ /resource\.$partid\.$id\.submission/) {
  196: 	    &Apache::lonxml::debug("Trying $key");
  197: 	    my $pastresponse=$Apache::lonhomework::history{$key};
  198: 	    if ($pastresponse eq $curresponse) {
  199: 		$previous{'used'} = 1;
  200: 		my $history;
  201: 		if ( $key =~ /^(\d+):/ ) {
  202: 		    $history=$1;
  203: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
  204: 		    $previous{'last'}='0';
  205: 		    push(@{ $previous{'version'} },$history);
  206: 		} else {
  207: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
  208: 		    $previous{'last'}='1';
  209: 		}
  210: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
  211: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
  212: 	    }
  213: 	}
  214:     }
  215:     &Apache::lonhomework::showhash(%previous);
  216:     return %previous;
  217: }
  218: 
  219: sub handle_previous {
  220:     my ($previous,$ad)=@_;
  221:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
  222: 	if ($$previous{'last'}) {
  223: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
  224: 	} else {
  225: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
  226: 	}
  227: 	push(@Apache::inputtags::previous_version,$$previous{'version'});
  228:     }
  229: }
  230: 
  231: sub view_or_modify {
  232:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  233:     my $myself=0;
  234:     if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
  235: 	$myself=1;
  236:     }
  237:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
  238:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
  239:     if ($mgr) { return "M"; }
  240:     if ($vgr) { return "V"; }
  241:     if ($myself) { return "V"; }
  242:     return '';
  243: }
  244: 
  245: sub start_dataresponse {
  246:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  247:     my $id = &Apache::response::start_response($parstack,$safeeval);
  248:     my $result;
  249:     if ($target eq 'web') {
  250: 	$result = $token->[2]->{'display'}.':';
  251:     } elsif ($target eq 'meta') {
  252: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
  253: 						       $token->[2]->{'type'},
  254: 						       $token->[2]->{'display'});
  255: 	$result .= &Apache::response::meta_package_write('dataresponse');
  256:     }
  257:     return $result;
  258: }
  259: 
  260: sub end_dataresponse {
  261:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  262:     my $result;
  263:     if ( $target eq 'web' ) {
  264:     } elsif ($target eq 'grade' ) {
  265: 	if ( defined $ENV{'form.submitted'}) {
  266: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
  267: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
  268: 	    if ($allowed) {
  269: 		&Apache::response::setup_params('dataresponse');
  270: 		my $partid = $Apache::inputtags::part;
  271: 		my $id = $Apache::inputtags::response['-1'];
  272: 		my $response = $ENV{'form.HWVAL_'.$id};
  273: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  274: 		if ( $response =~ /[^\s]/) {
  275: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
  276: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  277: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
  278: 		}
  279: 	    } else {
  280: 		$result='Not Permitted to change values.'
  281: 	    }
  282: 	}
  283:     }
  284:     &Apache::response::end_response;
  285:     return $result;
  286: }
  287: 
  288: sub decide_package {
  289:     my ($tagstack)=@_;
  290:     my $package;
  291:     if ($$tagstack[-1] eq 'parameter') {
  292: 	$package='part';
  293:     } else {
  294: 	my $i=-1;
  295: 	while (defined($$tagstack[$i])) {
  296: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
  297: 		$package=$$tagstack[$i];
  298: 		last;
  299: 	    }
  300: 	    $i--;
  301: 	}
  302:     }
  303:     return $package;
  304: }
  305: 
  306: sub start_responseparam {
  307:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  308:     my $result='';
  309:     if ($target eq 'meta') {
  310: 	$result = &meta_parameter_write($token->[2]->{'name'},
  311: 					$token->[2]->{'type'},
  312: 					$token->[2]->{'default'},
  313: 					$token->[2]->{'description'});
  314:     } elsif ($target eq 'edit') {
  315: 	$result.=&Apache::edit::tag_start($target,$token);
  316: 	my $optionlist;
  317: 	my $package=&decide_package($tagstack);
  318: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
  319: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
  320: 		$optionlist.='<option value="'.$1.'">'.
  321: 		    $Apache::lonnet::packagetab{$key}.'</option>';
  322: 	    }
  323: 	}
  324: 	if (defined($optionlist)) {
  325: 	    $result.='Use template: <select name="'.
  326: 		&Apache::edit::html_element_name('parameter_package').'">'.
  327: 		    '<option value=""></option>'.$optionlist.'</select><br />';
  328: 	}
  329: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
  330: 	    &Apache::edit::text_arg('Type:','type',$token).
  331: 		&Apache::edit::text_arg('Description:','description',$token).
  332: 		    &Apache::edit::text_arg('Default:','default',$token).
  333: 			"</td></tr>";
  334: 	$result.=&Apache::edit::end_table;
  335:     } elsif ($target eq 'modified') {
  336: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  337: 						     $safeeval,'name','type',
  338: 						     'description','default');
  339: 	my $element=&Apache::edit::html_element_name('parameter_package');
  340: 	if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
  341: 	    my $name=$ENV{"form.$element"};
  342: 	    my $tag=&decide_package($tagstack);
  343: 	    $token->[2]->{'name'}=$name;
  344: 	    $token->[2]->{'type'}=
  345: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
  346: 	    $token->[2]->{'description'}=
  347: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
  348: 	    $token->[2]->{'default'}=
  349: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
  350: 	    $constructtag=1;
  351: 	}
  352: 	if ($constructtag) {
  353: 	    $result = &Apache::edit::rebuild_tag($token);
  354: 	    $result.=&Apache::edit::handle_insert();
  355: 	}
  356:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
  357: 	     $target eq 'tex' || $target eq 'analyze' ) {
  358: 	if ($ENV{'request.state'} eq 'construct') {
  359: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
  360: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
  361: 						     $safeeval);
  362: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
  363: 	}
  364:     }
  365:     return $result;
  366: }
  367: 
  368: sub end_responseparam {
  369:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  370:     if ($target eq 'edit') { return ('','no'); }
  371:     return '';
  372: }
  373: 
  374: sub start_parameter {
  375:     my $result = &start_responseparam(@_);
  376:     return $result;
  377: }
  378: 
  379: sub end_parameter {
  380:     my $result = &end_responseparam(@_);
  381:     return $result;
  382: }
  383: 
  384: sub reset_params {
  385:     %Apache::inputtags::params=();
  386: }
  387: 
  388: sub setup_params {
  389:     my ($tag) = @_;
  390: 
  391:     if ($ENV{'request.state'} eq 'construct') { return; }
  392:     my %paramlist=();
  393:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
  394: 	if ($key =~ /^$tag/) {
  395: 	    my ($package,$name) = split(/&/,$key);
  396: 	    $paramlist{$name}=1;
  397: 	}
  398:     }
  399:     foreach my $key (keys(%paramlist)) {
  400: 	my $entry= 'resource.'.$Apache::inputtags::part;
  401: 	if (defined($Apache::inputtags::response[-1])) {
  402: 	    $entry.='_'.$Apache::inputtags::response[-1];
  403: 	}
  404: 	$entry.='.'.$key;
  405: 	&Apache::lonxml::debug("looking for $entry");
  406: 	my $value = &Apache::lonnet::EXT("$entry");
  407: 	&Apache::lonxml::debug("$key has value :$value:");
  408: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
  409: 	    &Apache::lonxml::debug("using nothing");
  410: 	    $Apache::inputtags::params{$key}='';
  411: 	} else {
  412: 	    &Apache::lonxml::debug("using value");
  413: 	    $Apache::inputtags::params{$key}=$value;
  414: 	}
  415:     }
  416: }
  417: 
  418: sub answer_header {
  419:     my ($type) = @_;
  420:     my $result;
  421:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
  422: 	$result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
  423:                   $Apache::inputtags::part.'| \\\\ \hline ';
  424:     } else {
  425: 	$result = '<table border="1"><tr><td>Answer for Part:'.
  426: 	    $Apache::inputtags::part. '</td>'."\n";
  427:     }
  428:     return $result;
  429: }
  430: 
  431: sub answer_part {
  432:     my ($type,$answer) = @_;
  433:     my $result;
  434:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
  435: 	$result = ' \verb|'.$answer.'|\\\\ \hline ';
  436:     } else {
  437: 	$result = '<td>'.$answer.'</td>';
  438:     }
  439:     return $result;
  440: }
  441: 
  442: sub answer_footer {
  443:     my ($type) = @_;
  444:     my $result;
  445:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
  446: 	$result = ' \end{tabular} \vskip 0 mm ';
  447:     } else {
  448: 	$result = '</tr></table>';
  449:     }
  450:     return $result;
  451: }
  452: 
  453: sub showallfoils {
  454:     my $return=0;
  455:     if (defined($ENV{'form.showallfoils'}) &&
  456: 	$ENV{'request.state'} eq 'construct') {
  457: 	$return=1;
  458:     }
  459:     return $return;
  460: }
  461: 
  462: sub getresponse {
  463:     my ($temp)=@_;
  464:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
  465:     my $response;
  466:     if (!defined($temp)) {
  467: 	$temp=1;
  468:     } else {
  469: 	$formparm.=":$temp";
  470:     }
  471:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
  472: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
  473: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
  474: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
  475:     if ($ENV{'form.submitted'} eq 'scantron') {
  476: 	my $part  = $Apache::inputtags::part;
  477: 	my $id    = $Apache::inputtags::response[-1];
  478: 	$response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
  479: 			 '.answer'};
  480: 	# save bubbled letter for later
  481: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
  482: 	    $response;
  483: 	$response = $let_to_num{$response};
  484:     } else {
  485: 	$response = $ENV{$formparm};
  486:     }
  487:     return $response;
  488: }
  489: 
  490: sub repetition {
  491:     my $id = $Apache::inputtags::part;
  492:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  493:     my $repetition = int $weight/9;
  494:     if ($weight % 9 != 0) {$repetition++;} 
  495:     return $repetition;
  496: }
  497: 
  498: sub scored_response {
  499:     my ($part,$id)=@_;
  500:     my $repetition=&repetition();
  501:     my $score=0;
  502:     for (my $i=0;$i<$repetition;$i++) {
  503: 	my $increase=&Apache::response::getresponse($i+1);
  504: 	if ($increase ne '') { $score+=$increase+1; }
  505:     }
  506:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
  507:     my $pcr=$score/$weight;
  508:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
  509:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  510: 	'ASSIGNED_SCORE';
  511:     return $repetition;
  512: }
  513: 
  514: sub whichorder {
  515:     my ($max,$randomize,$showall,$hash)=@_;
  516:     #&Apache::lonxml::debug("man $max randomize $randomize");
  517:     if (!defined(@{ $$hash{'names'} })) { return; }
  518:     my @names = @{ $$hash{'names'} };
  519:     my @whichopt =();
  520:     my (%top,@toplist,%bottom,@bottomlist);
  521:     if (!($showall || ($randomize eq 'no'))) {
  522: 	my $current=0;
  523: 	foreach my $name (@names) {
  524: 	    $current++;
  525: 	    if ($$hash{"$name.location"} eq 'top') {
  526: 		$top{$name}=$current;
  527: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
  528: 		$bottom{$name}=$current;
  529: 	    }
  530: 	}
  531:     }
  532:     my $topcount=0;
  533:     my $bottomcount=0;
  534:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
  535: 	   && ($#names > -1)) {
  536: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
  537: 	my $aopt;
  538: 	if ($showall || ($randomize eq 'no')) {
  539: 	    $aopt=0;
  540: 	} else {
  541: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  542: 	}
  543: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  544: 	$aopt=splice(@names,$aopt,1);
  545: 	#&Apache::lonxml::debug("Picked $aopt");
  546: 	if ($top{$aopt}) {
  547: 	    $toplist[$top{$aopt}]=$aopt;
  548: 	    $topcount++;
  549: 	} elsif ($bottom{$aopt}) {
  550: 	    $bottomlist[$bottom{$aopt}]=$aopt;
  551: 	    $bottomcount++;
  552: 	} else {
  553: 	    push (@whichopt,$aopt);
  554: 	}
  555:     }
  556:     for (my $i=0;$i<=$#toplist;$i++) {
  557: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
  558:     }
  559:     for (my $i=0;$i<=$#bottomlist;$i++) {
  560: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
  561:     }
  562: 
  563:     return @whichopt;
  564: }
  565: 
  566: sub show_answer {
  567:     my $part   = $Apache::inputtags::part;
  568:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
  569:     my $status = $Apache::inputtags::status[-1];
  570:     return  ( ($award =~ /^correct/
  571: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
  572: 	      || $status eq "SHOW_ANSWER");
  573: }
  574: 
  575: sub analyze_store_foilgroup {
  576:     my ($shown,$attrs)=@_;
  577:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  578:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
  579: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
  580: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
  581: 	foreach my $attr (@$attrs) {
  582: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
  583: 		$Apache::response::foilgroup{"$name.".$attr};
  584: 	}
  585:     }
  586:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
  587: }
  588: 
  589: sub pick_foil_for_concept {
  590:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
  591:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
  592:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  593:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
  594:     my $name=$names[$pick];
  595:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  596:     foreach my $attr (@$attrs) {
  597: 	$Apache::response::foilgroup{"$name.".$attr} =
  598: 	    $Apache::response::conceptgroup{"$name.".$attr};
  599:     }
  600:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  601:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  602:     &Apache::lonxml::debug("Selecting $name in $concept");
  603:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  604:     if ($target eq 'analyze') {
  605: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
  606: 	      $concept);
  607: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
  608: 	    $Apache::response::conceptgroup{'names'};
  609: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
  610: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
  611: 		  $name);
  612: 	    foreach my $attr (@$attrs) {
  613: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
  614: 		    $Apache::response::conceptgroup{"$name.$attr"};
  615: 	    }
  616: 	}
  617:     }
  618:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
  619:     $hinthash->{"$part_id.concept.$concept"}=
  620: 	$Apache::response::conceptgroup{'names'};
  621: 
  622: }
  623: 
  624: 
  625: 1;
  626: __END__
  627:  

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