--- loncom/homework/functionplotresponse.pm 2012/08/16 23:03:01 1.97 +++ loncom/homework/functionplotresponse.pm 2013/07/22 11:46:26 1.103 @@ -1,7 +1,7 @@ # LearningOnline Network with CAPA # Functionplot responses # -# $Id: functionplotresponse.pm,v 1.97 2012/08/16 23:03:01 www Exp $ +# $Id: functionplotresponse.pm,v 1.103 2013/07/22 11:46:26 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -49,9 +49,48 @@ BEGIN { # Return a true value if HTML5 should be used. sub useHTML5 { - return 1; + if ($env{'browser.type'} eq 'chrome') { + if ($env{'browser.version'} >= 14) { + return 1; + } + } elsif ($env{'browser.type'} eq 'safari') { + if ($env{'browser.os'} eq 'mac') { + my ($prefix,$version) = ($env{'browser.version'} =~ /^(\d*)(\d{3})\./); + if ((!$env{'browser.mobile'}) || + (($env{'browser.mobile'}) && length($prefix))) { + if ($version >= 536) { + return 1; + } + } + } + } elsif ($env{'browser.type'} eq 'mozilla') { + if ($env{'browser.info'} =~ /^firefox\-(\d+)/) { + my $firefox = $1; + if ((($env{'browser.os'} eq 'mac') && ($firefox >= 20)) || + (($env{'browser.os'} eq 'unix') && ($firefox >= 17)) || + (($env{'browser.os'} eq 'win') && ($firefox >= 14))) { + return 1; + } + } + } elsif ($env{'browser.type'} eq 'explorer') { + if (($env{'browser.os'} eq 'win') && ($env{'browser.version'} >= 10)) { + return 1; + } + } + return 0; } +# +# HTML5 version does not understand "_" in IDs +# +sub appid { + my ($id)=@_; + $id=~s/\_/rid/gs; + $id=~s/\W//gs; + return $id; +} + +# # Routines to start the applet (Java) or the HTML5/JavaScript # # There can be a number of applets on a page, each called ggbApplet_$id, @@ -88,12 +127,13 @@ sub geogebra_default_parameters { sub java_geogebra_startcode { my ($id,$width,$height)=@_; + my $appid=&appid($id); $width=int(1.*$width); $height=int(1.*$height); unless ($width) { $width=700; } unless ($height) { $height=400; } return (< ENDSTARTCODE @@ -111,6 +151,7 @@ sub java_geogebra_code_param { sub html5_geogebra_startcode { my ($id,$width,$height)=@_; + my $appid=&appid($id); $width=int(1.*$width); $height=int(1.*$height); unless ($width) { $width=700; } @@ -118,7 +159,7 @@ sub html5_geogebra_startcode { my $code=&geogebra_internal_program(); return (< ENDSTARTCODE } @@ -136,6 +177,7 @@ sub geogebra_internal_program { # sub java_geogebra_default_parameters { my ($id)=@_; + my $appid=&appid($id); return(< @@ -156,7 +198,7 @@ sub java_geogebra_default_parameters { - + ENDDEFAULTPARAMETERS } @@ -171,7 +213,7 @@ sub init_script { if ($#Apache::functionplotresponse::callscripts>=0) { my $script=''; foreach my $id (@Apache::functionplotresponse::callscripts) { - $script.="if (param=='applet_$id') { loaded_$id=true; }\n"; + $script.="if (param=='ggbApplet".&appid($id)."') { loaded_$id=true; }\n"; } $script.="if (".join(' && ',map { "loaded_$_" } (@Apache::functionplotresponse::callscripts)). ") { setTimeout('ggbInitAll()',200) }"; @@ -179,7 +221,7 @@ sub init_script { my $html5init=''; if (&useHTML5()) { $html5init= - ''; + ''; } return (< // 0)) { $return.=(<0)) { $return.=(<8) { $order=8; } $Apache::functionplotresponse::counter++; my $label='CSpline'.$Apache::functionplotresponse::counter; - my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'['; + my $output='document.ggbApplet'.$appid.'.evalCommand("'.$label.'=Spline'.$order.'['; for (my $i=0;$i<=$#points;$i+=4) { $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),"; } $output=~s/\,$//; $output.=']");'."\n"; for (my $i=2; $i<2*$order; $i+=2) { - $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n"; + $output.='document.ggbApplet'.$appid.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n"; } for (my $i=1; $i<2*$order; $i+=2) { - $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n"; + $output.='document.ggbApplet'.$appid.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n"; } return $output; @@ -531,6 +583,7 @@ sub answer_spline_script { sub generate_spline { my ($id,$label,$xmin,$xmax,$ymin,$ymax,$fixed)=@_; + my $appid=&appid($id); my $result=''; my $order=$Apache::functionplotresponse::splineorder{$label}; my $x=$Apache::functionplotresponse::splineinitx{$label}; @@ -547,7 +600,7 @@ sub generate_spline { $x+=$sx/(2.*($order-1)); push(@coords,$label.'S'.$i); } - $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n"; + $result.='document.ggbApplet'.$appid.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n"; return $result; } @@ -666,6 +719,7 @@ sub start_drawvectorsum { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; my $result=''; my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1]; + my $internalappid=&appid($internalid); my $tailx=&Apache::lonxml::get_param('tailx',$parstack,$safeeval); my $taily=&Apache::lonxml::get_param('taily',$parstack,$safeeval); my $showvalue=&Apache::lonxml::get_param('showvalue',$parstack,$safeeval); @@ -694,25 +748,25 @@ sub start_drawvectorsum { my $vectorx=$thisvector.'X'; my $vectory=$thisvector.'Y'; $result.=(<'.$Apache::functionplotresponse::ruleslog.'', + '
'.$Apache::functionplotresponse::ruleslog.'
', &mt('Rules Log'))."
"); } return $result;