Neil Norris
Neil Norris

Reputation: 411

Cant retrieve userid for external page (moodle)

I have an external page in moodle, I would like to use the userid in the page.

The page has a link to the moodle config (from what I have been told to do) but it just isn't working.

Any idea what else I would need to do?

Thanks!

Upvotes: 0

Views: 114

Answers (1)

Mitxel
Mitxel

Reputation: 453

Assuming that by userid you mean the current user id, a minimal approach to a page that can retrieve the current user id would be:

<?php
require_once(__DIR__ . '/config.php'); // Assuming the page is in the root dir.

$PAGE->set_context(context_system::instance());
$PAGE->set_url('/mypage.php');
$PAGE->set_title('My title');
$PAGE->set_heading('My heading');

echo $OUTPUT->header();
echo $USER->id; // Here we go!
echo $OUTPUT->footer();

Notice that $USER is one of those bootstrapped Moodle global objects (like $DB, $PAGE and so on) and do not require any kind of instantiation.

Also, don't forget that the current user may not be logged in and in that case $USER will be a representation of a guest user with a "fake" ID (not related to any user in database).

Upvotes: 1

Related Questions