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

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

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