Hi Shakib,
Yes, I got it to work. It's been quite a while since I asked the question, and couldn't work on it much after that. However, I picked it up again last week and got something working.
Since I don't quite know all the inner workings of the coding, I'm more of the copy-and-paste type with this. But those coding classes definitely helped with understanding what code does what. I still need to take a PHP class or two...
Anyway, my reasoning was that, if the /user/profile.php showed the custom profiles "automatically," then the code is in there. Once I figured out the relevant function ( called profile_display_fields ) and I found the code in /user/profile/lib.php. I made a copy of the function code and commented out the original, so if something got messed up, I could easily start over. The original code would go through and display every custom field containing data, but I only want it to display one in particular. I ended up doing some trial-and-error with the code to find out what does what. I still had to do a little bit of coding to get it to display just one field.
In addition to taking in the user id, I also was able to get it to take a string. It seems to compare the string to the shortnames of the custom fields, and gets the relevant id and datatype. I figured it would be easier to know the shortname, rather than trying to know or find out the id number and it's datatype.
Here's the original code:
The Function:
profile_display_fields($user->id);
The Function's Code:
function profile_display_fields($userid) {
global $CFG, $USER, $DB;
if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $userid);
if ($formfield->is_visible() and !$formfield->is_empty()) {
print_row(format_string($formfield->field->name.':'), $formfield->display_data());
}
}
}
}
}
}
Here's my modified code:
The Function:
require_once($CFG->dirroot.'/user/profile/lib.php');
The above line, I usually put up with the other "require_once" lines, just to keep things organized. It is required in order to make function below work.$row[] = profile_display_custom_field($user->id, 'ActivityStatus');
The Function's Code: (in /user/profile/lib.php)
//DISPLAY CUSTOM FIELD WITH SHORTNAME COMPARISION
function profile_display_custom_field($userid, $fieldshortname)
{
global $CFG, $USER, $DB;
if ($fields = $DB->get_records('user_info_field', array('shortname'=>$fieldshortname), 'sortorder ASC'))
{
foreach ($fields as $field)
{
$fielddatatype = $field->datatype;
$fieldid = $field->id;
require_once($CFG->dirroot.'/user/profile/field/'.$fielddatatype.'/field.class.php');
$newfield = 'profile_field_'.$fielddatatype;
$formfield = new $newfield($fieldid, $userid);
if ($formfield->is_visible() and !$formfield->is_empty()) {
return($formfield->display_data());
}
}
}
}
The part that I'm not quite happy with is the line:
if ($fields = $DB->get_records('user_info_field', array('shortname'=>$fieldshortname), 'sortorder ASC'))
I'm not happy with it because I'm not sure it's doing what I want the right way, or the best way to get it to do what I want. I only say "the right way" because I don't quite understand what all this line is doing, just that the end result with the rest of the code I have displays what I want.
In pseudo-code, what I want is:
- Compare the string $fieldshortname to the shortnames in the user_info_field table.
- When the match is found (of which there should only be one, right?) take the corresponding id number as $fieldid, and combine it with $userid to get the specified field's data for the given user.
- Display the data.
Here's a couple of questions, if someone could answer them:
- It seems that my pseudo-code is what my modified code seems to be doing, but the line for the comparison for the shortname seems to be more code than necessary. Is there a way to do the comparison with less code? (Like the foreach ($fields as $field) line...)
- Is my modified code in the proper place (/user/profile/lib.php)? If not, where would be a better place to put it?
- My next challenge is to get the header to turn into a link to sort the column. Currently it does not appear as a link, and I'm not sure how to get it to display as one. Suggestions?
The code I'm referring to is:
$table->head[] = $lastaccess;
$table->align[] = 'left';
$table->head[] = 'Activity Status';
$table->align[] = 'left';
The second header is mine for Activity Status, the first is for Last Access. Last Access appears as a link to sort, but Activity Status does not. There's obviously more to it, but I haven't been able to isolate it yet.