Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

Contains the user_favourite_service class, part of the service layer for the favourites subsystem.

Copyright: 2018 Jake Dallimore <jrhdallimore@gmail.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 281 lines (13 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: user_favourite_service  - X-Ref

Class service, providing an single API for interacting with the favourites subsystem for a SINGLE USER.

This class is responsible for exposing key operations (add, remove, find) and enforces any business logic necessary to validate
authorization/data integrity for these operations.

All object persistence is delegated to the favourite_repository_interface object.

__construct(\context_user $usercontext, favourite_repository_interface $repository)   X-Ref
The user_favourite_service constructor.

param: \context_user $usercontext The context of the user to which this service operations are scoped.
param: \core_favourites\local\repository\favourite_repository_interface $repository a favourites repository.

create_favourite(string $component, string $itemtype, int $itemid, \context $context,int $ordering = null)   X-Ref
Favourite an item defined by itemid/context, in the area defined by component/itemtype.

param: string $component the frankenstyle component name.
param: string $itemtype the type of the item being favourited.
param: int $itemid the id of the item which is to be favourited.
param: \context $context the context in which the item is to be favourited.
param: int|null $ordering optional ordering integer used for sorting the favourites in an area.
return: favourite the favourite, once created.

find_favourites_by_type(string $component, string $itemtype, int $limitfrom = 0, int $limitnum = 0)   X-Ref
Find a list of favourites, by type, where type is the component/itemtype pair.

E.g. "Find all favourite courses" might result in:
$favcourses = find_favourites_by_type('core_course', 'course');

param: string $component the frankenstyle component name.
param: string $itemtype the type of the favourited item.
param: int $limitfrom optional pagination control for returning a subset of records, starting at this point.
param: int $limitnum optional pagination control for returning a subset comprising this many records.
return: array the list of favourites found.

find_all_favourites(string $component, array $itemtypes = [], int $limitfrom = 0, int $limitnum = 0)   X-Ref
Find a list of favourites, by multiple types within a component.

E.g. "Find all favourites in the activity chooser" might result in:
$favcourses = find_all_favourites('core_course', ['contentitem_mod_assign','contentitem_mod_assignment');

param: string $component the frankenstyle component name.
param: array $itemtypes optional the type of the favourited item.
param: int $limitfrom optional pagination control for returning a subset of records, starting at this point.
param: int $limitnum optional pagination control for returning a subset comprising this many records.
return: array the list of favourites found.

get_join_sql_by_type(string $component, string $itemtype, string $tablealias, string $joinitemid)   X-Ref
Returns the SQL required to include favourite information for a given component/itemtype combination.

Generally, find_favourites_by_type() is the recommended way to fetch favourites.

This method is used to include favourite information in external queries, for items identified by their
component and itemtype, matching itemid to the $joinitemid, and for the user to which this service is scoped.

It uses a LEFT JOIN to preserve the original records. If you wish to restrict your records, please consider using a
"WHERE {$tablealias}.id IS NOT NULL" in your query.

Example usage:

list($sql, $params) = $service->get_join_sql_by_type('core_message', 'message_conversations', 'myfavouritetablealias',
'conv.id');
Results in $sql:
"LEFT JOIN {favourite} fav
ON fav.component = :favouritecomponent
AND fav.itemtype = :favouriteitemtype
AND fav.userid = 1234
AND fav.itemid = conv.id"
and $params:
['favouritecomponent' => 'core_message', 'favouriteitemtype' => 'message_conversations']

param: string $component the frankenstyle component name.
param: string $itemtype the type of the favourited item.
param: string $tablealias the desired alias for the favourites table.
param: string $joinitemid the table and column identifier which the itemid is joined to. E.g. conversation.id.
return: array the list of sql and params, in the format [$sql, $params].

delete_favourite(string $component, string $itemtype, int $itemid, \context $context)   X-Ref
Delete a favourite item from an area and from within a context.

E.g. delete a favourite course from the area 'core_course', 'course' with itemid 3 and from within the CONTEXT_USER context.

param: string $component the frankenstyle component name.
param: string $itemtype the type of the favourited item.
param: int $itemid the id of the item which was favourited (not the favourite's id).
param: \context $context the context of the item which was favourited.

favourite_exists(string $component, string $itemtype, int $itemid, \context $context)   X-Ref
Check whether an item has been marked as a favourite in the respective area.

param: string $component the frankenstyle component name.
param: string $itemtype the type of the favourited item.
param: int $itemid the id of the item which was favourited (not the favourite's id).
param: \context $context the context of the item which was favourited.
return: bool true if the item is favourited, false otherwise.

get_favourite(string $component, string $itemtype, int $itemid, \context $context)   X-Ref
Get the favourite.

param: string $component the frankenstyle component name.
param: string $itemtype the type of the favourited item.
param: int $itemid the id of the item which was favourited (not the favourite's id).
param: \context $context the context of the item which was favourited.
return: favourite|null

count_favourites_by_type(string $component, string $itemtype, \context $context = null)   X-Ref
Count the favourite by item type.

param: string $component the frankenstyle component name.
param: string $itemtype the type of the favourited item.
param: \context|null $context the context of the item which was favourited.
return: int