Merge branch 'wip-MDL-40612-master-one' of git://github.com/abgreeve/moodle

This commit is contained in:
Sam Hemelryk 2013-11-11 13:10:44 +08:00
commit 0fd6069ecd
11 changed files with 182 additions and 115 deletions

View File

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

View File

@ -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;

View File

@ -3657,20 +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 $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);
@ -3678,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.

View File

@ -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 Jordan 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 Jordan 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 Jordan draining another basket.';
$this->assertEquals($user, $expectedarray);
}
}

View File

@ -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 '<td class="picture">';
echo $OUTPUT->user_picture($postuser, array('courseid'=>$forum->course));
echo "</td>\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)) ? '' : '&amp;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 '<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.'">'.
@ -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;
}

View File

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

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

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

View File

@ -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();