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); }
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