From f490f988b7afb61ae794cde0e51e06e7d928aad5 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Wed, 30 Oct 2013 14:41:33 +0800 Subject: [PATCH 1/2] MDL-40612 libraries: Unit test expansion for get_all_user_name_fields() and the inclusion of tests for set_user_name_object() --- lib/moodlelib.php | 2 + lib/tests/moodlelib_test.php | 100 ++++++++++++++++++++++++++++++++--- user/tests/editlib_test.php | 7 +-- 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index c21b5fed5b9..7e387888d16 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3658,6 +3658,8 @@ function fullname($user, $override=false) { * * @param bool $returnsql True for an sql select field snippet. * @param string $alias table alias to use in front of each field. + * @param string $prefix prefix added to the name fields e.g. authorfirstname. + * @param string $title sql column alias e.g. id AS userid. * @return array|string All name fields. */ function get_all_user_name_fields($returnsql = false, $alias = null) { diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index de6482e016f..9df17abb247 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2406,12 +2406,12 @@ class core_moodlelib_testcase extends advanced_testcase { $this->resetAfterTest(); // Additional names in an array. - $testarray = array('firstnamephonetic', - 'lastnamephonetic', - 'middlename', - 'alternatename', - 'firstname', - 'lastname'); + $testarray = array('firstnamephonetic' => 'firstnamephonetic', + 'lastnamephonetic' => 'lastnamephonetic', + 'middlename' => 'middlename', + 'alternatename' => 'alternatename', + 'firstname' => 'firstname', + 'lastname' => 'lastname'); $this->assertEquals($testarray, get_all_user_name_fields()); // Additional names as a string. @@ -2421,6 +2421,19 @@ class core_moodlelib_testcase extends advanced_testcase { // Additional names as a string with an alias. $teststring = 't.firstnamephonetic,t.lastnamephonetic,t.middlename,t.alternatename,t.firstname,t.lastname'; $this->assertEquals($teststring, get_all_user_name_fields(true, 't')); + + // Additional name fields with a prefix - object + $testarray = array('firstnamephonetic' => 'authorfirstnamephonetic', + 'lastnamephonetic' => 'authorlastnamephonetic', + 'middlename' => 'authormiddlename', + 'alternatename' => 'authoralternatename', + 'firstname' => 'authorfirstname', + 'lastname' => 'authorlastname'); + $this->assertEquals($testarray, get_all_user_name_fields(false, null, 'author')); + + // Additional name fields with an alias and a title - string + $teststring = 'u.firstnamephonetic AS authorfirstnamephonetic,u.lastnamephonetic AS authorlastnamephonetic,u.middlename AS authormiddlename,u.alternatename AS authoralternatename,u.firstname AS authorfirstname,u.lastname AS authorlastname'; + $this->assertEquals($teststring, get_all_user_name_fields(true, 'u', null, 'author')); } public function test_order_in_string() { @@ -2599,4 +2612,79 @@ class core_moodlelib_testcase extends advanced_testcase { } } + /** + * Test function username_load_fields_from_object(). + */ + public function test_username_load_fields_from_object() { + $this->resetAfterTest(); + + // This object represents the information returned from an sql query. + $userinfo = new stdClass(); + $userinfo->userid = 1; + $userinfo->username = 'loosebruce'; + $userinfo->firstname = 'Bruce'; + $userinfo->lastname = 'Campbell'; + $userinfo->firstnamephonetic = 'ブルース'; + $userinfo->lastnamephonetic = 'カンベッル'; + $userinfo->middlename = ''; + $userinfo->alternatename = ''; + $userinfo->email = ''; + $userinfo->picture = 23; + $userinfo->imagealt = 'Michael Jordon draining another basket.'; + $userinfo->idnumber = 3982; + + + // Just user name fields. + $user = new stdClass(); + $user = username_load_fields_from_object($user, $userinfo); + $expectedarray = new stdClass(); + $expectedarray->firstname = 'Bruce'; + $expectedarray->lastname = 'Campbell'; + $expectedarray->firstnamephonetic = 'ブルース'; + $expectedarray->lastnamephonetic = 'カンベッル'; + $expectedarray->middlename = ''; + $expectedarray->alternatename = ''; + $this->assertEquals($user, $expectedarray); + + // User information for showing a picture. + $user = new stdClass(); + $additionalfields = array('id' => 'userid', 'imagealt', 'picture', 'email'); + $user = username_load_fields_from_object($user, $userinfo, null, $additionalfields); + $expectedarray = new stdClass(); + $expectedarray->id = 1; + $expectedarray->firstname = 'Bruce'; + $expectedarray->lastname = 'Campbell'; + $expectedarray->firstnamephonetic = 'ブルース'; + $expectedarray->lastnamephonetic = 'カンベッル'; + $expectedarray->middlename = ''; + $expectedarray->alternatename = ''; + $expectedarray->email = ''; + $expectedarray->picture = 23; + $expectedarray->imagealt = 'Michael Jordon draining another basket.'; + $this->assertEquals($user, $expectedarray); + + // Alter the userinfo object to have a prefix. + $userinfo->authorfirstname = 'Bruce'; + $userinfo->authorlastname = 'Campbell'; + $userinfo->authorfirstnamephonetic = 'ブルース'; + $userinfo->authorlastnamephonetic = 'カンベッル'; + $userinfo->authormiddlename = ''; + + // Return an object with user picture information. + $user = new stdClass(); + $additionalfields = array('id' => 'userid', 'imagealt', 'picture', 'email'); + $user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields); + $expectedarray = new stdClass(); + $expectedarray->id = 1; + $expectedarray->firstname = 'Bruce'; + $expectedarray->lastname = 'Campbell'; + $expectedarray->firstnamephonetic = 'ブルース'; + $expectedarray->lastnamephonetic = 'カンベッル'; + $expectedarray->middlename = ''; + $expectedarray->alternatename = ''; + $expectedarray->email = ''; + $expectedarray->picture = 23; + $expectedarray->imagealt = 'Michael Jordon draining another basket.'; + $this->assertEquals($user, $expectedarray); + } } diff --git a/user/tests/editlib_test.php b/user/tests/editlib_test.php index a4c76fd169c..0b71e393dbd 100644 --- a/user/tests/editlib_test.php +++ b/user/tests/editlib_test.php @@ -100,13 +100,14 @@ class core_user_editlib_testcase extends advanced_testcase { $originalcfg->fullnamedisplay = $CFG->fullnamedisplay; $CFG->fullnamedisplay = 'language'; - $expectedresult = array('firstnamephonetic', 'lastnamephonetic', 'middlename', 'alternatename'); + $expectedresult = array('firstnamephonetic' => 'firstnamephonetic', 'lastnamephonetic' => 'lastnamephonetic', + 'middlename' => 'middlename', 'alternatename' => 'alternatename'); $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult); $CFG->fullnamedisplay = 'firstname lastname firstnamephonetic'; - $expectedresult = array(1 => 'lastnamephonetic', 2 => 'middlename', 3 => 'alternatename'); + $expectedresult = array('lastnamephonetic' => 'lastnamephonetic', 'middlename' => 'middlename', 'alternatename' => 'alternatename'); $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult); $CFG->fullnamedisplay = 'firstnamephonetic, lastname lastnamephonetic (alternatename)'; - $expectedresult = array(2 => 'middlename'); + $expectedresult = array('middlename' => 'middlename'); $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult); $CFG->fullnamedisplay = 'firstnamephonetic lastnamephonetic alternatename middlename'; $expectedresult = array(); From 5b1944bbba57f4a077d1ff2db32345d298209ab5 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Wed, 30 Oct 2013 14:41:59 +0800 Subject: [PATCH 2/2] MDL-40612 libraries: Tidy up and improvement of the code for additional name fields. Includes an update to get_all_user_name_fields() and the inclusion of set_user_name_object() --- group/overview.php | 9 +-- lib/coursecatlib.php | 6 +- lib/moodlelib.php | 72 ++++++++++++++++++++---- lib/tests/moodlelib_test.php | 10 ++-- mod/forum/lib.php | 41 +++----------- mod/quiz/report/attemptsreport_table.php | 9 +-- mod/scorm/report/basic/report.php | 11 +--- mod/scorm/report/interactions/report.php | 11 +--- mod/scorm/report/objectives/report.php | 11 +--- question/editlib.php | 20 +------ 10 files changed, 88 insertions(+), 112 deletions(-) diff --git a/group/overview.php b/group/overview.php index 134261e027f..b3ae333aea6 100644 --- a/group/overview.php +++ b/group/overview.php @@ -102,14 +102,7 @@ $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnu $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams)); foreach ($rs as $row) { $user = new stdClass(); - $user->id = $row->userid; - $user->firstname = $row->firstname; - $user->lastname = $row->lastname; - $user->username = $row->username; - $user->idnumber = $row->idnumber; - foreach (get_all_user_name_fields() as $addname) { - $user->$addname = $row->$addname; - } + $user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber')); if (!$row->groupingid) { $row->groupingid = -1; } diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index ce39f5e7090..9323ea57f16 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -2716,11 +2716,7 @@ class course_in_list implements IteratorAggregate { continue; } $user = new stdClass(); - $user->id = $ruser->id; - $user->username = $ruser->username; - foreach (get_all_user_name_fields() as $addname) { - $user->$addname = $ruser->$addname; - } + $user = username_load_fields_from_object($user, $ruser, null, array('id', 'username')); $role = new stdClass(); $role->id = $ruser->roleid; $role->name = $ruser->rolename; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 7e387888d16..decda4cfb6f 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3657,22 +3657,37 @@ function fullname($user, $override=false) { * A centralised location for the all name fields. Returns an array / sql string snippet. * * @param bool $returnsql True for an sql select field snippet. - * @param string $alias table alias to use in front of each field. + * @param string $tableprefix table query prefix to use in front of each field. * @param string $prefix prefix added to the name fields e.g. authorfirstname. - * @param string $title sql column alias e.g. id AS userid. + * @param string $fieldprefix sql field prefix e.g. id AS userid. * @return array|string All name fields. */ -function get_all_user_name_fields($returnsql = false, $alias = null) { - $alternatenames = array('firstnamephonetic', - 'lastnamephonetic', - 'middlename', - 'alternatename', - 'firstname', - 'lastname'); +function get_all_user_name_fields($returnsql = false, $tableprefix = null, $prefix = null, $fieldprefix = null) { + $alternatenames = array('firstnamephonetic' => 'firstnamephonetic', + 'lastnamephonetic' => 'lastnamephonetic', + 'middlename' => 'middlename', + 'alternatename' => 'alternatename', + 'firstname' => 'firstname', + 'lastname' => 'lastname'); + + // Let's add a prefix to the array of user name fields if provided. + if ($prefix) { + foreach ($alternatenames as $key => $altname) { + $alternatenames[$key] = $prefix . $altname; + } + } + + // Create an sql field snippet if requested. if ($returnsql) { - if ($alias) { - foreach ($alternatenames as $key => $altname) { - $alternatenames[$key] = "$alias.$altname"; + if ($tableprefix) { + if ($fieldprefix) { + foreach ($alternatenames as $key => $altname) { + $alternatenames[$key] = $tableprefix . '.' . $altname . ' AS ' . $fieldprefix . $altname; + } + } else { + foreach ($alternatenames as $key => $altname) { + $alternatenames[$key] = $tableprefix . '.' . $altname; + } } } $alternatenames = implode(',', $alternatenames); @@ -3680,6 +3695,39 @@ function get_all_user_name_fields($returnsql = false, $alias = null) { return $alternatenames; } +/** + * Reduces lines of duplicated code for getting user name fields. + * + * @param object $addtoobject Object to add user name fields to. + * @param object $secondobject Object that contains user name field information. + * @param string $prefix prefix to be added to the user name field e.g. authorfirstname. + * @param array $additionalfields Additional fields to be matched with data in the second object. + * The key can be set to the user table field name. + * @return object User name fields. + */ +function username_load_fields_from_object($addtoobject, $secondobject, $prefix = null, $additionalfields = null) { + $fields = get_all_user_name_fields(false, null, $prefix); + if ($additionalfields) { + // Additional fields can specify their own 'alias' such as 'id' => 'userid'. This checks to see if + // the key is a number and then sets the key to the array value. + foreach ($additionalfields as $key => $value) { + if (is_numeric($key)) { + $additionalfields[$value] = $value; + unset($additionalfields[$key]); + } + } + $fields = array_merge($fields, $additionalfields); + } + foreach ($fields as $key => $field) { + // Important that we have all of the user name fields present in the object that we are sending back. + $addtoobject->$key = ''; + if (isset($secondobject->$field)) { + $addtoobject->$key = $secondobject->$field; + } + } + return $addtoobject; +} + /** * Returns an array of values in order of occurance in a provided string. * The key in the result is the character postion in the string. diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 9df17abb247..4d138668298 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2422,7 +2422,7 @@ class core_moodlelib_testcase extends advanced_testcase { $teststring = 't.firstnamephonetic,t.lastnamephonetic,t.middlename,t.alternatename,t.firstname,t.lastname'; $this->assertEquals($teststring, get_all_user_name_fields(true, 't')); - // Additional name fields with a prefix - object + // Additional name fields with a prefix - object. $testarray = array('firstnamephonetic' => 'authorfirstnamephonetic', 'lastnamephonetic' => 'authorlastnamephonetic', 'middlename' => 'authormiddlename', @@ -2431,7 +2431,7 @@ class core_moodlelib_testcase extends advanced_testcase { 'lastname' => 'authorlastname'); $this->assertEquals($testarray, get_all_user_name_fields(false, null, 'author')); - // Additional name fields with an alias and a title - string + // Additional name fields with an alias and a title - string. $teststring = 'u.firstnamephonetic AS authorfirstnamephonetic,u.lastnamephonetic AS authorlastnamephonetic,u.middlename AS authormiddlename,u.alternatename AS authoralternatename,u.firstname AS authorfirstname,u.lastname AS authorlastname'; $this->assertEquals($teststring, get_all_user_name_fields(true, 'u', null, 'author')); } @@ -2630,7 +2630,7 @@ class core_moodlelib_testcase extends advanced_testcase { $userinfo->alternatename = ''; $userinfo->email = ''; $userinfo->picture = 23; - $userinfo->imagealt = 'Michael Jordon draining another basket.'; + $userinfo->imagealt = 'Michael Jordan draining another basket.'; $userinfo->idnumber = 3982; @@ -2660,7 +2660,7 @@ class core_moodlelib_testcase extends advanced_testcase { $expectedarray->alternatename = ''; $expectedarray->email = ''; $expectedarray->picture = 23; - $expectedarray->imagealt = 'Michael Jordon draining another basket.'; + $expectedarray->imagealt = 'Michael Jordan draining another basket.'; $this->assertEquals($user, $expectedarray); // Alter the userinfo object to have a prefix. @@ -2684,7 +2684,7 @@ class core_moodlelib_testcase extends advanced_testcase { $expectedarray->alternatename = ''; $expectedarray->email = ''; $expectedarray->picture = 23; - $expectedarray->imagealt = 'Michael Jordon draining another basket.'; + $expectedarray->imagealt = 'Michael Jordan draining another basket.'; $this->assertEquals($user, $expectedarray); } } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 0477a1a30c9..a4af6491605 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -2704,11 +2704,7 @@ function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost= $umfields = ""; $umtable = ""; } else { - $umfields = ''; - $umnames = get_all_user_name_fields(); - foreach ($umnames as $umname) { - $umfields .= ', um.' . $umname . ' AS um' . $umname; - } + $umfields = ', ' . get_all_user_name_fields(true, 'um', null, 'um'); $umtable = " LEFT JOIN {user} um ON (d.usermodified = um.id)"; } @@ -3334,14 +3330,8 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa // Build an object that represents the posting user $postuser = new stdClass; - $postuser->id = $post->userid; - foreach (get_all_user_name_fields() as $addname) { - $postuser->$addname = $post->$addname; - } - $postuser->imagealt = $post->imagealt; - $postuser->picture = $post->picture; - $postuser->email = $post->email; - // Some handy things for later on + $postuserfields = array('id' => 'userid', 'imagealt', 'picture', 'email'); + $postuser = username_load_fields_from_object($postuser, $post, null, $postuserfields); $postuser->fullname = fullname($postuser, $cm->cache->caps['moodle/site:viewfullnames']); $postuser->profilelink = new moodle_url('/user/view.php', array('id'=>$post->userid, 'course'=>$course->id)); @@ -3777,14 +3767,8 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring="" // Picture $postuser = new stdClass(); - $postuser->id = $post->userid; - foreach (get_all_user_name_fields() as $addname) { - $postuser->$addname = $post->$addname; - } - $postuser->imagealt = $post->imagealt; - $postuser->picture = $post->picture; - $postuser->email = $post->email; - + $postuserfields = array('id' => 'userid', 'imagealt', 'picture', 'email'); + $postuser = username_load_fields_from_object($postuser, $post, null, $postuserfields); echo ''; echo $OUTPUT->user_picture($postuser, array('courseid'=>$forum->course)); echo "\n"; @@ -3846,11 +3830,7 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring="" $usedate = (empty($post->timemodified)) ? $post->modified : $post->timemodified; // Just in case $parenturl = (empty($post->lastpostid)) ? '' : '&parent='.$post->lastpostid; $usermodified = new stdClass(); - $usermodified->id = $post->usermodified; - foreach (get_all_user_name_fields() as $addname) { - $temp = 'um' . $addname; - $usermodified->$addname = $post->$temp; - } + $usermodified = username_load_fields_from_object($usermodified, $post, 'um', array('id' => 'usermodified')); echo ''. fullname($usermodified).'
'; echo ''. @@ -6129,13 +6109,8 @@ function forum_get_recent_mod_activity(&$activities, &$index, $timestart, $cours $tmpactivity->content->parent = $post->parent; $tmpactivity->user = new stdClass(); - $tmpactivity->user->id = $post->userid; - $tmpactivity->user->picture = $post->picture; - $tmpactivity->user->imagealt = $post->imagealt; - $tmpactivity->user->email = $post->email; - foreach (get_all_user_name_fields() as $addname) { - $tmpactivity->user->$addname = $post->$addname; - } + $additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email'); + $tmpactivity->user = username_load_fields_from_object($tmpactivity->user, $post, null, $additionalfields); $activities[$index++] = $tmpactivity; } diff --git a/mod/quiz/report/attemptsreport_table.php b/mod/quiz/report/attemptsreport_table.php index 6c19c50883c..5c56c505873 100644 --- a/mod/quiz/report/attemptsreport_table.php +++ b/mod/quiz/report/attemptsreport_table.php @@ -121,13 +121,8 @@ abstract class quiz_attempts_report_table extends table_sql { public function col_picture($attempt) { global $OUTPUT; $user = new stdClass(); - $user->id = $attempt->userid; - $user->imagealt = $attempt->imagealt; - $user->picture = $attempt->picture; - $user->email = $attempt->email; - foreach (get_all_user_name_fields() as $addname) { - $user->$addname = $attempt->$addname; - } + $additionalfields = array('id' => 'userid', 'imagealt', 'picture', 'email'); + $user = username_load_fields_from_object($user, $attempt, null, $additionalfields); return $OUTPUT->user_picture($user); } diff --git a/mod/scorm/report/basic/report.php b/mod/scorm/report/basic/report.php index 5c55e9c7032..b2a6032d9c4 100644 --- a/mod/scorm/report/basic/report.php +++ b/mod/scorm/report/basic/report.php @@ -380,14 +380,9 @@ class scorm_basic_report extends scorm_default_report { } } if (in_array('picture', $columns)) { - $user = (object)array( - 'id'=>$scouser->userid, - 'picture'=>$scouser->picture, - 'imagealt'=>$scouser->imagealt, - 'email'=>$scouser->email); - foreach (get_all_user_name_fields() as $addname) { - $user->$addname = $scouser->$addname; - } + $user = new stdClass(); + $additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email'); + $user = username_load_fields_from_object($user, $scouser, null, $additionalfields); $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id)); } if (!$download) { diff --git a/mod/scorm/report/interactions/report.php b/mod/scorm/report/interactions/report.php index 8a2881c8d4e..66ea5cc9100 100644 --- a/mod/scorm/report/interactions/report.php +++ b/mod/scorm/report/interactions/report.php @@ -415,14 +415,9 @@ class scorm_interactions_report extends scorm_default_report { } } if (in_array('picture', $columns)) { - $user = (object)array( - 'id'=>$scouser->userid, - 'picture'=>$scouser->picture, - 'imagealt'=>$scouser->imagealt, - 'email'=>$scouser->email); - foreach (get_all_user_name_fields() as $addname) { - $user->$addname = $scouser->$addname; - } + $user = new stdClass(); + $additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email'); + $user = username_load_fields_from_object($user, $scouser, null, $additionalfields); $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id)); } if (!$download) { diff --git a/mod/scorm/report/objectives/report.php b/mod/scorm/report/objectives/report.php index 3593961015e..7b26703a07f 100644 --- a/mod/scorm/report/objectives/report.php +++ b/mod/scorm/report/objectives/report.php @@ -419,14 +419,9 @@ class scorm_objectives_report extends scorm_default_report { } } if (in_array('picture', $columns)) { - $user = (object)array( - 'id'=>$scouser->userid, - 'picture'=>$scouser->picture, - 'imagealt'=>$scouser->imagealt, - 'email'=>$scouser->email); - foreach (get_all_user_name_fields() as $addname) { - $user->$addname = $scouser->$addname; - } + $user = new stdClass(); + $additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email'); + $user = username_load_fields_from_object($user, $scouser, null, $additionalfields); $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id)); } if (!$download) { diff --git a/question/editlib.php b/question/editlib.php index 765492f5cb1..b89b1d48bbf 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -559,15 +559,7 @@ class question_bank_creator_name_column extends question_bank_column_base { protected function display_content($question, $rowclasses) { if (!empty($question->creatorfirstname) && !empty($question->creatorlastname)) { $u = new stdClass(); - $allnames = get_all_user_name_fields(); - foreach ($allnames as $allname) { - $tempname = 'creator' . $allname; - if (isset($question->$tempname)) { - $u->$allname = $question->$tempname; - } else { - $u->$allname = ''; - } - } + $u = username_load_fields_from_object($u, $question, 'creator'); echo fullname($u); } } @@ -612,15 +604,7 @@ class question_bank_modifier_name_column extends question_bank_column_base { protected function display_content($question, $rowclasses) { if (!empty($question->modifierfirstname) && !empty($question->modifierlastname)) { $u = new stdClass(); - $allnames = get_all_user_name_fields(); - foreach ($allnames as $allname) { - $tempname = 'modifier' . $allname; - if (isset($question->$tempname)) { - $u->$allname = $question->$tempname; - } else { - $u->$allname = ''; - } - } + $u = username_load_fields_from_object($u, $question, 'modifier'); echo fullname($u); } }