protected function get_cached_user_profile_field($userid) {
        global $USER, $DB, $CFG;
        $iscurrentuser = $USER->id == $userid;
        if (isguestuser($userid) || ($iscurrentuser && !isloggedin())) {
            // Must be logged in and can't be the guest.
            return false;
        }

        // Custom profile fields will be numeric, there are no numeric standard profile fields so this is not a problem.
        $iscustomprofilefield = $this->customfield ? true : false;
        if ($iscustomprofilefield) {
            // As its a custom profile field we need to map the id back to the actual field.
            // We'll also preload all of the other custom profile fields just in case and ensure we have the
            // default value available as well.
            if (!array_key_exists($this->customfield, self::get_custom_profile_fields())) {
                // No such field exists.
                // This shouldn't normally happen but occur if things go wrong when deleting a custom profile field
                // or when restoring a backup of a course with user profile field conditions.
                return false;
            }
            $field = $this->customfield;
        } else {
            $field = $this->standardfield;
        }

        // If its the current user than most likely we will be able to get this information from $USER.
        // If its a regular profile field then it should already be available, if not then we have a mega problem.
        // If its a custom profile field then it should be available but may not be. If it is then we use the value
        // available, otherwise we load all custom profile fields into a temp object and refer to that.
        // Noting its not going be great for performance if we have to use the temp object as it involves loading the
        // custom profile field API and classes.
        if ($iscurrentuser) {
            if (!$iscustomprofilefield) {
                if (property_exists($USER, $field)) {
                    return $USER->{$field};
                } else {
                    // Unknown user field. This should not happen.
                    throw new \coding_exception('Requested user profile field does not exist');
                }
            }
            // Checking if the custom profile fields are already available.
            if (!isset($USER->profile)) {
                // Drat! they're not. We need to use a temp object and load them.
                // We don't use $USER as the profile fields are loaded into the object.
                $user = new \stdClass;
                $user->id = $USER->id;
                // This should ALWAYS be set, but just in case we check.
                require_once($CFG->dirroot . '/user/profile/lib.php');
                profile_load_custom_fields($user);
                if (array_key_exists($field, $user->profile)) {
                    return $user->profile[$field];
                }
            } else if (array_key_exists($field, $USER->profile)) {
                // Hurrah they're available, this is easy.
                return $USER->profile[$field];
            }
            // The profile field doesn't exist.
            return false;
        } else {
            // Loading for another user.
            if ($iscustomprofilefield) {
                // Fetch the data for the field. Noting we keep this query simple so that Database caching takes care of performance
                // for us (this will likely be hit again).
                // We are able to do this because we've already pre-loaded the custom fields.
                $data = $DB->get_field('user_info_data', 'data', array('userid' => $userid,
                        'fieldid' => self::$customprofilefields[$field]->id), IGNORE_MISSING);
                // If we have data return that, otherwise return the default.
                if ($data !== false) {
                    return $data;
                } else {
                    return self::$customprofilefields[$field]->defaultdata;
                }
            } else {
                // Its a standard field, retrieve it from the user.
                return $DB->get_field('user', $field, array('id' => $userid), MUST_EXIST);
            }
        }
        return false;
    }
protected function get_cached_user_profile_field($userid) {
        global $USER, $DB, $CFG;
        $iscurrentuser = $USER->id == $userid;
        if (isguestuser($userid) || ($iscurrentuser && !isloggedin())) {
            // Must be logged in and can't be the guest.
            return false;
        }

        // Custom profile fields will be numeric, there are no numeric standard profile fields so this is not a problem.
        $iscustomprofilefield = $this->customfield ? true : false;
        if ($iscustomprofilefield) {
            // As its a custom profile field we need to map the id back to the actual field.
            // We'll also preload all of the other custom profile fields just in case and ensure we have the
            // default value available as well.
            if (!array_key_exists($this->customfield, self::get_custom_profile_fields())) {
                // No such field exists.
                // This shouldn't normally happen but occur if things go wrong when deleting a custom profile field
                // or when restoring a backup of a course with user profile field conditions.
                return false;
            }
            $field = $this->customfield;
        } else {
            $field = $this->standardfield;
        }

        // If its the current user than most likely we will be able to get this information from $USER.
        // If its a regular profile field then it should already be available, if not then we have a mega problem.
        // If its a custom profile field then it should be available but may not be. If it is then we use the value
        // available, otherwise we load all custom profile fields into a temp object and refer to that.
        // Noting its not going be great for performance if we have to use the temp object as it involves loading the
        // custom profile field API and classes.
        if ($iscurrentuser) {
            if (!$iscustomprofilefield) {
                if (property_exists($USER, $field)) {
                    return $USER->{$field};
                } else {
                    // Unknown user field. This should not happen.
                    throw new \coding_exception('Requested user profile field does not exist');
                }
            }
            // Checking if the custom profile fields are already available.
            if (!isset($USER->profile)) {
                // Drat! they're not. We need to use a temp object and load them.
                // We don't use $USER as the profile fields are loaded into the object.
                $user = new \stdClass;
                $user->id = $USER->id;
                // This should ALWAYS be set, but just in case we check.
                require_once($CFG->dirroot . '/user/profile/lib.php');
                profile_load_custom_fields($user);
                if (array_key_exists($field, $user->profile)) {
                    return $user->profile[$field];
                }
            } else if (array_key_exists($field, $USER->profile)) {
                // Hurrah they're available, this is easy.
                return $USER->profile[$field];
            }
            // The profile field doesn't exist.
            return false;
        } else {
            // Loading for another user.
            if ($iscustomprofilefield) {
                // Fetch the data for the field. Noting we keep this query simple so that Database caching takes care of performance
                // for us (this will likely be hit again).
                // We are able to do this because we've already pre-loaded the custom fields.
                $data = $DB->get_field('user_info_data', 'data', array('userid' => $userid,
                        'fieldid' => self::$customprofilefields[$field]->id), IGNORE_MISSING);
                // If we have data return that, otherwise return the default.
                if ($data !== false) {
                    return $data;
                } else {
                    return self::$customprofilefields[$field]->defaultdata;
                }
            } else {
                // Its a standard field, retrieve it from the user.
                return $DB->get_field('user', $field, array('id' => $userid), MUST_EXIST);
            }
        }
        return false;
    }
protected function get_cached_user_profile_field ( $ userid ) { global $ USER , $ DB , $ CFG ; $ iscurrentuser = $ USER -> id == $ userid ; if ( isguestuser ( $ userid ) || ( $ iscurrentuser && ! isloggedin ( ) ) ) { // Must be logged in and can't be the guest. return false ; } // Custom profile fields will be numeric, there are no numeric standard profile fields so this is not a problem. $ iscustomprofilefield = $ this -> customfield ? true : false ; if ( $ iscustomprofilefield ) { // As its a custom profile field we need to map the id back to the actual field. // We'll also preload all of the other custom profile fields just in case and ensure we have the // default value available as well. if ( ! array_key_exists ( $ this -> customfield , self :: get_custom_profile_fields ( ) ) ) { // No such field exists. // This shouldn't normally happen but occur if things go wrong when deleting a custom profile field // or when restoring a backup of a course with user profile field conditions. return false ; } $ field = $ this -> customfield ; } else { $ field = $ this -> standardfield ; } // If its the current user than most likely we will be able to get this information from $USER. // If its a regular profile field then it should already be available, if not then we have a mega problem. // If its a custom profile field then it should be available but may not be. If it is then we use the value // available, otherwise we load all custom profile fields into a temp object and refer to that. // Noting its not going be great for performance if we have to use the temp object as it involves loading the // custom profile field API and classes. if ( $ iscurrentuser ) { if ( ! $ iscustomprofilefield ) { if ( property_exists ( $ USER , $ field ) ) { return $ USER -> { $ field } ; } else { // Unknown user field. This should not happen. throw new \ coding_exception ( 'Requested user profile field does not exist' ) ; } } // Checking if the custom profile fields are already available. if ( ! isset ( $ USER -> profile ) ) { // Drat! they're not. We need to use a temp object and load them. // We don't use $USER as the profile fields are loaded into the object. $ user = new \ stdClass ; $ user -> id = $ USER -> id ; // This should ALWAYS be set, but just in case we check. require_once ( $ CFG -> dirroot . '/user/profile/lib.php' ) ; profile_load_custom_fields ( $ user ) ; if ( array_key_exists ( $ field , $ user -> profile ) ) { return $ user -> profile [ $ field ] ; } } else if ( array_key_exists ( $ field , $ USER -> profile ) ) { // Hurrah they're available, this is easy. return $ USER -> profile [ $ field ] ; } // The profile field doesn't exist. return false ; } else { // Loading for another user. if ( $ iscustomprofilefield ) { // Fetch the data for the field. Noting we keep this query simple so that Database caching takes care of performance // for us (this will likely be hit again). // We are able to do this because we've already pre-loaded the custom fields. $ data = $ DB -> get_field ( 'user_info_data' , 'data' , array ( 'userid' => $ userid , 'fieldid' => self :: $ customprofilefields [ $ field ] -> id ) , IGNORE_MISSING ) ; // If we have data return that, otherwise return the default. if ( $ data !== false ) { return $ data ; } else { return self :: $ customprofilefields [ $ field ] -> defaultdata ; } } else { // Its a standard field, retrieve it from the user. return $ DB -> get_field ( 'user' , $ field , array ( 'id' => $ userid ) , MUST_EXIST ) ; } } return false ; }
Return the value for a user's profile field

@param int $userid User ID
@return string|bool Value, or false if user does not have a value for this field
Return the value for a user s profile field