--- loncom/homework/structuretags.pm 2019/11/04 18:01:04 1.512.2.20 +++ loncom/homework/structuretags.pm 2020/09/29 18:03:03 1.512.2.23.2.2 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # definition of tags that give a structure to a document # -# $Id: structuretags.pm,v 1.512.2.20 2019/11/04 18:01:04 raeburn Exp $ +# $Id: structuretags.pm,v 1.512.2.23.2.2 2020/09/29 18:03:03 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -63,6 +63,7 @@ use Apache::londefdef; use Apache::lonenc(); use Apache::loncommon(); use Time::HiRes qw( gettimeofday tv_interval ); +use HTML::Entities(); use lib '/home/httpd/lib/perl/'; use LONCAPA; @@ -652,13 +653,28 @@ sub setup_rndseed { } if ($Apache::lonhomework::type eq 'randomizetry') { my $partfortries = $Apache::inputtags::part; +# +# Where question type is "randomizetry" for a problem containing +# a single part (and unless type is explicitly set to not be +# "randomizetry" for that part), the number of tries used to +# determine randomization will be for that part, and randomization +# from calls to &random() in a perl script block before the part tag, +# will change based on the number of tries, and value of the +# "randomizeontries" parameter in effect for the single part. +# if (ref($probpartlist) eq 'ARRAY') { - if ((@{$probpartlist} == 1) && ($probpartlist->[0] ne $Apache::inputtags::part)) { - $partfortries = $probpartlist->[0]; + if ((@{$probpartlist} == 1) && ($probpartlist->[0] ne $partfortries)) { + if (&Apache::lonnet::EXT('resource.'.$probpartlist->[0].'.type') eq 'randomizetry') { + $partfortries = $probpartlist->[0]; + } else { + $partfortries = ''; + } } } - $numtries = $Apache::lonhomework::history{"resource.$partfortries.tries"}; - $reqtries = &Apache::lonnet::EXT("resource.$partfortries.randomizeontries"); + if ($partfortries ne '') { + $numtries = $Apache::lonhomework::history{"resource.$partfortries.tries"}; + $reqtries = &Apache::lonnet::EXT("resource.$partfortries.randomizeontries"); + } } if (($env{'request.state'} eq "construct") || ($symb eq '') @@ -1373,9 +1389,11 @@ sub firstaccess_msg { my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'}); my $buttontext = &mt('Show Resource'); my $timertext = &mt('Start Timer?'); + my $shownsymb = &HTML::Entities::encode(&Apache::lonenc::check_encrypt($symb),'\'"<>&'); $result .= (< + ENDCHECKOUT @@ -1417,6 +1435,7 @@ sub init_problem_globals { @Apache::structuretags::whilebody=(); @Apache::structuretags::whileline=(); $Apache::lonhomework::scantronmode=0; + $Apache::lonhomework::randomizetrypart=0; undef($Apache::lonhomework::name); undef($Apache::lonhomework::default_type); undef($Apache::lonhomework::type); @@ -1440,6 +1459,7 @@ sub reset_problem_globals { undef($Apache::lonhomework::default_type); undef($Apache::lonhomework::type); undef($Apache::lonhomework::scantronmode); + undef($Apache::inputtags::randomizetrypart); undef($Apache::lonhomework::ignore_response_errors); undef(@Apache::functionplotresponse::callscripts); &Apache::lonhomework::reset_show_problem_status(); @@ -1539,7 +1559,8 @@ sub start_problem { $target eq 'tex') { if ($env{'form.markaccess'}) { my @interval=&Apache::lonnet::EXT("resource.0.interval"); - my $is_set = &Apache::lonnet::set_first_access($interval[1],$interval[0]); + my ($timelimit) = ($interval[0] =~ /^(\d+)/); + my $is_set = &Apache::lonnet::set_first_access($interval[1],$timelimit); unless (($is_set eq 'ok') || ($is_set eq 'already_set')) { $firstaccres = $is_set; } @@ -1557,11 +1578,13 @@ sub start_problem { $name); } elsif (($target eq 'grade') && ($Apache::lonhomework::type eq 'randomizetry')) { my ($symb)= &Apache::lonnet::whichuser(); - my $navmap = Apache::lonnavmaps::navmap->new(); - if (ref($navmap)) { - my $res = $navmap->getBySymb($symb); - if (ref($res)) { - $probpartlist = $res->parts(); + if ($symb ne '') { + my $navmap = Apache::lonnavmaps::navmap->new(); + if (ref($navmap)) { + my $res = $navmap->getBySymb($symb); + if (ref($res)) { + $probpartlist = $res->parts(); + } } } } @@ -1572,7 +1595,14 @@ sub start_problem { if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') { + my ($symb) = &Apache::lonnet::whichuser(); #handle rand seed in construction space + if (($env{'request.state'} eq 'construct') || ($symb eq '')) { + my $partorder=&Apache::lonnet::metadata($env{'request.uri'},'partorder'); + if ($partorder ne '') { + @{$probpartlist} = split(/,/,$partorder); + } + } my $rndseed=&setup_rndseed($safeeval,$target,$probpartlist); if (($target eq 'grade') && &Apache::response::submitted()) { if ($Apache::lonhomework::type eq 'randomizetry') { @@ -1587,7 +1617,6 @@ sub start_problem { } } } - my ($symb)=&Apache::lonnet::whichuser(); if ($env{'request.state'} ne "construct" && ($symb eq '' || $Apache::lonhomework::type eq 'practice')) { @@ -1622,9 +1651,15 @@ sub start_problem { if (($env{'request.state'} ne "construct") && ($Apache::lonhomework::type eq 'randomizetry') && ($status eq 'CAN_ANSWER')) { - my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries"); - my $problemstatus = &get_problem_status($Apache::inputtags::part); - $form_tag_start.=&randomizetry_problem_header($problemstatus,$reqtries); + my @parts; + if (ref($probpartlist) eq 'ARRAY') { + @parts = @{$probpartlist}; + } + unless (@parts) { + my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries"); + my $problemstatus = &get_problem_status($Apache::inputtags::part); + $form_tag_start.=&randomizetry_problem_header($problemstatus,$reqtries,$symb); + } } my $expression='$external::datestatus="'.$status.'";'; @@ -1945,7 +1980,24 @@ sub end_problem { } } if ($target eq 'web') { - $result.=&Apache::functionplotresponse::init_script(); + $result.=&Apache::functionplotresponse::init_script(); + if ($Apache::lonhomework::default_type eq 'randomizetry') { + my ($symb) = &Apache::lonnet::whichuser(); + if ((($env{'request.state'} eq 'construct') || ($symb eq '')) && + ($status eq 'CAN_ANSWER')) { + unless (@Apache::inputtags::partlist > 1) { + $result.= <<"ENDJS"; + +ENDJS + } + } + } } if ($target eq 'grade') { &Apache::lonhomework::showhash(%Apache::lonhomework::results); @@ -2744,20 +2796,14 @@ sub start_part { } elsif ($target eq 'web') { if ($status eq 'CAN_ANSWER') { my $problemstatus = &get_problem_status($Apache::inputtags::part); - my $probrandomize = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].type"); - my $probrandtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].randomizeontries"); my $num = scalar(@Apache::inputtags::partlist)-1; - if ($probrandomize eq 'randomizetry') { - if (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.type") ne 'randomizetry') { - $result .= &randomizetry_part_header($problemstatus,'none',$num); - } else { - my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries"); - if ($probrandtries ne $reqtries) { - $result .= &randomizetry_part_header($problemstatus,$reqtries,$num); - } - } - } elsif (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.type") eq 'randomizetry') { - my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries"); + if ((($Apache::lonhomework::default_type eq 'randomizetry') || + ($Apache::lonhomework::randomizetrypart)) && + ($Apache::lonhomework::type ne 'randomizetry')) { + $result .= &randomizetry_part_header($problemstatus,'none',$num); + } elsif ($Apache::lonhomework::type eq 'randomizetry') { + $Apache::lonhomework::randomizetrypart = 1; + my $reqtries = &Apache::lonnet::EXT("resource.$id.randomizeontries"); $result .= &randomizetry_part_header($problemstatus,$reqtries,$num); } } @@ -3077,7 +3123,7 @@ sub practice_problem_header { } sub randomizetry_problem_header { - my ($problemstatus,$reqtries) = @_; + my ($problemstatus,$reqtries,$symb) = @_; my ($header,$text); if ($reqtries > 1) { $header = &mt('New Problem Variation After Every [quant,_1,Try,Tries]',$reqtries); @@ -3097,8 +3143,13 @@ sub randomizetry_problem_header { $text = &mt('A new variation will be generated after each try until correct or tries limit is reached.'); } } - return '

'.$header.'

'. - ''.$text.'
'; + if (($env{'request.state'} eq "construct") || ($symb eq '')) { + return ''; + } else { + return '

'.$header.'

'. + ''.$text.'
'; + } } sub randomizetry_part_header { @@ -3106,7 +3157,7 @@ sub randomizetry_part_header { my ($header,$text); if ($reqtries eq 'none') { $header = &mt('No Question Variation'); - $text = &mt('For this question there will no new variation after a try.'); + $text = &mt('For this question there will be no new variation after a try.'); } elsif ($reqtries > 1) { $header = &mt('New Question Variation After Every [quant,_1,Try,Tries]',$reqtries); if (($problemstatus eq 'no') ||