3

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

Irvan Santoso
  • 51
  • 1
  • 6

5 Answers5

4

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.

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • i get the error:Call to undefined function profile_load_custom_fields() – Irvan Santoso Feb 27 '15 at 02:32
  • 1
    OK - you might need to add require_once($CFG->dirroot.'/user/profile/lib.php'); to get the first bit of code to work - but the second option should still work fine. – davosmith Feb 27 '15 at 08:26
2

You can use:

$Data = $USER->profile['YourCustomField'];
1

i hope my solutions help anybody:

need this lib

require_once($CFG->dirroot.'/user/profile/lib.php'); 

and now you can use:

profile_load_data($USER);
$USER->profile_field_%your_field_name%

example $USER->profile_field_HaveAnotherGradeBook;

0

I finally made it!!!
$sql = 'SELECT DISTINCT u.id AS userid, u.username, uif.shortname, uid.data FROM prefix_user u JOIN prefix_user_info_data as uid on uid.userid = u.id JOIN prefix_user_info_field as uif on uid.fieldid = uif.id JOIN prefix_user_enrolments ue ON ue.userid = u.id JOIN prefix_enrol e ON e.id = ue.enrolid JOIN prefix_role_assignments ra ON ra.userid = u.id JOIN prefix_context ct ON ct.id = ra.contextid AND ct.contextlevel = 50 JOIN prefix_course c ON c.id = ct.instanceid AND e.courseid = c.id JOIN prefix_role r ON r.id = ra.roleid where uif.shortname = \'fieldname\' and u.id ='.$userid;
$records = $DB->get_recordset_sql($sql, array(), $params=null, $limitfrom=0, $limitnum=0);

foreach($records as $record){
$user['fieldname'] = $record->data;
}
Irvan Santoso
  • 51
  • 1
  • 6
  • This query seems to be doing a lot more than you asked for: it is checking for enrolment status, not just retrieving the field information. I also wonder why it uses get_recordset_sql, when it is only retrieving a single item (get_field_sql or even get_record_sql would be more appropriate). The use of 'prefix_' would be much better handled by, for example, {user_enrolments} (with curly brackets). Also you really should use params (as demonstrated in my answer), rather than inserting values directly into the SQL. – davosmith Feb 27 '15 at 08:30
  • I copied all the code im using right now, all i want is showing my custom profile field in enrolled user table – Irvan Santoso Feb 27 '15 at 09:39
0

custom fields are stored in 'user_info_field' table and data for each field and user are stored in 'user_info_data' table then you should know your fieldid in database and select data based on this id:

$field = $DB->get_record('user_info_field', array('shortname' => 'customfieldshortname'));
$fieldvalues = $DB->get_records('user_info_data',array('fieldid' => $field->id));
AminFarajzadeh
  • 346
  • 5
  • 17