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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.188   ! www         4: # $Id: response.pm,v 1.187 2008/02/01 22:05:45 raeburn 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: 	}
                    395:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    396: 	&Apache::response::reset_params();
                    397:     } elsif ($target eq 'meta') {
1.129     albertel  398: 	$result .= &Apache::response::meta_package_write('customresponse');
1.128     albertel  399:     }
                    400:     return $result;
                    401: }
                    402: 
1.129     albertel  403: sub end_customresponse {
1.128     albertel  404:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    405:     my $result;
                    406:     my $part=$Apache::inputtags::part;
                    407:     my $id=$Apache::inputtags::response[-1];
                    408:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    409: 	my $response = &Apache::response::getresponse();
1.146     albertel  410: 	if ($Apache::lonhomework::type eq 'exam' ||
                    411: 	    &Apache::response::submitted('scantron')) {
                    412: 	    &Apache::response::scored_response($part,$id);
                    413: 	} elsif ( $response =~ /[^\s]/ && 
1.158     www       414: 		  $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
1.128     albertel  415: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    416: 		&Apache::lonxml::default_homework_load($safeeval);
                    417: 	    }
                    418: 	    my %previous = &Apache::response::check_for_previous($response,
                    419: 								 $part,$id);
                    420: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    421: 		$response;
                    422: 	    my $error;
1.129     albertel  423: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128     albertel  424: 		$response;
                    425: 	    
1.157     www       426: 	    my $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
1.128     albertel  427: 	    if (!&Apache::inputtags::valid_award($award)) {
                    428: 		$error = $award;
                    429: 		$award = 'ERROR';
                    430: 	    }
                    431: 	    &Apache::response::handle_previous(\%previous,$award);
                    432: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    433: 		$award;
                    434: 	    if ($error) {
                    435: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    436: 		    $error;
                    437: 	    }
                    438: 	}
1.146     albertel  439:     } elsif ( $target eq 'answer') {
                    440: 	$result  = &Apache::response::answer_header('customresponse');
                    441: 	my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    442: 						$safeeval);
                    443: 	if ($env{'form.answer_output_mode'} ne 'tex') {
                    444: 	    $answer = '<b>'.$answer.'</b>';
                    445: 	}
                    446: 	$result .= &Apache::response::answer_part('customresponse',$answer);
                    447: 	$result .= &Apache::response::answer_footer('customresponse');
                    448:     }
1.163     albertel  449:     if ($target eq 'web') {
                    450: 	&setup_prior_tries_hash(\&format_prior_response_math);
                    451:     }
1.146     albertel  452:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    453: 	$target eq 'tex' || $target eq 'analyze') {
1.179     foxr      454: 	&Apache::lonxml::increment_counter(&Apache::response::repetition(),
1.180     foxr      455: 					   "$part.$id");
1.179     foxr      456: 	if ($target eq 'analyze') {
1.187     raeburn   457:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'customresponse';
1.179     foxr      458: 	    &Apache::lonhomework::set_bubble_lines();
                    459: 	}
1.128     albertel  460:     }
                    461:     pop(@Apache::lonxml::namespace);
1.157     www       462:     pop(@Apache::response::custom_answer);
                    463:     pop(@Apache::response::custom_answer_type);
1.128     albertel  464:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    465:     &Apache::response::end_response();
                    466:     return $result;
                    467: }
                    468: 
1.163     albertel  469: sub format_prior_response_custom {
                    470:     my ($mode,$answer) =@_;
                    471:     return '<span class="LC_prior_custom">'.
                    472: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    473: }
1.140     www       474: 
1.139     www       475: sub start_mathresponse {
1.140     www       476:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    477:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    478:     push(@Apache::lonxml::namespace,'mathresponse');
1.139     www       479:     my $result;
1.157     www       480:     @Apache::response::custom_answer=();
                    481:     @Apache::response::custom_answer_type=();
1.140     www       482:     &Apache::lonxml::register('Apache::response',('answer'));
                    483:     if ($target eq 'web') {
                    484:   	if (  &Apache::response::show_answer() ) {
                    485: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    486: 						   $safeeval);
1.152     albertel  487: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.140     www       488: 	}
                    489:     } elsif ($target eq 'edit') {
                    490: 	$result.=&Apache::edit::tag_start($target,$token);
                    491: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    492: 					 'answerdisplay',$token);
1.150     www       493: 	$result.=&Apache::edit::select_arg('Algebra System:',
                    494: 					   'cas',
                    495: 					   ['maxima'],
                    496: 					   $token);
                    497: 	$result.=&Apache::edit::text_arg('Argument Array:',
                    498: 					 'args',$token);
1.140     www       499: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    500:     } elsif ($target eq 'modified') {
                    501: 	my $constructtag;
                    502: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
1.153     www       503: 						  $safeeval,'answerdisplay','cas','args');
1.140     www       504: 	if ($constructtag) {
                    505: 	    $result = &Apache::edit::rebuild_tag($token);
                    506: 	}
                    507:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    508: 	&Apache::response::reset_params();
                    509:     } elsif ($target eq 'meta') {
                    510: 	$result .= &Apache::response::meta_package_write('mathresponse');
1.139     www       511:     }
                    512:     return $result;
                    513: }
                    514: 
                    515: sub end_mathresponse {
1.140     www       516:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    517:     my $result;
                    518:     my $part=$Apache::inputtags::part;
                    519:     my $id=$Apache::inputtags::response[-1];
                    520:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    521: 	my $response = &Apache::response::getresponse();
1.150     www       522: 	if ( $response =~ /[^\s]/ ) {
1.140     www       523: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    524: 		&Apache::lonxml::default_homework_load($safeeval);
                    525: 	    }
                    526: 	    my %previous = &Apache::response::check_for_previous($response,
                    527: 								 $part,$id);
                    528: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    529: 		$response;
                    530: 	    my $error;
1.155     www       531: 	    my $award;
                    532: 	    my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
                    533:             if ($cas eq 'maxima') {
                    534:                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
1.157     www       535:                 $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
1.155     www       536:             }
1.140     www       537: 	    if (!&Apache::inputtags::valid_award($award)) {
1.151     albertel  538: 		$error = $award;
                    539: 		$award = 'ERROR';
1.140     www       540: 	    }
                    541: 	    &Apache::response::handle_previous(\%previous,$award);
                    542: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    543: 		$award;
                    544: 	    if ($error) {
                    545: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    546: 		    $error;
                    547: 	    }
                    548: 	}
                    549:     }
1.163     albertel  550:     if ($target eq 'web') {
                    551: 	&setup_prior_tries_hash(\&format_prior_response_math);
                    552:     }
                    553: 
1.140     www       554:     pop(@Apache::lonxml::namespace);
1.157     www       555:     pop(@Apache::response::custom_answer);
                    556:     pop(@Apache::response::custom_answer_type);
1.140     www       557:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    558:     &Apache::response::end_response();
                    559:     return $result;
1.139     www       560: }
                    561: 
1.163     albertel  562: sub format_prior_response_math {
                    563:     my ($mode,$answer) =@_;
                    564:     return '<span class="LC_prior_math">'.
                    565: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    566: }
                    567: 
1.156     www       568: sub implicit_multiplication {
                    569:     my ($expression)=@_;
                    570: # Escape scientific notation, so 3e8 does not become 3*e*8
                    571: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
                    572:     $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
                    573: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
                    574:     $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
                    575: # Fill in multiplication signs
                    576: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
1.176     albertel  577:     $expression=~s/([A-Za-z0-9])\s+(?=[A-Za-z0-9])/$1\*/gs;
1.156     www       578: # )( -> )*(; ) ( -> )*(
                    579:     $expression=~s/\)\s*\(/\)\*\(/gs;
                    580: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
                    581:     $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
                    582: # a ( -> a*(
1.174     riegler   583:     $expression=~s/([A-Za-z0-9])\s+\(/$1\*\(/gs;
1.156     www       584: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
1.174     riegler   585:     $expression=~s/\)\s*([A-Za-z0-9])/\)\*$1/gs;
1.156     www       586: # 3&8 -> 3e8; 3&-4 -> 3e-4
                    587:     $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
                    588:     return $expression;
                    589: }
1.140     www       590: 
1.128     albertel  591: sub start_answer {
                    592:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    593:     my $result;
1.157     www       594:     push(@Apache::response::custom_answer,
                    595: 	&Apache::lonxml::get_all_text_unbalanced("/answer",$parser));
                    596:     push(@Apache::response::custom_answer_type,
                    597: 	lc(&Apache::lonxml::get_param('type',$parstack,$safeeval)));
                    598:     $Apache::response::custom_answer_type[-1] =~ s/\s+//g;
1.128     albertel  599:     if ($target eq "edit" ) {
                    600: 	$result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
                    601: 	$result.=&Apache::edit::editfield($token->[1],
1.158     www       602: 					  $Apache::response::custom_answer[-1],
1.128     albertel  603: 					  '',80,4);
                    604:     } elsif ( $target eq "modified" ) {
                    605: 	$result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
                    606:     }
                    607:     return $result;
                    608: }
                    609: 
                    610: sub end_answer {
                    611:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    612:     if ($target eq 'edit' ) {
                    613: 	return &Apache::edit::end_table();
                    614:     }
                    615: }
                    616: 
1.83      albertel  617: sub decide_package {
                    618:     my ($tagstack)=@_;
                    619:     my $package;
                    620:     if ($$tagstack[-1] eq 'parameter') {
                    621: 	$package='part';
                    622:     } else {
                    623: 	my $i=-1;
                    624: 	while (defined($$tagstack[$i])) {
                    625: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    626: 		$package=$$tagstack[$i];
                    627: 		last;
                    628: 	    }
                    629: 	    $i--;
                    630: 	}
                    631:     }
                    632:     return $package;
                    633: }
                    634: 
1.3       albertel  635: sub start_responseparam {
1.73      albertel  636:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    637:     my $result='';
                    638:     if ($target eq 'meta') {
                    639: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    640: 					$token->[2]->{'type'},
                    641: 					$token->[2]->{'default'},
                    642: 					$token->[2]->{'description'});
                    643:     } elsif ($target eq 'edit') {
                    644: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  645: 	my $optionlist;
                    646: 	my $package=&decide_package($tagstack);
                    647: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    648: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    649: 		$optionlist.='<option value="'.$1.'">'.
                    650: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    651: 	    }
                    652: 	}
                    653: 	if (defined($optionlist)) {
                    654: 	    $result.='Use template: <select name="'.
                    655: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    656: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    657: 	}
1.73      albertel  658: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    659: 	    &Apache::edit::text_arg('Type:','type',$token).
                    660: 		&Apache::edit::text_arg('Description:','description',$token).
                    661: 		    &Apache::edit::text_arg('Default:','default',$token).
                    662: 			"</td></tr>";
                    663: 	$result.=&Apache::edit::end_table;
                    664:     } elsif ($target eq 'modified') {
1.83      albertel  665: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    666: 						     $safeeval,'name','type',
                    667: 						     'description','default');
                    668: 	my $element=&Apache::edit::html_element_name('parameter_package');
1.120     albertel  669: 	if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
                    670: 	    my $name=$env{"form.$element"};
1.83      albertel  671: 	    my $tag=&decide_package($tagstack);
                    672: 	    $token->[2]->{'name'}=$name;
                    673: 	    $token->[2]->{'type'}=
                    674: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    675: 	    $token->[2]->{'description'}=
                    676: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    677: 	    $token->[2]->{'default'}=
                    678: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
1.186     raeburn   679:             $token->[3] = ['name','type','description','default'];
1.83      albertel  680: 	    $constructtag=1;
                    681: 	}
1.73      albertel  682: 	if ($constructtag) {
                    683: 	    $result = &Apache::edit::rebuild_tag($token);
                    684: 	}
                    685:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    686: 	     $target eq 'tex' || $target eq 'analyze' ) {
1.120     albertel  687: 	if ($env{'request.state'} eq 'construct') {
1.73      albertel  688: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    689: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    690: 						     $safeeval);
                    691: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    692: 	}
1.52      albertel  693:     }
1.73      albertel  694:     return $result;
1.3       albertel  695: }
                    696: 
                    697: sub end_responseparam {
1.73      albertel  698:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    699:     if ($target eq 'edit') { return ('','no'); }
                    700:     return '';
1.55      albertel  701: }
                    702: 
                    703: sub start_parameter {
1.114     albertel  704:     return &start_responseparam(@_);
1.55      albertel  705: }
                    706: 
                    707: sub end_parameter {
1.114     albertel  708:     return &end_responseparam(@_);
1.42      albertel  709: }
                    710: 
1.67      albertel  711: sub reset_params {
                    712:     %Apache::inputtags::params=();
                    713: }
                    714: 
1.42      albertel  715: sub setup_params {
1.94      albertel  716:     my ($tag,$safeeval) = @_;
1.42      albertel  717: 
1.120     albertel  718:     if ($env{'request.state'} eq 'construct') { return; }
1.73      albertel  719:     my %paramlist=();
                    720:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
1.161     albertel  721: 	if ($key =~ /^\Q$tag\E/) {
1.73      albertel  722: 	    my ($package,$name) = split(/&/,$key);
                    723: 	    $paramlist{$name}=1;
                    724: 	}
1.42      albertel  725:     }
1.73      albertel  726:     foreach my $key (keys(%paramlist)) {
                    727: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    728: 	if (defined($Apache::inputtags::response[-1])) {
                    729: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    730: 	}
                    731: 	$entry.='.'.$key;
                    732: 	&Apache::lonxml::debug("looking for $entry");
                    733: 	my $value = &Apache::lonnet::EXT("$entry");
                    734: 	&Apache::lonxml::debug("$key has value :$value:");
                    735: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    736: 	    &Apache::lonxml::debug("using nothing");
                    737: 	    $Apache::inputtags::params{$key}='';
                    738: 	} else {
1.94      albertel  739: 	    my $string="{return qq\0".$value."\0}";
                    740: 	    my $newvalue=&Apache::run::run($string,$safeeval,1);
                    741: 	    if (defined($newvalue)) { $value=$newvalue; }
1.73      albertel  742: 	    $Apache::inputtags::params{$key}=$value;
                    743: 	}
1.42      albertel  744:     }
1.48      albertel  745: }
                    746: 
1.132     albertel  747: {
                    748:     my @answer_bits;
1.147     albertel  749:     my $need_row_start;
1.132     albertel  750: 
1.48      albertel  751: sub answer_header {
1.147     albertel  752:     my ($type,$increment,$rows) = @_;
1.73      albertel  753:     my $result;
1.120     albertel  754:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.132     albertel  755: 	undef(@answer_bits);
1.133     albertel  756: 	my $bit;
                    757: 	if ($Apache::lonhomework::type eq 'exam') {
                    758: 	    $bit = ($Apache::lonxml::counter+$increment).') ';
                    759: 	} else {
                    760: 	    $bit .= ' Answer for Part: \verb|'.
                    761: 		$Apache::inputtags::part.'| ';
                    762: 	}
                    763: 	push(@answer_bits,$bit);
1.73      albertel  764:     } else {
1.147     albertel  765: 	my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
1.132     albertel  766: 	$result  = '<table border="1"><tr>';
                    767: 	if ($Apache::lonhomework::type eq 'exam') {
1.147     albertel  768: 	    $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
1.132     albertel  769: 	} else {
1.147     albertel  770: 	    $result .= $td.&mt('Answer for Part: [_1]',
                    771: 			       $Apache::inputtags::part).'</td>';
1.132     albertel  772: 	}
                    773: 	$result .= "\n";
1.147     albertel  774: 	$need_row_start = 0;
                    775:     }
                    776:     return $result;
                    777: }
                    778: 
                    779: sub next_answer {
                    780:     my ($type) = @_;
                    781:     my $result;
                    782:     if ($env{'form.answer_output_mode'} eq 'tex') {
                    783: 	# FIXME ... need to do something with tex mode
                    784:     } else {
                    785: 	$result .= "</tr>";
                    786: 	$need_row_start = 1;
1.73      albertel  787:     }
                    788:     return $result;
1.48      albertel  789: }
                    790: 
                    791: sub answer_part {
1.148     albertel  792:     my ($type,$answer,$args) = @_;
1.188   ! www       793:     &Apache::lonxml::debug("In answerpart :$type:$answer:$args:");
        !           794: 
1.73      albertel  795:     my $result;
1.120     albertel  796:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.148     albertel  797: 	if (!$args->{'no_verbatim'}) {
                    798: 	    my $to_use='|';
                    799: 	    foreach my $value (32..126) {
                    800: 		my $char=pack('c',$value);
                    801: 		if ($answer !~ /\Q$char\E/) {
                    802: 		    $to_use=$char;
                    803: 		    last;
                    804: 		}
                    805: 	    }
1.188   ! www       806:             my $answerelements=$answer;
        !           807:             $answer='';
        !           808:             foreach my $element (split(/[\;\:]/,$answerelements)) {
        !           809: 	        if ($element ne '') {
        !           810: 		    $answer.= '\noindent\verb'.$to_use.$element.$to_use.'\newline';
        !           811: 	        }
        !           812:             }
1.124     albertel  813: 	}
1.132     albertel  814: 	if ($answer ne '') {
1.148     albertel  815: 	    push(@answer_bits,$answer);
1.132     albertel  816: 	}
1.73      albertel  817:     } else {
1.147     albertel  818: 	if ($need_row_start) {
                    819: 	    $result .= '<tr>';
                    820: 	    $need_row_start = 0;
                    821: 	}
1.188   ! www       822:         $answer=~s/\; /\<\/nobr\>\<br \/\>\<nobr\>/gs;
        !           823: 	$result .= '<td><nobr>'.$answer.'</nobr></td>';
1.73      albertel  824:     }
                    825:     return $result;
1.48      albertel  826: }
                    827: 
                    828: sub answer_footer {
1.73      albertel  829:     my ($type) = @_;
                    830:     my $result;
1.120     albertel  831:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.188   ! www       832: 	my $columns = scalar(@answer_bits)-1;
        !           833: 	$result  = ' \vskip 0 mm \noindent \begin{tabular}{|p{1.5cm}|'.'p{3cm}|'x$columns.'}\hline ';
1.133     albertel  834: 	$result .= join(' & ',@answer_bits);
1.132     albertel  835: 	$result .= ' \\\\ \\hline \end{tabular} \vskip 0 mm ';
1.73      albertel  836:     } else {
1.185     albertel  837: 	if (!$need_row_start) {
                    838: 	    $result .= '</tr>';
                    839: 	}
                    840: 	$result .= '</table>';
1.73      albertel  841:     }
                    842:     return $result;
1.1       albertel  843: }
1.2       albertel  844: 
1.132     albertel  845: }
                    846: 
1.62      albertel  847: sub showallfoils {
1.120     albertel  848:     if (defined($env{'form.showallfoils'})) {
1.149     albertel  849: 	my ($symb)=&Apache::lonnet::whichuser();
1.120     albertel  850: 	if (($env{'request.state'} eq 'construct') || 
                    851: 	    ($env{'user.adv'} && $symb eq '')      ||
1.118     foxr      852:             ($Apache::lonhomework::viewgrades) ) {
1.102     albertel  853: 	    return 1;
                    854: 	}
1.73      albertel  855:     }
1.108     albertel  856:     if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.102     albertel  857:     return 0;
1.70      albertel  858: }
                    859: 
1.168     albertel  860: =pod
                    861: 
                    862: =item &getresponse($offset,$resulttype);
                    863: 
                    864: Retreives the current submitted response, helps out in the case of
                    865: scantron mode.
                    866: 
                    867: Returns either the exact text of the submission, or a bubbled response
                    868: converted to something usable.
                    869: 
                    870: Optional Arguments:
1.171     albertel  871:   $offset - (defaults to 1) if a problem has more than one bubble
                    872:             response, pass in the number of the bubble wanted, (the
                    873:             first bubble associated with a problem has an offset of 1,
                    874:             the second bubble is 2
                    875: 
1.168     albertel  876:   $resulttype - undef    -> a number between 0 and 25
                    877:                 'A is 1' -> a number between 1 and 26
                    878:                 'letter' -> a letter between 'A' and 'Z'
1.172     foxr      879:   $lines  - undef problem only needs a single line of bubbles.
                    880:             nonzero  Problem wants the first nonempty response in 
                    881:                       $lines lines of bubbles.
                    882:   $bubbles_per_line - Must be provided if lines is defined.. number of
                    883:                       bubbles on a line.
1.173     albertel  884: 
1.168     albertel  885: =cut
                    886: 
1.70      albertel  887: sub getresponse {
1.172     foxr      888:     my ($offset,$resulttype, $lines, $bubbles_per_line)=@_;
1.70      albertel  889:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    890:     my $response;
1.168     albertel  891:     if (!defined($offset)) {
1.170     albertel  892: 	$offset=1;
1.70      albertel  893:     } else {
1.168     albertel  894: 	$formparm.=":$offset";
1.70      albertel  895:     }
1.172     foxr      896:     if (!defined($lines)) {
                    897: 	$lines = 1;
                    898:     }
1.70      albertel  899:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    900: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    901: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    902: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120     albertel  903:     if ($env{'form.submitted'} eq 'scantron') {
1.71      albertel  904: 	my $part  = $Apache::inputtags::part;
                    905: 	my $id    = $Apache::inputtags::response[-1];
1.172     foxr      906: 	
                    907: 	my $line;
                    908: 	for ($line = 0; $line < $lines; $line++) {
1.184     foxr      909: 	    my $theline = $Apache::lonxml::counter+$offset-1+$line;
                    910: 	    $response = $env{"scantron.$theline.answer"};
                    911: 	    if ((defined($response)) && ($response ne "") && ($response ne " ")) {
1.172     foxr      912: 		last;
                    913: 	    }
                    914: 	    
                    915: 	}
1.178     albertel  916: 
                    917: 	# save bubbled letter for later
                    918: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    919: 	    $response;
1.90      albertel  920: 	if ($resulttype ne 'letter') {
1.104     albertel  921: 	    if ($resulttype eq 'A is 1') {
1.105     albertel  922: 		$response = $let_to_num{$response}+1;
                    923: 	    } else {
1.104     albertel  924: 		$response = $let_to_num{$response};
                    925: 	    }
1.172     foxr      926: 	    if ($response ne "") {
                    927: 		$response += $line * $bubbles_per_line;
                    928: 	    }
                    929: 	} else {
                    930: 	    if ($response ne "") {
                    931: 		$response = chr(ord($response) + $line * $bubbles_per_line);
                    932: 	    }
1.90      albertel  933: 	}
1.172     foxr      934: 
1.70      albertel  935:     } else {
1.120     albertel  936: 	$response = $env{$formparm};
1.70      albertel  937:     }
1.172     foxr      938:     # 
                    939:     #  If we have a nonempty answer, correct the numeric value
                    940:     #  of the answer for the line on which it was found.
                    941:     #
                    942: 
1.70      albertel  943:     return $response;
1.62      albertel  944: }
1.71      albertel  945: 
1.168     albertel  946: =pod
                    947: 
                    948: =item &repetition();
                    949: 
                    950: Returns the number of lines that are required to encode the weight.
                    951: (Currently expects that there are 10 bubbles per line)
                    952: 
                    953: =cut
                    954: 
1.71      albertel  955: sub repetition {
                    956:     my $id = $Apache::inputtags::part;
                    957:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121     albertel  958:     if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.125     albertel  959:     my $repetition = int($weight/10);
                    960:     if ($weight % 10 != 0) { $repetition++; } 
1.72      albertel  961:     return $repetition;
                    962: }
                    963: 
1.168     albertel  964: =pod
                    965: 
                    966: =item &scored_response($part_id,$response_id);
                    967: 
                    968: Sets the results hash elements
                    969: 
                    970:    resource.$part_id.$response_id.awarded - to the floating point
                    971:      number between 0 and 1 that was awarded on the bubbled input
                    972: 
                    973:    resource.$part_id.$response_id.awarddetail - to 'ASSIGNED_SCORE'
                    974: 
                    975: Returns
                    976: 
                    977:    the number of bubble sheet lines that were used (and likely need to
                    978:      be passed to &Apache::lonxml::increment_counter()
                    979: 
                    980: Arguments
                    981: 
                    982:    $part_id - id of the part to grade
                    983:    $response_id - id of the response to grade
                    984:   
                    985: 
                    986: =cut
                    987: 
1.72      albertel  988: sub scored_response {
                    989:     my ($part,$id)=@_;
                    990:     my $repetition=&repetition();
                    991:     my $score=0;
                    992:     for (my $i=0;$i<$repetition;$i++) {
1.125     albertel  993: 	# A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
1.72      albertel  994: 	my $increase=&Apache::response::getresponse($i+1);
                    995: 	if ($increase ne '') { $score+=$increase+1; }
                    996:     }
                    997:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  998:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  999:     my $pcr=$score/$weight;
                   1000:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                   1001:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                   1002: 	'ASSIGNED_SCORE';
1.71      albertel 1003:     return $repetition;
1.78      albertel 1004: }
                   1005: 
                   1006: sub whichorder {
                   1007:     my ($max,$randomize,$showall,$hash)=@_;
                   1008:     #&Apache::lonxml::debug("man $max randomize $randomize");
                   1009:     if (!defined(@{ $$hash{'names'} })) { return; }
                   1010:     my @names = @{ $$hash{'names'} };
                   1011:     my @whichopt =();
                   1012:     my (%top,@toplist,%bottom,@bottomlist);
                   1013:     if (!($showall || ($randomize eq 'no'))) {
                   1014: 	my $current=0;
                   1015: 	foreach my $name (@names) {
                   1016: 	    $current++;
                   1017: 	    if ($$hash{"$name.location"} eq 'top') {
                   1018: 		$top{$name}=$current;
                   1019: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                   1020: 		$bottom{$name}=$current;
                   1021: 	    }
                   1022: 	}
                   1023:     }
                   1024:     my $topcount=0;
                   1025:     my $bottomcount=0;
                   1026:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                   1027: 	   && ($#names > -1)) {
                   1028: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                   1029: 	my $aopt;
                   1030: 	if ($showall || ($randomize eq 'no')) {
                   1031: 	    $aopt=0;
                   1032: 	} else {
                   1033: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                   1034: 	}
                   1035: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                   1036: 	$aopt=splice(@names,$aopt,1);
                   1037: 	#&Apache::lonxml::debug("Picked $aopt");
                   1038: 	if ($top{$aopt}) {
                   1039: 	    $toplist[$top{$aopt}]=$aopt;
                   1040: 	    $topcount++;
                   1041: 	} elsif ($bottom{$aopt}) {
                   1042: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                   1043: 	    $bottomcount++;
                   1044: 	} else {
                   1045: 	    push (@whichopt,$aopt);
                   1046: 	}
                   1047:     }
                   1048:     for (my $i=0;$i<=$#toplist;$i++) {
                   1049: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                   1050:     }
                   1051:     for (my $i=0;$i<=$#bottomlist;$i++) {
                   1052: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                   1053:     }
                   1054:     return @whichopt;
1.71      albertel 1055: }
                   1056: 
1.85      albertel 1057: sub show_answer {
                   1058:     my $part   = $Apache::inputtags::part;
                   1059:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                   1060:     my $status = $Apache::inputtags::status[-1];
                   1061:     return  ( ($award =~ /^correct/
1.181     albertel 1062: 	       && &Apache::lonhomework::show_problem_status())
1.85      albertel 1063: 	      || $status eq "SHOW_ANSWER");
                   1064: }
1.87      albertel 1065: 
                   1066: sub analyze_store_foilgroup {
                   1067:     my ($shown,$attrs)=@_;
                   1068:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1069:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                   1070: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                   1071: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                   1072: 	foreach my $attr (@$attrs) {
                   1073: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                   1074: 		$Apache::response::foilgroup{"$name.".$attr};
                   1075: 	}
                   1076:     }
                   1077:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel 1078: }
                   1079: 
                   1080: sub check_if_computed {
                   1081:     my ($token,$parstack,$safeeval,$name)=@_;
                   1082:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106     matthew  1083:     if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96      albertel 1084: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1085: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                   1086:     }
1.87      albertel 1087: }
                   1088: 
                   1089: sub pick_foil_for_concept {
                   1090:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                   1091:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                   1092:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                   1093:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                   1094:     my $name=$names[$pick];
                   1095:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                   1096:     foreach my $attr (@$attrs) {
                   1097: 	$Apache::response::foilgroup{"$name.".$attr} =
                   1098: 	    $Apache::response::conceptgroup{"$name.".$attr};
                   1099:     }
                   1100:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                   1101:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                   1102:     &Apache::lonxml::debug("Selecting $name in $concept");
                   1103:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1104:     if ($target eq 'analyze') {
                   1105: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                   1106: 	      $concept);
                   1107: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                   1108: 	    $Apache::response::conceptgroup{'names'};
                   1109: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                   1110: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                   1111: 		  $name);
                   1112: 	    foreach my $attr (@$attrs) {
                   1113: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                   1114: 		    $Apache::response::conceptgroup{"$name.$attr"};
                   1115: 	    }
                   1116: 	}
                   1117:     }
                   1118:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                   1119:     $hinthash->{"$part_id.concept.$concept"}=
                   1120: 	$Apache::response::conceptgroup{'names'};
                   1121: 
                   1122: }
1.164     foxr     1123: #------------------------------------------------------------
                   1124: #
                   1125: #  Get a parameter associated with a problem.
                   1126: # Parameters:
1.166     albertel 1127: #  $id        - the id of the paramater, either a part id, 
                   1128: #               or a partid and responspe id joined by _
1.164     foxr     1129: #  $name      - Name of the parameter to fetch
                   1130: #  $default   - Default value for the paramter.
                   1131: #
                   1132: #  
                   1133: #
1.95      albertel 1134: sub get_response_param {
                   1135:     my ($id,$name,$default)=@_;
                   1136:     my $parameter;
1.120     albertel 1137:     if ($env{'request.state'} eq 'construct' &&
1.95      albertel 1138: 	defined($Apache::inputtags::params{$name})) {
                   1139: 	$parameter=$Apache::inputtags::params{$name};
                   1140:     } else {
                   1141: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                   1142:     }
                   1143:     if (!defined($parameter) ||	$parameter eq '') {
                   1144: 	$parameter = $default;
                   1145:     }
                   1146:     return $parameter;
                   1147: }
1.87      albertel 1148: 
1.113     albertel 1149: sub submitted {
                   1150:     my ($who)=@_;
                   1151:     
                   1152:     # when scatron grading any submission is a submission
1.120     albertel 1153:     if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113     albertel 1154:     # if the caller only cared if this was a scantron submission
                   1155:     if ($who eq 'scantron') { return 0; }
                   1156:     # if the Submit Answer button for this particular part was pressed
                   1157:     my $partid=$Apache::inputtags::part;
1.159     albertel 1158:     if ($env{'form.submitted'} eq "part_$partid") {
                   1159: 	return 1;
                   1160:     }
                   1161:     if ($env{'form.submitted'} eq "yes"
                   1162: 	&& defined($env{'form.submit_'.$partid})) {
                   1163: 	return 1;
                   1164:     }
1.122     albertel 1165:     # Submit All button on a .page was pressed
                   1166:     if (defined($env{'form.all_submit'})) { return 1; }
1.113     albertel 1167:     # otherwise no submission occured
                   1168:     return 0;
                   1169: }
1.117     albertel 1170: 
1.130     albertel 1171: sub add_to_gradingqueue {
1.149     albertel 1172:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.131     albertel 1173:     if (   $courseid eq ''
                   1174: 	|| $symb eq ''
1.135     albertel 1175: 	|| $env{'request.state'} eq 'construct'
                   1176: 	|| $Apache::lonhomework::type ne 'problem') {
1.131     albertel 1177: 	return;
                   1178:     }
1.130     albertel 1179: 
                   1180:     my %queue_info = ( 'type' => 'problem',
                   1181: 		       'time' => time);
                   1182: 
                   1183:     if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
                   1184: 	$queue_info{'slot'}=
                   1185: 	     $Apache::lonhomework::history{"resource.0.checkedin.slot"};
                   1186:     }
                   1187: 
                   1188:     my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
                   1189:     if ($result ne 'ok') {
                   1190: 	&Apache::lonxml::error("add_to_queue said $result");
                   1191:     }
                   1192: }
                   1193: 
1.117     albertel 1194: # basically undef and 0 (both false) mean that they still have work to do
                   1195: # and all true values mean that they can't do any more work
                   1196: #
                   1197: # a return of undef means it is unattempted
                   1198: # a return of 0 means it is attmpted and wrong but still has tries
                   1199: # a return of 1 means it is marked correct
                   1200: # a return of 2 means they have exceed maximum number of tries
                   1201: # a return of 3 means it after the answer date
                   1202: sub check_status {
                   1203:     my ($id)=@_;
1.143     albertel 1204:     if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117     albertel 1205:     my $curtime=&Apache::lonnet::EXT('system.time');
                   1206:     my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
                   1207:     my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
                   1208:     my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
                   1209:     if ( $opendate && $curtime > $opendate &&
                   1210:          $duedate && $curtime > $duedate &&
                   1211:          $answerdate && $curtime > $answerdate) {
                   1212:         return 3;
                   1213:     }
                   1214:     my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
                   1215:     if ($status =~ /^correct/) { return 1; }
                   1216:     if (!$status) { return undef; }
                   1217:     my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
                   1218:     if ($maxtries eq '') { $maxtries=2; }
                   1219:     my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
                   1220:     if ($curtries < $maxtries) { return 0; }
                   1221:     return 2;
                   1222: }
                   1223: 
1.177     albertel 1224: =pod
                   1225: 
                   1226: =item setup_prior_tries_hash($func,$data)
                   1227: 
                   1228:   Foreach each past .submission $func is called with 3 arguments
                   1229:      - the mode to set things up for (currently always 'grade')
                   1230:      - the stored .submission string
                   1231:      - The expansion of $data
                   1232: 
                   1233:   $data is an array ref containing elements that are either
                   1234:     - scalars that are other elements of the history hash to pass to $func
                   1235:     - ref to data to be passed untouched to $func
                   1236: 
                   1237: =cut
                   1238: 
1.162     albertel 1239: sub setup_prior_tries_hash {
                   1240:     my ($func,$data) = @_;
                   1241:     my $part = $Apache::inputtags::part;
                   1242:     my $id   = $Apache::inputtags::response[-1];	
                   1243:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                   1244: 	my $sub_key   = "$i:resource.$part.$id.submission";
                   1245: 	next if (!exists($Apache::lonhomework::history{$sub_key}));
                   1246: 	my @other_data;
                   1247: 	foreach my $datum (@{ $data }) {
                   1248: 	    if (ref($datum)) {
                   1249: 		push(@other_data,$datum);
                   1250: 	    } else {
                   1251: 		my $info_key = "$i:resource.$part.$id.$datum";
                   1252: 		push(@other_data,$Apache::lonhomework::history{$info_key});
                   1253: 	    }
                   1254: 	}
                   1255: 
                   1256: 	my $output =
                   1257: 	    &$func('grade',
                   1258: 		   $Apache::lonhomework::history{$sub_key},
                   1259: 		   \@other_data);
                   1260: 	if (defined($output)) {
                   1261: 	    $Apache::inputtags::submission_display{$sub_key} = $output;
                   1262: 	}
                   1263:     }
                   1264: }
                   1265: 
1.1       albertel 1266: 1;
                   1267: __END__
1.38      albertel 1268:  

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