如何从moodle中的自定义配置文件字段获取值?
我想在user.php中包含配置文件字段
答案 0 :(得分:2)
如果要加载所有用户配置文件字段,则可以从用户对象
开始$user = $DB->get_record('user', array('id' => $userid));
然后致电
profile_load_custom_fields($user);
使用所有自定义字段的数组填写$ user->个人资料。
或者,您可以通过以下方式获取单个个人资料字段的内容:
$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);
请注意,此代码是在我的头顶编写的,未经测试,因此可能会有一些拼写错误。
答案 1 :(得分:1)
您可以使用:
$Data = $USER->profile['YourCustomField'];
答案 2 :(得分:0)
我终于成功了!!!
$ 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
其中uif.shortname = \'fieldname \'和u.id ='。$ userid;
$ records = $ DB-> get_recordset_sql($ sql,array(),$ params = null,$ limitfrom = 0,$ limitnum = 0);
foreach($records as $record){
$user['fieldname'] = $record->data;
}
答案 3 :(得分:0)
自定义字段存储在“user_info_field”表中,每个字段和用户的数据存储在“user_info_data”表中,然后您应该知道数据库中的fieldid并根据此ID选择数据:
$field = $DB->get_record('user_info_field', array('shortname' => 'customfieldshortname'));
$fieldvalues = $DB->get_records('user_info_data',array('fieldid' => $field->id));
答案 4 :(得分:0)
我希望我的解决方案可以帮助任何人:
需要这个库
require_once($CFG->dirroot.'/user/profile/lib.php');
现在您可以使用:
profile_load_data($USER);
$USER->profile_field_%your_field_name%
示例$ USER-> profile_field_HaveAnotherGradeBook;