Editing fullname() causes weird bugs with the forum module

Editing fullname() causes weird bugs with the forum module

by Darryl Pogue -
Number of replies: 3
Hi everyone,
I've been working with Moodle for a few months now with my school; and I've been writing a lot of little hacks to get it working exactly how we'd like it. One of these hacks was designed to replace a teacher's first name with a title.

First, I created a user profile field to store the title text (so that each teacher can specify their title). Then I went hunting for the fullname function in moodlelib.php. I added an additional argument (which is set to false by default) that will display the title instead of the first name.

/** 
* Given an object containing firstname and lastname
* values, this function returns a string with the
* full name of the person.
* The result may depend on system settings
* or language. 'override' will force both names
* to be used even if system settings specify one.
*
* @uses $CFG
* @uses $SESSION
* @param object $user A {@link $USER} object to get full name of
* @param bool $override If true then the name will be first name followed by last name rather than adhering to fullnamedisplay setting.
* @param cool $title If true then the name will be a title (if available) followed by the last name.
*/ function fullname($user, $override=false, $title=false) { global $CFG, $SESSION; if (!isset($user->firstname) and !isset($user->lastname)) { return ''; } if (!$override) { if (!empty($CFG->forcefirstname)) { $user->firstname = $CFG->forcefirstname; } if (!empty($CFG->forcelastname)) { $user->lastname = $CFG->forcelastname; } } if (!empty($SESSION->fullnamedisplay)) { $CFG->fullnamedisplay = $SESSION->fullnamedisplay; } if ($title) { require_once($CFG->dirroot.'/user/profile/lib.php'); $pref = grab_profile_fields_data($user->id, 2); if(!empty($pref)) { return $pref .' '. $user->lastname; } } if ($CFG->fullnamedisplay == 'firstname lastname') { return $user->firstname .' '. $user->lastname; } else if ($CFG->fullnamedisplay == 'lastname firstname') { return $user->lastname .' '. $user->firstname; } else if ($CFG->fullnamedisplay == 'firstname') { if ($override) { return get_string('fullnamedisplay', '', $user); } else { return $user->firstname; } } return get_string('fullnamedisplay', '', $user); }
It was working great for the primary usage that I'd given it. However, I preferably wanted it working across the entire site (except on a few pages where the full teacher names would be acceptable).

So I changed $title to true in the arguments. It worked for the most part. The problem came up when I was looking at forums though.

Often, it just wouldn't change the name (displaying instead the usual "firstname lastname"). Then (sporadically it seemed) it would pick a random title out of the database and use that as a title. (Thus we had Mr., Mrs., and Mme all replying to each other, even though none of those users had titles set in their profiles).

It was, to say the least, a shock to me that everything else would seem in working order, yet the forum module would break. I've read the thread in which a Moodle user wanted to replace forum names with usernames, and someone replied that it was necessary to select from the database using modified code. However, I'm not sure if that will help in this case, and I'm not confident in changing database code for fear that I'll break something permanently.

If anyone has any ideas as to why the forum module is breaking, or what can be done to fix it, they would be very much appreciated.

Darryl Pogue
THSS Moodle
Average of ratings: -
In reply to Darryl Pogue

Re: Editing fullname() causes weird bugs with the forum module

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Without looking at the code in forum, my guess is that $user->id may not always be present, or may not refer to the actual user id.

The only fields that function ever used was firstname and lastname, and some code might be depending on that.
In reply to Martin Dougiamas

Re: Editing fullname() causes weird bugs with the forum module

by Darryl Pogue -
I've found the problem!

mod/forum/lib.php calls the fullname function using a $post sql record.
$post->id gets the id of the actual post (not the user)
What I'm looking for is $post->userid...

Unfortunately, I've yet to see an easy way to solve this (unless I add more unnecessary code to fullname to specify the user id as an argument)
In reply to Darryl Pogue

Re: Editing fullname() causes weird bugs with the forum module

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
The lowest-impact hack would be to test for the existence of $user->userid first in fullname() and if it exists, use that instead. That might actually fix other cases besides forum.


if (!empty($user->userid)) {
$userid = $user->userid;
} else if (!empty($user->id)) {
$userid = $user->id;
} else {
// abort this extra bit
}



Otherwise, we'd have to construct a fake $tempuser object in the forum code to pass to fullname() and check elsewhere for similar problems.

Or you could just tell your teachers to put their title in their firstname field tongueout