MDL-63058 core_favourites: add existence checks to the service layer

This allows someone using the user_favourite_service to check whether
an item is already marked as a favourite.
This commit is contained in:
Jake Dallimore 2018-10-18 16:01:13 +08:00 committed by Bas Brands
parent cc486e6125
commit b81722e22f
2 changed files with 68 additions and 0 deletions

View file

@ -135,4 +135,25 @@ class user_favourite_service {
$this->repo->delete($favourite->id);
}
/**
* 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.
*/
public function favourite_exists(string $component, string $itemtype, int $itemid, \context $context) : bool {
return $this->repo->exists_by(
[
'userid' => $this->userid,
'component' => $component,
'itemtype' => $itemtype,
'itemid' => $itemid,
'contextid' => $context->id
]
);
}
}