--- loncom/homework/response.pm 2003/04/30 21:23:45 1.74 +++ loncom/homework/response.pm 2004/10/15 16:22:25 1.106 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # various response type definitons response definition # -# $Id: response.pm,v 1.74 2003/04/30 21:23:45 albertel Exp $ +# $Id: response.pm,v 1.106 2004/10/15 16:22:25 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -25,12 +25,10 @@ # # http://www.lon-capa.org/ # -# 11/23,11/24,11/28 Gerd Kortemeyer -# Guy Albertelli -# 08/04,08/07 Gerd Kortemeyer package Apache::response; use strict; +use Apache::lonlocal; BEGIN { &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse')); @@ -48,11 +46,22 @@ sub start_response { push (@Apache::inputtags::response,$id); push (@Apache::inputtags::responselist,$id); @Apache::inputtags::inputlist=(); + if ($Apache::inputtags::part eq '' && + !$Apache::lonhomework::ignore_response_errors) { + &Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a in a ed problem"),'<>&"')); + } + if ($Apache::inputtags::response_with_no_part && + $Apache::inputtags::part ne '0') { + &Apache::lonxml::error(&HTML::Entities::encode(&mt("<*response>s are both inside of and outside of , this is not a valid problem, errors in grading may occur."),'<>&"').'
'); + } + if ($Apache::inputtags::part eq '0') { + $Apache::inputtags::response_with_no_part=1; + } return $id; } sub end_response { - pop @Apache::inputtags::response; + #pop @Apache::inputtags::response; @Apache::inputtags::inputlist=(); return ''; } @@ -62,6 +71,7 @@ sub start_hintresponse { my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval); if ($id eq '') { $id = $Apache::lonxml::curdepth; } push (@Apache::inputtags::response,$id); + push (@Apache::inputtags::responselist,$id); push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]); return $id; } @@ -75,27 +85,58 @@ sub end_hintresponse { return ''; } -# used by response to set the non-safe space random number generator to something -# that is stable and unique based on the part number and response number -sub setrandomnumber { - my $rndseed; - if ($ENV{'request.state'} eq "construct") { - $rndseed=$ENV{'form.rndseed'}; - if (!$rndseed) { $rndseed=time; } +my @randomseeds; +sub pushrandomnumber { + my $rand_alg=&Apache::lonnet::get_rand_alg(); + if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' || + $rand_alg eq '64bit2') { + # do nothing + } else { + my @seed=&Math::Random::random_get_seed(); + push (@randomseeds,\@seed); + } + &Apache::response::setrandomnumber(); +} +sub poprandomnumber { + my $rand_alg=&Apache::lonnet::get_rand_alg(); + if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' || + $rand_alg eq '64bit2') { + return; + } + my $seed=pop(@randomseeds); + if ($seed) { + &Math::Random::random_set_seed(@$seed); } else { - $rndseed=&Apache::lonnet::rndseed(); + &Apache::lonxml::error("Unable to restore random algorithm."); } +} +sub setrandomnumber { + my $rndseed; + $rndseed=&Apache::structuretags::setup_rndseed(); + if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); } &Apache::lonxml::debug("randseed $rndseed"); # $rndseed=unpack("%32i",$rndseed); - my $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10); - if (defined($Apache::inputtags::response['-1'])) { - $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]); + my $rand_alg=&Apache::lonnet::get_rand_alg(); + my $rndmod; + if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' || + $rand_alg eq '64bit2') { + $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10); + if (defined($Apache::inputtags::response[-1])) { + $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]); + } + } else { + $rndmod=(&Apache::lonnet::numval2($Apache::inputtags::part) << 10); + if (defined($Apache::inputtags::response[-1])) { + $rndmod+=&Apache::lonnet::numval2($Apache::inputtags::response[-1]); + } } - if ($rndseed =~/,/) { - my ($num1,$num2)=split(/,/,$rndseed); + if ($rndseed =~/([,:])/) { + my $char=$1; + use integer; + my ($num1,$num2)=split(/\Q$char\E/,$rndseed); $num1+=$rndmod; $num2+=$rndmod; - $rndseed="$num1,$num2"; + $rndseed=$num1.$char.$num2; } else { $rndseed+=$rndmod; } @@ -114,8 +155,8 @@ sub meta_parameter_write { } $result.= ' name="'.$name.'"'. ' type="'.$type.'"'. -($default?' default="'.$default.'"':''). -($display?' display="'.$display.' [Part: '.$partref.']"':'') +(defined($default)?' default="'.$default.'"':''). +(defined($display)?' display="'.$display.' [Part: '.$partref.']"':'') .'>' ."\n"; return $result; @@ -176,12 +217,29 @@ sub mandatory_part_meta { # } +sub meta_part_order { + if (@Apache::inputtags::partlist) { + my @parts=@Apache::inputtags::partlist; + shift(@parts); + return ''.join(',',@parts).''."\n"; + } else { + return '0'."\n"; + } +} + +sub meta_response_order { + if (@Apache::inputtags::responselist) { + return ''.join(',',@Apache::inputtags::responselist). + ''."\n"; + } +} + sub check_for_previous { my ($curresponse,$partid,$id) = @_; my %previous; $previous{'used'} = 0; foreach my $key (sort(keys(%Apache::lonhomework::history))) { - if ($key =~ /resource\.$partid\.$id\.submission/) { + if ($key =~ /resource\.$partid\.$id\.submission$/) { &Apache::lonxml::debug("Trying $key"); my $pastresponse=$Apache::lonhomework::history{$key}; if ($pastresponse eq $curresponse) { @@ -255,7 +313,7 @@ sub end_dataresponse { my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser(); my $allowed=&Apache::lonnet::allowed('mgr',$courseid); if ($allowed) { - &Apache::response::setup_params('dataresponse'); + &Apache::response::setup_params('dataresponse',$safeeval); my $partid = $Apache::inputtags::part; my $id = $Apache::inputtags::response['-1']; my $response = $ENV{'form.HWVAL_'.$id}; @@ -274,6 +332,24 @@ sub end_dataresponse { return $result; } +sub decide_package { + my ($tagstack)=@_; + my $package; + if ($$tagstack[-1] eq 'parameter') { + $package='part'; + } else { + my $i=-1; + while (defined($$tagstack[$i])) { + if ($$tagstack[$i] =~ /(response|hint)$/) { + $package=$$tagstack[$i]; + last; + } + $i--; + } + } + return $package; +} + sub start_responseparam { my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_; my $result=''; @@ -284,6 +360,19 @@ sub start_responseparam { $token->[2]->{'description'}); } elsif ($target eq 'edit') { $result.=&Apache::edit::tag_start($target,$token); + my $optionlist; + my $package=&decide_package($tagstack); + foreach my $key (sort(keys(%Apache::lonnet::packagetab))) { + if ($key =~ /^\Q$package\E&(.*)&display$/) { + $optionlist.=''; + } + } + if (defined($optionlist)) { + $result.='Use template:
'; + } $result.=&Apache::edit::text_arg('Name:','name',$token). &Apache::edit::text_arg('Type:','type',$token). &Apache::edit::text_arg('Description:','description',$token). @@ -291,9 +380,22 @@ sub start_responseparam { ""; $result.=&Apache::edit::end_table; } elsif ($target eq 'modified') { - my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval, - 'name','type','description', - 'default'); + my $constructtag=&Apache::edit::get_new_args($token,$parstack, + $safeeval,'name','type', + 'description','default'); + my $element=&Apache::edit::html_element_name('parameter_package'); + if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') { + my $name=$ENV{"form.$element"}; + my $tag=&decide_package($tagstack); + $token->[2]->{'name'}=$name; + $token->[2]->{'type'}= + $Apache::lonnet::packagetab{"$tag&$name&type"}; + $token->[2]->{'description'}= + $Apache::lonnet::packagetab{"$tag&$name&display"}; + $token->[2]->{'default'}= + $Apache::lonnet::packagetab{"$tag&$name&default"}; + $constructtag=1; + } if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); $result.=&Apache::edit::handle_insert(); @@ -331,7 +433,7 @@ sub reset_params { } sub setup_params { - my ($tag) = @_; + my ($tag,$safeeval) = @_; if ($ENV{'request.state'} eq 'construct') { return; } my %paramlist=(); @@ -354,7 +456,9 @@ sub setup_params { &Apache::lonxml::debug("using nothing"); $Apache::inputtags::params{$key}=''; } else { - &Apache::lonxml::debug("using value"); + my $string="{return qq\0".$value."\0}"; + my $newvalue=&Apache::run::run($string,$safeeval,1); + if (defined($newvalue)) { $value=$newvalue; } $Apache::inputtags::params{$key}=$value; } } @@ -363,9 +467,9 @@ sub setup_params { sub answer_header { my ($type) = @_; my $result; - if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) { - $result = ''."\n"; + if ($ENV{'form.answer_output_mode'} eq 'tex') { + $result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'. + $Apache::inputtags::part.'| \\\\ \hline '; } else { $result = '
Answer for Part:'. - $Apache::inputtags::part. '
'."\n"; @@ -376,8 +480,8 @@ sub answer_header { sub answer_part { my ($type,$answer) = @_; my $result; - if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') { - $result = ''; + if ($ENV{'form.answer_output_mode'} eq 'tex') { + $result = ' \verb|'.$answer.'|\\\\ \hline '; } else { $result = ''; } @@ -387,8 +491,8 @@ sub answer_part { sub answer_footer { my ($type) = @_; my $result; - if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') { - $result = '
Answer for Part:'. $Apache::inputtags::part. ''.$answer.''.$answer.'
'; + if ($ENV{'form.answer_output_mode'} eq 'tex') { + $result = ' \end{tabular} \vskip 0 mm '; } else { $result = ''; } @@ -396,16 +500,18 @@ sub answer_footer { } sub showallfoils { - my $return=0; - if (defined($ENV{'form.showallfoils'}) && - $ENV{'request.state'} eq 'construct') { - $return=1; + if (defined($ENV{'form.showallfoils'})) { + my ($symb)=&Apache::lonxml::whichuser(); + if ($ENV{'request.state'} eq 'construct' || + ($ENV{'user.adv'} && $symb eq '')) { + return 1; + } } - return $return; + return 0; } sub getresponse { - my ($temp)=@_; + my ($temp,$resulttype)=@_; my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1']; my $response; if (!defined($temp)) { @@ -425,7 +531,13 @@ sub getresponse { # save bubbled letter for later $Apache::lonhomework::results{"resource.$part.$id.scantron"}.= $response; - $response = $let_to_num{$response}; + if ($resulttype ne 'letter') { + if ($resulttype eq 'A is 1') { + $response = $let_to_num{$response}+1; + } else { + $response = $let_to_num{$response}; + } + } } else { $response = $ENV{$formparm}; } @@ -449,6 +561,7 @@ sub scored_response { if ($increase ne '') { $score+=$increase+1; } } my $weight = &Apache::lonnet::EXT("resource.$part.weight"); + if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; } my $pcr=$score/$weight; $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr; $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}= @@ -456,6 +569,139 @@ sub scored_response { return $repetition; } +sub whichorder { + my ($max,$randomize,$showall,$hash)=@_; + #&Apache::lonxml::debug("man $max randomize $randomize"); + if (!defined(@{ $$hash{'names'} })) { return; } + my @names = @{ $$hash{'names'} }; + my @whichopt =(); + my (%top,@toplist,%bottom,@bottomlist); + if (!($showall || ($randomize eq 'no'))) { + my $current=0; + foreach my $name (@names) { + $current++; + if ($$hash{"$name.location"} eq 'top') { + $top{$name}=$current; + } elsif ($$hash{"$name.location"} eq 'bottom') { + $bottom{$name}=$current; + } + } + } + my $topcount=0; + my $bottomcount=0; + while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall) + && ($#names > -1)) { + #&Apache::lonxml::debug("Have $#whichopt max is $max"); + my $aopt; + if ($showall || ($randomize eq 'no')) { + $aopt=0; + } else { + $aopt=int(&Math::Random::random_uniform() * ($#names+1)); + } + #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt"); + $aopt=splice(@names,$aopt,1); + #&Apache::lonxml::debug("Picked $aopt"); + if ($top{$aopt}) { + $toplist[$top{$aopt}]=$aopt; + $topcount++; + } elsif ($bottom{$aopt}) { + $bottomlist[$bottom{$aopt}]=$aopt; + $bottomcount++; + } else { + push (@whichopt,$aopt); + } + } + for (my $i=0;$i<=$#toplist;$i++) { + if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) } + } + for (my $i=0;$i<=$#bottomlist;$i++) { + if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) } + } + return @whichopt; +} + +sub show_answer { + my $part = $Apache::inputtags::part; + my $award = $Apache::lonhomework::history{"resource.$part.solved"}; + my $status = $Apache::inputtags::status[-1]; + return ( ($award =~ /^correct/ + && lc($Apache::lonhomework::problemstatus) ne 'no') + || $status eq "SHOW_ANSWER"); +} + +sub analyze_store_foilgroup { + my ($shown,$attrs)=@_; + my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]"; + foreach my $name (@{ $Apache::response::foilgroup{'names'} }) { + if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; } + push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name); + foreach my $attr (@$attrs) { + $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} = + $Apache::response::foilgroup{"$name.".$attr}; + } + } + push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown }); +} + +sub check_if_computed { + my ($token,$parstack,$safeeval,$name)=@_; + my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval); + if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) { + my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]"; + $Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1; + } +} + +sub pick_foil_for_concept { + my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_; + if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; } + my @names = @{ $Apache::response::conceptgroup{'names'} }; + my $pick=int(&Math::Random::random_uniform() * ($#names+1)); + my $name=$names[$pick]; + push @{ $Apache::response::foilgroup{'names'} }, $name; + foreach my $attr (@$attrs) { + $Apache::response::foilgroup{"$name.".$attr} = + $Apache::response::conceptgroup{"$name.".$attr}; + } + my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval); + $Apache::response::foilgroup{"$name.concept"} = $concept; + &Apache::lonxml::debug("Selecting $name in $concept"); + my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]"; + if ($target eq 'analyze') { + push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} }, + $concept); + $Apache::lonhomework::analyze{"$part_id.concept.$concept"}= + $Apache::response::conceptgroup{'names'}; + foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) { + push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} }, + $name); + foreach my $attr (@$attrs) { + $Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}= + $Apache::response::conceptgroup{"$name.$attr"}; + } + } + } + push(@{ $hinthash->{"$part_id.concepts"} },$concept); + $hinthash->{"$part_id.concept.$concept"}= + $Apache::response::conceptgroup{'names'}; + +} + +sub get_response_param { + my ($id,$name,$default)=@_; + my $parameter; + if ($ENV{'request.state'} eq 'construct' && + defined($Apache::inputtags::params{$name})) { + $parameter=$Apache::inputtags::params{$name}; + } else { + $parameter=&Apache::lonnet::EXT("resource.$id.$name"); + } + if (!defined($parameter) || $parameter eq '') { + $parameter = $default; + } + return $parameter; +} + 1; __END__