set_context() for a page accessed from "My Moodle"

set_context() for a page accessed from "My Moodle"

by sakai user -
Number of replies: 4

Hi all. 

I'm working on a block that is accessed and made for an instructors /my (aka: My Moodle) page. 

The block installs correctly and the page that does my work is coming up fine in the browser pane. However, I'm getting a $PAGE->context was not set error in my logs.. and in the debug output. 

I do call require_login() in my code at the top. 

I also am generally following the page API to create my page.. In following the page guidlines I have used 

$PAGE->set_context(context_system::instance());

This does not seem to work though in setting the context. All of the other referenced set_context() calls take an argument such as a courseid or other. 

How do I get a correct page context with the type of block I'm creating? Can I give it the site courseid as the arguemnt?

Looking for guidance.. 

thanks -- kevin

Average of ratings: -
In reply to sakai user

Re: set_context() for a page accessed from "My Moodle"

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

As long as you call $PAGE->set_context(context_system::instance()); early enough on the page (mostly before you use any language strings) you should be fine with that - assuming of course, that the page really is meant to be part of the 'system' context.

If you want the page to be part of a different context (e.g. the course that the block is on, or the user context if the block is installed on a user page), then you need to pass a parameter to the page to tell it where it is being displayed (e.g. blocks/myblock/mypage.php?courseid=XX, where XX can be inserted into the link via $PAGE->course->id on the original location, or possibly discovered via $PAGE->context->instance if you are looking for the userid instead and are on the 'my' page).

In reply to Davo Smith

Re: set_context() for a page accessed from "My Moodle"

by sakai user -

Thanks! This did the trick. Or at least got me thinking on my ordering etc.. So for a page that is accessed on / from the "My Moodle" space.. the in effect context could be the user context.. Is this correct? 

Currently I have it as the system context via (below) which seems to work fine.

 

$PAGE->set_context(context_system::instance());

But if it is better practice to actually give this the user context then I can change my page. 

thanks - kevin

In reply to sakai user

Re: set_context() for a page accessed from "My Moodle"

by Hubert Chathi -

It doesn't matter where the page is accessed from, but rather what the page represents.  For example, if you had a block on the My Moodle page that linked to course assignment, then the course activity pages would still use the assignment context.

Without knowing more about what your page is for, I'd say that you probably want to just stick with the system context for your page.

In reply to Hubert Chathi

Re: set_context() for a page accessed from "My Moodle"

by sakai user -

 

"It doesn't matter where the page is accessed from, but rather what the page represents."

Excelent! That is exactly what I needed to know.. So yea I'll stick with system context for my block.

thanks - kevin