From a49bc6c940fdc0874d98669f8aeb489e1eb88a4a Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Wed, 2 Oct 2013 14:55:20 +0800 Subject: [PATCH 1/3] MDL-42098 - Libraries: Alter fullname debugging logic to display when user name fields are missing. --- lib/moodlelib.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 5e714a42f67..b8682b1cc44 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3562,6 +3562,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 ''; } @@ -3589,17 +3602,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); - } } } From 73d363f0fa96f524a399e01630aa66ebbe398799 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Wed, 2 Oct 2013 16:11:47 +0800 Subject: [PATCH 2/3] MDL-42098 - Libraries: Unit test for fullname function to ensure that debug message is shown even when additional names are enabled. --- lib/tests/moodlelib_test.php | 10 ++++++++++ 1 file changed, 10 insertions(+) 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; } From 94b1577a5ad9c2c85f8c90d230623346b44a2bba Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 7 Oct 2013 10:02:44 +0800 Subject: [PATCH 3/3] MDL-42098 - mod_forum: Update webservice sql query to include all name fields. --- mod/forum/externallib.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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);