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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.154   ! www         4: # $Id: response.pm,v 1.153 2006/12/11 17:37:43 www 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.73      albertel  268:     my ($curresponse,$partid,$id) = @_;
                    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.73      albertel  273: 	    &Apache::lonxml::debug("Trying $key");
                    274: 	    my $pastresponse=$Apache::lonhomework::history{$key};
                    275: 	    if ($pastresponse eq $curresponse) {
                    276: 		$previous{'used'} = 1;
                    277: 		my $history;
                    278: 		if ( $key =~ /^(\d+):/ ) {
                    279: 		    $history=$1;
                    280: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    281: 		    $previous{'last'}='0';
                    282: 		    push(@{ $previous{'version'} },$history);
                    283: 		} else {
                    284: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    285: 		    $previous{'last'}='1';
                    286: 		}
                    287: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    288: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    289: 	    }
1.32      albertel  290: 	}
1.73      albertel  291:     }
                    292:     &Apache::lonhomework::showhash(%previous);
                    293:     return %previous;
1.54      albertel  294: }
                    295: 
                    296: sub handle_previous {
1.73      albertel  297:     my ($previous,$ad)=@_;
                    298:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    299: 	if ($$previous{'last'}) {
                    300: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107     albertel  301: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
                    302: 	} elsif ($Apache::lonhomework::type ne 'survey') {
1.73      albertel  303: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107     albertel  304: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73      albertel  305: 	}
1.54      albertel  306:     }
1.44      albertel  307: }
                    308: 
1.45      albertel  309: sub view_or_modify {
1.149     albertel  310:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.73      albertel  311:     my $myself=0;
1.120     albertel  312:     if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
1.73      albertel  313: 	$myself=1;
                    314:     }
                    315:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    316:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    317:     if ($mgr) { return "M"; }
                    318:     if ($vgr) { return "V"; }
                    319:     if ($myself) { return "V"; }
                    320:     return '';
1.45      albertel  321: }
                    322: 
1.44      albertel  323: sub start_dataresponse {
1.73      albertel  324:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    325:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    326:     my $result;
                    327:     if ($target eq 'web') {
                    328: 	$result = $token->[2]->{'display'}.':';
                    329:     } elsif ($target eq 'meta') {
                    330: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    331: 						       $token->[2]->{'type'},
                    332: 						       $token->[2]->{'display'});
                    333: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    334:     }
                    335:     return $result;
1.44      albertel  336: }
                    337: 
                    338: sub end_dataresponse {
1.73      albertel  339:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    340:     my $result;
                    341:     if ( $target eq 'web' ) {
                    342:     } elsif ($target eq 'grade' ) {
1.120     albertel  343: 	if ( defined $env{'form.submitted'}) {
1.149     albertel  344: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
1.73      albertel  345: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    346: 	    if ($allowed) {
1.94      albertel  347: 		&Apache::response::setup_params('dataresponse',$safeeval);
1.73      albertel  348: 		my $partid = $Apache::inputtags::part;
                    349: 		my $id = $Apache::inputtags::response['-1'];
1.120     albertel  350: 		my $response = $env{'form.HWVAL_'.$id};
1.73      albertel  351: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    352: 		if ( $response =~ /[^\s]/) {
                    353: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    354: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    355: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    356: 		}
                    357: 	    } else {
                    358: 		$result='Not Permitted to change values.'
                    359: 	    }
1.45      albertel  360: 	}
1.73      albertel  361:     }
                    362:     &Apache::response::end_response;
                    363:     return $result;
1.3       albertel  364: }
                    365: 
1.129     albertel  366: sub start_customresponse {
1.128     albertel  367:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    368:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.129     albertel  369:     push(@Apache::lonxml::namespace,'customresponse');
1.128     albertel  370:     my $result;
1.129     albertel  371:     undef($Apache::response::custom_answer);
1.128     albertel  372:     &Apache::lonxml::register('Apache::response',('answer'));
                    373:     if ($target eq 'web') {
                    374:   	if (  &Apache::response::show_answer() ) {
                    375: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    376: 						   $safeeval);
1.152     albertel  377: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.128     albertel  378: 	}
                    379:     } elsif ($target eq 'edit') {
                    380: 	$result.=&Apache::edit::tag_start($target,$token);
                    381: 	$result.=&Apache::edit::text_arg('String to display for answer:',
1.145     albertel  382: 					 'answerdisplay',$token);
1.128     albertel  383: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    384:     } elsif ($target eq 'modified') {
                    385: 	my $constructtag;
                    386: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    387: 						  $safeeval,'answerdisplay');
                    388: 	if ($constructtag) {
                    389: 	    $result = &Apache::edit::rebuild_tag($token);
                    390: 	    $result.=&Apache::edit::handle_insert();
                    391: 	}
                    392:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    393: 	&Apache::response::reset_params();
                    394:     } elsif ($target eq 'meta') {
1.129     albertel  395: 	$result .= &Apache::response::meta_package_write('customresponse');
1.128     albertel  396:     }
                    397:     return $result;
                    398: }
                    399: 
1.129     albertel  400: sub end_customresponse {
1.128     albertel  401:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    402:     my $result;
                    403:     my $part=$Apache::inputtags::part;
                    404:     my $id=$Apache::inputtags::response[-1];
                    405:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    406: 	my $response = &Apache::response::getresponse();
1.146     albertel  407: 	if ($Apache::lonhomework::type eq 'exam' ||
                    408: 	    &Apache::response::submitted('scantron')) {
                    409: 	    &Apache::response::scored_response($part,$id);
                    410: 	} elsif ( $response =~ /[^\s]/ && 
                    411: 		  $Apache::response::custom_answer_type eq 'loncapa/perl') {
1.128     albertel  412: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    413: 		&Apache::lonxml::default_homework_load($safeeval);
                    414: 	    }
                    415: 	    my %previous = &Apache::response::check_for_previous($response,
                    416: 								 $part,$id);
                    417: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    418: 		$response;
                    419: 	    my $error;
1.129     albertel  420: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128     albertel  421: 		$response;
                    422: 	    
1.129     albertel  423: 	    my $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer.'}',$safeeval);
1.128     albertel  424: 	    if (!&Apache::inputtags::valid_award($award)) {
                    425: 		$error = $award;
                    426: 		$award = 'ERROR';
                    427: 	    }
                    428: 	    &Apache::response::handle_previous(\%previous,$award);
                    429: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    430: 		$award;
                    431: 	    if ($error) {
                    432: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    433: 		    $error;
                    434: 	    }
                    435: 	}
1.146     albertel  436:     } elsif ( $target eq 'answer') {
                    437: 	$result  = &Apache::response::answer_header('customresponse');
                    438: 	my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    439: 						$safeeval);
                    440: 	if ($env{'form.answer_output_mode'} ne 'tex') {
                    441: 	    $answer = '<b>'.$answer.'</b>';
                    442: 	}
                    443: 	$result .= &Apache::response::answer_part('customresponse',$answer);
                    444: 	$result .= &Apache::response::answer_footer('customresponse');
                    445:     }
                    446:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    447: 	$target eq 'tex' || $target eq 'analyze') {
                    448: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
1.128     albertel  449:     }
                    450:     pop(@Apache::lonxml::namespace);
                    451:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    452:     &Apache::response::end_response();
                    453:     return $result;
                    454: }
                    455: 
1.140     www       456: 
1.139     www       457: sub start_mathresponse {
1.140     www       458:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    459:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    460:     push(@Apache::lonxml::namespace,'mathresponse');
1.139     www       461:     my $result;
1.140     www       462:     undef($Apache::response::custom_answer);
                    463:     &Apache::lonxml::register('Apache::response',('answer'));
                    464:     if ($target eq 'web') {
                    465:   	if (  &Apache::response::show_answer() ) {
                    466: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    467: 						   $safeeval);
1.152     albertel  468: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.140     www       469: 	}
                    470:     } elsif ($target eq 'edit') {
                    471: 	$result.=&Apache::edit::tag_start($target,$token);
                    472: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    473: 					 'answerdisplay',$token);
1.150     www       474: 	$result.=&Apache::edit::select_arg('Algebra System:',
                    475: 					   'cas',
                    476: 					   ['maxima'],
                    477: 					   $token);
                    478: 	$result.=&Apache::edit::text_arg('Argument Array:',
                    479: 					 'args',$token);
1.140     www       480: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    481:     } elsif ($target eq 'modified') {
                    482: 	my $constructtag;
                    483: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
1.153     www       484: 						  $safeeval,'answerdisplay','cas','args');
1.140     www       485: 	if ($constructtag) {
                    486: 	    $result = &Apache::edit::rebuild_tag($token);
                    487: 	    $result.=&Apache::edit::handle_insert();
                    488: 	}
                    489:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    490: 	&Apache::response::reset_params();
1.153     www       491: 	my $args_ref= \%{$safeeval->varglob('LONCAPA::mathresponse_args')};
                    492: 	undef(%{ $args_ref });
                    493: 	$$args_ref{'cas'}=$token->[2]{'cas'};
1.154   ! www       494:         if ($token->[2]{'args'}=~/\$/) {
        !           495: 	    $$args_ref{'args'}=&Apache::run::run($token->[2]{'args'},$safeeval);
        !           496: 	} elsif ($token->[2]{'args'}=~/\@/) {
        !           497: 	    $$args_ref{'args'}=&Apache::run::run('join(",",'.$token->[2]{'args'}.')',$safeeval);
        !           498: 	} else {
        !           499: 	    $$args_ref{'args'}=$token->[2]{'args'};
        !           500: 	}
1.140     www       501:     } elsif ($target eq 'meta') {
                    502: 	$result .= &Apache::response::meta_package_write('mathresponse');
1.139     www       503:     }
                    504:     return $result;
                    505: }
                    506: 
                    507: sub end_mathresponse {
1.140     www       508:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    509:     my $result;
                    510:     my $part=$Apache::inputtags::part;
                    511:     my $id=$Apache::inputtags::response[-1];
                    512:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    513: 	my $response = &Apache::response::getresponse();
1.150     www       514: 	if ( $response =~ /[^\s]/ ) {
1.140     www       515: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    516: 		&Apache::lonxml::default_homework_load($safeeval);
                    517: 	    }
                    518: 	    my %previous = &Apache::response::check_for_previous($response,
                    519: 								 $part,$id);
                    520: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    521: 		$response;
                    522: 	    my $error;
1.153     www       523: 	    my $args_ref= \%{$safeeval->varglob('LONCAPA::mathresponse_args')};
                    524:             my $award;
                    525:             if ($$args_ref{'cas'} eq 'maxima') {
                    526: 		$award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer,$response,$$args_ref{'args'});
                    527: 	    }
1.140     www       528: 	    if (!&Apache::inputtags::valid_award($award)) {
1.151     albertel  529: 		$error = $award;
                    530: 		$award = 'ERROR';
1.140     www       531: 	    }
                    532: 	    &Apache::response::handle_previous(\%previous,$award);
                    533: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    534: 		$award;
                    535: 	    if ($error) {
                    536: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    537: 		    $error;
                    538: 	    }
                    539: 	}
                    540:     }
                    541:     pop(@Apache::lonxml::namespace);
                    542:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    543:     &Apache::response::end_response();
                    544:     return $result;
1.139     www       545: }
                    546: 
1.140     www       547: 
1.128     albertel  548: sub start_answer {
                    549:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    550:     my $result;
1.129     albertel  551:     $Apache::response::custom_answer=
1.128     albertel  552: 	&Apache::lonxml::get_all_text_unbalanced("/answer",$parser);
1.129     albertel  553:     $Apache::response::custom_answer_type=
1.128     albertel  554: 	lc(&Apache::lonxml::get_param('type',$parstack,$safeeval));
1.129     albertel  555:     $Apache::response::custom_answer_type =~ s/\s+//g;
1.128     albertel  556:     if ($target eq "edit" ) {
                    557: 	$result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
                    558: 	$result.=&Apache::edit::editfield($token->[1],
1.129     albertel  559: 					  $Apache::response::custom_answer,
1.128     albertel  560: 					  '',80,4);
                    561:     } elsif ( $target eq "modified" ) {
                    562: 	$result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
                    563:     }
                    564:     return $result;
                    565: }
                    566: 
                    567: sub end_answer {
                    568:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    569:     if ($target eq 'edit' ) {
                    570: 	return &Apache::edit::end_table();
                    571:     }
                    572: }
                    573: 
1.83      albertel  574: sub decide_package {
                    575:     my ($tagstack)=@_;
                    576:     my $package;
                    577:     if ($$tagstack[-1] eq 'parameter') {
                    578: 	$package='part';
                    579:     } else {
                    580: 	my $i=-1;
                    581: 	while (defined($$tagstack[$i])) {
                    582: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    583: 		$package=$$tagstack[$i];
                    584: 		last;
                    585: 	    }
                    586: 	    $i--;
                    587: 	}
                    588:     }
                    589:     return $package;
                    590: }
                    591: 
1.3       albertel  592: sub start_responseparam {
1.73      albertel  593:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    594:     my $result='';
                    595:     if ($target eq 'meta') {
                    596: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    597: 					$token->[2]->{'type'},
                    598: 					$token->[2]->{'default'},
                    599: 					$token->[2]->{'description'});
                    600:     } elsif ($target eq 'edit') {
                    601: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  602: 	my $optionlist;
                    603: 	my $package=&decide_package($tagstack);
                    604: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    605: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    606: 		$optionlist.='<option value="'.$1.'">'.
                    607: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    608: 	    }
                    609: 	}
                    610: 	if (defined($optionlist)) {
                    611: 	    $result.='Use template: <select name="'.
                    612: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    613: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    614: 	}
1.73      albertel  615: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    616: 	    &Apache::edit::text_arg('Type:','type',$token).
                    617: 		&Apache::edit::text_arg('Description:','description',$token).
                    618: 		    &Apache::edit::text_arg('Default:','default',$token).
                    619: 			"</td></tr>";
                    620: 	$result.=&Apache::edit::end_table;
                    621:     } elsif ($target eq 'modified') {
1.83      albertel  622: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    623: 						     $safeeval,'name','type',
                    624: 						     'description','default');
                    625: 	my $element=&Apache::edit::html_element_name('parameter_package');
1.120     albertel  626: 	if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
                    627: 	    my $name=$env{"form.$element"};
1.83      albertel  628: 	    my $tag=&decide_package($tagstack);
                    629: 	    $token->[2]->{'name'}=$name;
                    630: 	    $token->[2]->{'type'}=
                    631: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    632: 	    $token->[2]->{'description'}=
                    633: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    634: 	    $token->[2]->{'default'}=
                    635: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
                    636: 	    $constructtag=1;
                    637: 	}
1.73      albertel  638: 	if ($constructtag) {
                    639: 	    $result = &Apache::edit::rebuild_tag($token);
                    640: 	    $result.=&Apache::edit::handle_insert();
                    641: 	}
                    642:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    643: 	     $target eq 'tex' || $target eq 'analyze' ) {
1.120     albertel  644: 	if ($env{'request.state'} eq 'construct') {
1.73      albertel  645: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    646: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    647: 						     $safeeval);
                    648: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    649: 	}
1.52      albertel  650:     }
1.73      albertel  651:     return $result;
1.3       albertel  652: }
                    653: 
                    654: sub end_responseparam {
1.73      albertel  655:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    656:     if ($target eq 'edit') { return ('','no'); }
                    657:     return '';
1.55      albertel  658: }
                    659: 
                    660: sub start_parameter {
1.114     albertel  661:     return &start_responseparam(@_);
1.55      albertel  662: }
                    663: 
                    664: sub end_parameter {
1.114     albertel  665:     return &end_responseparam(@_);
1.42      albertel  666: }
                    667: 
1.67      albertel  668: sub reset_params {
                    669:     %Apache::inputtags::params=();
                    670: }
                    671: 
1.42      albertel  672: sub setup_params {
1.94      albertel  673:     my ($tag,$safeeval) = @_;
1.42      albertel  674: 
1.120     albertel  675:     if ($env{'request.state'} eq 'construct') { return; }
1.73      albertel  676:     my %paramlist=();
                    677:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    678: 	if ($key =~ /^$tag/) {
                    679: 	    my ($package,$name) = split(/&/,$key);
                    680: 	    $paramlist{$name}=1;
                    681: 	}
1.42      albertel  682:     }
1.73      albertel  683:     foreach my $key (keys(%paramlist)) {
                    684: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    685: 	if (defined($Apache::inputtags::response[-1])) {
                    686: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    687: 	}
                    688: 	$entry.='.'.$key;
                    689: 	&Apache::lonxml::debug("looking for $entry");
                    690: 	my $value = &Apache::lonnet::EXT("$entry");
                    691: 	&Apache::lonxml::debug("$key has value :$value:");
                    692: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    693: 	    &Apache::lonxml::debug("using nothing");
                    694: 	    $Apache::inputtags::params{$key}='';
                    695: 	} else {
1.94      albertel  696: 	    my $string="{return qq\0".$value."\0}";
                    697: 	    my $newvalue=&Apache::run::run($string,$safeeval,1);
                    698: 	    if (defined($newvalue)) { $value=$newvalue; }
1.73      albertel  699: 	    $Apache::inputtags::params{$key}=$value;
                    700: 	}
1.42      albertel  701:     }
1.48      albertel  702: }
                    703: 
1.132     albertel  704: {
                    705:     my @answer_bits;
1.147     albertel  706:     my $need_row_start;
1.132     albertel  707: 
1.48      albertel  708: sub answer_header {
1.147     albertel  709:     my ($type,$increment,$rows) = @_;
1.73      albertel  710:     my $result;
1.120     albertel  711:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.132     albertel  712: 	undef(@answer_bits);
1.133     albertel  713: 	my $bit;
                    714: 	if ($Apache::lonhomework::type eq 'exam') {
                    715: 	    $bit = ($Apache::lonxml::counter+$increment).') ';
                    716: 	} else {
                    717: 	    $bit .= ' Answer for Part: \verb|'.
                    718: 		$Apache::inputtags::part.'| ';
                    719: 	}
                    720: 	push(@answer_bits,$bit);
1.73      albertel  721:     } else {
1.147     albertel  722: 	my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
1.132     albertel  723: 	$result  = '<table border="1"><tr>';
                    724: 	if ($Apache::lonhomework::type eq 'exam') {
1.147     albertel  725: 	    $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
1.132     albertel  726: 	} else {
1.147     albertel  727: 	    $result .= $td.&mt('Answer for Part: [_1]',
                    728: 			       $Apache::inputtags::part).'</td>';
1.132     albertel  729: 	}
                    730: 	$result .= "\n";
1.147     albertel  731: 	$need_row_start = 0;
                    732:     }
                    733:     return $result;
                    734: }
                    735: 
                    736: sub next_answer {
                    737:     my ($type) = @_;
                    738:     my $result;
                    739:     if ($env{'form.answer_output_mode'} eq 'tex') {
                    740: 	# FIXME ... need to do something with tex mode
                    741:     } else {
                    742: 	$result .= "</tr>";
                    743: 	$need_row_start = 1;
1.73      albertel  744:     }
                    745:     return $result;
1.48      albertel  746: }
                    747: 
                    748: sub answer_part {
1.148     albertel  749:     my ($type,$answer,$args) = @_;
1.73      albertel  750:     my $result;
1.120     albertel  751:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.148     albertel  752: 	if (!$args->{'no_verbatim'}) {
                    753: 	    my $to_use='|';
                    754: 	    foreach my $value (32..126) {
                    755: 		my $char=pack('c',$value);
                    756: 		if ($answer !~ /\Q$char\E/) {
                    757: 		    $to_use=$char;
                    758: 		    last;
                    759: 		}
                    760: 	    }
                    761: 	    if ($answer ne '') {
                    762: 		$answer = '\verb'.$to_use.$answer.$to_use;
1.124     albertel  763: 	    }
                    764: 	}
1.132     albertel  765: 	if ($answer ne '') {
1.148     albertel  766: 	    push(@answer_bits,$answer);
1.132     albertel  767: 	}
1.73      albertel  768:     } else {
1.147     albertel  769: 	if ($need_row_start) {
                    770: 	    $result .= '<tr>';
                    771: 	    $need_row_start = 0;
                    772: 	}
                    773: 	$result .= '<td>'.$answer.'</td>';
1.73      albertel  774:     }
                    775:     return $result;
1.48      albertel  776: }
                    777: 
                    778: sub answer_footer {
1.73      albertel  779:     my ($type) = @_;
                    780:     my $result;
1.120     albertel  781:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.133     albertel  782: 	my $columns = scalar(@answer_bits);
1.134     albertel  783: 	$result  = ' \vskip 0 mm \noindent \begin{tabular}{|'.'c|'x$columns.'}\hline ';
1.133     albertel  784: 	$result .= join(' & ',@answer_bits);
1.132     albertel  785: 	$result .= ' \\\\ \\hline \end{tabular} \vskip 0 mm ';
1.73      albertel  786:     } else {
1.80      albertel  787: 	$result = '</tr></table>';
1.73      albertel  788:     }
                    789:     return $result;
1.1       albertel  790: }
1.2       albertel  791: 
1.132     albertel  792: }
                    793: 
1.62      albertel  794: sub showallfoils {
1.120     albertel  795:     if (defined($env{'form.showallfoils'})) {
1.149     albertel  796: 	my ($symb)=&Apache::lonnet::whichuser();
1.120     albertel  797: 	if (($env{'request.state'} eq 'construct') || 
                    798: 	    ($env{'user.adv'} && $symb eq '')      ||
1.118     foxr      799:             ($Apache::lonhomework::viewgrades) ) {
1.102     albertel  800: 	    return 1;
                    801: 	}
1.73      albertel  802:     }
1.108     albertel  803:     if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.102     albertel  804:     return 0;
1.70      albertel  805: }
                    806: 
                    807: sub getresponse {
1.90      albertel  808:     my ($temp,$resulttype)=@_;
1.70      albertel  809:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    810:     my $response;
                    811:     if (!defined($temp)) {
                    812: 	$temp=1;
                    813:     } else {
                    814: 	$formparm.=":$temp";
                    815:     }
                    816:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    817: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    818: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    819: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120     albertel  820:     if ($env{'form.submitted'} eq 'scantron') {
1.71      albertel  821: 	my $part  = $Apache::inputtags::part;
                    822: 	my $id    = $Apache::inputtags::response[-1];
1.120     albertel  823: 	$response = $env{'scantron.'.($Apache::lonxml::counter+$temp-1).
1.70      albertel  824: 			 '.answer'};
1.71      albertel  825: 	# save bubbled letter for later
                    826: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    827: 	    $response;
1.90      albertel  828: 	if ($resulttype ne 'letter') {
1.104     albertel  829: 	    if ($resulttype eq 'A is 1') {
1.105     albertel  830: 		$response = $let_to_num{$response}+1;
                    831: 	    } else {
1.104     albertel  832: 		$response = $let_to_num{$response};
                    833: 	    }
1.90      albertel  834: 	}
1.70      albertel  835:     } else {
1.120     albertel  836: 	$response = $env{$formparm};
1.70      albertel  837:     }
                    838:     return $response;
1.62      albertel  839: }
1.71      albertel  840: 
                    841: sub repetition {
                    842:     my $id = $Apache::inputtags::part;
                    843:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121     albertel  844:     if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.125     albertel  845:     my $repetition = int($weight/10);
                    846:     if ($weight % 10 != 0) { $repetition++; } 
1.72      albertel  847:     return $repetition;
                    848: }
                    849: 
                    850: sub scored_response {
                    851:     my ($part,$id)=@_;
                    852:     my $repetition=&repetition();
                    853:     my $score=0;
                    854:     for (my $i=0;$i<$repetition;$i++) {
1.125     albertel  855: 	# A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
1.72      albertel  856: 	my $increase=&Apache::response::getresponse($i+1);
                    857: 	if ($increase ne '') { $score+=$increase+1; }
                    858:     }
                    859:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  860:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  861:     my $pcr=$score/$weight;
                    862:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    863:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    864: 	'ASSIGNED_SCORE';
1.71      albertel  865:     return $repetition;
1.78      albertel  866: }
                    867: 
                    868: sub whichorder {
                    869:     my ($max,$randomize,$showall,$hash)=@_;
                    870:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    871:     if (!defined(@{ $$hash{'names'} })) { return; }
                    872:     my @names = @{ $$hash{'names'} };
                    873:     my @whichopt =();
                    874:     my (%top,@toplist,%bottom,@bottomlist);
                    875:     if (!($showall || ($randomize eq 'no'))) {
                    876: 	my $current=0;
                    877: 	foreach my $name (@names) {
                    878: 	    $current++;
                    879: 	    if ($$hash{"$name.location"} eq 'top') {
                    880: 		$top{$name}=$current;
                    881: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    882: 		$bottom{$name}=$current;
                    883: 	    }
                    884: 	}
                    885:     }
                    886:     my $topcount=0;
                    887:     my $bottomcount=0;
                    888:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    889: 	   && ($#names > -1)) {
                    890: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    891: 	my $aopt;
                    892: 	if ($showall || ($randomize eq 'no')) {
                    893: 	    $aopt=0;
                    894: 	} else {
                    895: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    896: 	}
                    897: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    898: 	$aopt=splice(@names,$aopt,1);
                    899: 	#&Apache::lonxml::debug("Picked $aopt");
                    900: 	if ($top{$aopt}) {
                    901: 	    $toplist[$top{$aopt}]=$aopt;
                    902: 	    $topcount++;
                    903: 	} elsif ($bottom{$aopt}) {
                    904: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    905: 	    $bottomcount++;
                    906: 	} else {
                    907: 	    push (@whichopt,$aopt);
                    908: 	}
                    909:     }
                    910:     for (my $i=0;$i<=$#toplist;$i++) {
                    911: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    912:     }
                    913:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    914: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    915:     }
                    916:     return @whichopt;
1.71      albertel  917: }
                    918: 
1.85      albertel  919: sub show_answer {
                    920:     my $part   = $Apache::inputtags::part;
                    921:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    922:     my $status = $Apache::inputtags::status[-1];
                    923:     return  ( ($award =~ /^correct/
                    924: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    925: 	      || $status eq "SHOW_ANSWER");
                    926: }
1.87      albertel  927: 
                    928: sub analyze_store_foilgroup {
                    929:     my ($shown,$attrs)=@_;
                    930:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    931:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    932: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    933: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    934: 	foreach my $attr (@$attrs) {
                    935: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    936: 		$Apache::response::foilgroup{"$name.".$attr};
                    937: 	}
                    938:     }
                    939:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel  940: }
                    941: 
                    942: sub check_if_computed {
                    943:     my ($token,$parstack,$safeeval,$name)=@_;
                    944:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106     matthew   945:     if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96      albertel  946: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    947: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                    948:     }
1.87      albertel  949: }
                    950: 
                    951: sub pick_foil_for_concept {
                    952:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    953:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    954:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    955:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    956:     my $name=$names[$pick];
                    957:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    958:     foreach my $attr (@$attrs) {
                    959: 	$Apache::response::foilgroup{"$name.".$attr} =
                    960: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    961:     }
                    962:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    963:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    964:     &Apache::lonxml::debug("Selecting $name in $concept");
                    965:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    966:     if ($target eq 'analyze') {
                    967: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    968: 	      $concept);
                    969: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    970: 	    $Apache::response::conceptgroup{'names'};
                    971: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    972: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    973: 		  $name);
                    974: 	    foreach my $attr (@$attrs) {
                    975: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    976: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    977: 	    }
                    978: 	}
                    979:     }
                    980:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                    981:     $hinthash->{"$part_id.concept.$concept"}=
                    982: 	$Apache::response::conceptgroup{'names'};
                    983: 
                    984: }
                    985: 
1.95      albertel  986: sub get_response_param {
                    987:     my ($id,$name,$default)=@_;
                    988:     my $parameter;
1.120     albertel  989:     if ($env{'request.state'} eq 'construct' &&
1.95      albertel  990: 	defined($Apache::inputtags::params{$name})) {
                    991: 	$parameter=$Apache::inputtags::params{$name};
                    992:     } else {
                    993: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                    994:     }
                    995:     if (!defined($parameter) ||	$parameter eq '') {
                    996: 	$parameter = $default;
                    997:     }
                    998:     return $parameter;
                    999: }
1.87      albertel 1000: 
1.113     albertel 1001: sub submitted {
                   1002:     my ($who)=@_;
                   1003:     
                   1004:     # when scatron grading any submission is a submission
1.120     albertel 1005:     if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113     albertel 1006:     # if the caller only cared if this was a scantron submission
                   1007:     if ($who eq 'scantron') { return 0; }
                   1008:     # if the Submit Answer button for this particular part was pressed
                   1009:     my $partid=$Apache::inputtags::part;
1.120     albertel 1010:     if (defined($env{'form.submit_'.$partid})) { return 1; }
1.122     albertel 1011:     # Submit All button on a .page was pressed
                   1012:     if (defined($env{'form.all_submit'})) { return 1; }
1.113     albertel 1013:     # otherwise no submission occured
                   1014:     return 0;
                   1015: }
1.117     albertel 1016: 
1.130     albertel 1017: sub add_to_gradingqueue {
1.149     albertel 1018:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.131     albertel 1019:     if (   $courseid eq ''
                   1020: 	|| $symb eq ''
1.135     albertel 1021: 	|| $env{'request.state'} eq 'construct'
                   1022: 	|| $Apache::lonhomework::type ne 'problem') {
1.131     albertel 1023: 	return;
                   1024:     }
1.130     albertel 1025: 
                   1026:     my %queue_info = ( 'type' => 'problem',
                   1027: 		       'time' => time);
                   1028: 
                   1029:     if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
                   1030: 	$queue_info{'slot'}=
                   1031: 	     $Apache::lonhomework::history{"resource.0.checkedin.slot"};
                   1032:     }
                   1033: 
                   1034:     my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
                   1035:     if ($result ne 'ok') {
                   1036: 	&Apache::lonxml::error("add_to_queue said $result");
                   1037:     }
                   1038: }
                   1039: 
1.117     albertel 1040: # basically undef and 0 (both false) mean that they still have work to do
                   1041: # and all true values mean that they can't do any more work
                   1042: #
                   1043: # a return of undef means it is unattempted
                   1044: # a return of 0 means it is attmpted and wrong but still has tries
                   1045: # a return of 1 means it is marked correct
                   1046: # a return of 2 means they have exceed maximum number of tries
                   1047: # a return of 3 means it after the answer date
                   1048: sub check_status {
                   1049:     my ($id)=@_;
1.143     albertel 1050:     if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117     albertel 1051:     my $curtime=&Apache::lonnet::EXT('system.time');
                   1052:     my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
                   1053:     my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
                   1054:     my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
                   1055:     if ( $opendate && $curtime > $opendate &&
                   1056:          $duedate && $curtime > $duedate &&
                   1057:          $answerdate && $curtime > $answerdate) {
                   1058:         return 3;
                   1059:     }
                   1060:     my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
                   1061:     if ($status =~ /^correct/) { return 1; }
                   1062:     if (!$status) { return undef; }
                   1063:     my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
                   1064:     if ($maxtries eq '') { $maxtries=2; }
                   1065:     my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
                   1066:     if ($curtries < $maxtries) { return 0; }
                   1067:     return 2;
                   1068: }
                   1069: 
1.1       albertel 1070: 1;
                   1071: __END__
1.38      albertel 1072:  

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