General developer forum

 
 
Picture of Guilherme Radiske
How to return coursemodule id?
 

Hey guys,

I'm trying to return the value of coursemodule id but i'm not having success...

<?php
require_once('config.php');
global $COURSE;
echo $COURSE->id;
?>

I don't have experience with moodle scripting but, i manage to have access to the COURSE with the code above, so i've tried many different ways to do the same with coursemodule id but no success. Someone can help?

 
Average of ratings: -
Davo
Re: How to return coursemodule id?
Group DevelopersGroup Particularly helpful Moodlers

If you don't pass any information into the PHP file, there is no way to determine which course you are in (for example, if you have 3 different browser pages open, you could be in 3 different courses at the same time).

For this to work, the place you link to your page from would need to be something like this:

siteurl/mod/mymod/myfile.php?id=[coursemoduleid]

Then call:

$cmid = required_param('id', PARAM_INT);

To get the coursemodule ID.

See any existing Moodle page for examples of this.

 
Average of ratings: -
Picture of Guilherme Radiske
Re: How to return coursemodule id?
 

mod/page/view.php?id=137

 

That's how my url appears in the browser.


Tried almost every single exemple about $cmid but with no success. The only thing i can return with success on my test.php is the $COURSE->id that i've posted above.
 =/

I could post any exemple here that i've tested, but it is too much to see... tried again after your post to do this

 

<?php
require_once('config.php');
require_once('lib.php');
require_once($CFG->dirroot.'/mod/forum/lib.php');
require_once($CFG->libdir.'/completionlib.php');

$cmid = required_param('id', PARAM_INT);
echo $cmid;
?>

Returns nothing.

 
Average of ratings: -
Davo
Re: How to return coursemodule id?
Group DevelopersGroup Particularly helpful Moodlers

When you write:

require_once('config.php');

Is the main 'config.php' file in the same folder as your PHP script, or do you actually mean to write:

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

(as per existing examples in Moodle core).

You should also make sure you have debugging set to 'developer', if you haven't already done so (although, that won't help in this case, as you have to load the 'config.php' file correctly, before the debugging settings are initialised).

 
Average of ratings: -