Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from custom profile field in moodle in code?

How to get value from custom profile field in moodle?
i want to include the profile field in user.php

like image 557
Irvan Santoso Avatar asked Oct 27 '25 23:10

Irvan Santoso


1 Answers

If you want to load all the user profile fields, then you can start with a user object

$user = $DB->get_record('user', array('id' => $userid));

and then call

profile_load_custom_fields($user); 

to fill in the $user->profile with an array of all the custom fields.

Alternatively, you can get the contents of a single profile field via:

$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' =>  $userid, 'fieldname' => 'NameOfCustomField');

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

Note this code is written off the top of my head and untested, so it may have some typos in it.

like image 189
davosmith Avatar answered Oct 30 '25 14:10

davosmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!