global variable

global variable

von Antonio Garmendía -
Anzahl Antworten: 11

Hi Gareth:

I'm using Moodle 2.4.1 and when I use the global variable $COURSE->id, debugging with firebug(var_dump($COURSE)), the id number is different when I look into the table mdl_course. What I have to do to achieve the true id of the course?

 

Cheers,

Tony

 

Als Antwort auf Antonio Garmendía

Re: global variable

von Gareth J Barnard -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers

Dear Tony,

Odd as '$COURSE->id' should match the 'id' attribute of the course table.

Humm.  Do you have a screen shot of the comparison?

Cheers,

Gareth

Als Antwort auf Gareth J Barnard

Re: global variable

von Antonio Garmendía -

Dear Gareth,

There are the screenshot. The true id is 5 and the global variable has , maybe I'm calling the variable in a way that I shouldn't?

Cheers,

Tony

 

Als Antwort auf Antonio Garmendía

Re: global variable

von Gareth J Barnard -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers

Dear Tony,

Thanks lächelnd

I see that course id 5 matches course with the short code of SNCI both in the screen shot and in the database.  The $COURSE global appears to be pointing to the site course - id of 1.  Therefore, what file are you accessing the global from or is this a debug in FireBug thing?

Where have you put 'firebug(var_dump($COURSE))'?

I'll do a compare with one of my Moodles.

Cheers,

Gareth

Als Antwort auf Gareth J Barnard

Re: global variable

von Davo Smith -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Peer reviewers Nutzerbild von Plugin developers

You might want to take a look through the code (starting at course/view.php) to see where 'require_login' is called.

The first parameter passed to 'require_login' is the $course object and that is used to initialise the $COURSE global variable and (and $PAGE->course). 

If you add in some var_dump() commands just after require_login you should see the correct details listed for the $COURSE object (and they should match the $course variable passed in to require_login).

Als Antwort auf Davo Smith

Re: global variable

von Antonio Garmendía -

Hi: Sorry for the delay. I've been busy and I still have this problem. This is the piece of code(save_comment.php)

if (!defined('AJAX_SCRIPT')) {
    define('AJAX_SCRIPT', true);
}

require_once(dirname(__FILE__).'/../../../../config.php');

global $DB, $USER, $CFG, $COURSE;

require_once($CFG->dirroot.'/course/format/socialmedia/lib.php');

require_login();//Login!!

//require_once($CFG->dirroot.'/course/format/socialmedia/lib.php');
//Login!!
// Get POST data.
$comment  = required_param('comment',PARAM_TEXT);

//Atributos de bbs_comment
$insert_bbs = new stdClass();
$insert_bbs->courseid  = $COURSE->id;
$insert_bbs->userid  = $USER->id;
$insert_bbs->content = $comment;
$insert_bbs->timecreated = time();
$insert_bbs->parent_id = 0;

//Insertar bbs_comment
$resultado_comment = $DB->insert_record('bbs_comment',$insert_bbs);
echo $resultado_comment;    
var_dump($COURSE);

I call this .php file with yui(Ajax)

var onclick_guardar = function(){
        var comments = document.getElementById('bbs_comment');
        var temparray = '';
        temparray = 'comment' + '=' + comments.value + '&';
        Y.io(M.cfg.wwwroot + '/course/format/socialmedia/actions/save_comment.php', {
            on: {
                    success:M.format_socialmedia.success,
                    failure:M.format_socialmedia.failure                    
                }, context: M.format_socialmedia, method: 'post', data: temparray});                
    }

 

Hope that you can help me. Thansk!!

Cheers,

Antonio

 

Als Antwort auf Antonio Garmendía

Re: global variable

von Gareth J Barnard -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers

Dear Antonio,

Where is 'save_comment.php' being included / called from?

Cheers,

Gareth

Als Antwort auf Gareth J Barnard

Re: global variable

von Antonio Garmendía -

Dear Gareth:

Sorry but I don´t understand your question, I'll try to answer. The .php file is a folder that I create it and called "actions" inside the socialmedia(name of the course format) folder. Maybe this folder don´t have permission to access to the variable. Hope that you understand me.

Cheers,

Antonio

Als Antwort auf Antonio Garmendía

Re: global variable

von Gareth J Barnard -
Nutzerbild von Core developers Nutzerbild von Particularly helpful Moodlers Nutzerbild von Plugin developers

Dear Antonio,

My thought was that $COURSE was not populated as desired because it was being used outside of a course (the format).

I also thought that course format folders can only have ten characters in their name.  Need to double check this has not changed in M2.x+.  Have to go now but will look later.

Could you zip and post the entire code please?

Cheers,

Gareth

Als Antwort auf Antonio Garmendía

Re: global variable

von Marina Glancy -
Nutzerbild von Core developers Nutzerbild von Moodle HQ Nutzerbild von Moodle Workplace team Nutzerbild von Peer reviewers Nutzerbild von Plugin developers Nutzerbild von Testers

Hello Antonio,

you need to pass the courseid parameter to your AJAX script and call

$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course)

instead of just

require_login();

Otherwise your script has no idea where it is used

Als Antwort auf Marina Glancy

Re: global variable

von Antonio Garmendía -

Hi,

Finally, I get the parameter courseid from the current url using javascript and pass it to the .php file. If there is a better way, please let me now. Gareth I check the characters and in Moodle 2.4 is until 21 characters. Thanks to all!!

Cheers,

Antonio

var onclick_guardar = function(){
        
        var comments = document.getElementById('bbs_comment');
        var temparray = '';
        var courseid = QueryString.id;
        temparray = 'comment' + '=' + comments.value + '&' + 'courseid' + '=' + courseid + '&';
        Y.io(M.cfg.wwwroot + '/course/format/socmed/actions/save_comment.php', {
            on: {
                    success:M.format_socmed.success,
                    failure:M.format_socmed.failure                    
                }, context: M.format_socmed, method: 'post', data: temparray});                
    }

//save_comment.php

  
 if (!defined('AJAX_SCRIPT')) {
    define('AJAX_SCRIPT', true);
}

require_once(dirname(__FILE__).'/../../../../config.php');

global $DB, $USER, $CFG;

require_once($CFG->dirroot.'/course/format/socmed/lib.php');


$comment  = required_param('comment',PARAM_TEXT);
$courseid  = required_param('courseid',PARAM_INT);


$course = $DB->get_record('course', array('id' => $courseid),'*', MUST_EXIST);
require_login($course);


//Atributos de bbs_comment
$insert_bbs = new stdClass();
$insert_bbs->courseid  = $course->id;
$insert_bbs->userid  = $USER->id;
$insert_bbs->content = $comment;
$insert_bbs->timecreated = time();
$insert_bbs->parent_id = 0;

//Insertar bbs_comment
$resultado_comment = $DB->insert_record('bbs_comment',$insert_bbs);