Upgrading Javascript code embebed in php

Upgrading Javascript code embebed in php

by Javier Laguna -
Number of replies: 10
Hello! I have a php file with embebed javascript code. It seems to be working good(it access the database correctly) but it only appears a white screen after doing the process and no message is display. What could be wrong? Should I create a new js file?can I avoid this somehow? thank you, Javier.
Average of ratings: -
In reply to Javier Laguna

Re: Upgrading Javascript code embebed in php

by Javier Laguna -
What I have discovered is that is not redirecting to another file. It was using a window.location javascript function. How can I export it to a js file and call it from the php?
In reply to Javier Laguna

Re: Upgrading Javascript code embebed in php

by Deleted user -

Javier,

 

may be it helps if you explain your problem better and attach your PHP file.

 

Currently it's not very clear what your actual question and problem is

 

RIes

In reply to Deleted user

Re: Upgrading Javascript code embebed in php

by Alejandro Michavila Pallarés -
Almost all javascript should be in separated .js files: http://docs.moodle.org/en/Development:JavaScript_guidelines And then call them using: $PAGE->requries->js_init_call()
In reply to Alejandro Michavila Pallarés

Re: Upgrading Javascript code embebed in php

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Note that $PAGE->requries->js_init_call() is Moodle 2.0 code. In Moodle 1.9 the require_js function is a close equivalent.

Average of ratings: Useful (2)
In reply to Alejandro Michavila Pallarés

Re: Upgrading Javascript code embebed in php

by Javier Laguna -

Hello,

sorry for the delay, but I was for a while doing another tasks.

My problem "uptodate":

In the old module I have a "view.php" there is some inline javascript code that create a new class:

var info = new Info('<?php echo "$CFG->wwwroot";?>', <?php echo "$course->id";?>);

I have to upgrade it to the new javascript way of coding so I'm moving it to a new "module.js" file.

I know how to use functions that I have in this "module.js" using $PAGE->requires->js_init_call but I dont know the way to create new classes in the php file. How I "store" them in the "module.js" file and how I call them?

Thank you very much beforehand for the help and sorry for the delay to answer again.

Javier.

In reply to Javier Laguna

Re: Upgrading Javascript code embebed in php

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, it would be better if you did not have to add your var info to the global namespace. After all, that is a fairly common word, that someone else might use as a variable name.

It looks like you are trying to pass some basic configuration information to your JavaScript code. I think you will find that wwwroot is already available in JS as M.cfg.wwwroot, so you can just use that.

For the current course id. Well, you could just use the fact that among the many classes on the body tag, there is one that is course-123, where 123 is the course id. Or, you can pass it to your init function. That is, in PHP do

$PAGE->requires->js_init_call('M.mod_mymodule.init', array($course->id));

And in module.js, do

var init; // It would be better to get rid of this.

M.mod_mymodule = {};

M.mod_mymodule.init = function(courseid) {

init = new Init(M.cfg.wwwroot, courseid);
}

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Upgrading Javascript code embebed in php

by hon wai lau -

Hi Tim,

What is the equivalent way to get the wwwroot in the moodle 1.9? My javascript needs this information to connect with the correct php file. It is used in my plugin so it is better to be self-contained (does not need to change code in other lib) and it should work in both question editing form, quiz and preview.

Thanks
Hon Wai

In reply to hon wai lau

Re: Upgrading Javascript code embebed in php

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

In 1.9, you use the require_js function to link to your JavaScript.

Then you have to just output a script tag, e.g.:

echo '<script type="text/javascript">init_my_code(' . json_encode($CFG->wwwroot) . ')</script>'

In reply to Tim Hunt

Re: Upgrading Javascript code embebed in php

by hon wai lau -

Thanks. One more thing. The API of question type is changing the function find_standard_scripts_and_css() to find_standard_scripts() in moodle 2.0. So how to include the css for the question type plugin?

Hon Wai

In reply to hon wai lau

Re: Upgrading Javascript code embebed in php

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

In Moodle 2.0, and plugin can have a file called styles.css in the plugin folder (for example question/type/myqtype/styles.css). The Moodle theme system finds all these files, and automatically serves them to the user. That is why the question type code no longer has to do anything.