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

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

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