Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Class: subscriptions  - X-Ref

Forum subscription manager.

is_subscribed($userid, $forum, $discussionid = null, $cm = null)   X-Ref
Whether a user is subscribed to this forum, or a discussion within
the forum.

If a discussion is specified, then report whether the user is
subscribed to posts to this particular discussion, taking into
account the forum preference.

If it is not specified then only the forum preference is considered.

param: int $userid The user ID
param: \stdClass $forum The record of the forum to test
param: int $discussionid The ID of the discussion to check
param: $cm The coursemodule record. If not supplied, this will be calculated using get_fast_modinfo instead.
return: boolean

is_subscribed_to_forum($userid, $forum)   X-Ref
Whether a user is subscribed to this forum.

param: int $userid The user ID
param: \stdClass $forum The record of the forum to test
return: boolean

is_forcesubscribed($forum)   X-Ref
Helper to determine whether a forum has it's subscription mode set
to forced subscription.

param: \stdClass $forum The record of the forum to test
return: bool

subscription_disabled($forum)   X-Ref
Helper to determine whether a forum has it's subscription mode set to disabled.

param: \stdClass $forum The record of the forum to test
return: bool

is_subscribable($forum)   X-Ref
Helper to determine whether the specified forum can be subscribed to.

param: \stdClass $forum The record of the forum to test
return: bool

set_subscription_mode($forum, $status = FORUM_FORCESUBSCRIBE)   X-Ref
Set the forum subscription mode.

By default when called without options, this is set to FORUM_FORCESUBSCRIBE.

param: \stdClass $forum The record of the forum to set
param: int $status The new subscription state
return: bool true

get_subscription_mode($forum)   X-Ref
Returns the current subscription mode for the forum.

param: \stdClass $forum The record of the forum to set
return: int The forum subscription mode

get_unsubscribable_forums()   X-Ref
Returns an array of forums that the current user is subscribed to and is allowed to unsubscribe from

return: array An array of unsubscribable forums

get_potential_subscribers($context, $groupid, $fields, $sort = '')   X-Ref
Get the list of potential subscribers to a forum.

param: context_module $context the forum context.
param: integer $groupid the id of a group, or 0 for all groups.
param: string $fields the list of fields to return for each user. As for get_users_by_capability.
param: string $sort sort order. As for get_users_by_capability.
return: array list of users.

fetch_subscription_cache($forumid, $userid)   X-Ref
Fetch the forum subscription data for the specified userid and forum.

param: int $forumid The forum to retrieve a cache for
param: int $userid The user ID
return: boolean

fill_subscription_cache($forumid, $userid = null)   X-Ref
Fill the forum subscription data for the specified userid and forum.

If the userid is not specified, then all subscription data for that forum is fetched in a single query and used
for subsequent lookups without requiring further database queries.

param: int $forumid The forum to retrieve a cache for
param: int $userid The user ID
return: void

fill_subscription_cache_for_course($courseid, $userid)   X-Ref
Fill the forum subscription data for all forums that the specified userid can subscribe to in the specified course.

param: int $courseid The course to retrieve a cache for
param: int $userid The user ID
return: void

fetch_subscribed_users($forum, $groupid = 0, $context = null, $fields = null,$includediscussionsubscriptions = false)   X-Ref
Returns a list of user objects who are subscribed to this forum.

param: stdClass $forum The forum record.
param: int $groupid The group id if restricting subscriptions to a group of users, or 0 for all.
param: context_module $context the forum context, to save re-fetching it where possible.
param: string $fields requested user fields (with "u." table prefix).
param: boolean $includediscussionsubscriptions Whether to take discussion subscriptions and unsubscriptions into consideration.
return: array list of users.

fetch_discussion_subscription($forumid, $userid = null)   X-Ref
Retrieve the discussion subscription data for the specified userid and forum.

This is returned as an array of discussions for that forum which contain the preference in a stdClass.

param: int $forumid The forum to retrieve a cache for
param: int $userid The user ID
return: array of stdClass objects with one per discussion in the forum.

fill_discussion_subscription_cache($forumid, $userid = null)   X-Ref
Fill the discussion subscription data for the specified userid and forum.

If the userid is not specified, then all discussion subscription data for that forum is fetched in a single query
and used for subsequent lookups without requiring further database queries.

param: int $forumid The forum to retrieve a cache for
param: int $userid The user ID
return: void

add_to_discussion_cache($forumid, $userid, $discussion, $preference)   X-Ref
Add the specified discussion and user preference to the discussion
subscription cache.

param: int $forumid The ID of the forum that this preference belongs to
param: int $userid The ID of the user that this preference belongs to
param: int $discussion The ID of the discussion that this preference relates to
param: int $preference The preference to store

reset_discussion_cache()   X-Ref
Reset the discussion cache.

This cache is used to reduce the number of database queries when
checking forum discussion subscription states.

reset_forum_cache()   X-Ref
Reset the forum cache.

This cache is used to reduce the number of database queries when
checking forum subscription states.

subscribe_user($userid, $forum, $context = null, $userrequest = false)   X-Ref
Adds user to the subscriber list.

param: int $userid The ID of the user to subscribe
param: \stdClass $forum The forum record for this forum.
param: \context_module|null $context Module context, may be omitted if not known or if called for the current
param: boolean $userrequest Whether the user requested this change themselves. This has an effect on whether
return: bool|int Returns true if the user is already subscribed, or the forum_subscriptions ID if the user was

unsubscribe_user($userid, $forum, $context = null, $userrequest = false)   X-Ref
Removes user from the subscriber list

param: int $userid The ID of the user to unsubscribe
param: \stdClass $forum The forum record for this forum.
param: \context_module|null $context Module context, may be omitted if not known or if called for the current
param: boolean $userrequest Whether the user requested this change themselves. This has an effect on whether
return: boolean Always returns true.

subscribe_user_to_discussion($userid, $discussion, $context = null)   X-Ref
Subscribes the user to the specified discussion.

param: int $userid The userid of the user being subscribed
param: \stdClass $discussion The discussion to subscribe to
param: \context_module|null $context Module context, may be omitted if not known or if called for the current
return: boolean Whether a change was made

unsubscribe_user_from_discussion($userid, $discussion, $context = null)   X-Ref
Unsubscribes the user from the specified discussion.

param: int $userid The userid of the user being unsubscribed
param: \stdClass $discussion The discussion to unsubscribe from
param: \context_module|null $context Module context, may be omitted if not known or if called for the current
return: boolean Whether a change was made

get_user_default_subscription($forum, $context, $cm, ?int $discussionid)   X-Ref
Gets the default subscription value for the logged in user.

param: \stdClass $forum The forum record
param: \context $context The course context
param: \cm_info $cm cm_info
param: int|null $discussionid The discussion we are checking against
return: bool Default subscription