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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.239   ! damieng     4: # $Id: response.pm,v 1.238 2014/12/30 20:03:10 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.208     jms        29: =pod
                     30: 
                     31: =head1 NAME
                     32: 
1.218     raeburn    33: Apache::response.pm
1.208     jms        34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: This is part of the LearningOnline Network with CAPA project
                     38: described at http://www.lon-capa.org.
                     39: 
                     40: 
                     41: =head1 NOTABLE SUBROUTINES
                     42: 
                     43: =over
                     44: 
                     45: =item 
                     46: 
                     47: =back
                     48: 
                     49: =cut
                     50: 
                     51: 
1.1       albertel   52: package Apache::response;
                     53: use strict;
1.93      albertel   54: use Apache::lonlocal;
1.120     albertel   55: use Apache::lonnet;
1.228     raeburn    56: use Apache::inputtags();
1.153     www        57: use Apache::lonmaxima();
1.214     www        58: use Apache::lonr();
1.238     raeburn    59: use Apache::lontexconvert();
1.1       albertel   60: 
1.57      harris41   61: BEGIN {
1.139     www        62:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse'));
1.1       albertel   63: }
                     64: 
1.13      albertel   65: sub start_response {
1.73      albertel   66:     my ($parstack,$safeeval)=@_;
1.136     albertel   67:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.73      albertel   68:     if ($#Apache::inputtags::import > -1) {
                     69: 	&Apache::lonxml::debug("Turning :$id: into");
                     70: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
                     71: 	&Apache::lonxml::debug("New  :$id:");
                     72:     }
                     73:     push (@Apache::inputtags::response,$id);
                     74:     push (@Apache::inputtags::responselist,$id);
                     75:     @Apache::inputtags::inputlist=();
1.101     albertel   76:     if ($Apache::inputtags::part eq '' && 
                     77: 	!$Apache::lonhomework::ignore_response_errors) {
1.97      albertel   78: 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
1.92      albertel   79:     }
                     80:     if ($Apache::inputtags::response_with_no_part &&
                     81: 	$Apache::inputtags::part ne '0') {
1.97      albertel   82: 	&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   83:     }
                     84:     if ($Apache::inputtags::part eq '0') {
                     85: 	$Apache::inputtags::response_with_no_part=1;
                     86:     }
1.73      albertel   87:     return $id;
1.13      albertel   88: }
                     89: 
                     90: sub end_response {
1.79      albertel   91:     #pop @Apache::inputtags::response;
1.73      albertel   92:     @Apache::inputtags::inputlist=();
                     93:     return '';
1.13      albertel   94: }
                     95: 
1.41      albertel   96: sub start_hintresponse {
1.73      albertel   97:     my ($parstack,$safeeval)=@_;
1.136     albertel   98:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.123     albertel   99:     push (@Apache::inputtags::hint,$id);
                    100:     push (@Apache::inputtags::hintlist,$id);
1.73      albertel  101:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
                    102:     return $id;
1.41      albertel  103: }
                    104: 
                    105: sub end_hintresponse {
1.123     albertel  106:     pop @Apache::inputtags::hint;
1.73      albertel  107:     if (defined($Apache::inputtags::paramstack[-1])) {
                    108: 	%Apache::inputtags::params=
                    109: 	    @{ pop(@Apache::inputtags::paramstack) };
                    110:     }
                    111:     return '';
1.41      albertel  112: }
                    113: 
1.99      albertel  114: my @randomseeds;
                    115: sub pushrandomnumber {
                    116:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                    117:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    118: 	$rand_alg eq '64bit2') {
                    119: 	# do nothing
                    120:     } else {
                    121: 	my @seed=&Math::Random::random_get_seed();
1.127     albertel  122: 	push(@randomseeds,\@seed);
1.99      albertel  123:     }
1.127     albertel  124:     &Apache::response::setrandomnumber(@_);
1.99      albertel  125: }
                    126: sub poprandomnumber {
                    127:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                    128:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    129: 	$rand_alg eq '64bit2') {
                    130: 	return;
                    131:     }
                    132:     my $seed=pop(@randomseeds);
                    133:     if ($seed) {
                    134: 	&Math::Random::random_set_seed(@$seed);
                    135:     } else {
                    136: 	&Apache::lonxml::error("Unable to restore random algorithm.");
                    137:     }
                    138: }
1.117     albertel  139: 
1.26      albertel  140: sub setrandomnumber {
1.221     raeburn   141:     my ($ignore_id2,$target,$rndseed) = @_;
                    142:     if (!defined($rndseed)) {
                    143:         $rndseed=&Apache::structuretags::setup_rndseed(undef,$target);
                    144:     } 
1.88      albertel  145:     if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73      albertel  146:     &Apache::lonxml::debug("randseed $rndseed");
                    147:     #  $rndseed=unpack("%32i",$rndseed);
1.99      albertel  148:     my $rand_alg=&Apache::lonnet::get_rand_alg();
1.126     albertel  149:     my ($rndmod,$rndmod2);
1.119     albertel  150: 
                    151:     my ($id1,$id2,$shift_amt);
                    152:     if ($Apache::lonhomework::parsing_a_problem) {
                    153: 	$id1=$Apache::inputtags::part;
                    154: 	if (defined($Apache::inputtags::response[-1])) {
                    155: 	    $id2=$Apache::inputtags::response[-1];
                    156: 	}
                    157: 	$shift_amt=scalar(@Apache::inputtags::responselist);
                    158:     } elsif ($Apache::lonhomework::parsing_a_task) {
1.141     albertel  159: 	$id1=&Apache::bridgetask::get_dim_id();
                    160: 	if (!$ignore_id2 && ref($Apache::bridgetask::instance{$id1})) {
                    161: 	    $id2=$Apache::bridgetask::instance{$id1}[-1];
1.142     albertel  162: 	    $shift_amt=scalar(@{$Apache::bridgetask::instance{$id1}});
                    163: 	} else {
                    164: 	    $shift_amt=0;
1.119     albertel  165: 	}
                    166:     } 
                    167:     &Apache::lonxml::debug("id1: $id1, id2: $id2, shift_amt: $shift_amt");
1.99      albertel  168:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    169: 	$rand_alg eq '64bit2') {
1.119     albertel  170: 	$rndmod=(&Apache::lonnet::numval($id1) << 10);
                    171: 	if (defined($id2)) { $rndmod+=&Apache::lonnet::numval($id2); }
1.110     albertel  172:     } elsif ($rand_alg eq '64bit3') {
1.119     albertel  173: 	$rndmod=(&Apache::lonnet::numval2($id1) << 10);
                    174: 	if (defined($id2)) { $rndmod+=&Apache::lonnet::numval2($id2); }
1.126     albertel  175:     } elsif ($rand_alg eq '64bit4') {
1.119     albertel  176: 	my $shift=(4*$shift_amt)%30;
                    177: 	$rndmod=(&Apache::lonnet::numval3($id1) << (($shift+15)%30));
                    178: 	if (defined($id2)) {
                    179: 	    $rndmod+=(&Apache::lonnet::numval3($id2) << $shift );
1.110     albertel  180: 	}
1.126     albertel  181:     } else {
                    182: 	($rndmod,$rndmod2)=&Apache::lonnet::digest("$id1,$id2");
1.99      albertel  183:     }
1.224     www       184:     $Apache::lonhomework::results{'resource.'.$id1.'.rawrndseed'}=$rndseed;
1.99      albertel  185:     if ($rndseed =~/([,:])/) {
                    186: 	my $char=$1;
                    187: 	use integer;
                    188: 	my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
                    189: 	$num1+=$rndmod;
1.126     albertel  190: 	$num2+= ((defined($rndmod2)) ? $rndmod2 : $rndmod);
1.109     albertel  191: 	if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99      albertel  192: 	$rndseed=$num1.$char.$num2;
                    193:     } else {
1.74      albertel  194: 	$rndseed+=$rndmod;
1.109     albertel  195: 	if($Apache::lonnet::_64bit) {
                    196: 	    use integer;
                    197: 	    $rndseed=(($rndseed<<32)>>32);
                    198: 	}
1.74      albertel  199:     }
1.111     albertel  200:     &Apache::lonxml::debug("randseed $rndmod $rndseed");
1.223     www       201:     $Apache::lonhomework::results{'resource.'.$id1.'.rndseed'}=$rndseed;
1.74      albertel  202:     &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73      albertel  203:     return '';
1.26      albertel  204: }
                    205: 
1.7       www       206: sub meta_parameter_write {
1.38      albertel  207:     my ($name,$type,$default,$display)=@_;
1.41      albertel  208:     my $partref=$Apache::inputtags::part;
                    209:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                    210:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  211: 	$result.=            ' id="'.$Apache::inputtags::response[-1].'"';
                    212: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  213:     }
                    214:     $result.=            ' name="'.$name.'"'.
                    215:                          ' type="'.$type.'"'.
1.89      albertel  216: (defined($default)?' default="'.$default.'"':'').
                    217: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41      albertel  218:              .'></parameter>'
                    219:              ."\n";
                    220:     return $result;
1.33      www       221: }
                    222: 
                    223: sub meta_package_write {
                    224:     my $name=shift;
1.41      albertel  225:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    226:     if(defined($Apache::inputtags::response[-1])) {
1.73      albertel  227: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41      albertel  228:     }
                    229:     $result.=' package="'.$name.'"></parameter>'."\n";
                    230:     return $result;
1.7       www       231: }
                    232: 
                    233: sub meta_stores_write {
1.10      www       234:     my ($name,$type,$display)=@_;
1.41      albertel  235:     my $partref=$Apache::inputtags::part;
                    236:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    237:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  238: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                    239: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  240:     }	
                    241:     $result.=          ' name="'.$name.'"'.
                    242:                        ' type="'.$type.'"'.
                    243: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    244: 		      "></stores>\n";
1.7       www       245: }
                    246: 
1.207     jms       247: =pod
                    248: 
1.210     raeburn   249: =item mandatory_part_meta()
1.207     jms       250: 
                    251: Autogenerate metadata for mandatory
1.210     raeburn   252: input (from RAT or lonparmset) and
1.207     jms       253: output (to lonspreadsheet)
                    254: of each part
                    255: 
                    256: Note: responseid-specific data 'submission' and 'awarddetail'
                    257: not available to spreadsheet -> skip here
                    258: 
                    259: =cut
                    260: 
1.210     raeburn   261: 
                    262: sub mandatory_part_meta {
                    263:     return &meta_package_write('part').
                    264:            &meta_stores_write('solved','string','Problem Status').
                    265:            &meta_stores_write('tries','int_zeropos','Number of Attempts').
                    266:            &meta_stores_write('awarded','float','Partial Credit Factor');
1.86      albertel  267: }
                    268: 
                    269: sub meta_part_order {
                    270:     if (@Apache::inputtags::partlist) {
                    271: 	my @parts=@Apache::inputtags::partlist;
                    272: 	shift(@parts);
1.100     albertel  273: 	return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.86      albertel  274:     } else {
1.100     albertel  275: 	return '<partorder>0</partorder>'."\n";
                    276:     }
                    277: }
                    278: 
                    279: sub meta_response_order {
                    280:     if (@Apache::inputtags::responselist) {
                    281: 	return '<responseorder>'.join(',',@Apache::inputtags::responselist).
                    282: 	    '</responseorder>'."\n";
1.86      albertel  283:     }
1.14      albertel  284: }
                    285: 
1.15      albertel  286: sub check_for_previous {
1.219     raeburn   287:     my ($curresponse,$partid,$id,$last,$type) = @_;
1.73      albertel  288:     my %previous;
                    289:     $previous{'used'} = 0;
1.221     raeburn   290:     my $questiontype = $Apache::lonhomework::type;
                    291:     my $curr_rndseed = $env{'form.'.$partid.'.rndseed'};
1.73      albertel  292:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.212     raeburn   293: 	if ($key =~ /resource\.\Q$partid\E\.\Q$id\E\.submission$/) {
1.160     albertel  294: 	    if ( $last && $key =~ /^(\d+):/ ) {
                    295: 		next if ($1 >= $last);
                    296: 	    }
1.73      albertel  297: 	    &Apache::lonxml::debug("Trying $key");
                    298: 	    my $pastresponse=$Apache::lonhomework::history{$key};
                    299: 	    if ($pastresponse eq $curresponse) {
                    300: 		my $history;
                    301: 		if ( $key =~ /^(\d+):/ ) {
1.221     raeburn   302:                     $history=$1;
                    303:                     next if ((($questiontype eq 'randomizetry') ||
                    304:                              ($Apache::lonhomework::history{"$history:resource.$partid.type"} eq 'randomizetry')) &&
                    305:                              ($curr_rndseed ne $Apache::lonhomework::history{"$history:resource.$partid.rndseed"}));
1.73      albertel  306: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    307: 		    $previous{'last'}='0';
                    308: 		    push(@{ $previous{'version'} },$history);
                    309: 		} else {
1.221     raeburn   310:                     next if ((($questiontype eq 'randomizetry') ||
                    311:                              ($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry')) &&
                    312:                              ($curr_rndseed ne $Apache::lonhomework::history{"resource.$partid.rndseed"}));
1.73      albertel  313: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    314: 		    $previous{'last'}='1';
                    315: 		}
1.221     raeburn   316:                 $previous{'used'} = 1;
1.73      albertel  317: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
1.209     www       318:                 if ($previous{'award'} eq 'INTERNAL_ERROR') { $previous{'used'}=0; }
1.73      albertel  319: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
1.219     raeburn   320:             } elsif ($type eq 'ci') {
                    321:                 if (lc($pastresponse) eq lc($curresponse)) {
                    322:                     if ($key =~ /^(\d+):/) {
1.221     raeburn   323:                         my $history = $1;
                    324:                         next if (($questiontype eq 'randomizetry') &&
                    325:                              ($curr_rndseed ne $Apache::lonhomework::history{"$history:resource.$partid.rndseed"}));
                    326:                         push (@{$previous{'versionci'}},$history);
1.225     raeburn   327:                         $previous{'awardci'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
1.219     raeburn   328:                         $previous{'usedci'} = 1;
                    329:                     }
                    330:                 }
                    331:             }
1.32      albertel  332: 	}
1.73      albertel  333:     }
                    334:     &Apache::lonhomework::showhash(%previous);
                    335:     return %previous;
1.54      albertel  336: }
                    337: 
                    338: sub handle_previous {
1.73      albertel  339:     my ($previous,$ad)=@_;
                    340:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    341: 	if ($$previous{'last'}) {
                    342: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107     albertel  343: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
1.217     raeburn   344: 	} elsif (($Apache::lonhomework::type ne 'survey') &&
                    345:                  ($Apache::lonhomework::type ne 'surveycred') &&
                    346:                  ($Apache::lonhomework::type ne 'anonsurvey') &&
1.221     raeburn   347:                  ($Apache::lonhomework::type ne 'anonsurveycred')) {
1.73      albertel  348: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107     albertel  349: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73      albertel  350: 	}
1.54      albertel  351:     }
1.44      albertel  352: }
                    353: 
1.45      albertel  354: sub view_or_modify {
1.149     albertel  355:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.73      albertel  356:     my $myself=0;
1.120     albertel  357:     if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
1.73      albertel  358: 	$myself=1;
                    359:     }
                    360:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    361:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    362:     if ($mgr) { return "M"; }
                    363:     if ($vgr) { return "V"; }
                    364:     if ($myself) { return "V"; }
                    365:     return '';
1.45      albertel  366: }
                    367: 
1.44      albertel  368: sub start_dataresponse {
1.73      albertel  369:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    370:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    371:     my $result;
                    372:     if ($target eq 'web') {
                    373: 	$result = $token->[2]->{'display'}.':';
                    374:     } elsif ($target eq 'meta') {
                    375: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    376: 						       $token->[2]->{'type'},
                    377: 						       $token->[2]->{'display'});
                    378: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    379:     }
                    380:     return $result;
1.44      albertel  381: }
                    382: 
                    383: sub end_dataresponse {
1.73      albertel  384:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    385:     my $result;
                    386:     if ( $target eq 'web' ) {
                    387:     } elsif ($target eq 'grade' ) {
1.120     albertel  388: 	if ( defined $env{'form.submitted'}) {
1.149     albertel  389: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
1.73      albertel  390: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    391: 	    if ($allowed) {
1.94      albertel  392: 		&Apache::response::setup_params('dataresponse',$safeeval);
1.73      albertel  393: 		my $partid = $Apache::inputtags::part;
                    394: 		my $id = $Apache::inputtags::response['-1'];
1.120     albertel  395: 		my $response = $env{'form.HWVAL_'.$id};
1.73      albertel  396: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    397: 		if ( $response =~ /[^\s]/) {
                    398: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    399: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    400: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    401: 		}
                    402: 	    } else {
1.235     bisitz    403:                 $result=&mt('Not Permitted to change values');
1.73      albertel  404: 	    }
1.45      albertel  405: 	}
1.73      albertel  406:     }
                    407:     &Apache::response::end_response;
                    408:     return $result;
1.3       albertel  409: }
                    410: 
1.129     albertel  411: sub start_customresponse {
1.128     albertel  412:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    413:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.129     albertel  414:     push(@Apache::lonxml::namespace,'customresponse');
1.128     albertel  415:     my $result;
1.157     www       416:     @Apache::response::custom_answer=();
                    417:     @Apache::response::custom_answer_type=();
1.128     albertel  418:     &Apache::lonxml::register('Apache::response',('answer'));
                    419:     if ($target eq 'web') {
                    420:   	if (  &Apache::response::show_answer() ) {
                    421: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    422: 						   $safeeval);
1.152     albertel  423: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.128     albertel  424: 	}
                    425:     } elsif ($target eq 'edit') {
                    426: 	$result.=&Apache::edit::tag_start($target,$token);
                    427: 	$result.=&Apache::edit::text_arg('String to display for answer:',
1.220     www       428: 					 'answerdisplay',$token,'50');
1.128     albertel  429: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    430:     } elsif ($target eq 'modified') {
                    431: 	my $constructtag;
                    432: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    433: 						  $safeeval,'answerdisplay');
                    434: 	if ($constructtag) {
                    435: 	    $result = &Apache::edit::rebuild_tag($token);
                    436: 	}
                    437:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    438: 	&Apache::response::reset_params();
                    439:     } elsif ($target eq 'meta') {
1.129     albertel  440: 	$result .= &Apache::response::meta_package_write('customresponse');
1.128     albertel  441:     }
                    442:     return $result;
                    443: }
                    444: 
1.129     albertel  445: sub end_customresponse {
1.128     albertel  446:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    447:     my $result;
                    448:     my $part=$Apache::inputtags::part;
                    449:     my $id=$Apache::inputtags::response[-1];
                    450:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    451: 	my $response = &Apache::response::getresponse();
1.146     albertel  452: 	if ($Apache::lonhomework::type eq 'exam' ||
                    453: 	    &Apache::response::submitted('scantron')) {
                    454: 	    &Apache::response::scored_response($part,$id);
                    455: 	} elsif ( $response =~ /[^\s]/ && 
1.158     www       456: 		  $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
1.128     albertel  457: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    458: 		&Apache::lonxml::default_homework_load($safeeval);
                    459: 	    }
                    460: 	    my %previous = &Apache::response::check_for_previous($response,
                    461: 								 $part,$id);
                    462: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    463: 		$response;
                    464: 	    my $error;
1.129     albertel  465: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128     albertel  466: 		$response;
                    467: 	    
1.216     www       468: 	    my ($award,$score) = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
1.128     albertel  469: 	    if (!&Apache::inputtags::valid_award($award)) {
                    470: 		$error = $award;
                    471: 		$award = 'ERROR';
                    472: 	    }
1.233     raeburn   473:             if (($award eq 'INCORRECT' || $award eq 'APPROX_ANS' ||
                    474:                  $award eq 'EXACT_ANS')) {
                    475:                 if ($Apache::lonhomework::type eq 'survey') {
                    476:                     $award='SUBMITTED';
                    477:                 } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    478:                     $award='SUBMITTED_CREDIT';
                    479:                 } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
                    480:                     $award='ANONYMOUS';
                    481:                 } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    482:                     $award='ANONYMOUS_CREDIT';
                    483:                 }
                    484:             }
1.128     albertel  485: 	    &Apache::response::handle_previous(\%previous,$award);
                    486: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    487: 		$award;
1.216     www       488:             if ($award eq 'ASSIGNED_SCORE') {
                    489:                 $Apache::lonhomework::results{"resource.$part.$id.awarded"}=1.0*$score;
                    490:             }
1.128     albertel  491: 	    if ($error) {
                    492: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    493: 		    $error;
                    494: 	    }
                    495: 	}
1.146     albertel  496:     } elsif ( $target eq 'answer') {
                    497: 	$result  = &Apache::response::answer_header('customresponse');
                    498: 	my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    499: 						$safeeval);
                    500: 	if ($env{'form.answer_output_mode'} ne 'tex') {
                    501: 	    $answer = '<b>'.$answer.'</b>';
                    502: 	}
                    503: 	$result .= &Apache::response::answer_part('customresponse',$answer);
                    504: 	$result .= &Apache::response::answer_footer('customresponse');
                    505:     }
1.163     albertel  506:     if ($target eq 'web') {
1.229     raeburn   507: 	&setup_prior_tries_hash(\&format_prior_response_custom);
1.163     albertel  508:     }
1.146     albertel  509:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    510: 	$target eq 'tex' || $target eq 'analyze') {
1.227     raeburn   511:         my $repetition = &repetition();
                    512: 	&Apache::lonxml::increment_counter($repetition,"$part.$id");
1.179     foxr      513: 	if ($target eq 'analyze') {
1.187     raeburn   514:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'customresponse';
1.179     foxr      515: 	    &Apache::lonhomework::set_bubble_lines();
                    516: 	}
1.128     albertel  517:     }
                    518:     pop(@Apache::lonxml::namespace);
1.157     www       519:     pop(@Apache::response::custom_answer);
                    520:     pop(@Apache::response::custom_answer_type);
1.128     albertel  521:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    522:     &Apache::response::end_response();
                    523:     return $result;
                    524: }
                    525: 
1.163     albertel  526: sub format_prior_response_custom {
                    527:     my ($mode,$answer) =@_;
1.229     raeburn   528:     if (ref($answer) eq 'ARRAY') {
                    529:         $answer = '('.join(', ', @{ $answer }).')';
                    530:     }
1.163     albertel  531:     return '<span class="LC_prior_custom">'.
                    532: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    533: }
1.140     www       534: 
1.139     www       535: sub start_mathresponse {
1.140     www       536:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    537:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    538:     push(@Apache::lonxml::namespace,'mathresponse');
1.139     www       539:     my $result;
1.157     www       540:     @Apache::response::custom_answer=();
                    541:     @Apache::response::custom_answer_type=();
1.140     www       542:     &Apache::lonxml::register('Apache::response',('answer'));
                    543:     if ($target eq 'web') {
                    544:   	if (  &Apache::response::show_answer() ) {
                    545: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    546: 						   $safeeval);
1.152     albertel  547: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.140     www       548: 	}
1.191     www       549: 
1.140     www       550:     } elsif ($target eq 'edit') {
                    551: 	$result.=&Apache::edit::tag_start($target,$token);
                    552: 	$result.=&Apache::edit::text_arg('String to display for answer:',
1.220     www       553: 					 'answerdisplay',$token,'50');
1.150     www       554: 	$result.=&Apache::edit::select_arg('Algebra System:',
                    555: 					   'cas',
1.214     www       556: 					   ['maxima','R'],
1.150     www       557: 					   $token);
                    558: 	$result.=&Apache::edit::text_arg('Argument Array:',
1.193     www       559: 					 'args',$token).
                    560:                  &Apache::loncommon::help_open_topic('Maxima_Argument_Array');
1.192     www       561:         $result.=&Apache::edit::text_arg('Libraries:',
1.193     www       562:                                          'libraries',$token).
                    563:                  &Apache::loncommon::help_open_topic('Maxima_Libraries');
1.140     www       564: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    565:     } elsif ($target eq 'modified') {
                    566: 	my $constructtag;
                    567: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
1.192     www       568: 						  $safeeval,'answerdisplay','cas','args','libraries');
1.140     www       569: 	if ($constructtag) {
                    570: 	    $result = &Apache::edit::rebuild_tag($token);
                    571: 	}
                    572:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    573: 	&Apache::response::reset_params();
                    574:     } elsif ($target eq 'meta') {
                    575: 	$result .= &Apache::response::meta_package_write('mathresponse');
1.139     www       576:     }
                    577:     return $result;
                    578: }
                    579: 
1.191     www       580: sub edit_mathresponse_button {
1.238     raeburn   581:     my ($partid,$id)=@_;
                    582:     my $field = 'HWVAL_'.$partid.'_'.$id;
1.237     damieng   583:     my $btype = $env{'browser.type'};
                    584:     my $bversion = $env{'browser.version'};
                    585:     if (($btype eq 'explorer' && $bversion < 9) || ($btype eq 'safari' && $bversion < 3) ||
                    586:         ($btype eq 'mozilla' && $bversion < 3)) {
                    587:       # DragMath applet
                    588:       my $button=&mt('Edit Answer');
                    589: #     my $helplink=&Apache::loncommon::help_open_topic('Formula_Editor');
                    590:       my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
                    591:       return(<<ENDFORMULABUTTON);
1.213     bisitz    592: <script type="text/javascript" language="JavaScript">
1.238     raeburn   593: function LC_mathedit_${field} (LCtextline) {
                    594:     thenumber = LCtextline;
                    595:     var thedata = '';
                    596:     if (document.getElementById(LCtextline)) {
                    597:         thedata = document.getElementById(LCtextline).value;
                    598:     }
1.234     raeburn   599:     newwin = window.open("/adm/dragmath/MaximaPopup.html","","width=565,height=400,resizable");
1.191     www       600: }
                    601: </script>
1.238     raeburn   602: <a href="javascript:LC_mathedit_${field}('${field}');void(0);"><img class="stift" src="$iconpath/stift.gif" alt="$button" title="$button" /></a>
1.191     www       603: ENDFORMULABUTTON
1.237     damieng   604:       
                    605:     } else {
1.238     raeburn   606:         # LON-CAPA math equation editor
                    607:         my $mathjaxjs;
                    608:         unless (lc(&Apache::lontexconvert::tex_engine()) eq 'mathjax') {
                    609:             $mathjaxjs = <<"MATHJAX_SCRIPT";
                    610: var mathjaxscript = document.createElement("script");
                    611:     mathjaxscript.type = "text/javascript";
                    612:     mathjaxscript.src = "/adm/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
                    613:     document.body.appendChild(mathjaxscript);
                    614: MATHJAX_SCRIPT
                    615:         }
                    616:         return(<<EQ_EDITOR_SCRIPT);
1.237     damieng   617: <script type="text/javascript">
1.238     raeburn   618:   var LCmathField = document.getElementById('${field}');
                    619:   LCmathField.className += ' math'; // note the space
1.239   ! damieng   620:   LCmathField.setAttribute('data-implicit_operators', 'true');
1.237     damieng   621:   var LCMATH_started;
                    622:   if (typeof LCMATH_started === 'undefined') {
1.238     raeburn   623:     $mathjaxjs
1.237     damieng   624:     LCMATH_started = true;
                    625:     var script = document.createElement("script");
                    626:     script.type = "text/javascript";
                    627:     script.src = "/adm/LC_math_editor/LC_math_editor.min.js";
                    628:     document.body.appendChild(script);
                    629:     window.addEventListener('load', function(e) {
                    630:         LCMATH.initEditors();
                    631:     }, false);
                    632:   }
                    633: </script>
                    634: EQ_EDITOR_SCRIPT
                    635:     }
1.191     www       636: }
                    637: 
1.139     www       638: sub end_mathresponse {
1.140     www       639:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    640:     my $result;
                    641:     my $part=$Apache::inputtags::part;
                    642:     my $id=$Apache::inputtags::response[-1];
                    643:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    644: 	my $response = &Apache::response::getresponse();
1.150     www       645: 	if ( $response =~ /[^\s]/ ) {
1.140     www       646: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    647: 		&Apache::lonxml::default_homework_load($safeeval);
                    648: 	    }
                    649: 	    my %previous = &Apache::response::check_for_previous($response,
                    650: 								 $part,$id);
                    651: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    652: 		$response;
                    653: 	    my $error;
1.155     www       654: 	    my $award;
                    655: 	    my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
                    656:             if ($cas eq 'maxima') {
                    657:                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
1.192     www       658:                 $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args,
                    659:                                                       &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
1.155     www       660:             }
1.214     www       661:             if ($cas eq 'R') {
                    662:                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
                    663:                 $award=&Apache::lonr::r_run($Apache::response::custom_answer[-1],$response,$args,
                    664:                                             &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
                    665:             }
                    666: 
1.140     www       667: 	    if (!&Apache::inputtags::valid_award($award)) {
1.151     albertel  668: 		$error = $award;
                    669: 		$award = 'ERROR';
1.140     www       670: 	    }
1.233     raeburn   671:             if (($award eq 'INCORRECT' || $award eq 'APPROX_ANS' ||
                    672:                  $award eq 'EXACT_ANS')) {
                    673:                 if ($Apache::lonhomework::type eq 'survey') {
                    674:                     $award='SUBMITTED';
                    675:                 } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    676:                     $award='SUBMITTED_CREDIT';
                    677:                 } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
                    678:                     $award='ANONYMOUS';
                    679:                 } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    680:                     $award='ANONYMOUS_CREDIT';
                    681:                 }
                    682:             }
1.140     www       683: 	    &Apache::response::handle_previous(\%previous,$award);
                    684: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    685: 		$award;
                    686: 	    if ($error) {
                    687: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    688: 		    $error;
                    689: 	    }
                    690: 	}
                    691:     }
1.163     albertel  692:     if ($target eq 'web') {
                    693: 	&setup_prior_tries_hash(\&format_prior_response_math);
1.203     riegler   694:         my $partid = $Apache::inputtags::part;
                    695:         my $id = $Apache::inputtags::response[-1];
                    696:         if (($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')
1.205     raeburn   697:            && (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) {
1.238     raeburn   698:             $result.=&edit_mathresponse_button($partid,$id);
1.202     riegler   699:         }
1.163     albertel  700:     }
                    701: 
1.140     www       702:     pop(@Apache::lonxml::namespace);
1.157     www       703:     pop(@Apache::response::custom_answer);
                    704:     pop(@Apache::response::custom_answer_type);
1.140     www       705:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    706:     &Apache::response::end_response();
                    707:     return $result;
1.139     www       708: }
                    709: 
1.163     albertel  710: sub format_prior_response_math {
                    711:     my ($mode,$answer) =@_;
                    712:     return '<span class="LC_prior_math">'.
                    713: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    714: }
                    715: 
1.156     www       716: sub implicit_multiplication {
                    717:     my ($expression)=@_;
                    718: # Escape scientific notation, so 3e8 does not become 3*e*8
                    719: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
                    720:     $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
                    721: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
                    722:     $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
                    723: # Fill in multiplication signs
                    724: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
1.176     albertel  725:     $expression=~s/([A-Za-z0-9])\s+(?=[A-Za-z0-9])/$1\*/gs;
1.156     www       726: # )( -> )*(; ) ( -> )*(
                    727:     $expression=~s/\)\s*\(/\)\*\(/gs;
                    728: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
                    729:     $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
                    730: # a ( -> a*(
1.174     riegler   731:     $expression=~s/([A-Za-z0-9])\s+\(/$1\*\(/gs;
1.156     www       732: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
1.174     riegler   733:     $expression=~s/\)\s*([A-Za-z0-9])/\)\*$1/gs;
1.156     www       734: # 3&8 -> 3e8; 3&-4 -> 3e-4
                    735:     $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
                    736:     return $expression;
                    737: }
1.140     www       738: 
1.128     albertel  739: sub start_answer {
                    740:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    741:     my $result;
1.157     www       742:     push(@Apache::response::custom_answer,
                    743: 	&Apache::lonxml::get_all_text_unbalanced("/answer",$parser));
                    744:     push(@Apache::response::custom_answer_type,
                    745: 	lc(&Apache::lonxml::get_param('type',$parstack,$safeeval)));
                    746:     $Apache::response::custom_answer_type[-1] =~ s/\s+//g;
1.128     albertel  747:     if ($target eq "edit" ) {
                    748: 	$result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
                    749: 	$result.=&Apache::edit::editfield($token->[1],
1.158     www       750: 					  $Apache::response::custom_answer[-1],
1.128     albertel  751: 					  '',80,4);
                    752:     } elsif ( $target eq "modified" ) {
                    753: 	$result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
                    754:     }
                    755:     return $result;
                    756: }
                    757: 
                    758: sub end_answer {
                    759:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    760:     if ($target eq 'edit' ) {
                    761: 	return &Apache::edit::end_table();
                    762:     }
                    763: }
                    764: 
1.83      albertel  765: sub decide_package {
                    766:     my ($tagstack)=@_;
                    767:     my $package;
                    768:     if ($$tagstack[-1] eq 'parameter') {
                    769: 	$package='part';
                    770:     } else {
                    771: 	my $i=-1;
                    772: 	while (defined($$tagstack[$i])) {
                    773: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    774: 		$package=$$tagstack[$i];
                    775: 		last;
                    776: 	    }
                    777: 	    $i--;
                    778: 	}
                    779:     }
                    780:     return $package;
                    781: }
                    782: 
1.3       albertel  783: sub start_responseparam {
1.73      albertel  784:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    785:     my $result='';
                    786:     if ($target eq 'meta') {
                    787: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    788: 					$token->[2]->{'type'},
                    789: 					$token->[2]->{'default'},
                    790: 					$token->[2]->{'description'});
                    791:     } elsif ($target eq 'edit') {
                    792: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  793: 	my $optionlist;
                    794: 	my $package=&decide_package($tagstack);
                    795: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    796: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    797: 		$optionlist.='<option value="'.$1.'">'.
                    798: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    799: 	    }
                    800: 	}
                    801: 	if (defined($optionlist)) {
1.206     bisitz    802: 	    $result.=&mt('Use template:').' <select name="'.
1.83      albertel  803: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    804: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    805: 	}
1.73      albertel  806: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    807: 	    &Apache::edit::text_arg('Type:','type',$token).
                    808: 		&Apache::edit::text_arg('Description:','description',$token).
                    809: 		    &Apache::edit::text_arg('Default:','default',$token).
                    810: 			"</td></tr>";
                    811: 	$result.=&Apache::edit::end_table;
                    812:     } elsif ($target eq 'modified') {
1.83      albertel  813: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    814: 						     $safeeval,'name','type',
                    815: 						     'description','default');
                    816: 	my $element=&Apache::edit::html_element_name('parameter_package');
1.120     albertel  817: 	if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
                    818: 	    my $name=$env{"form.$element"};
1.83      albertel  819: 	    my $tag=&decide_package($tagstack);
                    820: 	    $token->[2]->{'name'}=$name;
                    821: 	    $token->[2]->{'type'}=
                    822: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    823: 	    $token->[2]->{'description'}=
                    824: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    825: 	    $token->[2]->{'default'}=
                    826: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
1.186     raeburn   827:             $token->[3] = ['name','type','description','default'];
1.83      albertel  828: 	    $constructtag=1;
                    829: 	}
1.73      albertel  830: 	if ($constructtag) {
                    831: 	    $result = &Apache::edit::rebuild_tag($token);
                    832: 	}
                    833:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    834: 	     $target eq 'tex' || $target eq 'analyze' ) {
1.120     albertel  835: 	if ($env{'request.state'} eq 'construct') {
1.73      albertel  836: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    837: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    838: 						     $safeeval);
                    839: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    840: 	}
1.52      albertel  841:     }
1.73      albertel  842:     return $result;
1.3       albertel  843: }
                    844: 
                    845: sub end_responseparam {
1.73      albertel  846:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    847:     if ($target eq 'edit') { return ('','no'); }
                    848:     return '';
1.55      albertel  849: }
                    850: 
                    851: sub start_parameter {
1.114     albertel  852:     return &start_responseparam(@_);
1.55      albertel  853: }
                    854: 
                    855: sub end_parameter {
1.114     albertel  856:     return &end_responseparam(@_);
1.42      albertel  857: }
                    858: 
1.67      albertel  859: sub reset_params {
                    860:     %Apache::inputtags::params=();
                    861: }
                    862: 
1.42      albertel  863: sub setup_params {
1.94      albertel  864:     my ($tag,$safeeval) = @_;
1.42      albertel  865: 
1.120     albertel  866:     if ($env{'request.state'} eq 'construct') { return; }
1.73      albertel  867:     my %paramlist=();
                    868:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
1.161     albertel  869: 	if ($key =~ /^\Q$tag\E/) {
1.73      albertel  870: 	    my ($package,$name) = split(/&/,$key);
                    871: 	    $paramlist{$name}=1;
                    872: 	}
1.42      albertel  873:     }
1.73      albertel  874:     foreach my $key (keys(%paramlist)) {
                    875: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    876: 	if (defined($Apache::inputtags::response[-1])) {
                    877: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    878: 	}
                    879: 	$entry.='.'.$key;
                    880: 	&Apache::lonxml::debug("looking for $entry");
                    881: 	my $value = &Apache::lonnet::EXT("$entry");
                    882: 	&Apache::lonxml::debug("$key has value :$value:");
                    883: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    884: 	    &Apache::lonxml::debug("using nothing");
                    885: 	    $Apache::inputtags::params{$key}='';
                    886: 	} else {
1.94      albertel  887: 	    my $string="{return qq\0".$value."\0}";
                    888: 	    my $newvalue=&Apache::run::run($string,$safeeval,1);
                    889: 	    if (defined($newvalue)) { $value=$newvalue; }
1.73      albertel  890: 	    $Apache::inputtags::params{$key}=$value;
                    891: 	}
1.42      albertel  892:     }
1.48      albertel  893: }
                    894: 
1.132     albertel  895: {
                    896:     my @answer_bits;
1.147     albertel  897:     my $need_row_start;
1.132     albertel  898: 
1.48      albertel  899: sub answer_header {
1.147     albertel  900:     my ($type,$increment,$rows) = @_;
1.73      albertel  901:     my $result;
1.120     albertel  902:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.132     albertel  903: 	undef(@answer_bits);
1.133     albertel  904: 	my $bit;
                    905: 	if ($Apache::lonhomework::type eq 'exam') {
                    906: 	    $bit = ($Apache::lonxml::counter+$increment).') ';
                    907: 	} else {
1.235     bisitz    908:             $bit .= ' '.&mt('Answer for Part: [_1]',
                    909:                                 '\verb|'.$Apache::inputtags::part.'|').' ';
1.133     albertel  910: 	}
                    911: 	push(@answer_bits,$bit);
1.73      albertel  912:     } else {
1.147     albertel  913: 	my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
1.132     albertel  914: 	$result  = '<table border="1"><tr>';
                    915: 	if ($Apache::lonhomework::type eq 'exam') {
1.147     albertel  916: 	    $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
1.132     albertel  917: 	} else {
1.147     albertel  918: 	    $result .= $td.&mt('Answer for Part: [_1]',
                    919: 			       $Apache::inputtags::part).'</td>';
1.132     albertel  920: 	}
                    921: 	$result .= "\n";
1.147     albertel  922: 	$need_row_start = 0;
                    923:     }
                    924:     return $result;
                    925: }
                    926: 
                    927: sub next_answer {
                    928:     my ($type) = @_;
                    929:     my $result;
                    930:     if ($env{'form.answer_output_mode'} eq 'tex') {
                    931: 	# FIXME ... need to do something with tex mode
                    932:     } else {
                    933: 	$result .= "</tr>";
                    934: 	$need_row_start = 1;
1.73      albertel  935:     }
                    936:     return $result;
1.48      albertel  937: }
                    938: 
                    939: sub answer_part {
1.148     albertel  940:     my ($type,$answer,$args) = @_;
1.73      albertel  941:     my $result;
1.120     albertel  942:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.148     albertel  943: 	if (!$args->{'no_verbatim'}) {
                    944: 	    my $to_use='|';
                    945: 	    foreach my $value (32..126) {
                    946: 		my $char=pack('c',$value);
                    947: 		if ($answer !~ /\Q$char\E/) {
                    948: 		    $to_use=$char;
                    949: 		    last;
                    950: 		}
                    951: 	    }
1.189     www       952:             my $fullanswer=$answer;
1.188     www       953:             $answer='';
1.189     www       954:             foreach my $element (split(/[\;]/,$fullanswer)) {
                    955: 	       if ($element ne '') {
                    956: 	 	  $answer.= '\verb'.$to_use.$element.$to_use.' \newline';
                    957: 	       }
1.188     www       958:             }
1.124     albertel  959: 	}
1.132     albertel  960: 	if ($answer ne '') {
1.148     albertel  961: 	    push(@answer_bits,$answer);
1.132     albertel  962: 	}
1.73      albertel  963:     } else {
1.147     albertel  964: 	if ($need_row_start) {
                    965: 	    $result .= '<tr>';
                    966: 	    $need_row_start = 0;
                    967: 	}
1.189     www       968: 	$result .= '<td>'.$answer.'</td>';
1.73      albertel  969:     }
                    970:     return $result;
1.48      albertel  971: }
                    972: 
                    973: sub answer_footer {
1.73      albertel  974:     my ($type) = @_;
                    975:     my $result;
1.120     albertel  976:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.189     www       977: 	$result  = ' \vskip 0 mm \noindent \begin{tabular}{|p{1.5cm}|p{6.8cm}|}\hline ';
                    978: 	$result .= $answer_bits[0].'&\vspace*{-4mm}\begin{itemize}';
                    979:         for (my $i=1;$i<=$#answer_bits;$i++) {
                    980:             $result.='\item '.$answer_bits[$i].'\vspace*{-7mm}';
                    981:         }
                    982: 	$result .= ' \end{itemize} \\\\ \hline \end{tabular} \vskip 0 mm ';
1.73      albertel  983:     } else {
1.185     albertel  984: 	if (!$need_row_start) {
                    985: 	    $result .= '</tr>';
                    986: 	}
                    987: 	$result .= '</table>';
1.73      albertel  988:     }
                    989:     return $result;
1.1       albertel  990: }
1.2       albertel  991: 
1.132     albertel  992: }
                    993: 
1.62      albertel  994: sub showallfoils {
1.120     albertel  995:     if (defined($env{'form.showallfoils'})) {
1.149     albertel  996: 	my ($symb)=&Apache::lonnet::whichuser();
1.120     albertel  997: 	if (($env{'request.state'} eq 'construct') || 
                    998: 	    ($env{'user.adv'} && $symb eq '')      ||
1.118     foxr      999:             ($Apache::lonhomework::viewgrades) ) {
1.102     albertel 1000: 	    return 1;
                   1001: 	}
1.73      albertel 1002:     }
1.108     albertel 1003:     if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.217     raeburn  1004:     if ($Apache::lonhomework::type eq 'surveycred') { return 1; }
                   1005:     if ($Apache::lonhomework::type eq 'anonsurvey') { return 1; }
                   1006:     if ($Apache::lonhomework::type eq 'anonsurveycred') { return 1; }
                   1007: 
1.102     albertel 1008:     return 0;
1.70      albertel 1009: }
                   1010: 
1.168     albertel 1011: =pod
                   1012: 
1.210     raeburn  1013: =item &getresponse();
1.168     albertel 1014: 
                   1015: Retreives the current submitted response, helps out in the case of
                   1016: scantron mode.
                   1017: 
                   1018: Returns either the exact text of the submission, or a bubbled response
                   1019: converted to something usable.
                   1020: 
                   1021: Optional Arguments:
1.171     albertel 1022:   $offset - (defaults to 1) if a problem has more than one bubble
                   1023:             response, pass in the number of the bubble wanted, (the
                   1024:             first bubble associated with a problem has an offset of 1,
                   1025:             the second bubble is 2
                   1026: 
1.168     albertel 1027:   $resulttype - undef    -> a number between 0 and 25
                   1028:                 'A is 1' -> a number between 1 and 26
                   1029:                 'letter' -> a letter between 'A' and 'Z'
1.172     foxr     1030:   $lines  - undef problem only needs a single line of bubbles.
                   1031:             nonzero  Problem wants the first nonempty response in 
                   1032:                       $lines lines of bubbles.
                   1033:   $bubbles_per_line - Must be provided if lines is defined.. number of
                   1034:                       bubbles on a line.
1.173     albertel 1035: 
1.168     albertel 1036: =cut
                   1037: 
1.70      albertel 1038: sub getresponse {
1.172     foxr     1039:     my ($offset,$resulttype, $lines, $bubbles_per_line)=@_;
1.70      albertel 1040:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                   1041:     my $response;
1.168     albertel 1042:     if (!defined($offset)) {
1.170     albertel 1043: 	$offset=1;
1.70      albertel 1044:     } else {
1.168     albertel 1045: 	$formparm.=":$offset";
1.70      albertel 1046:     }
1.172     foxr     1047:     if (!defined($lines)) {
                   1048: 	$lines = 1;
                   1049:     }
1.70      albertel 1050:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                   1051: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                   1052: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                   1053: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120     albertel 1054:     if ($env{'form.submitted'} eq 'scantron') {
1.71      albertel 1055: 	my $part  = $Apache::inputtags::part;
                   1056: 	my $id    = $Apache::inputtags::response[-1];
1.172     foxr     1057: 	
                   1058: 	my $line;
1.211     raeburn  1059: 	my $startline = $env{'form.scantron_questnum_start.'.$part.'.'.$id};
                   1060:         if (!$startline) {
                   1061:             $startline = $Apache::lonxml::counter;
                   1062:         }
1.172     foxr     1063: 	for ($line = 0; $line < $lines; $line++) {
1.211     raeburn  1064:             my $theline = $startline+$offset-1+$line;
1.184     foxr     1065: 	    $response = $env{"scantron.$theline.answer"};
                   1066: 	    if ((defined($response)) && ($response ne "") && ($response ne " ")) {
1.172     foxr     1067: 		last;
                   1068: 	    }
1.211     raeburn  1069:  
1.172     foxr     1070: 	}
1.178     albertel 1071: 
                   1072: 	# save bubbled letter for later
                   1073: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                   1074: 	    $response;
1.90      albertel 1075: 	if ($resulttype ne 'letter') {
1.226     raeburn  1076:             $response = $let_to_num{$response};
                   1077:             if ($resulttype eq 'A is 1') {
                   1078:                 if ($response ne "") {
                   1079:                     $response = $response+1;
                   1080:                 }
1.104     albertel 1081: 	    }
1.172     foxr     1082: 	    if ($response ne "") {
                   1083: 		$response += $line * $bubbles_per_line;
                   1084: 	    }
                   1085: 	} else {
                   1086: 	    if ($response ne "") {
1.226     raeburn  1087:                 my $raw = $response;
1.172     foxr     1088: 		$response = chr(ord($response) + $line * $bubbles_per_line);
                   1089: 	    }
1.90      albertel 1090: 	}
1.172     foxr     1091: 
1.70      albertel 1092:     } else {
1.120     albertel 1093: 	$response = $env{$formparm};
1.70      albertel 1094:     }
1.172     foxr     1095:     # 
                   1096:     #  If we have a nonempty answer, correct the numeric value
                   1097:     #  of the answer for the line on which it was found.
                   1098:     #
                   1099: 
1.70      albertel 1100:     return $response;
1.62      albertel 1101: }
1.71      albertel 1102: 
1.168     albertel 1103: =pod
                   1104: 
                   1105: =item &repetition();
                   1106: 
1.227     raeburn  1107: In scalar context:
                   1108: 
                   1109: returns: the number of lines that are required to encode the weight.
1.226     raeburn  1110: (Default is for 10 bubbles per bubblesheet item; other (integer) 
                   1111: values can be specified by using a custom Bubblesheet format file 
                   1112: with an eighteenth entry (BubblesPerRow) set to the integer 
                   1113: appropriate for the bubblesheets which will be used to assign weights.
1.168     albertel 1114: 
1.227     raeburn  1115: In array context:
                   1116:  
                   1117: returns: number of lines required to encode weight, and bubbles/line.
                   1118: 
1.168     albertel 1119: =cut
                   1120: 
1.71      albertel 1121: sub repetition {
                   1122:     my $id = $Apache::inputtags::part;
                   1123:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121     albertel 1124:     if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.226     raeburn  1125:     my $bubbles_per_row;
                   1126:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) && 
                   1127:         ($env{'form.bubbles_per_row'} > 0)) {
                   1128:         $bubbles_per_row = $env{'form.bubbles_per_row'};
                   1129:     } else {
                   1130:         $bubbles_per_row = 10;
                   1131:     }
1.227     raeburn  1132:     my $denominator = $bubbles_per_row;
                   1133:     if (($env{'form.scantron_lastbubblepoints'} == 0) && 
                   1134:         ($bubbles_per_row > 1)) {
                   1135:         $denominator = $bubbles_per_row - 1;
                   1136:     } 
                   1137:     my $repetition = int($weight/$denominator);
                   1138:     if ($weight % $denominator != 0) { $repetition++; } 
                   1139:     if (wantarray) {
                   1140:         return ($repetition,$bubbles_per_row);
                   1141:     }
1.72      albertel 1142:     return $repetition;
1.227     raeburn  1143: 
1.72      albertel 1144: }
                   1145: 
1.168     albertel 1146: =pod
                   1147: 
1.210     raeburn  1148: =item &scored_response();
1.168     albertel 1149: 
                   1150: Sets the results hash elements
                   1151: 
                   1152:    resource.$part_id.$response_id.awarded - to the floating point
                   1153:      number between 0 and 1 that was awarded on the bubbled input
                   1154: 
                   1155:    resource.$part_id.$response_id.awarddetail - to 'ASSIGNED_SCORE'
                   1156: 
                   1157: Returns
                   1158: 
                   1159:    the number of bubble sheet lines that were used (and likely need to
                   1160:      be passed to &Apache::lonxml::increment_counter()
                   1161: 
                   1162: Arguments
                   1163: 
                   1164:    $part_id - id of the part to grade
                   1165:    $response_id - id of the response to grade
                   1166:   
                   1167: 
                   1168: =cut
                   1169: 
1.72      albertel 1170: sub scored_response {
                   1171:     my ($part,$id)=@_;
                   1172:     my $repetition=&repetition();
1.227     raeburn  1173:     my $bubbles_per_row;
                   1174:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
                   1175:         ($env{'form.bubbles_per_row'} > 0)) {
                   1176:         $bubbles_per_row = $env{'form.bubbles_per_row'};
                   1177:     } else {
                   1178:         $bubbles_per_row = 10;
                   1179:     }
1.72      albertel 1180:     my $score=0;
                   1181:     for (my $i=0;$i<$repetition;$i++) {
1.227     raeburn  1182: 	# A is 1, B is 2, etc.
1.72      albertel 1183: 	my $increase=&Apache::response::getresponse($i+1);
1.227     raeburn  1184:         unless (($increase == $bubbles_per_row-1) &&
                   1185:                 ($env{'form.scantron_lastbubblepoints'} == 0)) {
                   1186:             # (get response return 0-9 and then we add 1)
                   1187:             if ($increase ne '') {
                   1188:                 $score+=$increase+1;
                   1189:             }
                   1190:         }
1.72      albertel 1191:     }
                   1192:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel 1193:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel 1194:     my $pcr=$score/$weight;
                   1195:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                   1196:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                   1197: 	'ASSIGNED_SCORE';
1.71      albertel 1198:     return $repetition;
1.78      albertel 1199: }
                   1200: 
                   1201: sub whichorder {
1.221     raeburn  1202:     my ($max,$randomize,$showall,$hash,$rndseed)=@_;
1.78      albertel 1203:     #&Apache::lonxml::debug("man $max randomize $randomize");
1.231     raeburn  1204:     my @names;
                   1205:     if (ref($hash->{'names'}) eq 'ARRAY') {
                   1206:         @names = @{$hash->{'names'}};
                   1207:     }
                   1208:     return if (!@names);
1.78      albertel 1209:     my @whichopt =();
                   1210:     my (%top,@toplist,%bottom,@bottomlist);
                   1211:     if (!($showall || ($randomize eq 'no'))) {
                   1212: 	my $current=0;
                   1213: 	foreach my $name (@names) {
                   1214: 	    $current++;
                   1215: 	    if ($$hash{"$name.location"} eq 'top') {
                   1216: 		$top{$name}=$current;
                   1217: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                   1218: 		$bottom{$name}=$current;
                   1219: 	    }
                   1220: 	}
                   1221:     }
                   1222:     my $topcount=0;
                   1223:     my $bottomcount=0;
                   1224:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                   1225: 	   && ($#names > -1)) {
                   1226: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                   1227: 	my $aopt;
                   1228: 	if ($showall || ($randomize eq 'no')) {
                   1229: 	    $aopt=0;
                   1230: 	} else {
                   1231: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                   1232: 	}
                   1233: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                   1234: 	$aopt=splice(@names,$aopt,1);
                   1235: 	#&Apache::lonxml::debug("Picked $aopt");
                   1236: 	if ($top{$aopt}) {
                   1237: 	    $toplist[$top{$aopt}]=$aopt;
                   1238: 	    $topcount++;
                   1239: 	} elsif ($bottom{$aopt}) {
                   1240: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                   1241: 	    $bottomcount++;
                   1242: 	} else {
                   1243: 	    push (@whichopt,$aopt);
                   1244: 	}
                   1245:     }
                   1246:     for (my $i=0;$i<=$#toplist;$i++) {
                   1247: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                   1248:     }
                   1249:     for (my $i=0;$i<=$#bottomlist;$i++) {
                   1250: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                   1251:     }
                   1252:     return @whichopt;
1.71      albertel 1253: }
                   1254: 
1.85      albertel 1255: sub show_answer {
                   1256:     my $part   = $Apache::inputtags::part;
1.228     raeburn  1257:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
1.85      albertel 1258:     my $status = $Apache::inputtags::status[-1];
1.228     raeburn  1259:     my $canshow = 0;
                   1260:     if ($award =~ /^correct/) {
1.230     raeburn  1261:         if (($Apache::lonhomework::history{"resource.$part.awarded"} >= 1) ||
                   1262:             (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/)) {
1.228     raeburn  1263:             $canshow = 1;
                   1264:         }   
                   1265:     }
                   1266:     return  (($canshow && &Apache::lonhomework::show_problem_status()) 
                   1267: 	     || $status eq "SHOW_ANSWER");
1.85      albertel 1268: }
1.87      albertel 1269: 
                   1270: sub analyze_store_foilgroup {
                   1271:     my ($shown,$attrs)=@_;
                   1272:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1273:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                   1274: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                   1275: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                   1276: 	foreach my $attr (@$attrs) {
                   1277: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                   1278: 		$Apache::response::foilgroup{"$name.".$attr};
                   1279: 	}
                   1280:     }
                   1281:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel 1282: }
                   1283: 
                   1284: sub check_if_computed {
                   1285:     my ($token,$parstack,$safeeval,$name)=@_;
                   1286:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106     matthew  1287:     if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96      albertel 1288: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1289: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                   1290:     }
1.87      albertel 1291: }
                   1292: 
                   1293: sub pick_foil_for_concept {
                   1294:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
1.231     raeburn  1295:     my @names;
                   1296:     if (ref($Apache::response::conceptgroup{'names'}) eq 'ARRAY') {
                   1297:         @names = @{ $Apache::response::conceptgroup{'names'} };
                   1298:     }
                   1299:     return if (!@names);
1.87      albertel 1300:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                   1301:     my $name=$names[$pick];
                   1302:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                   1303:     foreach my $attr (@$attrs) {
                   1304: 	$Apache::response::foilgroup{"$name.".$attr} =
                   1305: 	    $Apache::response::conceptgroup{"$name.".$attr};
                   1306:     }
                   1307:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                   1308:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                   1309:     &Apache::lonxml::debug("Selecting $name in $concept");
                   1310:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1311:     if ($target eq 'analyze') {
                   1312: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                   1313: 	      $concept);
                   1314: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                   1315: 	    $Apache::response::conceptgroup{'names'};
                   1316: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                   1317: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                   1318: 		  $name);
                   1319: 	    foreach my $attr (@$attrs) {
                   1320: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                   1321: 		    $Apache::response::conceptgroup{"$name.$attr"};
                   1322: 	    }
                   1323: 	}
                   1324:     }
                   1325:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                   1326:     $hinthash->{"$part_id.concept.$concept"}=
                   1327: 	$Apache::response::conceptgroup{'names'};
                   1328: 
                   1329: }
1.208     jms      1330: 
                   1331: =pod
                   1332: 
                   1333: =item get_response_param()
                   1334: 
                   1335: Get a parameter associated with a problem.
                   1336: Parameters:
                   1337: 	$id        - the id of the paramater, either a part id, 
                   1338:               or a partid and responspe id joined by _
                   1339: 	$name      - Name of the parameter to fetch
                   1340: 	$default   - Default value for the paramter.
                   1341: 
                   1342: =cut
                   1343: 
1.95      albertel 1344: sub get_response_param {
                   1345:     my ($id,$name,$default)=@_;
                   1346:     my $parameter;
1.120     albertel 1347:     if ($env{'request.state'} eq 'construct' &&
1.95      albertel 1348: 	defined($Apache::inputtags::params{$name})) {
                   1349: 	$parameter=$Apache::inputtags::params{$name};
                   1350:     } else {
                   1351: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                   1352:     }
                   1353:     if (!defined($parameter) ||	$parameter eq '') {
                   1354: 	$parameter = $default;
                   1355:     }
                   1356:     return $parameter;
                   1357: }
1.87      albertel 1358: 
1.113     albertel 1359: sub submitted {
                   1360:     my ($who)=@_;
                   1361:     
                   1362:     # when scatron grading any submission is a submission
1.120     albertel 1363:     if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113     albertel 1364:     # if the caller only cared if this was a scantron submission
                   1365:     if ($who eq 'scantron') { return 0; }
                   1366:     # if the Submit Answer button for this particular part was pressed
                   1367:     my $partid=$Apache::inputtags::part;
1.159     albertel 1368:     if ($env{'form.submitted'} eq "part_$partid") {
                   1369: 	return 1;
                   1370:     }
                   1371:     if ($env{'form.submitted'} eq "yes"
                   1372: 	&& defined($env{'form.submit_'.$partid})) {
                   1373: 	return 1;
                   1374:     }
1.122     albertel 1375:     # Submit All button on a .page was pressed
                   1376:     if (defined($env{'form.all_submit'})) { return 1; }
1.204     bisitz   1377:     # otherwise no submission occurred
1.113     albertel 1378:     return 0;
                   1379: }
1.117     albertel 1380: 
1.130     albertel 1381: sub add_to_gradingqueue {
1.149     albertel 1382:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.131     albertel 1383:     if (   $courseid eq ''
                   1384: 	|| $symb eq ''
1.135     albertel 1385: 	|| $env{'request.state'} eq 'construct'
                   1386: 	|| $Apache::lonhomework::type ne 'problem') {
1.131     albertel 1387: 	return;
                   1388:     }
1.130     albertel 1389: 
                   1390:     my %queue_info = ( 'type' => 'problem',
                   1391: 		       'time' => time);
                   1392: 
                   1393:     if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
                   1394: 	$queue_info{'slot'}=
                   1395: 	     $Apache::lonhomework::history{"resource.0.checkedin.slot"};
                   1396:     }
                   1397: 
                   1398:     my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
                   1399:     if ($result ne 'ok') {
                   1400: 	&Apache::lonxml::error("add_to_queue said $result");
                   1401:     }
                   1402: }
                   1403: 
1.208     jms      1404: =pod 
                   1405: 
                   1406: =item check_status()
                   1407: 
                   1408: basically undef and 0 (both false) mean that they still have work to do
                   1409: and all true values mean that they can't do any more work
                   1410: 
                   1411: 	a return of undef means it is unattempted
                   1412: 	a return of 0 means it is attmpted and wrong but still has tries
                   1413: 	a return of 1 means it is marked correct
                   1414: 	a return of 2 means they have exceed maximum number of tries
                   1415: 	a return of 3 means it after the answer date
                   1416: 
                   1417: =cut
                   1418: 
1.117     albertel 1419: sub check_status {
                   1420:     my ($id)=@_;
1.143     albertel 1421:     if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117     albertel 1422:     my $curtime=&Apache::lonnet::EXT('system.time');
                   1423:     my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
1.236     raeburn  1424:     my $duedate=&Apache::lonhomework::due_date($id);
1.117     albertel 1425:     my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
                   1426:     if ( $opendate && $curtime > $opendate &&
                   1427:          $duedate && $curtime > $duedate &&
                   1428:          $answerdate && $curtime > $answerdate) {
                   1429:         return 3;
                   1430:     }
                   1431:     my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
                   1432:     if ($status =~ /^correct/) { return 1; }
                   1433:     if (!$status) { return undef; }
                   1434:     my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
                   1435:     if ($maxtries eq '') { $maxtries=2; }
                   1436:     my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
                   1437:     if ($curtries < $maxtries) { return 0; }
                   1438:     return 2;
                   1439: }
                   1440: 
1.177     albertel 1441: =pod
                   1442: 
1.210     raeburn  1443: =item setup_prior_tries_hash()
1.177     albertel 1444: 
                   1445:   Foreach each past .submission $func is called with 3 arguments
                   1446:      - the mode to set things up for (currently always 'grade')
                   1447:      - the stored .submission string
                   1448:      - The expansion of $data
                   1449: 
                   1450:   $data is an array ref containing elements that are either
                   1451:     - scalars that are other elements of the history hash to pass to $func
                   1452:     - ref to data to be passed untouched to $func
                   1453: 
1.221     raeburn  1454:   $questiontype is the questiontype (currently only passed in if
                   1455:       randomizebytry.
                   1456: 
1.177     albertel 1457: =cut
                   1458: 
1.162     albertel 1459: sub setup_prior_tries_hash {
1.221     raeburn  1460:     my ($func,$data,$questiontype) = @_;
1.162     albertel 1461:     my $part = $Apache::inputtags::part;
1.221     raeburn  1462:     my $id   = $Apache::inputtags::response[-1];
1.162     albertel 1463:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1.221     raeburn  1464:         my $partprefix = "$i:resource.$part";
                   1465: 	my $sub_key   = "$partprefix.$id.submission";
1.162     albertel 1466: 	next if (!exists($Apache::lonhomework::history{$sub_key}));
1.221     raeburn  1467:         my $type_key = "$partprefix.type";
                   1468:         my $type = $Apache::lonhomework::history{$type_key};
1.162     albertel 1469: 	my @other_data;
1.221     raeburn  1470:         if (ref($data) eq 'ARRAY') {
                   1471: 	    foreach my $datum (@{ $data }) {
                   1472: 	        if (ref($datum)) {
                   1473: 		    push(@other_data,$datum);
                   1474: 	        } else {
                   1475: 		    my $info_key = "$i:resource.$part.$id.$datum";
                   1476: 		    push(@other_data,$Apache::lonhomework::history{$info_key});
                   1477: 	        }
1.162     albertel 1478: 	    }
1.221     raeburn  1479:         }
                   1480:         if ($questiontype eq 'randomizetry') { 
                   1481:             my $order_key = "$partprefix.$id.foilorder";
                   1482:             my @whichopts = &Apache::lonnet::str2array($Apache::lonhomework::history{$order_key});
                   1483:             if (@whichopts > 0) {
                   1484:                 shift(@other_data);
                   1485:                 unshift(@other_data,\@whichopts);
                   1486:             }
                   1487:         }
1.162     albertel 1488: 	my $output =
                   1489: 	    &$func('grade',
                   1490: 		   $Apache::lonhomework::history{$sub_key},
                   1491: 		   \@other_data);
                   1492: 	if (defined($output)) {
                   1493: 	    $Apache::inputtags::submission_display{$sub_key} = $output;
                   1494: 	}
                   1495:     }
                   1496: }
                   1497: 
1.1       albertel 1498: 1;
                   1499: __END__
1.38      albertel 1500:  
1.208     jms      1501: =pod
                   1502: 
1.209     www      1503: =cut

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