Hi, I got fatal error and some warning when I tried to include the myclassfile, in the current php page the code look like this
require_once('/blocks/myblock/classes/myclassfile.php');
If you're using Moodle 2.6 or above, you shouldn't need to do the require_once, you should just need to write 'new myclassfile()' (assuming the class defined in the file matches the name of the file) and automatic class loading should handle the rest.
However, if really need the require_once, it should look like every other example in Moodle:
require_once($CFG->dirroot. '/blocks/myblock/classes/myclassfile.php');
Thanks but still I got the same problem, these are the codes
<?php
require_once(dirname(__FILE__) . '/../config.php');
require_once($CFG->dirroot. '/blocks/totalpoints/classes/pointHandler.php');
$myPontO=new point_Data();
if(isset($_REQUEST['p'])){
if($_REQUEST['p']=='getPoint'){
echo $myPointO->getPoints();
//echo getP();
echo $_REQUEST['p'];
} else {
echo "0";
}
}
// When I run, the index page I got these errors
Warning:
require_once(C:\xampp\htdocs\moodle\blocks\totalpoints/../config.php): failed
to open stream: No such file or directory in C:\xampp\htdocs\moodle\blocks\totalpoints\ajaxP.php
on line 2
Fatal error: require_once(): Failed
opening required 'C:\xampp\htdocs\moodle\blocks\totalpoints/../config.php'
(include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\moodle\blocks\totalpoints\ajaxP.php
on line 2
The error you get is for the config file and not the other one.
Try replacing it into
require_once(dirname(__FILE__) . '/../../config.php');
Your code looked for the config file inside the blocks directory and needs to go up to its parent directory.
Now I don't get error, but the output is blank
Now I don't get error, but the output is blank!!, I am using Moodle 2.9.3+???
and this is my jquery afax function code
function getScorePoint(){
var p='getPoint';
$.ajax({
dataType: "Text",
data: {p:p},
url: "blocks/totalpoints/ajaxP.php",
success: function (data) {
$(".np").html(data);
},
error: function (xhr, status, error) {
alert(status + error + xhr.responseText);
}
});
}
the concept is I want to display points, using ajax method, so I created a class with a method 'getPoints()' that fetch the points from the database, then I created the ajaxP.php to handle the ajax operation as shown below
<?php
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot. '/blocks/totalpoints/classes/pointHandler.php'); // this is a file with a getPoints() method
$myPontO=new point_Data();
print_object($myPontO);
if(isset($_REQUEST['p'])){
if($_REQUEST['p']=='getPoint'){
echo $myPointO->getPoints();
echo $_REQUEST['p'];
} else {
echo "0";
}
}
But when I run the index page I got blank, no output,
Any assistance please!!!
Some correction to the code above
this line of code print_object($myPontO);
is not there, I placed before for just testing
You should enable Moodle debugging at DEVELOPER level to read what is going on with your code.
Besides, try to use the Moodle framework and avoid using $_REQUEST['p'] but $p = required_param('p', PARAM_ALPHANUM);.
HTH,
Matteo
Thanks, let me work on your suggestions.
Happy new year ARI,
Your first require_once is pointing to the blocks directory instead of the moodleroot directory. Try this instead...
Replace:
require_once(dirname(__FILE__) . '/../config.php');
...with:
require_once dirname(dirname(dirname(__FILE__))).'/config.php';
Note: Use one additional dirname if your php file is located in a subdirectory of your plugin's directory.
The result is that it will look for config.php in:
C:\xampp\htdocs\moodle\config.php
...instead of:
C:\xampp\htdocs\moodle\blocks\config.php
Hope you find this useful.
Best regards,
Michael Milette
Thanks Michael Milette
It works, but now the problem is my ajax page run well only in site home when I try to navigate to other page, my plugin page displays but the ajax operation fail and display this error info:
errorNot Found<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Object not found!</title>
<link rev="made" href="mailto:postmaster@localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/
body { color: #000000; background-color: #FFFFFF; }
a:link { color: #0000CC; }
p, address {margin-left: 3em;}
span {font-size: smaller;}
/*]]>*/--></style>
</head>
<body>
<h1>Object not found!</h1>
<p>
The requested URL was not found on this server.
The link on the
<a href="http://localhost/moodle/course/view.php%3fid=3">referring
page</a> seems to be wrong or outdated. Please inform the author of
<a href="http://localhost/moodle/course/view.php%3fid=3">that page</a>
about the error.
</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.
</p>
<h2>Error 404</h2>
<address>
<a href="/">localhost</a><br />
<span>Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7</span>
</address>
</body>
</html>
Any assistance please?..
The problem is in your Javascript code, specifically the following line:
url: "blocks/totalpoints/ajaxP.php",
This path only exists when it is accessed from the Moodle root directory. As soon as you leave it and go to /course/ for example, it would end up looking for /course/blocks/totalpoints/ajaxP.php which doesn't exist of course.
There are a few ways I can think of to make this work. If this plugin is only for this particular site and the site will always be located in the same path relative to the webroot, you could simply hard code the path to be /blocks/totalpoints/ajaxP.php (assuming your site is in the webroot). While this is definitely the simplest and quickest approach, it will only work if your file is in a particular location on your website.
If you are developing a plugin that needs to work regardless of where the Moodle site is installed or you are developing a plugin that will be shared with others, this approach will quickly fail if Moodle, for example, is installed in http://localhost/mymoodle/ instead of http://localhost/.
One way to get around that would be to put your script in a .php instead of a .js file and have the path generated dynamically using something like "url: "<?php echo $CFG->wwwroot; ?>/blocks/totalpoints/ajaxP.php". If you don't want to put your whole script in a PHP file, you could have your PHP script set a global Javascript variable that would contain the path to the root of your Moodle site which would be referenced in your ajax call. Something as simple as the following could be enough:
echo '<script>var wwwroot="'.$CFG->wwwroot.'";</script>';
Then, in your javascript file, the url: "blocks/totalpoints/ajaxP.php", line would be changed to:
url: wwwroot+"/blocks/totalpoints/ajaxP.php",
There likely other ways to do this but this one is pretty straight forward and should quickly get you moving in the right direction.
Let me know if you have any further questions.
Hope you find this useful.
Best regards,
Michael Milette
If you are writing javascript in Moodle, then the website address is already provided to you - you don't need to add extra PHP to add such information.
Try:
var url = M.cfg.wwwroot + '/blocks/totalpoints/ajaxP.php'
(It is worth browsing through the Moodle javascript code, as there are quite a few useful bits and pieces in M.cfg and M.util).
Thanks now It works fine.
Thanks for the tip Davo! I'm going to find this useful as well.