Retrieve custom field value to be used in mod plugin

Retrieve custom field value to be used in mod plugin

by David Pesce -
Number of replies: 1
Picture of Plugin developers

I have an existing mod plugin that I'm looking to retrieve a custom user field value within.

I have access to $USER, $CFG, and $DB objects, but am not sure exactly how to retrieve a custom user field (yes it's already created).

This thread mentions it, but does not resolve it. https://moodle.org/mod/forum/discuss.php?d=266474

Thank you!

Average of ratings: -
In reply to David Pesce

Re: Retrieve custom field value to be used in mod plugin

by David Pesce -
Picture of Plugin developers

Here's how I ended up doing it:

$sql = "SELECT ud.data FROM {user_info_data} ud 
JOIN {user_info_field} uf ON uf.id = ud.fieldid
WHERE ud.userid = :userid and uf.shortname = :fieldname";

$params = array('userid' => $USER->id, 'fieldname' => 'uuid');

$fieldvalue = $DB->get_field_sql($sql, $params);

Works like a charm!

Average of ratings: Useful (1)