Reputation: 411
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
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