Getting an Id of a course in theme

Getting an Id of a course in theme

by Tarcísio Rosa -
Number of replies: 6

Guys,

I'm trying to develop a theme based on boost where I can choose settings like preset, backgroundimage and etc by course, but when I try to recover a courseid inside the function pluginfile of my theme I can't retrieve the actual course id. When I use the global $PAGE variable he always return id from the first course (which means, the site)...

There's any way I can retrieve de current course that the theme are been loaded wihtout $PAGE or $COURSE globals ? Have anyone tried this ?


Thanks in advance.

Average of ratings: -
In reply to Tarcísio Rosa

Re: Getting an Id of a course in theme

by Anthony Rimmer -
Hi Tarcísio,

That sounds like it could be a logic error - Returning a Boolean value instead (1 = font page/dashboard depending on your setup).

If you're trying to get the Course Id, you probably will need the $COURSE global.

Can you show a little of the code where you are trying to get ID?

Anthony
In reply to Anthony Rimmer

Re: Getting an Id of a course in theme

by Tarcísio Rosa -
Sorry for delaying in answer...

Here, I took a piece of code from example of implementation of pluginfile function. https://docs.moodle.org/dev/File_API#Serving_files_to_users

function theme_custom_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {

global $PAGE;

if ($context->contextlevel == CONTEXT_SYSTEM && ($filearea === 'customfilearea')) {
// here I'm taking the configuration from config_plugins basically
$theme = theme_config::load('boost');
// By default, theme files must be cache-able by both browsers and proxies.
if (!array_key_exists('cacheability', $options)) {
$options['cacheability'] = 'public';
}
// Here I have my function that I want to pass the course being accessed to take the configurations from my table created with the instalation of the theme
$courseconfig = my_custom_function_in_lib_of_theme_plugin($PAGE->course->id); // I've put $PAGE->course->id here but doesn't work

return $theme->setting_file_serve($filearea, [$courseconfig->$customfilearea], $forcedownload, $options);
} else {
send_file_not_found();
}
}

In reply to Tarcísio Rosa

Re: Getting an Id of a course in theme

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

$course->id

should work.
If I've correctly understand the problem. I suppose you're in this function
theme_cdead_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array())

Dominique.
In reply to Dominique Palumbo

Re: Getting an Id of a course in theme

by Tarcísio Rosa -
Well,

I've tried with $course (from the function param) and even evoking $COURSE global variable, both them were empty.

I've had this idea to cutomize the boost theme so I could in the same theme using multiple presets file, and those are bonded to a course, then when I load, enter in a course, the theme load the preset related to that course. I will keep trying. If I solve this I will put the answer here.
In reply to Tarcísio Rosa

Re: Getting an Id of a course in theme

by Tarcísio Rosa -
An information, the variable $COURSE show the main course, categoryid = 0 and the first course, even if I enter in another course. with id 3 for instance.
In reply to Tarcísio Rosa

Re: Getting an Id of a course in theme

by Anthony Rimmer -
Just to clarify, you are entering the variable like this(?):

$COURSE->id, and not $PAGE->$COURSE->id?

You could try printing/dumping the ID property without going through all of your function to see if you get an expected result?

 global $COURSE;
var_dump("The Course ID is: " . $COURSE->id);
You might need to inspect your page with something such as Chrome Developer Tools to see the result.

Anthony
Average of ratings: Useful (2)