class user_support

internal  
 

Utility functions for Moodle users and custom profile fields.

Methods

static mixed
get_field_by_user(int|string $fieldidorshortname, int|null $userid = null)

Retrieves a custom profile field value for a user.

static array
get_options_for_user_fields(bool $includeemailfield = false)

Retrieves a list of user fields available for selection.

static array
get_all_profile_fields()

Retrieves all custom profile fields.

static false|stdClass|null
get_user_field(int $fieldid, int|null $userid = null)

Retrieves a specific user profile data record.

static false|int|stdClass|null
get_user_field_data_by_shortname(string $shortname, int|null $userid = null)

Retrieves profile field information or user data by shortname.

static void
save_profile_fields(int $userid, int|string $fieldidorshortname, string $value)

Saves a custom profile field value for a specific user.

static user|null
get_user_by_email(string $email, string $fields = '*', int|null $mnethostid = null, int $strictness = IGNORE_MISSING)

Retrieves a user by their email address.

static user|null
get_user(int $userid, string $fields = '*', int $strictness = IGNORE_MISSING)

Retrieves a user by their database ID.

static user|null
get_user_by_username(string $username, int|null $mnethostid = null, int $strictness = IGNORE_MISSING)

Retrieves a user by their username.

static int
create_user(stdClass $userobj, bool $updatepassword = false, bool $nologin = false)

Creates a new Moodle user with sensible defaults.

static void
update_user(stdClass $userobj, bool $updatepassword = true, bool $triggerevent = true)

Updates an existing Moodle user.

static bool
delete_user(stdClass $user)

Soft-deletes a user.

static string
fullname(user $user)

Retrieves the full name of a user.

static int
get_current_user_id()

Retrieves the current logged-in user ID.

static user|null
get_current()

Retrieves the current user entity.

Details

at line 47
static mixed get_field_by_user(int|string $fieldidorshortname, int|null $userid = null)

Retrieves a custom profile field value for a user.

Parameters

int|string $fieldidorshortname

field ID or shortname

int|null $userid

optional user ID (default: current user)

Return Value

mixed

field value or null if not found

at line 85
static array get_options_for_user_fields(bool $includeemailfield = false)

Retrieves a list of user fields available for selection.

Parameters

bool $includeemailfield

whether to include the core 'email' field

Return Value

array

Map of field identifier to label

at line 120
static array get_all_profile_fields()

Retrieves all custom profile fields.

Return Value

array

Map of shortname to field name

at line 139
static false|stdClass|null get_user_field(int $fieldid, int|null $userid = null)

Retrieves a specific user profile data record.

Parameters

int $fieldid

custom profile field ID

int|null $userid

optional user ID (default: current user)

Return Value

false|stdClass|null

data record or false if not found

at line 182
static false|int|stdClass|null get_user_field_data_by_shortname(string $shortname, int|null $userid = null)

Retrieves profile field information or user data by shortname.

If $userid is null/0, returns the field ID (int). If $userid is provided, returns the user data record (stdClass).

Parameters

string $shortname

custom profile field shortname

int|null $userid

optional User ID

Return Value

false|int|stdClass|null

field ID, data record, or false

at line 222
static void save_profile_fields(int $userid, int|string $fieldidorshortname, string $value)

Saves a custom profile field value for a specific user.

Parameters

int $userid

User ID

int|string $fieldidorshortname

field ID or shortname

string $value

value to save

Return Value

void

at line 252
static user|null get_user_by_email(string $email, string $fields = '*', int|null $mnethostid = null, int $strictness = IGNORE_MISSING)

Retrieves a user by their email address.

Parameters

string $email

the email address

string $fields

comma-separated list of fields to select

int|null $mnethostid

MNet host ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE or MUST_EXIST)

Return Value

user|null

user entity or null if not found

at line 268
static user|null get_user(int $userid, string $fields = '*', int $strictness = IGNORE_MISSING)

Retrieves a user by their database ID.

Parameters

int $userid

User ID

string $fields

comma-separated list of fields to select

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

user|null

user entity or null if not found

at line 284
static user|null get_user_by_username(string $username, int|null $mnethostid = null, int $strictness = IGNORE_MISSING)

Retrieves a user by their username.

Parameters

string $username username
int|null $mnethostid

MNet host ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

user|null

user entity or null if not found

at line 300
static int create_user(stdClass $userobj, bool $updatepassword = false, bool $nologin = false)

Creates a new Moodle user with sensible defaults.

Parameters

stdClass $userobj

user data object

bool $updatepassword

whether to update the password

bool $nologin

whether to prevent the user from logging in

Return Value

int

new User ID

at line 327
static void update_user(stdClass $userobj, bool $updatepassword = true, bool $triggerevent = true)

Updates an existing Moodle user.

Parameters

stdClass $userobj

user data object (must include 'id')

bool $updatepassword

whether to update the password

bool $triggerevent

whether to trigger Moodle events

Return Value

void

at line 343
static bool delete_user(stdClass $user)

Soft-deletes a user.

Parameters

stdClass $user

user object to delete

Return Value

bool

True on success, false otherwise

at line 359
static string fullname(user $user)

Retrieves the full name of a user.

Parameters

user $user

user object or entity

Return Value

string

formatted full name

at line 371
static int get_current_user_id()

Retrieves the current logged-in user ID.

Return Value

int

current user ID or 0 if not logged in

at line 383
static user|null get_current()

Retrieves the current user entity.

Return Value

user|null

current user entity or null if not logged in