1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Contains the component_favourite_service class, part of the service layer for the favourites subsystem. 19 * 20 * @package core_favourites 21 * @copyright 2019 Jake Dallimore <jrhdallimore@gmail.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace core_favourites\local\service; 25 use \core_favourites\local\repository\favourite_repository_interface; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Class service, providing an single API for interacting with the favourites subsystem, for all favourites of a specific component. 31 * 32 * This class provides operations which can be applied to favourites within a component, based on type and context identifiers. 33 * 34 * All object persistence is delegated to the favourite_repository_interface object. 35 * 36 * @copyright 2019 Jake Dallimore <jrhdallimore@gmail.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class component_favourite_service { 40 41 /** @var favourite_repository_interface $repo the favourite repository object. */ 42 protected $repo; 43 44 /** @var int $component the frankenstyle component name to which this favourites service is scoped. */ 45 protected $component; 46 47 /** 48 * The component_favourite_service constructor. 49 * 50 * @param string $component The frankenstyle name of the component to which this service operations are scoped. 51 * @param \core_favourites\local\repository\favourite_repository_interface $repository a favourites repository. 52 * @throws \moodle_exception if the component name is invalid. 53 */ 54 public function __construct(string $component, favourite_repository_interface $repository) { 55 if (!in_array($component, \core_component::get_component_names())) { 56 throw new \moodle_exception("Invalid component name '$component'"); 57 } 58 $this->repo = $repository; 59 $this->component = $component; 60 } 61 62 63 /** 64 * Delete a collection of favourites by type and item, and optionally for a given context. 65 * 66 * E.g. delete all favourites of type 'message_conversations' for the conversation '11' and in the CONTEXT_COURSE context. 67 * 68 * @param string $itemtype the type of the favourited items. 69 * @param int $itemid the id of the item to which the favourites relate 70 * @param \context $context the context of the items which were favourited. 71 */ 72 public function delete_favourites_by_type_and_item(string $itemtype, int $itemid, \context $context = null) { 73 $criteria = ['component' => $this->component, 'itemtype' => $itemtype, 'itemid' => $itemid] + 74 ($context ? ['contextid' => $context->id] : []); 75 $this->repo->delete_by($criteria); 76 } 77 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body