From a1c947bde2148cc9bb22397fed0a890eba0cea4a Mon Sep 17 00:00:00 2001 From: sam marshall Date: Wed, 13 Apr 2011 15:43:28 +0100 Subject: [PATCH] MDL-26647 (2) New lib function get_extra_user_fields and related get_extra_user_fields to get a list of extra user fields for display (that are permitted for current user in given context). get_extra_user_fields_sql returns the same thing as a SELECT list. get_user_field_name returns the display name of a user field (basically just get_string($field) except phone1=>phone) --- lib/moodlelib.php | 82 ++++++++++++++++++++++++++++++++ lib/simpletest/testmoodlelib.php | 75 +++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 2a9fed00293..64c423acacf 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3354,6 +3354,88 @@ function fullname($user, $override=false) { return get_string('fullnamedisplay', '', $user); } +/** + * Checks if current user is shown any extra fields when listing users. + * @param object $context Context + * @param array $already Array of fields that we're going to show anyway + * so don't bother listing them + * @return array Array of field names from user table, not including anything + * listed in $already + */ +function get_extra_user_fields($context, $already = array()) { + global $CFG; + + // Only users with permission get the extra fields + if (!has_capability('moodle/site:viewuseridentity', $context)) { + return array(); + } + + // Split showuseridentity on comma + if ($CFG->showuseridentity === '') { + // Explode gives wrong result with empty string + $extra = array(); + } else { + $extra = explode(',', $CFG->showuseridentity); + } + $renumber = false; + foreach ($extra as $key => $field) { + if (in_array($field, $already)) { + unset($extra[$key]); + $renumber = true; + } + } + if ($renumber) { + // For consistency, if entries are removed from array, renumber it + // so they are numbered as you would expect + $extra = array_merge($extra); + } + return $extra; +} + +/** + * If the current user is to be shown extra user fields when listing or + * selecting users, returns a string suitable for including in an SQL select + * clause to retrieve those fields. + * @param object $context Context + * @param string $alias Alias of user table, e.g. 'u' (default none) + * @param string $prefix Prefix for field names using AS, e.g. 'u_' (default none) + * @param array $already Array of fields that we're going to include anyway + * so don't list them (default none) + * @return string Partial SQL select clause, beginning with comma, for example + * ',u.idnumber,u.department' unless it is blank + */ +function get_extra_user_fields_sql($context, $alias='', $prefix='', + $already = array()) { + $fields = get_extra_user_fields($context, $already); + $result = ''; + // Add punctuation for alias + if ($alias !== '') { + $alias .= '.'; + } + foreach ($fields as $field) { + $result .= ', ' . $alias . $field; + if ($prefix) { + $result .= ' AS ' . $prefix . $field; + } + } + return $result; +} + +/** + * Returns the display name of a field in the user table. Works for most fields + * that are commonly displayed to users. + * @param string $field Field name, e.g. 'phone1' + * @return string Text description taken from language file, e.g. 'Phone number' + */ +function get_user_field_name($field) { + // Some fields have language strings which are not the same as field name + switch ($field) { + case 'phone1' : return get_string('phone'); + } + // Otherwise just use the same lang string + return get_string($field); +} + /** * Returns whether a given authentication plugin exists. * diff --git a/lib/simpletest/testmoodlelib.php b/lib/simpletest/testmoodlelib.php index fed0767a061..d0070546638 100644 --- a/lib/simpletest/testmoodlelib.php +++ b/lib/simpletest/testmoodlelib.php @@ -1203,6 +1203,81 @@ class moodlelib_test extends UnitTestCase { } } + public function test_get_extra_user_fields() { + global $CFG; + $oldshowuseridentity = $CFG->showuseridentity; + + // It would be really nice if there were a way to 'mock' has_capability + // checks (either to return true or false) but as there is not, this + // test doesn't test the capability check. Presumably, anyone running + // unit tests will have the capability. + $context = context_system::instance(); + + // No fields + $CFG->showuseridentity = ''; + $this->assertEqual(array(), get_extra_user_fields($context)); + + // One field + $CFG->showuseridentity = 'frog'; + $this->assertEqual(array('frog'), get_extra_user_fields($context)); + + // Two fields + $CFG->showuseridentity = 'frog,zombie'; + $this->assertEqual(array('frog', 'zombie'), get_extra_user_fields($context)); + + // No fields, except + $CFG->showuseridentity = ''; + $this->assertEqual(array(), get_extra_user_fields($context, array('frog'))); + + // One field + $CFG->showuseridentity = 'frog'; + $this->assertEqual(array(), get_extra_user_fields($context, array('frog'))); + + // Two fields + $CFG->showuseridentity = 'frog,zombie'; + $this->assertEqual(array('zombie'), get_extra_user_fields($context, array('frog'))); + + // As long as this test passes, the value will be set back. This is only + // in-memory anyhow + $CFG->showuseridentity = $oldshowuseridentity; + } + + public function test_get_extra_user_fields_sql() { + global $CFG; + $oldshowuseridentity = $CFG->showuseridentity; + $context = context_system::instance(); + + // No fields + $CFG->showuseridentity = ''; + $this->assertEqual('', get_extra_user_fields_sql($context)); + + // One field + $CFG->showuseridentity = 'frog'; + $this->assertEqual(', frog', get_extra_user_fields_sql($context)); + + // Two fields with table prefix + $CFG->showuseridentity = 'frog,zombie'; + $this->assertEqual(', u1.frog, u1.zombie', get_extra_user_fields_sql($context, 'u1')); + + // Two fields with field prefix + $CFG->showuseridentity = 'frog,zombie'; + $this->assertEqual(', frog AS u_frog, zombie AS u_zombie', + get_extra_user_fields_sql($context, '', 'u_')); + + // One field excluded + $CFG->showuseridentity = 'frog'; + $this->assertEqual('', get_extra_user_fields_sql($context, '', '', array('frog'))); + + // Two fields, one excluded, table+field prefix + $CFG->showuseridentity = 'frog,zombie'; + $this->assertEqual(', u1.zombie AS u_zombie', + get_extra_user_fields_sql($context, 'u1', 'u_', array('frog'))); + + // As long as this test passes, the value will be set back. This is only + // in-memory anyhow + $CFG->showuseridentity = $oldshowuseridentity; + } + public function test_userdate() { global $USER, $CFG;