Editing the PHP

Editing the PHP

by Gary McKnight -
Number of replies: 5

I am trying to customized the PHP so that the SQL can compare one of the ID fields to the current user's ID and only bring up users who have the same ID. I'm trying to do this by adding a function like the %%USERID%% in to the php. The problem I am having is that the block refuses to believe that the ID field I'm looking  for exists.

My Code: 

$sql = str_replace('%%AGENCYID%%', $USER->profile["agencyid"], $sql);


I tested the '$USER->profile["agencyid"]' bit on a different page and it echoed just fine. I've also attempted to assign it to a variable and even forced it into a string just to be safe. I've also tried json encoding/decoding it into an array and using $USER["profile"]["agencyid"] as well which again worked in the test page but only throws a read error when used in this block.

The exact error that I get is, "Error reading from database." I don't get any other information, even with debug mode on as it will get. I've tried replacing the %%USERID%% with the "agencyid" variables and I've tried shoving the %%AGENCYID%% in different places in the SQL just to make triple sure, but nothing has worked or given me any hint on what is wrong. Please help. 

Average of ratings: -
In reply to Gary McKnight

Re: Editing the 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, we cannot debug your code from this distance. You need to learn the techniquest of debugging code in Moodle yourself.

First, have you got Debugging turned on, and set to DEVELOPER level?

Next, what is you code actually doing. What is $sql before and after the str_replace call? Add

print_object($sql);

Twice. If that does not make it clear, perhaps check that $USER is set to what you think it is:

print_object($USER);

... and so on.

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

Re: Editing the PHP

by Diego Couce -

Hi Tim,

I red the article on AOSA and I found it extremely useful for moodle newbies like me. tongueout

I seeking for debugging functions and I met accross print_object() and it's amaizing.

I got mad trying to use some like I found around:

echo $OUTPUT->box('zarazaaaa');

echo $OUTPUT->notification('Helloo');

print_error( $course->id, 'Hasta aca llego: ',$CFG->wwwroot.'/course/view.php?id='.$courseid); (Fatal: stops processing php code)


Could you point me on a mofe efective way to debug messages like print_object()?

Regards,
Diego.
Moodle 2.7.2+ (Build: 20140925)


In reply to Diego Couce

Re: Editing the PHP

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Moodle has its own debugging built in - just turn it on to Full Developer Mode (found under Admin/Development/Debugging).

Are you adding the code to the plugin php files or trying to add it directly in the Config Report query builder?