## General developer forum

### getting user id from context

getting user id from context

Hello everyone

I see out there thousands of posts about how to obtain various moodle objects by providing a user id. However I can't find anything about how to obtain the user id from a context

I'm working on an advanced grading form. I have the controller, I have the context module, I have the page, I can even obtain the assignment object. Is there any way I can get the ID of the user that submitted the assignment for grading?

Average of ratings: -
Re: getting user id from context

Just use global $USER object. Whenever user log's in Moodle instantiates this object and any part of code can use it. Average of ratings: - Re: getting user id from context Thank you for your reply, Darko, but$USER gives me the currently logged in user, not the owner of the assignment. I need to ID of the user who submitted the assignment.

See below how I obtain the context and the assignment.

$context = context_module::instance($PAGE->cm->id);$assignment = new assign($context, $PAGE->cm,$PAGE->cm->course);

Average of ratings: -
Re: getting user id from context

I guess your terminology is what is confusing me. When you say "owner of the assignment" is it the user who created the assignment in the course?

If that is the case than you would have to refer to one of the log tables you may have enabled. Whenever a new activity is added to a course and event \core\event\course_module_created is fired.

SELECT l.userid
FROM {logstore_standard_log} l
WHERE l.eventname = '\core\event\course_module_created'
AND
l.component = 'core'
AND
l.action = 'created'
AND
l.courseid = :courseid
AND
l.objectid = :cmid



cmid would be the course module id of the activity in question and courseid a course in question.

Average of ratings: -
Re: getting user id from context

Sorry for confusion, I am trying to get the id of user being graded, but as Sam pointed below, I might not even need it. Thanks for your help.

Average of ratings: -
Re: getting user id from context

Hi Lucian,

Are you creating a new advanced grading form? Or using the advanced grading form within your plugin? The grading_instances table is what connects a user to a score from the advanced grading form (via the itemid). However, it is up to the plugin that is using the advanced grading form to decide how that is connected.

For instance, the assignment plugin (mod/assign) sets the grading_instances.itemid as the assign_grades.id for the user who submitted the assignment.

Therefore, the grading form itself doesn't "know" who is the user that is being graded directly.

Cheers,

Sam

Average of ratings: Useful (1)
Re: getting user id from context

Hi Sam,

Thanks for you reply. That makes a lot of sense, I guess this allows for blind grading to work properly as well.

I am working on a new advanced grading form and indeed I can access the itemid. However I wanted to check the current grade in the gradebook when saving a new instance of my new form. I was trying to do it like this:

$context = context_module::instance($PAGE->cm->id);$assignment = new assign($context, $PAGE->cm,$PAGE->cm->course);$gradeInDB =$assignment->get_user_grades_for_gradebook($id_of_user_being_graded); so I was trying to obtain now the$id_of_user_being_graded.

I see now that there is a rawgrade column in grading_instances table, but it's always empty. Perhaps this should be handled by my advanced grading form, therefore I should populate it instead of relying on what goes to the gradebook?

Average of ratings: -
Re: getting user id from context

Hi Lucian,

No problem. The other thing it allows is for is other plugins besides Assignment to use the advanced grading forms. So it has to be agnostic about the user to allow for that.

For example, we have a plugin that uses advanced grading in certain circumstances and it uses a different table's id as the itemid.