Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

Profile field API library file.

Copyright: 2007 onwards Shane Elliot {@link http://pukunui.com}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 939 lines (32 kb)
Included or required:0 times
Referenced: 56 times
Includes or requires: 0 files

Defines 2 classes


Class: profile_field_base  - X-Ref

Base class for the customisable profile fields.

__construct($fieldid=0, $userid=0, $fielddata=null)   X-Ref
Constructor method.

param: int $fieldid id of the profile from the user_info_field table
param: int $userid id of the user for whom we are displaying data
param: stdClass $fielddata optional data for the field object plus additional fields 'hasuserdata', 'data' and 'dataformat'

profile_field_base($fieldid=0, $userid=0)   X-Ref
Old syntax of class constructor. Deprecated in PHP7.


Class: name  - X-Ref

edit_field_add($mform)   X-Ref
Abstract method: Adds the profile field to the moodle form class

param: MoodleQuickForm $mform instance of the moodleform class

display_data()   X-Ref
Display the data for this field

return: string

edit_field($mform)   X-Ref
Print out the form field in the edit profile page

param: MoodleQuickForm $mform instance of the moodleform class
return: bool

edit_after_data($mform)   X-Ref
Tweaks the edit form

param: MoodleQuickForm $mform instance of the moodleform class
return: bool

edit_save_data($usernew)   X-Ref
Saves the data coming from form

param: stdClass $usernew data coming from the form

edit_validate_field($usernew)   X-Ref
Validate the form field from profile page

param: stdClass $usernew
return: array  error messages for the form validation

edit_field_set_default($mform)   X-Ref
Sets the default data for the field in the form object

param: MoodleQuickForm $mform instance of the moodleform class

edit_field_set_required($mform)   X-Ref
Sets the required flag for the field in the form object

param: MoodleQuickForm $mform instance of the moodleform class

edit_field_set_locked($mform)   X-Ref
HardFreeze the field if locked.

param: MoodleQuickForm $mform instance of the moodleform class

edit_save_data_preprocess($data, $datarecord)   X-Ref
Hook for child classess to process the data before it gets saved in database

param: stdClass $data
param: stdClass $datarecord The object that will be used to save the record
return: mixed

edit_load_user_data($user)   X-Ref
Loads a user object with data for this field ready for the edit profile
form

param: stdClass $user a user object

is_user_object_data()   X-Ref
Check if the field data should be loaded into the user object
By default it is, but for field types where the data may be potentially
large, the child class should override this and return false

return: bool

set_userid($userid)   X-Ref
Accessor method: set the userid for this instance

param: integer $userid id from the user table

set_fieldid($fieldid)   X-Ref
Accessor method: set the fieldid for this instance

param: integer $fieldid id from the user_info_field table

set_field($field)   X-Ref
Sets the field object and default data and format into $this->data and $this->dataformat

This method should be called before {@link self::set_user_data}

param: stdClass $field

set_user_data($data, $dataformat)   X-Ref
Sets user id and user data for the field

param: mixed $data
param: int $dataformat

set_category_name($categoryname)   X-Ref
Set the name for the profile category where this field is

param: string $categoryname

get_shortname()   X-Ref
Return field short name

return: string

get_category_name()   X-Ref
Returns the name of the profile category where this field is

return: string

load_data()   X-Ref
Accessor method: Load the field record and user data associated with the
object's fieldid and userid


is_visible()   X-Ref
Check if the field data is visible to the current user

return: bool

is_editable()   X-Ref
Check if the field data is editable for the current user
This method should not generally be overwritten by child classes.

return: bool

is_empty()   X-Ref
Check if the field data is considered empty

return: boolean

is_required()   X-Ref
Check if the field is required on the edit profile page

return: bool

is_locked()   X-Ref
Check if the field is locked on the edit profile page

return: bool

is_unique()   X-Ref
Check if the field data should be unique

return: bool

is_signup_field()   X-Ref
Check if the field should appear on the signup page

return: bool

get_field_config_for_external()   X-Ref
Return the field settings suitable to be exported via an external function.
By default it return all the field settings.

return: array all the settings

get_field_properties()   X-Ref
Return the field type and null properties.
This will be used for validating the data submitted by a user.

return: array the param type and null property

profile_get_user_fields_with_data(int $userid)   X-Ref
Returns an array of all custom field records with any defined data (or empty data), for the specified user id.

param: int $userid
return: profile_field_base[]

profile_get_user_fields_with_data_by_category(int $userid)   X-Ref
Returns an array of all custom field records with any defined data (or empty data), for the specified user id, by category.

param: int $userid
return: profile_field_base[][]

profile_load_data(stdClass $user)   X-Ref
Loads user profile field data into the user object.

param: stdClass $user

profile_definition(MoodleQuickForm $mform, int $userid = 0)   X-Ref
Print out the customisable categories and fields for a users profile

param: MoodleQuickForm $mform instance of the moodleform class
param: int $userid id of user whose profile is being edited or 0 for the new user

profile_definition_after_data(MoodleQuickForm $mform, int $userid)   X-Ref
Adds profile fields to user edit forms.

param: MoodleQuickForm $mform
param: int $userid

profile_validation(stdClass $usernew, array $files)   X-Ref
Validates profile data.

param: stdClass $usernew
param: array $files
return: array array of errors, same as in {@see moodleform::validation()}

profile_save_data(stdClass $usernew)   X-Ref
Saves profile data for a user.

param: stdClass $usernew

profile_display_fields($userid)   X-Ref
Display profile fields.

param: int $userid

profile_get_signup_fields()   X-Ref
Retrieves a list of profile fields that must be displayed in the sign-up form.

return: array list of profile fields info

profile_signup_fields(MoodleQuickForm $mform)   X-Ref
Adds code snippet to a moodle form object for custom profile fields that
should appear on the signup page

param: MoodleQuickForm $mform moodle form object

profile_user_record(int $userid, bool $onlyinuserobject = true)   X-Ref
Returns an object with the custom profile fields set for the given user

param: int $userid
param: bool $onlyinuserobject True if you only want the ones in $USER.
return: stdClass object where properties names are shortnames of custom profile fields

profile_get_custom_fields(bool $onlyinuserobject = false)   X-Ref
Obtains a list of all available custom profile fields, indexed by id.

Some profile fields are not included in the user object data (see
profile_user_record function above). Optionally, you can obtain only those
fields that are included in the user object.

To be clear, this function returns the available fields, and does not
return the field values for a particular user.

param: bool $onlyinuserobject True if you only want the ones in $USER
return: array Array of field objects from database (indexed by id)

profile_load_custom_fields($user)   X-Ref
Load custom profile fields into user object

param: stdClass $user user object

profile_save_custom_fields($userid, $profilefields)   X-Ref
Save custom profile fields for a user.

param: int $userid The user id
param: array $profilefields The fields to save

profile_get_custom_field_data_by_shortname(string $shortname, bool $casesensitive = true)   X-Ref
Gets basic data about custom profile fields. This is minimal data that is cached within the
current request for all fields so that it can be used quickly.

param: string $shortname Shortname of custom profile field
param: bool $casesensitive Whether to perform case-sensitive matching of shortname. Note current limitations of custom profile
return: stdClass|null Object with properties id, shortname, name, visible, datatype, categoryid, etc

profile_view($user, $context, $course = null)   X-Ref
Trigger a user profile viewed event.

param: stdClass  $user user  object
param: stdClass  $context  context object (course or user)
param: stdClass  $course course  object

profile_has_required_custom_fields_set($userid)   X-Ref
Does the user have all required custom fields set?

Internal, to be exclusively used by {@link user_not_fully_set_up()} only.

Note that if users have no way to fill a required field via editing their
profiles (e.g. the field is not visible or it is locked), we still return true.
So this is actually checking if we should redirect the user to edit their
profile, rather than whether there is a value in the database.

param: int $userid
return: bool