Skip to content

Commit

Permalink
MDL-42812 libraries: Update username_load_fields_from_object to add t…
Browse files Browse the repository at this point in the history
…he prefix to everything.
  • Loading branch information
abgreeve committed Nov 12, 2013
1 parent 5b1944b commit 29fa62c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3700,7 +3700,7 @@ function get_all_user_name_fields($returnsql = false, $tableprefix = null, $pref
*
* @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 string $prefix prefix to be added to all fields (including $additionalfields) 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.
Expand All @@ -3712,8 +3712,10 @@ function username_load_fields_from_object($addtoobject, $secondobject, $prefix =
// 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;
$additionalfields[$value] = $prefix . $value;
unset($additionalfields[$key]);
} else {
$additionalfields[$key] = $prefix . $value;
}
}
$fields = array_merge($fields, $additionalfields);
Expand Down
13 changes: 9 additions & 4 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2633,7 +2633,6 @@ public function test_username_load_fields_from_object() {
$userinfo->imagealt = 'Michael Jordan draining another basket.';
$userinfo->idnumber = 3982;


// Just user name fields.
$user = new stdClass();
$user = username_load_fields_from_object($user, $userinfo);
Expand All @@ -2648,8 +2647,9 @@ public function test_username_load_fields_from_object() {

// User information for showing a picture.
$user = new stdClass();
$additionalfields = array('id' => 'userid', 'imagealt', 'picture', 'email');
$additionalfields = explode(',', user_picture::fields());
$user = username_load_fields_from_object($user, $userinfo, null, $additionalfields);
$user->id = $userinfo->userid;
$expectedarray = new stdClass();
$expectedarray->id = 1;
$expectedarray->firstname = 'Bruce';
Expand All @@ -2669,11 +2669,16 @@ public function test_username_load_fields_from_object() {
$userinfo->authorfirstnamephonetic = 'ブルース';
$userinfo->authorlastnamephonetic = 'カンベッル';
$userinfo->authormiddlename = '';
$userinfo->authorpicture = 23;
$userinfo->authorimagealt = 'Michael Jordan draining another basket.';
$userinfo->authoremail = '[email protected]';


// Return an object with user picture information.
$user = new stdClass();
$additionalfields = array('id' => 'userid', 'imagealt', 'picture', 'email');
$additionalfields = explode(',', user_picture::fields());
$user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields);
$user->id = $userinfo->userid;
$expectedarray = new stdClass();
$expectedarray->id = 1;
$expectedarray->firstname = 'Bruce';
Expand All @@ -2682,7 +2687,7 @@ public function test_username_load_fields_from_object() {
$expectedarray->lastnamephonetic = 'カンベッル';
$expectedarray->middlename = '';
$expectedarray->alternatename = '';
$expectedarray->email = '';
$expectedarray->email = '[email protected]';
$expectedarray->picture = 23;
$expectedarray->imagealt = 'Michael Jordan draining another basket.';
$this->assertEquals($user, $expectedarray);
Expand Down
11 changes: 8 additions & 3 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3330,8 +3330,9 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa

// Build an object that represents the posting user
$postuser = new stdClass;
$postuserfields = array('id' => 'userid', 'imagealt', 'picture', 'email');
$postuserfields = explode(',', user_picture::fields());
$postuser = username_load_fields_from_object($postuser, $post, null, $postuserfields);
$postuser->id = $post->userid;
$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));

Expand Down Expand Up @@ -3767,8 +3768,9 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring=""

// Picture
$postuser = new stdClass();
$postuserfields = array('id' => 'userid', 'imagealt', 'picture', 'email');
$postuserfields = explode(',', user_picture::fields());
$postuser = username_load_fields_from_object($postuser, $post, null, $postuserfields);
$postuser->id = $post->userid;
echo '<td class="picture">';
echo $OUTPUT->user_picture($postuser, array('courseid'=>$forum->course));
echo "</td>\n";
Expand Down Expand Up @@ -3830,7 +3832,8 @@ 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)) ? '' : '&amp;parent='.$post->lastpostid;
$usermodified = new stdClass();
$usermodified = username_load_fields_from_object($usermodified, $post, 'um', array('id' => 'usermodified'));
$usermodified->id = $post->usermodified;
$usermodified = username_load_fields_from_object($usermodified, $post, 'um');
echo '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$post->usermodified.'&amp;course='.$forum->course.'">'.
fullname($usermodified).'</a><br />';
echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.$parenturl.'">'.
Expand Down Expand Up @@ -6110,7 +6113,9 @@ function forum_get_recent_mod_activity(&$activities, &$index, $timestart, $cours

$tmpactivity->user = new stdClass();
$additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email');
$additionalfields = explode(',', user_picture::fields());
$tmpactivity->user = username_load_fields_from_object($tmpactivity->user, $post, null, $additionalfields);
$tmpactivity->user->id = $post->userid;

$activities[$index++] = $tmpactivity;
}
Expand Down
3 changes: 2 additions & 1 deletion mod/quiz/report/attemptsreport_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ public function col_checkbox($attempt) {
public function col_picture($attempt) {
global $OUTPUT;
$user = new stdClass();
$additionalfields = array('id' => 'userid', 'imagealt', 'picture', 'email');
$additionalfields = explode(',', user_picture::fields());
$user = username_load_fields_from_object($user, $attempt, null, $additionalfields);
$user->id = $attempt->userid
return $OUTPUT->user_picture($user);
}

Expand Down
3 changes: 2 additions & 1 deletion mod/scorm/report/basic/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ function display($scorm, $cm, $course, $download) {
}
if (in_array('picture', $columns)) {
$user = new stdClass();
$additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email');
$additionalfields = explode(',', user_picture::fields());
$user = username_load_fields_from_object($user, $scouser, null, $additionalfields);
$user->id = $scouser->userid;
$row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
}
if (!$download) {
Expand Down
3 changes: 2 additions & 1 deletion mod/scorm/report/interactions/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ function display($scorm, $cm, $course, $download) {
}
if (in_array('picture', $columns)) {
$user = new stdClass();
$additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email');
$additionalfields = explode(',', user_picture::fields());
$user = username_load_fields_from_object($user, $scouser, null, $additionalfields);
$user->id = $scouser->userid;
$row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
}
if (!$download) {
Expand Down
3 changes: 2 additions & 1 deletion mod/scorm/report/objectives/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ public function display($scorm, $cm, $course, $download) {
}
if (in_array('picture', $columns)) {
$user = new stdClass();
$additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email');
$additionalfields = explode(',', user_picture::fields());
$user = username_load_fields_from_object($user, $scouser, null, $additionalfields);
$user->id = $scouser->userid;
$row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
}
if (!$download) {
Expand Down

0 comments on commit 29fa62c

Please sign in to comment.