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
Re: Upgrading Javascript code embebed in php
Re: Upgrading Javascript code embebed in php
Note that $PAGE->requries->js_init_call() is Moodle 2.0 code. In Moodle 1.9 the require_js function is a close equivalent.
Re: Upgrading Javascript code embebed in php
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.
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);
}
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 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>'
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 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.