diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 72cad759d1e..a344c70dc48 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3559,6 +3559,19 @@ function ismoving($courseid) { function fullname($user, $override=false) { global $CFG, $SESSION; + // Get all of the name fields. + $allnames = get_all_user_name_fields(); + if ($CFG->debugdeveloper) { + foreach ($allnames as $allname) { + if (!array_key_exists($allname, $user)) { + // If all the user name fields are not set in the user object, then notify the programmer that it needs to be fixed. + debugging('You need to update your sql to include additional name fields in the user object.', DEBUG_DEVELOPER); + // Message has been sent, no point in sending the message multiple times. + break; + } + } + } + if (!isset($user->firstname) and !isset($user->lastname)) { return ''; } @@ -3586,17 +3599,11 @@ function fullname($user, $override=false) { return get_string('fullnamedisplay', null, $user); } - // Get all of the name fields. - $allnames = get_all_user_name_fields(); $requirednames = array(); // With each name, see if it is in the display name template, and add it to the required names array if it is. foreach ($allnames as $allname) { if (strpos($template, $allname) !== false) { $requirednames[] = $allname; - // If the field is in the template but not set in the user object then notify the programmer that it needs to be fixed. - if (!array_key_exists($allname, $user)) { - debugging('You need to update your sql to include additional name fields in the user object.', DEBUG_DEVELOPER); - } } } diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 4ef9da68073..ed648c00bec 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2375,6 +2375,16 @@ class core_moodlelib_testcase extends advanced_testcase { $this->assertSame($expectedname, $testname); } + // Test debugging message displays when + // fullnamedisplay setting is "normal". + $CFG->fullnamedisplay = 'firstname lastname'; + unset($user); + $user = new stdClass(); + $user->firstname = 'Stan'; + $user->lastname = 'Lee'; + $namedisplay = fullname($user); + $this->assertDebuggingCalled(); + // Tidy up after we finish testing. $CFG->fullnamedisplay = $originalcfg->fullnamedisplay; } diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 955bd62ad79..bbd9ccf10e4 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -240,10 +240,11 @@ class mod_forum_external extends external_api { if ($forum->type == 'qanda' && !forum_user_has_posted($discussion->forum, $discussion->id, $USER->id)) { require_capability('mod/forum:viewqandawithoutposting', $modcontext); } + $usernamefields = user_picture::fields(); // If we don't have the users details then perform DB call. if (empty($arrusers[$discussion->userid])) { $arrusers[$discussion->userid] = $DB->get_record('user', array('id' => $discussion->userid), - 'firstname, lastname, email, picture, imagealt', MUST_EXIST); + $usernamefields, MUST_EXIST); } // Get the subject. $subject = $DB->get_field('forum_posts', 'subject', array('id' => $discussion->firstpost), MUST_EXIST); @@ -284,7 +285,7 @@ class mod_forum_external extends external_api { $lastpost = $DB->get_record('forum_posts', array('id' => $return->lastpost), '*', MUST_EXIST); if (empty($arrusers[$lastpost->userid])) { $arrusers[$lastpost->userid] = $DB->get_record('user', array('id' => $lastpost->userid), - 'firstname, lastname, email, picture, imagealt', MUST_EXIST); + $usernamefields, MUST_EXIST); } $return->lastuserid = $lastpost->userid; $return->lastuserfullname = fullname($arrusers[$lastpost->userid], $canviewfullname);