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

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

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