Annotation of loncom/homework/response.pm, revision 1.162

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.162   ! albertel    4: # $Id: response.pm,v 1.161 2007/04/07 00:42:29 albertel Exp $
1.53      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.5       www        28: 
1.1       albertel   29: package Apache::response;
                     30: use strict;
1.93      albertel   31: use Apache::lonlocal;
1.120     albertel   32: use Apache::lonnet;
1.153     www        33: use Apache::lonmaxima();
1.1       albertel   34: 
1.57      harris41   35: BEGIN {
1.139     www        36:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse'));
1.1       albertel   37: }
                     38: 
1.13      albertel   39: sub start_response {
1.73      albertel   40:     my ($parstack,$safeeval)=@_;
1.136     albertel   41:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.73      albertel   42:     if ($#Apache::inputtags::import > -1) {
                     43: 	&Apache::lonxml::debug("Turning :$id: into");
                     44: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
                     45: 	&Apache::lonxml::debug("New  :$id:");
                     46:     }
                     47:     push (@Apache::inputtags::response,$id);
                     48:     push (@Apache::inputtags::responselist,$id);
                     49:     @Apache::inputtags::inputlist=();
1.101     albertel   50:     if ($Apache::inputtags::part eq '' && 
                     51: 	!$Apache::lonhomework::ignore_response_errors) {
1.97      albertel   52: 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
1.92      albertel   53:     }
                     54:     if ($Apache::inputtags::response_with_no_part &&
                     55: 	$Apache::inputtags::part ne '0') {
1.97      albertel   56: 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("<*response>s are both inside of <part> and outside of <part>, this is not a valid problem, errors in grading may occur."),'<>&"').'<br />');
1.92      albertel   57:     }
                     58:     if ($Apache::inputtags::part eq '0') {
                     59: 	$Apache::inputtags::response_with_no_part=1;
                     60:     }
1.73      albertel   61:     return $id;
1.13      albertel   62: }
                     63: 
                     64: sub end_response {
1.79      albertel   65:     #pop @Apache::inputtags::response;
1.73      albertel   66:     @Apache::inputtags::inputlist=();
                     67:     return '';
1.13      albertel   68: }
                     69: 
1.41      albertel   70: sub start_hintresponse {
1.73      albertel   71:     my ($parstack,$safeeval)=@_;
1.136     albertel   72:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.123     albertel   73:     push (@Apache::inputtags::hint,$id);
                     74:     push (@Apache::inputtags::hintlist,$id);
1.73      albertel   75:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
                     76:     return $id;
1.41      albertel   77: }
                     78: 
                     79: sub end_hintresponse {
1.123     albertel   80:     pop @Apache::inputtags::hint;
1.73      albertel   81:     if (defined($Apache::inputtags::paramstack[-1])) {
                     82: 	%Apache::inputtags::params=
                     83: 	    @{ pop(@Apache::inputtags::paramstack) };
                     84:     }
                     85:     return '';
1.41      albertel   86: }
                     87: 
1.99      albertel   88: my @randomseeds;
                     89: sub pushrandomnumber {
                     90:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                     91:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                     92: 	$rand_alg eq '64bit2') {
                     93: 	# do nothing
                     94:     } else {
                     95: 	my @seed=&Math::Random::random_get_seed();
1.127     albertel   96: 	push(@randomseeds,\@seed);
1.99      albertel   97:     }
1.127     albertel   98:     &Apache::response::setrandomnumber(@_);
1.99      albertel   99: }
                    100: sub poprandomnumber {
                    101:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                    102:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    103: 	$rand_alg eq '64bit2') {
                    104: 	return;
                    105:     }
                    106:     my $seed=pop(@randomseeds);
                    107:     if ($seed) {
                    108: 	&Math::Random::random_set_seed(@$seed);
                    109:     } else {
                    110: 	&Apache::lonxml::error("Unable to restore random algorithm.");
                    111:     }
                    112: }
1.117     albertel  113: 
1.26      albertel  114: sub setrandomnumber {
1.127     albertel  115:     my ($ignore_id2) = @_;
1.73      albertel  116:     my $rndseed;
1.88      albertel  117:     $rndseed=&Apache::structuretags::setup_rndseed();
                    118:     if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73      albertel  119:     &Apache::lonxml::debug("randseed $rndseed");
                    120:     #  $rndseed=unpack("%32i",$rndseed);
1.99      albertel  121:     my $rand_alg=&Apache::lonnet::get_rand_alg();
1.126     albertel  122:     my ($rndmod,$rndmod2);
1.119     albertel  123: 
                    124:     my ($id1,$id2,$shift_amt);
                    125:     if ($Apache::lonhomework::parsing_a_problem) {
                    126: 	$id1=$Apache::inputtags::part;
                    127: 	if (defined($Apache::inputtags::response[-1])) {
                    128: 	    $id2=$Apache::inputtags::response[-1];
                    129: 	}
                    130: 	$shift_amt=scalar(@Apache::inputtags::responselist);
                    131:     } elsif ($Apache::lonhomework::parsing_a_task) {
1.141     albertel  132: 	$id1=&Apache::bridgetask::get_dim_id();
                    133: 	if (!$ignore_id2 && ref($Apache::bridgetask::instance{$id1})) {
                    134: 	    $id2=$Apache::bridgetask::instance{$id1}[-1];
1.142     albertel  135: 	    $shift_amt=scalar(@{$Apache::bridgetask::instance{$id1}});
                    136: 	} else {
                    137: 	    $shift_amt=0;
1.119     albertel  138: 	}
                    139:     } 
                    140:     &Apache::lonxml::debug("id1: $id1, id2: $id2, shift_amt: $shift_amt");
1.99      albertel  141:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    142: 	$rand_alg eq '64bit2') {
1.119     albertel  143: 	$rndmod=(&Apache::lonnet::numval($id1) << 10);
                    144: 	if (defined($id2)) { $rndmod+=&Apache::lonnet::numval($id2); }
1.110     albertel  145:     } elsif ($rand_alg eq '64bit3') {
1.119     albertel  146: 	$rndmod=(&Apache::lonnet::numval2($id1) << 10);
                    147: 	if (defined($id2)) { $rndmod+=&Apache::lonnet::numval2($id2); }
1.126     albertel  148:     } elsif ($rand_alg eq '64bit4') {
1.119     albertel  149: 	my $shift=(4*$shift_amt)%30;
                    150: 	$rndmod=(&Apache::lonnet::numval3($id1) << (($shift+15)%30));
                    151: 	if (defined($id2)) {
                    152: 	    $rndmod+=(&Apache::lonnet::numval3($id2) << $shift );
1.110     albertel  153: 	}
1.126     albertel  154:     } else {
                    155: 	($rndmod,$rndmod2)=&Apache::lonnet::digest("$id1,$id2");
1.99      albertel  156:     }
1.127     albertel  157: 
1.99      albertel  158:     if ($rndseed =~/([,:])/) {
                    159: 	my $char=$1;
                    160: 	use integer;
                    161: 	my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
                    162: 	$num1+=$rndmod;
1.126     albertel  163: 	$num2+= ((defined($rndmod2)) ? $rndmod2 : $rndmod);
1.109     albertel  164: 	if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99      albertel  165: 	$rndseed=$num1.$char.$num2;
                    166:     } else {
1.74      albertel  167: 	$rndseed+=$rndmod;
1.109     albertel  168: 	if($Apache::lonnet::_64bit) {
                    169: 	    use integer;
                    170: 	    $rndseed=(($rndseed<<32)>>32);
                    171: 	}
1.74      albertel  172:     }
1.111     albertel  173:     &Apache::lonxml::debug("randseed $rndmod $rndseed");
1.74      albertel  174:     &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73      albertel  175:     return '';
1.26      albertel  176: }
                    177: 
1.7       www       178: sub meta_parameter_write {
1.38      albertel  179:     my ($name,$type,$default,$display)=@_;
1.41      albertel  180:     my $partref=$Apache::inputtags::part;
                    181:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                    182:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  183: 	$result.=            ' id="'.$Apache::inputtags::response[-1].'"';
                    184: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  185:     }
                    186:     $result.=            ' name="'.$name.'"'.
                    187:                          ' type="'.$type.'"'.
1.89      albertel  188: (defined($default)?' default="'.$default.'"':'').
                    189: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41      albertel  190:              .'></parameter>'
                    191:              ."\n";
                    192:     return $result;
1.33      www       193: }
                    194: 
                    195: sub meta_package_write {
                    196:     my $name=shift;
1.41      albertel  197:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    198:     if(defined($Apache::inputtags::response[-1])) {
1.73      albertel  199: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41      albertel  200:     }
                    201:     $result.=' package="'.$name.'"></parameter>'."\n";
                    202:     return $result;
1.7       www       203: }
                    204: 
                    205: sub meta_stores_write {
1.10      www       206:     my ($name,$type,$display)=@_;
1.41      albertel  207:     my $partref=$Apache::inputtags::part;
                    208:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    209:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  210: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                    211: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  212:     }	
                    213:     $result.=          ' name="'.$name.'"'.
                    214:                        ' type="'.$type.'"'.
                    215: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    216: 		      "></stores>\n";
1.7       www       217: }
                    218: 
                    219: sub mandatory_part_meta {
                    220: #
                    221: # Autogenerate metadata for mandatory
                    222: # input (from RAT or lonparmset) and 
                    223: # output (to lonspreadsheet)
                    224: # of each part
                    225: #
1.73      albertel  226:     return
1.34      www       227: #    &meta_parameter_write('opendate','date_start','',
                    228: #                          'Opening Date').
                    229: #    &meta_parameter_write('duedate','date_end','',
                    230: #                          'Due Date').
                    231: #    &meta_parameter_write('answerdate','date_start','',
                    232: #                          'Show Answer Date').
                    233: #    &meta_parameter_write('weight','int_zeropos','',
                    234: #                          'Available Points').
                    235: #    &meta_parameter_write('maxtries','int_pos','',
                    236: #                          'Maximum Number of Tries').
1.73      albertel  237: 	&meta_package_write('part').
                    238:         &meta_stores_write('solved','string',
                    239: 			   'Problem Status').
                    240:         &meta_stores_write('tries','int_zeropos',
                    241: 			   'Number of Attempts').
                    242:         &meta_stores_write('awarded','float',
                    243: 			   'Partial Credit Factor');
1.7       www       244: #
                    245: # Note: responseid-specific data 'submission' and 'awarddetail'
                    246: # not available to spreadsheet -> skip here
                    247: #
1.86      albertel  248: }
                    249: 
                    250: sub meta_part_order {
                    251:     if (@Apache::inputtags::partlist) {
                    252: 	my @parts=@Apache::inputtags::partlist;
                    253: 	shift(@parts);
1.100     albertel  254: 	return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.86      albertel  255:     } else {
1.100     albertel  256: 	return '<partorder>0</partorder>'."\n";
                    257:     }
                    258: }
                    259: 
                    260: sub meta_response_order {
                    261:     if (@Apache::inputtags::responselist) {
                    262: 	return '<responseorder>'.join(',',@Apache::inputtags::responselist).
                    263: 	    '</responseorder>'."\n";
1.86      albertel  264:     }
1.14      albertel  265: }
                    266: 
1.15      albertel  267: sub check_for_previous {
1.160     albertel  268:     my ($curresponse,$partid,$id,$last) = @_;
1.73      albertel  269:     my %previous;
                    270:     $previous{'used'} = 0;
                    271:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.98      albertel  272: 	if ($key =~ /resource\.$partid\.$id\.submission$/) {
1.160     albertel  273: 	    if ( $last && $key =~ /^(\d+):/ ) {
                    274: 		next if ($1 >= $last);
                    275: 	    }
1.73      albertel  276: 	    &Apache::lonxml::debug("Trying $key");
                    277: 	    my $pastresponse=$Apache::lonhomework::history{$key};
                    278: 	    if ($pastresponse eq $curresponse) {
                    279: 		$previous{'used'} = 1;
                    280: 		my $history;
                    281: 		if ( $key =~ /^(\d+):/ ) {
                    282: 		    $history=$1;
                    283: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    284: 		    $previous{'last'}='0';
                    285: 		    push(@{ $previous{'version'} },$history);
                    286: 		} else {
                    287: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    288: 		    $previous{'last'}='1';
                    289: 		}
                    290: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    291: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    292: 	    }
1.32      albertel  293: 	}
1.73      albertel  294:     }
                    295:     &Apache::lonhomework::showhash(%previous);
                    296:     return %previous;
1.54      albertel  297: }
                    298: 
                    299: sub handle_previous {
1.73      albertel  300:     my ($previous,$ad)=@_;
                    301:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    302: 	if ($$previous{'last'}) {
                    303: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107     albertel  304: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
                    305: 	} elsif ($Apache::lonhomework::type ne 'survey') {
1.73      albertel  306: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107     albertel  307: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73      albertel  308: 	}
1.54      albertel  309:     }
1.44      albertel  310: }
                    311: 
1.45      albertel  312: sub view_or_modify {
1.149     albertel  313:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.73      albertel  314:     my $myself=0;
1.120     albertel  315:     if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
1.73      albertel  316: 	$myself=1;
                    317:     }
                    318:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    319:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    320:     if ($mgr) { return "M"; }
                    321:     if ($vgr) { return "V"; }
                    322:     if ($myself) { return "V"; }
                    323:     return '';
1.45      albertel  324: }
                    325: 
1.44      albertel  326: sub start_dataresponse {
1.73      albertel  327:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    328:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    329:     my $result;
                    330:     if ($target eq 'web') {
                    331: 	$result = $token->[2]->{'display'}.':';
                    332:     } elsif ($target eq 'meta') {
                    333: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    334: 						       $token->[2]->{'type'},
                    335: 						       $token->[2]->{'display'});
                    336: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    337:     }
                    338:     return $result;
1.44      albertel  339: }
                    340: 
                    341: sub end_dataresponse {
1.73      albertel  342:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    343:     my $result;
                    344:     if ( $target eq 'web' ) {
                    345:     } elsif ($target eq 'grade' ) {
1.120     albertel  346: 	if ( defined $env{'form.submitted'}) {
1.149     albertel  347: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
1.73      albertel  348: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    349: 	    if ($allowed) {
1.94      albertel  350: 		&Apache::response::setup_params('dataresponse',$safeeval);
1.73      albertel  351: 		my $partid = $Apache::inputtags::part;
                    352: 		my $id = $Apache::inputtags::response['-1'];
1.120     albertel  353: 		my $response = $env{'form.HWVAL_'.$id};
1.73      albertel  354: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    355: 		if ( $response =~ /[^\s]/) {
                    356: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    357: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    358: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    359: 		}
                    360: 	    } else {
                    361: 		$result='Not Permitted to change values.'
                    362: 	    }
1.45      albertel  363: 	}
1.73      albertel  364:     }
                    365:     &Apache::response::end_response;
                    366:     return $result;
1.3       albertel  367: }
                    368: 
1.129     albertel  369: sub start_customresponse {
1.128     albertel  370:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    371:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.129     albertel  372:     push(@Apache::lonxml::namespace,'customresponse');
1.128     albertel  373:     my $result;
1.157     www       374:     @Apache::response::custom_answer=();
                    375:     @Apache::response::custom_answer_type=();
1.128     albertel  376:     &Apache::lonxml::register('Apache::response',('answer'));
                    377:     if ($target eq 'web') {
                    378:   	if (  &Apache::response::show_answer() ) {
                    379: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    380: 						   $safeeval);
1.152     albertel  381: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.128     albertel  382: 	}
                    383:     } elsif ($target eq 'edit') {
                    384: 	$result.=&Apache::edit::tag_start($target,$token);
                    385: 	$result.=&Apache::edit::text_arg('String to display for answer:',
1.145     albertel  386: 					 'answerdisplay',$token);
1.128     albertel  387: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    388:     } elsif ($target eq 'modified') {
                    389: 	my $constructtag;
                    390: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    391: 						  $safeeval,'answerdisplay');
                    392: 	if ($constructtag) {
                    393: 	    $result = &Apache::edit::rebuild_tag($token);
                    394: 	    $result.=&Apache::edit::handle_insert();
                    395: 	}
                    396:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    397: 	&Apache::response::reset_params();
                    398:     } elsif ($target eq 'meta') {
1.129     albertel  399: 	$result .= &Apache::response::meta_package_write('customresponse');
1.128     albertel  400:     }
                    401:     return $result;
                    402: }
                    403: 
1.129     albertel  404: sub end_customresponse {
1.128     albertel  405:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    406:     my $result;
                    407:     my $part=$Apache::inputtags::part;
                    408:     my $id=$Apache::inputtags::response[-1];
                    409:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    410: 	my $response = &Apache::response::getresponse();
1.146     albertel  411: 	if ($Apache::lonhomework::type eq 'exam' ||
                    412: 	    &Apache::response::submitted('scantron')) {
                    413: 	    &Apache::response::scored_response($part,$id);
                    414: 	} elsif ( $response =~ /[^\s]/ && 
1.158     www       415: 		  $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
1.128     albertel  416: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    417: 		&Apache::lonxml::default_homework_load($safeeval);
                    418: 	    }
                    419: 	    my %previous = &Apache::response::check_for_previous($response,
                    420: 								 $part,$id);
                    421: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    422: 		$response;
                    423: 	    my $error;
1.129     albertel  424: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128     albertel  425: 		$response;
                    426: 	    
1.157     www       427: 	    my $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
1.128     albertel  428: 	    if (!&Apache::inputtags::valid_award($award)) {
                    429: 		$error = $award;
                    430: 		$award = 'ERROR';
                    431: 	    }
                    432: 	    &Apache::response::handle_previous(\%previous,$award);
                    433: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    434: 		$award;
                    435: 	    if ($error) {
                    436: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    437: 		    $error;
                    438: 	    }
                    439: 	}
1.146     albertel  440:     } elsif ( $target eq 'answer') {
                    441: 	$result  = &Apache::response::answer_header('customresponse');
                    442: 	my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    443: 						$safeeval);
                    444: 	if ($env{'form.answer_output_mode'} ne 'tex') {
                    445: 	    $answer = '<b>'.$answer.'</b>';
                    446: 	}
                    447: 	$result .= &Apache::response::answer_part('customresponse',$answer);
                    448: 	$result .= &Apache::response::answer_footer('customresponse');
                    449:     }
                    450:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    451: 	$target eq 'tex' || $target eq 'analyze') {
                    452: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
1.128     albertel  453:     }
                    454:     pop(@Apache::lonxml::namespace);
1.157     www       455:     pop(@Apache::response::custom_answer);
                    456:     pop(@Apache::response::custom_answer_type);
1.128     albertel  457:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    458:     &Apache::response::end_response();
                    459:     return $result;
                    460: }
                    461: 
1.140     www       462: 
1.139     www       463: sub start_mathresponse {
1.140     www       464:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    465:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    466:     push(@Apache::lonxml::namespace,'mathresponse');
1.139     www       467:     my $result;
1.157     www       468:     @Apache::response::custom_answer=();
                    469:     @Apache::response::custom_answer_type=();
1.140     www       470:     &Apache::lonxml::register('Apache::response',('answer'));
                    471:     if ($target eq 'web') {
                    472:   	if (  &Apache::response::show_answer() ) {
                    473: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    474: 						   $safeeval);
1.152     albertel  475: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.140     www       476: 	}
                    477:     } elsif ($target eq 'edit') {
                    478: 	$result.=&Apache::edit::tag_start($target,$token);
                    479: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    480: 					 'answerdisplay',$token);
1.150     www       481: 	$result.=&Apache::edit::select_arg('Algebra System:',
                    482: 					   'cas',
                    483: 					   ['maxima'],
                    484: 					   $token);
                    485: 	$result.=&Apache::edit::text_arg('Argument Array:',
                    486: 					 'args',$token);
1.140     www       487: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    488:     } elsif ($target eq 'modified') {
                    489: 	my $constructtag;
                    490: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
1.153     www       491: 						  $safeeval,'answerdisplay','cas','args');
1.140     www       492: 	if ($constructtag) {
                    493: 	    $result = &Apache::edit::rebuild_tag($token);
                    494: 	    $result.=&Apache::edit::handle_insert();
                    495: 	}
                    496:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    497: 	&Apache::response::reset_params();
                    498:     } elsif ($target eq 'meta') {
                    499: 	$result .= &Apache::response::meta_package_write('mathresponse');
1.139     www       500:     }
                    501:     return $result;
                    502: }
                    503: 
                    504: sub end_mathresponse {
1.140     www       505:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    506:     my $result;
                    507:     my $part=$Apache::inputtags::part;
                    508:     my $id=$Apache::inputtags::response[-1];
                    509:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    510: 	my $response = &Apache::response::getresponse();
1.150     www       511: 	if ( $response =~ /[^\s]/ ) {
1.140     www       512: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    513: 		&Apache::lonxml::default_homework_load($safeeval);
                    514: 	    }
                    515: 	    my %previous = &Apache::response::check_for_previous($response,
                    516: 								 $part,$id);
                    517: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    518: 		$response;
                    519: 	    my $error;
1.155     www       520: 	    my $award;
                    521: 	    my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
                    522:             if ($cas eq 'maxima') {
                    523:                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
1.157     www       524:                 $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
1.155     www       525:             }
1.140     www       526: 	    if (!&Apache::inputtags::valid_award($award)) {
1.151     albertel  527: 		$error = $award;
                    528: 		$award = 'ERROR';
1.140     www       529: 	    }
                    530: 	    &Apache::response::handle_previous(\%previous,$award);
                    531: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    532: 		$award;
                    533: 	    if ($error) {
                    534: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    535: 		    $error;
                    536: 	    }
                    537: 	}
                    538:     }
                    539:     pop(@Apache::lonxml::namespace);
1.157     www       540:     pop(@Apache::response::custom_answer);
                    541:     pop(@Apache::response::custom_answer_type);
1.140     www       542:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    543:     &Apache::response::end_response();
                    544:     return $result;
1.139     www       545: }
                    546: 
1.156     www       547: sub implicit_multiplication {
                    548:     my ($expression)=@_;
                    549: # Escape scientific notation, so 3e8 does not become 3*e*8
                    550: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
                    551:     $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
                    552: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
                    553:     $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
                    554: # Fill in multiplication signs
                    555: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
                    556:     $expression=~s/(\w)\s+(\w)/$1\*$2/gs;
                    557: # )( -> )*(; ) ( -> )*(
                    558:     $expression=~s/\)\s*\(/\)\*\(/gs;
                    559: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
                    560:     $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
                    561: # a ( -> a*(
                    562:     $expression=~s/(\w)\s+\(/$1\*\(/gs;
                    563: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
                    564:     $expression=~s/\)\s*(\w)/\)\*$1/gs;
                    565: # 3&8 -> 3e8; 3&-4 -> 3e-4
                    566:     $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
                    567:     return $expression;
                    568: }
1.140     www       569: 
1.128     albertel  570: sub start_answer {
                    571:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    572:     my $result;
1.157     www       573:     push(@Apache::response::custom_answer,
                    574: 	&Apache::lonxml::get_all_text_unbalanced("/answer",$parser));
                    575:     push(@Apache::response::custom_answer_type,
                    576: 	lc(&Apache::lonxml::get_param('type',$parstack,$safeeval)));
                    577:     $Apache::response::custom_answer_type[-1] =~ s/\s+//g;
1.128     albertel  578:     if ($target eq "edit" ) {
                    579: 	$result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
                    580: 	$result.=&Apache::edit::editfield($token->[1],
1.158     www       581: 					  $Apache::response::custom_answer[-1],
1.128     albertel  582: 					  '',80,4);
                    583:     } elsif ( $target eq "modified" ) {
                    584: 	$result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
                    585:     }
                    586:     return $result;
                    587: }
                    588: 
                    589: sub end_answer {
                    590:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    591:     if ($target eq 'edit' ) {
                    592: 	return &Apache::edit::end_table();
                    593:     }
                    594: }
                    595: 
1.83      albertel  596: sub decide_package {
                    597:     my ($tagstack)=@_;
                    598:     my $package;
                    599:     if ($$tagstack[-1] eq 'parameter') {
                    600: 	$package='part';
                    601:     } else {
                    602: 	my $i=-1;
                    603: 	while (defined($$tagstack[$i])) {
                    604: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    605: 		$package=$$tagstack[$i];
                    606: 		last;
                    607: 	    }
                    608: 	    $i--;
                    609: 	}
                    610:     }
                    611:     return $package;
                    612: }
                    613: 
1.3       albertel  614: sub start_responseparam {
1.73      albertel  615:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    616:     my $result='';
                    617:     if ($target eq 'meta') {
                    618: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    619: 					$token->[2]->{'type'},
                    620: 					$token->[2]->{'default'},
                    621: 					$token->[2]->{'description'});
                    622:     } elsif ($target eq 'edit') {
                    623: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  624: 	my $optionlist;
                    625: 	my $package=&decide_package($tagstack);
                    626: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    627: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    628: 		$optionlist.='<option value="'.$1.'">'.
                    629: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    630: 	    }
                    631: 	}
                    632: 	if (defined($optionlist)) {
                    633: 	    $result.='Use template: <select name="'.
                    634: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    635: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    636: 	}
1.73      albertel  637: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    638: 	    &Apache::edit::text_arg('Type:','type',$token).
                    639: 		&Apache::edit::text_arg('Description:','description',$token).
                    640: 		    &Apache::edit::text_arg('Default:','default',$token).
                    641: 			"</td></tr>";
                    642: 	$result.=&Apache::edit::end_table;
                    643:     } elsif ($target eq 'modified') {
1.83      albertel  644: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    645: 						     $safeeval,'name','type',
                    646: 						     'description','default');
                    647: 	my $element=&Apache::edit::html_element_name('parameter_package');
1.120     albertel  648: 	if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
                    649: 	    my $name=$env{"form.$element"};
1.83      albertel  650: 	    my $tag=&decide_package($tagstack);
                    651: 	    $token->[2]->{'name'}=$name;
                    652: 	    $token->[2]->{'type'}=
                    653: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    654: 	    $token->[2]->{'description'}=
                    655: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    656: 	    $token->[2]->{'default'}=
                    657: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
                    658: 	    $constructtag=1;
                    659: 	}
1.73      albertel  660: 	if ($constructtag) {
                    661: 	    $result = &Apache::edit::rebuild_tag($token);
                    662: 	    $result.=&Apache::edit::handle_insert();
                    663: 	}
                    664:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    665: 	     $target eq 'tex' || $target eq 'analyze' ) {
1.120     albertel  666: 	if ($env{'request.state'} eq 'construct') {
1.73      albertel  667: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    668: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    669: 						     $safeeval);
                    670: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    671: 	}
1.52      albertel  672:     }
1.73      albertel  673:     return $result;
1.3       albertel  674: }
                    675: 
                    676: sub end_responseparam {
1.73      albertel  677:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    678:     if ($target eq 'edit') { return ('','no'); }
                    679:     return '';
1.55      albertel  680: }
                    681: 
                    682: sub start_parameter {
1.114     albertel  683:     return &start_responseparam(@_);
1.55      albertel  684: }
                    685: 
                    686: sub end_parameter {
1.114     albertel  687:     return &end_responseparam(@_);
1.42      albertel  688: }
                    689: 
1.67      albertel  690: sub reset_params {
                    691:     %Apache::inputtags::params=();
                    692: }
                    693: 
1.42      albertel  694: sub setup_params {
1.94      albertel  695:     my ($tag,$safeeval) = @_;
1.42      albertel  696: 
1.120     albertel  697:     if ($env{'request.state'} eq 'construct') { return; }
1.73      albertel  698:     my %paramlist=();
                    699:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
1.161     albertel  700: 	if ($key =~ /^\Q$tag\E/) {
1.73      albertel  701: 	    my ($package,$name) = split(/&/,$key);
                    702: 	    $paramlist{$name}=1;
                    703: 	}
1.42      albertel  704:     }
1.73      albertel  705:     foreach my $key (keys(%paramlist)) {
                    706: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    707: 	if (defined($Apache::inputtags::response[-1])) {
                    708: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    709: 	}
                    710: 	$entry.='.'.$key;
                    711: 	&Apache::lonxml::debug("looking for $entry");
                    712: 	my $value = &Apache::lonnet::EXT("$entry");
                    713: 	&Apache::lonxml::debug("$key has value :$value:");
                    714: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    715: 	    &Apache::lonxml::debug("using nothing");
                    716: 	    $Apache::inputtags::params{$key}='';
                    717: 	} else {
1.94      albertel  718: 	    my $string="{return qq\0".$value."\0}";
                    719: 	    my $newvalue=&Apache::run::run($string,$safeeval,1);
                    720: 	    if (defined($newvalue)) { $value=$newvalue; }
1.73      albertel  721: 	    $Apache::inputtags::params{$key}=$value;
                    722: 	}
1.42      albertel  723:     }
1.48      albertel  724: }
                    725: 
1.132     albertel  726: {
                    727:     my @answer_bits;
1.147     albertel  728:     my $need_row_start;
1.132     albertel  729: 
1.48      albertel  730: sub answer_header {
1.147     albertel  731:     my ($type,$increment,$rows) = @_;
1.73      albertel  732:     my $result;
1.120     albertel  733:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.132     albertel  734: 	undef(@answer_bits);
1.133     albertel  735: 	my $bit;
                    736: 	if ($Apache::lonhomework::type eq 'exam') {
                    737: 	    $bit = ($Apache::lonxml::counter+$increment).') ';
                    738: 	} else {
                    739: 	    $bit .= ' Answer for Part: \verb|'.
                    740: 		$Apache::inputtags::part.'| ';
                    741: 	}
                    742: 	push(@answer_bits,$bit);
1.73      albertel  743:     } else {
1.147     albertel  744: 	my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
1.132     albertel  745: 	$result  = '<table border="1"><tr>';
                    746: 	if ($Apache::lonhomework::type eq 'exam') {
1.147     albertel  747: 	    $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
1.132     albertel  748: 	} else {
1.147     albertel  749: 	    $result .= $td.&mt('Answer for Part: [_1]',
                    750: 			       $Apache::inputtags::part).'</td>';
1.132     albertel  751: 	}
                    752: 	$result .= "\n";
1.147     albertel  753: 	$need_row_start = 0;
                    754:     }
                    755:     return $result;
                    756: }
                    757: 
                    758: sub next_answer {
                    759:     my ($type) = @_;
                    760:     my $result;
                    761:     if ($env{'form.answer_output_mode'} eq 'tex') {
                    762: 	# FIXME ... need to do something with tex mode
                    763:     } else {
                    764: 	$result .= "</tr>";
                    765: 	$need_row_start = 1;
1.73      albertel  766:     }
                    767:     return $result;
1.48      albertel  768: }
                    769: 
                    770: sub answer_part {
1.148     albertel  771:     my ($type,$answer,$args) = @_;
1.73      albertel  772:     my $result;
1.120     albertel  773:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.148     albertel  774: 	if (!$args->{'no_verbatim'}) {
                    775: 	    my $to_use='|';
                    776: 	    foreach my $value (32..126) {
                    777: 		my $char=pack('c',$value);
                    778: 		if ($answer !~ /\Q$char\E/) {
                    779: 		    $to_use=$char;
                    780: 		    last;
                    781: 		}
                    782: 	    }
                    783: 	    if ($answer ne '') {
                    784: 		$answer = '\verb'.$to_use.$answer.$to_use;
1.124     albertel  785: 	    }
                    786: 	}
1.132     albertel  787: 	if ($answer ne '') {
1.148     albertel  788: 	    push(@answer_bits,$answer);
1.132     albertel  789: 	}
1.73      albertel  790:     } else {
1.147     albertel  791: 	if ($need_row_start) {
                    792: 	    $result .= '<tr>';
                    793: 	    $need_row_start = 0;
                    794: 	}
                    795: 	$result .= '<td>'.$answer.'</td>';
1.73      albertel  796:     }
                    797:     return $result;
1.48      albertel  798: }
                    799: 
                    800: sub answer_footer {
1.73      albertel  801:     my ($type) = @_;
                    802:     my $result;
1.120     albertel  803:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.133     albertel  804: 	my $columns = scalar(@answer_bits);
1.134     albertel  805: 	$result  = ' \vskip 0 mm \noindent \begin{tabular}{|'.'c|'x$columns.'}\hline ';
1.133     albertel  806: 	$result .= join(' & ',@answer_bits);
1.132     albertel  807: 	$result .= ' \\\\ \\hline \end{tabular} \vskip 0 mm ';
1.73      albertel  808:     } else {
1.80      albertel  809: 	$result = '</tr></table>';
1.73      albertel  810:     }
                    811:     return $result;
1.1       albertel  812: }
1.2       albertel  813: 
1.132     albertel  814: }
                    815: 
1.62      albertel  816: sub showallfoils {
1.120     albertel  817:     if (defined($env{'form.showallfoils'})) {
1.149     albertel  818: 	my ($symb)=&Apache::lonnet::whichuser();
1.120     albertel  819: 	if (($env{'request.state'} eq 'construct') || 
                    820: 	    ($env{'user.adv'} && $symb eq '')      ||
1.118     foxr      821:             ($Apache::lonhomework::viewgrades) ) {
1.102     albertel  822: 	    return 1;
                    823: 	}
1.73      albertel  824:     }
1.108     albertel  825:     if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.102     albertel  826:     return 0;
1.70      albertel  827: }
                    828: 
                    829: sub getresponse {
1.90      albertel  830:     my ($temp,$resulttype)=@_;
1.70      albertel  831:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    832:     my $response;
                    833:     if (!defined($temp)) {
                    834: 	$temp=1;
                    835:     } else {
                    836: 	$formparm.=":$temp";
                    837:     }
                    838:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    839: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    840: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    841: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120     albertel  842:     if ($env{'form.submitted'} eq 'scantron') {
1.71      albertel  843: 	my $part  = $Apache::inputtags::part;
                    844: 	my $id    = $Apache::inputtags::response[-1];
1.120     albertel  845: 	$response = $env{'scantron.'.($Apache::lonxml::counter+$temp-1).
1.70      albertel  846: 			 '.answer'};
1.71      albertel  847: 	# save bubbled letter for later
                    848: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    849: 	    $response;
1.90      albertel  850: 	if ($resulttype ne 'letter') {
1.104     albertel  851: 	    if ($resulttype eq 'A is 1') {
1.105     albertel  852: 		$response = $let_to_num{$response}+1;
                    853: 	    } else {
1.104     albertel  854: 		$response = $let_to_num{$response};
                    855: 	    }
1.90      albertel  856: 	}
1.70      albertel  857:     } else {
1.120     albertel  858: 	$response = $env{$formparm};
1.70      albertel  859:     }
                    860:     return $response;
1.62      albertel  861: }
1.71      albertel  862: 
                    863: sub repetition {
                    864:     my $id = $Apache::inputtags::part;
                    865:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121     albertel  866:     if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.125     albertel  867:     my $repetition = int($weight/10);
                    868:     if ($weight % 10 != 0) { $repetition++; } 
1.72      albertel  869:     return $repetition;
                    870: }
                    871: 
                    872: sub scored_response {
                    873:     my ($part,$id)=@_;
                    874:     my $repetition=&repetition();
                    875:     my $score=0;
                    876:     for (my $i=0;$i<$repetition;$i++) {
1.125     albertel  877: 	# A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
1.72      albertel  878: 	my $increase=&Apache::response::getresponse($i+1);
                    879: 	if ($increase ne '') { $score+=$increase+1; }
                    880:     }
                    881:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  882:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  883:     my $pcr=$score/$weight;
                    884:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    885:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    886: 	'ASSIGNED_SCORE';
1.71      albertel  887:     return $repetition;
1.78      albertel  888: }
                    889: 
                    890: sub whichorder {
                    891:     my ($max,$randomize,$showall,$hash)=@_;
                    892:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    893:     if (!defined(@{ $$hash{'names'} })) { return; }
                    894:     my @names = @{ $$hash{'names'} };
                    895:     my @whichopt =();
                    896:     my (%top,@toplist,%bottom,@bottomlist);
                    897:     if (!($showall || ($randomize eq 'no'))) {
                    898: 	my $current=0;
                    899: 	foreach my $name (@names) {
                    900: 	    $current++;
                    901: 	    if ($$hash{"$name.location"} eq 'top') {
                    902: 		$top{$name}=$current;
                    903: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    904: 		$bottom{$name}=$current;
                    905: 	    }
                    906: 	}
                    907:     }
                    908:     my $topcount=0;
                    909:     my $bottomcount=0;
                    910:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    911: 	   && ($#names > -1)) {
                    912: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    913: 	my $aopt;
                    914: 	if ($showall || ($randomize eq 'no')) {
                    915: 	    $aopt=0;
                    916: 	} else {
                    917: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    918: 	}
                    919: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    920: 	$aopt=splice(@names,$aopt,1);
                    921: 	#&Apache::lonxml::debug("Picked $aopt");
                    922: 	if ($top{$aopt}) {
                    923: 	    $toplist[$top{$aopt}]=$aopt;
                    924: 	    $topcount++;
                    925: 	} elsif ($bottom{$aopt}) {
                    926: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    927: 	    $bottomcount++;
                    928: 	} else {
                    929: 	    push (@whichopt,$aopt);
                    930: 	}
                    931:     }
                    932:     for (my $i=0;$i<=$#toplist;$i++) {
                    933: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    934:     }
                    935:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    936: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    937:     }
                    938:     return @whichopt;
1.71      albertel  939: }
                    940: 
1.85      albertel  941: sub show_answer {
                    942:     my $part   = $Apache::inputtags::part;
                    943:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    944:     my $status = $Apache::inputtags::status[-1];
                    945:     return  ( ($award =~ /^correct/
                    946: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    947: 	      || $status eq "SHOW_ANSWER");
                    948: }
1.87      albertel  949: 
                    950: sub analyze_store_foilgroup {
                    951:     my ($shown,$attrs)=@_;
                    952:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    953:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    954: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    955: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    956: 	foreach my $attr (@$attrs) {
                    957: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    958: 		$Apache::response::foilgroup{"$name.".$attr};
                    959: 	}
                    960:     }
                    961:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel  962: }
                    963: 
                    964: sub check_if_computed {
                    965:     my ($token,$parstack,$safeeval,$name)=@_;
                    966:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106     matthew   967:     if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96      albertel  968: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    969: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                    970:     }
1.87      albertel  971: }
                    972: 
                    973: sub pick_foil_for_concept {
                    974:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    975:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    976:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    977:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    978:     my $name=$names[$pick];
                    979:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    980:     foreach my $attr (@$attrs) {
                    981: 	$Apache::response::foilgroup{"$name.".$attr} =
                    982: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    983:     }
                    984:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    985:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    986:     &Apache::lonxml::debug("Selecting $name in $concept");
                    987:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    988:     if ($target eq 'analyze') {
                    989: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    990: 	      $concept);
                    991: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    992: 	    $Apache::response::conceptgroup{'names'};
                    993: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    994: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    995: 		  $name);
                    996: 	    foreach my $attr (@$attrs) {
                    997: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    998: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    999: 	    }
                   1000: 	}
                   1001:     }
                   1002:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                   1003:     $hinthash->{"$part_id.concept.$concept"}=
                   1004: 	$Apache::response::conceptgroup{'names'};
                   1005: 
                   1006: }
                   1007: 
1.95      albertel 1008: sub get_response_param {
                   1009:     my ($id,$name,$default)=@_;
                   1010:     my $parameter;
1.120     albertel 1011:     if ($env{'request.state'} eq 'construct' &&
1.95      albertel 1012: 	defined($Apache::inputtags::params{$name})) {
                   1013: 	$parameter=$Apache::inputtags::params{$name};
                   1014:     } else {
                   1015: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                   1016:     }
                   1017:     if (!defined($parameter) ||	$parameter eq '') {
                   1018: 	$parameter = $default;
                   1019:     }
                   1020:     return $parameter;
                   1021: }
1.87      albertel 1022: 
1.113     albertel 1023: sub submitted {
                   1024:     my ($who)=@_;
                   1025:     
                   1026:     # when scatron grading any submission is a submission
1.120     albertel 1027:     if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113     albertel 1028:     # if the caller only cared if this was a scantron submission
                   1029:     if ($who eq 'scantron') { return 0; }
                   1030:     # if the Submit Answer button for this particular part was pressed
                   1031:     my $partid=$Apache::inputtags::part;
1.159     albertel 1032:     if ($env{'form.submitted'} eq "part_$partid") {
                   1033: 	return 1;
                   1034:     }
                   1035:     if ($env{'form.submitted'} eq "yes"
                   1036: 	&& defined($env{'form.submit_'.$partid})) {
                   1037: 	return 1;
                   1038:     }
1.122     albertel 1039:     # Submit All button on a .page was pressed
                   1040:     if (defined($env{'form.all_submit'})) { return 1; }
1.113     albertel 1041:     # otherwise no submission occured
                   1042:     return 0;
                   1043: }
1.117     albertel 1044: 
1.130     albertel 1045: sub add_to_gradingqueue {
1.149     albertel 1046:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.131     albertel 1047:     if (   $courseid eq ''
                   1048: 	|| $symb eq ''
1.135     albertel 1049: 	|| $env{'request.state'} eq 'construct'
                   1050: 	|| $Apache::lonhomework::type ne 'problem') {
1.131     albertel 1051: 	return;
                   1052:     }
1.130     albertel 1053: 
                   1054:     my %queue_info = ( 'type' => 'problem',
                   1055: 		       'time' => time);
                   1056: 
                   1057:     if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
                   1058: 	$queue_info{'slot'}=
                   1059: 	     $Apache::lonhomework::history{"resource.0.checkedin.slot"};
                   1060:     }
                   1061: 
                   1062:     my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
                   1063:     if ($result ne 'ok') {
                   1064: 	&Apache::lonxml::error("add_to_queue said $result");
                   1065:     }
                   1066: }
                   1067: 
1.117     albertel 1068: # basically undef and 0 (both false) mean that they still have work to do
                   1069: # and all true values mean that they can't do any more work
                   1070: #
                   1071: # a return of undef means it is unattempted
                   1072: # a return of 0 means it is attmpted and wrong but still has tries
                   1073: # a return of 1 means it is marked correct
                   1074: # a return of 2 means they have exceed maximum number of tries
                   1075: # a return of 3 means it after the answer date
                   1076: sub check_status {
                   1077:     my ($id)=@_;
1.143     albertel 1078:     if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117     albertel 1079:     my $curtime=&Apache::lonnet::EXT('system.time');
                   1080:     my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
                   1081:     my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
                   1082:     my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
                   1083:     if ( $opendate && $curtime > $opendate &&
                   1084:          $duedate && $curtime > $duedate &&
                   1085:          $answerdate && $curtime > $answerdate) {
                   1086:         return 3;
                   1087:     }
                   1088:     my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
                   1089:     if ($status =~ /^correct/) { return 1; }
                   1090:     if (!$status) { return undef; }
                   1091:     my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
                   1092:     if ($maxtries eq '') { $maxtries=2; }
                   1093:     my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
                   1094:     if ($curtries < $maxtries) { return 0; }
                   1095:     return 2;
                   1096: }
                   1097: 
1.162   ! albertel 1098: sub setup_prior_tries_hash {
        !          1099:     my ($func,$data) = @_;
        !          1100:     my $part = $Apache::inputtags::part;
        !          1101:     my $id   = $Apache::inputtags::response[-1];	
        !          1102:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
        !          1103: 	my $sub_key   = "$i:resource.$part.$id.submission";
        !          1104: 	next if (!exists($Apache::lonhomework::history{$sub_key}));
        !          1105: 	my @other_data;
        !          1106: 	foreach my $datum (@{ $data }) {
        !          1107: 	    if (ref($datum)) {
        !          1108: 		push(@other_data,$datum);
        !          1109: 	    } else {
        !          1110: 		my $info_key = "$i:resource.$part.$id.$datum";
        !          1111: 		push(@other_data,$Apache::lonhomework::history{$info_key});
        !          1112: 	    }
        !          1113: 	}
        !          1114: 
        !          1115: 	my $output =
        !          1116: 	    &$func('grade',
        !          1117: 		   $Apache::lonhomework::history{$sub_key},
        !          1118: 		   \@other_data);
        !          1119: 	if (defined($output)) {
        !          1120: 	    $Apache::inputtags::submission_display{$sub_key} = $output;
        !          1121: 	}
        !          1122:     }
        !          1123: }
        !          1124: 
1.1       albertel 1125: 1;
                   1126: __END__
1.38      albertel 1127:  

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