first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the favourite class, each instance being a representation of a DB row for the 'favourite' table.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites\local\entity;
defined('MOODLE_INTERNAL') || die();
/**
* Contains the favourite class, each instance being a representation of a DB row for the 'favourite' table.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class favourite {
/** @var int $id the id of the favourite.*/
public $id;
/** @var string $component the frankenstyle name of the component containing the favourited item. E.g. 'core_course'.*/
public $component;
/** @var string $itemtype the type of the item being marked as a favourite. E.g. 'course', 'conversation', etc.*/
public $itemtype;
/** @var int $itemid the id of the item that is being marked as a favourite. e.g course->id, conversation->id, etc.*/
public $itemid;
/** @var int $contextid the id of the context in which this favourite was created.*/
public $contextid;
/** @var int $userid the id of user who owns this favourite.*/
public $userid;
/** @var int $ordering the ordering of the favourite within it's favourite area.*/
public $ordering;
/** @var int $timecreated the time at which the favourite was created.*/
public $timecreated;
/** @var int $timemodified the time at which the last modification of the favourite took place.*/
public $timemodified;
/** @var string $uniquekey favourite unique key.*/
public $uniquekey;
/**
* Favourite constructor.
* @param string $component the frankenstyle name of the component containing the favourited item. E.g. 'core_course'.
* @param string $itemtype the type of the item being marked as a favourite. E.g. 'course', 'conversation', etc.
* @param int $itemid the id of the item that is being marked as a favourite. e.g course->id, conversation->id, etc.
* @param int $contextid the id of the context in which this favourite was created.
* @param int $userid the id of user who owns this favourite.
*/
public function __construct(string $component, string $itemtype, int $itemid, int $contextid, int $userid) {
$this->component = $component;
$this->itemtype = $itemtype;
$this->itemid = $itemid;
$this->contextid = $contextid;
$this->userid = $userid;
}
}
@@ -0,0 +1,324 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the favourite_repository class, responsible for CRUD operations for favourites.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites\local\repository;
use \core_favourites\local\entity\favourite;
defined('MOODLE_INTERNAL') || die();
/**
* Class favourite_repository.
*
* This class handles persistence of favourites. Favourites from all areas are supported by this repository.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class favourite_repository implements favourite_repository_interface {
/**
* @var string the name of the table which favourites are stored in.
*/
protected $favouritetable = 'favourite';
/**
* Get a favourite object, based on a full record.
* @param \stdClass $record the record we wish to hydrate.
* @return favourite the favourite record.
*/
protected function get_favourite_from_record(\stdClass $record): favourite {
$favourite = new favourite(
$record->component,
$record->itemtype,
$record->itemid,
$record->contextid,
$record->userid
);
$favourite->id = $record->id;
$favourite->ordering = $record->ordering ?? null;
$favourite->timecreated = $record->timecreated ?? null;
$favourite->timemodified = $record->timemodified ?? null;
return $favourite;
}
/**
* Get a list of favourite objects, based on a list of records.
* @param array $records the record we wish to hydrate.
* @return array the list of favourites.
*/
protected function get_list_of_favourites_from_records(array $records) {
$list = [];
foreach ($records as $index => $record) {
$list[$index] = $this->get_favourite_from_record($record);
}
return $list;
}
/**
* Basic validation, confirming we have the minimum field set needed to save a record to the store.
*
* @param favourite $favourite the favourite record to validate.
* @throws \moodle_exception if the supplied favourite has missing or unsupported fields.
*/
protected function validate(favourite $favourite) {
$favourite = (array)$favourite;
// The allowed fields, and whether or not each is required to create a record.
// The timecreated, timemodified and id fields are generated during create/update.
$allowedfields = [
'userid' => true,
'component' => true,
'itemtype' => true,
'itemid' => true,
'contextid' => true,
'ordering' => false,
'timecreated' => false,
'timemodified' => false,
'id' => false,
'uniquekey' => false
];
$requiredfields = array_filter($allowedfields, function($field) {
return $field;
});
if ($missingfields = array_keys(array_diff_key($requiredfields, $favourite))) {
throw new \moodle_exception("Missing object property(s) '" . join(', ', $missingfields) . "'.");
}
// If the record contains fields we don't allow, throw an exception.
if ($unsupportedfields = array_keys(array_diff_key($favourite, $allowedfields))) {
throw new \moodle_exception("Unexpected object property(s) '" . join(', ', $unsupportedfields) . "'.");
}
}
/**
* Add a favourite to the repository.
*
* @param favourite $favourite the favourite to add.
* @return favourite the favourite which has been stored.
* @throws \dml_exception if any database errors are encountered.
* @throws \moodle_exception if the favourite has missing or invalid properties.
*/
public function add(favourite $favourite): favourite {
global $DB;
$this->validate($favourite);
$favourite = (array)$favourite;
$time = time();
$favourite['timecreated'] = $time;
$favourite['timemodified'] = $time;
$id = $DB->insert_record($this->favouritetable, $favourite);
return $this->find($id);
}
/**
* Add a collection of favourites to the repository.
*
* @param array $items the list of favourites to add.
* @return array the list of favourites which have been stored.
* @throws \dml_exception if any database errors are encountered.
* @throws \moodle_exception if any of the favourites have missing or invalid properties.
*/
public function add_all(array $items): array {
global $DB;
$time = time();
foreach ($items as $item) {
$this->validate($item);
$favourite = (array)$item;
$favourite['timecreated'] = $time;
$favourite['timemodified'] = $time;
$ids[] = $DB->insert_record($this->favouritetable, $favourite);
}
list($insql, $params) = $DB->get_in_or_equal($ids);
$records = $DB->get_records_select($this->favouritetable, "id $insql", $params);
return $this->get_list_of_favourites_from_records($records);
}
/**
* Find a favourite by id.
*
* @param int $id the id of the favourite.
* @return favourite the favourite.
* @throws \dml_exception if any database errors are encountered.
*/
public function find(int $id): favourite {
global $DB;
$record = $DB->get_record($this->favouritetable, ['id' => $id], '*', MUST_EXIST);
return $this->get_favourite_from_record($record);
}
/**
* Return all items in this repository, as an array, indexed by id.
*
* @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 all favourites stored within this repository.
* @throws \dml_exception if any database errors are encountered.
*/
public function find_all(int $limitfrom = 0, int $limitnum = 0): array {
global $DB;
$records = $DB->get_records($this->favouritetable, null, '', '*', $limitfrom, $limitnum);
return $this->get_list_of_favourites_from_records($records);
}
/**
* Return all items matching the supplied criteria (a [key => value,..] list).
*
* @param array $criteria the list of key/value(s) criteria pairs.
* @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 matching the criteria.
* @throws \dml_exception if any database errors are encountered.
*/
public function find_by(array $criteria, int $limitfrom = 0, int $limitnum = 0): array {
global $DB;
$conditions = [];
$params = [];
foreach ($criteria as $field => $value) {
if (is_array($value) && count($value)) {
list($insql, $inparams) = $DB->get_in_or_equal($value, SQL_PARAMS_NAMED);
$conditions[] = "$field $insql";
$params = array_merge($params, $inparams);
} else {
$conditions[] = "$field = :$field";
$params = array_merge($params, [$field => $value]);
}
}
$records = $DB->get_records_select($this->favouritetable, implode(' AND ', $conditions), $params,
'', '*', $limitfrom, $limitnum);
return $this->get_list_of_favourites_from_records($records);
}
/**
* Find a specific favourite, based on the properties known to identify it.
*
* Used if we don't know its id.
*
* @param int $userid the id of the user to which the favourite belongs.
* @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 int $contextid the contextid of the item which was favourited.
* @return favourite the favourite.
* @throws \dml_exception if any database errors are encountered or if the record could not be found.
*/
public function find_favourite(int $userid, string $component, string $itemtype, int $itemid, int $contextid): favourite {
global $DB;
// Favourites model: We know that only one favourite can exist based on these properties.
$record = $DB->get_record($this->favouritetable, [
'userid' => $userid,
'component' => $component,
'itemtype' => $itemtype,
'itemid' => $itemid,
'contextid' => $contextid
], '*', MUST_EXIST);
return $this->get_favourite_from_record($record);
}
/**
* Check whether a favourite exists in this repository, based on its id.
*
* @param int $id the id to search for.
* @return bool true if the favourite exists, false otherwise.
* @throws \dml_exception if any database errors are encountered.
*/
public function exists(int $id): bool {
global $DB;
return $DB->record_exists($this->favouritetable, ['id' => $id]);
}
/**
* Check whether an item exists in this repository, based on the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return bool true if the favourite exists, false otherwise.
* @throws \dml_exception if any database errors are encountered.
*/
public function exists_by(array $criteria): bool {
global $DB;
return $DB->record_exists($this->favouritetable, $criteria);
}
/**
* Update a favourite.
*
* @param favourite $favourite the favourite to update.
* @return favourite the updated favourite.
* @throws \dml_exception if any database errors are encountered.
*/
public function update(favourite $favourite): favourite {
global $DB;
$time = time();
$favourite->timemodified = $time;
$DB->update_record($this->favouritetable, $favourite);
return $this->find($favourite->id);
}
/**
* Delete a favourite, by id.
*
* @param int $id the id of the favourite to delete.
* @throws \dml_exception if any database errors are encountered.
*/
public function delete(int $id) {
global $DB;
$DB->delete_records($this->favouritetable, ['id' => $id]);
}
/**
* Delete all favourites matching the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @throws \dml_exception if any database errors are encountered.
*/
public function delete_by(array $criteria) {
global $DB;
$DB->delete_records($this->favouritetable, $criteria);
}
/**
* Return the total number of favourites in this repository.
*
* @return int the total number of items.
* @throws \dml_exception if any database errors are encountered.
*/
public function count(): int {
global $DB;
return $DB->count_records($this->favouritetable);
}
/**
* Return the number of user favourites matching the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return int the number of favourites matching the criteria.
* @throws \dml_exception if any database errors are encountered.
*/
public function count_by(array $criteria): int {
global $DB;
return $DB->count_records($this->favouritetable, $criteria);
}
}
@@ -0,0 +1,141 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the favourite_repository interface.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites\local\repository;
use \core_favourites\local\entity\favourite;
defined('MOODLE_INTERNAL') || die();
/**
* The favourite_repository interface, defining the basic CRUD operations for favourite type items within core_favourites.
*/
interface favourite_repository_interface {
/**
* Add one item to this repository.
*
* @param favourite $item the item to add.
* @return favourite the item which was added.
*/
public function add(favourite $item): favourite;
/**
* Add all the items in the list to this repository.
*
* @param array $items the list of items to add.
* @return array the list of items added to this repository.
*/
public function add_all(array $items): array;
/**
* Find an item in this repository based on its id.
*
* @param int $id the id of the item.
* @return favourite the item.
*/
public function find(int $id): favourite;
/**
* Find all items in this repository.
*
* @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 list of all items in this repository.
*/
public function find_all(int $limitfrom = 0, int $limitnum = 0): array;
/**
* Find all items with attributes matching certain values.
*
* @param array $criteria the array of attribute/value pairs.
* @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 items matching the criteria.
*/
public function find_by(array $criteria, int $limitfrom = 0, int $limitnum = 0): array;
/**
* Check whether an item exists in this repository, based on its id.
*
* @param int $id the id to search for.
* @return bool true if the item could be found, false otherwise.
*/
public function exists(int $id): bool;
/**
* Check whether an item exists in this repository, based on the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return bool true if the favourite exists, false otherwise.
*/
public function exists_by(array $criteria): bool;
/**
* Return the total number of items in this repository.
*
* @return int the total number of items.
*/
public function count(): int;
/**
* Return the number of favourites matching the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return int the number of favourites matching the criteria.
*/
public function count_by(array $criteria): int;
/**
* Update an item within this repository.
*
* @param favourite $item the item to update.
* @return favourite the updated item.
*/
public function update(favourite $item): favourite;
/**
* Delete an item by id.
*
* @param int $id the id of the item to delete.
* @return void
*/
public function delete(int $id);
/**
* Delete all favourites matching the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return void.
*/
public function delete_by(array $criteria);
/**
* Find a single favourite, based on it's unique identifiers.
*
* @param int $userid the id of the user to which the favourite belongs.
* @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 int $contextid the contextid of the item which was favourited.
* @return favourite the favourite.
*/
public function find_favourite(int $userid, string $component, string $itemtype, int $itemid, int $contextid): favourite;
}
@@ -0,0 +1,77 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the component_favourite_service class, part of the service layer for the favourites subsystem.
*
* @package core_favourites
* @copyright 2019 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites\local\service;
use \core_favourites\local\repository\favourite_repository_interface;
defined('MOODLE_INTERNAL') || die();
/**
* Class service, providing an single API for interacting with the favourites subsystem, for all favourites of a specific component.
*
* This class provides operations which can be applied to favourites within a component, based on type and context identifiers.
*
* All object persistence is delegated to the favourite_repository_interface object.
*
* @copyright 2019 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class component_favourite_service {
/** @var favourite_repository_interface $repo the favourite repository object. */
protected $repo;
/** @var int $component the frankenstyle component name to which this favourites service is scoped. */
protected $component;
/**
* The component_favourite_service constructor.
*
* @param string $component The frankenstyle name of the component to which this service operations are scoped.
* @param \core_favourites\local\repository\favourite_repository_interface $repository a favourites repository.
* @throws \moodle_exception if the component name is invalid.
*/
public function __construct(string $component, favourite_repository_interface $repository) {
if (!in_array($component, \core_component::get_component_names())) {
throw new \moodle_exception("Invalid component name '$component'");
}
$this->repo = $repository;
$this->component = $component;
}
/**
* Delete a collection of favourites by type and item, and optionally for a given context.
*
* E.g. delete all favourites of type 'message_conversations' for the conversation '11' and in the CONTEXT_COURSE context.
*
* @param string $itemtype the type of the favourited items.
* @param int $itemid the id of the item to which the favourites relate
* @param \context $context the context of the items which were favourited.
*/
public function delete_favourites_by_type_and_item(string $itemtype, int $itemid, \context $context = null) {
$criteria = ['component' => $this->component, 'itemtype' => $itemtype, 'itemid' => $itemid] +
($context ? ['contextid' => $context->id] : []);
$this->repo->delete_by($criteria);
}
}
@@ -0,0 +1,281 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the user_favourite_service class, part of the service layer for the favourites subsystem.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites\local\service;
use \core_favourites\local\entity\favourite;
use \core_favourites\local\repository\favourite_repository_interface;
defined('MOODLE_INTERNAL') || die();
/**
* 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.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_favourite_service {
/** @var favourite_repository_interface $repo the favourite repository object. */
protected $repo;
/** @var int $userid the id of the user to which this favourites service is scoped. */
protected $userid;
/**
* 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.
*/
public function __construct(\context_user $usercontext, favourite_repository_interface $repository) {
$this->repo = $repository;
$this->userid = $usercontext->instanceid;
}
/**
* 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.
* @throws \moodle_exception if the component name is invalid, or if the repository encounters any errors.
*/
public function create_favourite(string $component, string $itemtype, int $itemid, \context $context,
int $ordering = null): favourite {
// Access: Any component can ask to favourite something, we can't verify access to that 'something' here though.
// Validate the component name.
if (!in_array($component, \core_component::get_component_names())) {
throw new \moodle_exception("Invalid component name '$component'");
}
$favourite = new favourite($component, $itemtype, $itemid, $context->id, $this->userid);
$favourite->ordering = $ordering > 0 ? $ordering : null;
return $this->repo->add($favourite);
}
/**
* 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.
* @throws \moodle_exception if the component name is invalid, or if the repository encounters any errors.
*/
public function find_favourites_by_type(string $component, string $itemtype, int $limitfrom = 0, int $limitnum = 0): array {
if (!in_array($component, \core_component::get_component_names())) {
throw new \moodle_exception("Invalid component name '$component'");
}
return $this->repo->find_by(
[
'userid' => $this->userid,
'component' => $component,
'itemtype' => $itemtype
],
$limitfrom,
$limitnum
);
}
/**
* 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');
*
* @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.
* @throws \moodle_exception if the component name is invalid, or if the repository encounters any errors.
*/
public function find_all_favourites(string $component, array $itemtypes = [], int $limitfrom = 0, int $limitnum = 0): array {
if (!in_array($component, \core_component::get_component_names())) {
throw new \moodle_exception("Invalid component name '$component'");
}
$params = [
'userid' => $this->userid,
'component' => $component,
];
if ($itemtypes) {
$params['itemtype'] = $itemtypes;
}
return $this->repo->find_by(
$params,
$limitfrom,
$limitnum
);
}
/**
* 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].
*/
public function get_join_sql_by_type(string $component, string $itemtype, string $tablealias, string $joinitemid): array {
$sql = " LEFT JOIN {favourite} {$tablealias}
ON {$tablealias}.component = :favouritecomponent
AND {$tablealias}.itemtype = :favouriteitemtype
AND {$tablealias}.userid = {$this->userid}
AND {$tablealias}.itemid = {$joinitemid} ";
$params = [
'favouritecomponent' => $component,
'favouriteitemtype' => $itemtype,
];
return [$sql, $params];
}
/**
* 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.
* @throws \moodle_exception if the user does not control the favourite, or it doesn't exist.
*/
public function delete_favourite(string $component, string $itemtype, int $itemid, \context $context) {
if (!in_array($component, \core_component::get_component_names())) {
throw new \moodle_exception("Invalid component name '$component'");
}
// Business logic: check the user owns the favourite.
try {
$favourite = $this->repo->find_favourite($this->userid, $component, $itemtype, $itemid, $context->id);
} catch (\moodle_exception $e) {
throw new \moodle_exception("Favourite does not exist for the user. Cannot delete.");
}
$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
]
);
}
/**
* 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
*/
public function get_favourite(string $component, string $itemtype, int $itemid, \context $context) {
try {
return $this->repo->find_favourite(
$this->userid,
$component,
$itemtype,
$itemid,
$context->id
);
} catch (\dml_missing_record_exception $e) {
return null;
}
}
/**
* 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
*/
public function count_favourites_by_type(string $component, string $itemtype, \context $context = null) {
$criteria = [
'userid' => $this->userid,
'component' => $component,
'itemtype' => $itemtype
];
if ($context) {
$criteria['contextid'] = $context->id;
}
return $this->repo->count_by($criteria);
}
}
+270
View File
@@ -0,0 +1,270 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy class for requesting user data for the favourites subsystem.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\context;
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\transform;
/**
* Privacy class for requesting user data.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\subsystem\plugin_provider,
\core_privacy\local\request\shared_userlist_provider {
/**
* Returns metadata about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
return $collection->add_database_table('favourite', [
'userid' => 'privacy:metadata:favourite:userid',
'component' => 'privacy:metadata:favourite:component',
'itemtype' => 'privacy:metadata:favourite:itemtype',
'itemid' => 'privacy:metadata:favourite:itemid',
'ordering' => 'privacy:metadata:favourite:ordering',
'timecreated' => 'privacy:metadata:favourite:timecreated',
'timemodified' => 'privacy:metadata:favourite:timemodified',
], 'privacy:metadata:favourite');
}
/**
* Provide a list of contexts which have favourites for the user, in the respective area (component/itemtype combination).
*
* This method is to be called by consumers of the favourites subsystem (plugins), in their get_contexts_for_userid() method,
* to add the contexts for items which may have been favourited, but would normally not be reported as having user data by the
* plugin responsible for them.
*
* Consider an example: Favourite courses.
* Favourite courses will be handled by the core_course subsystem and courses can be favourited at site context.
*
* Now normally, the course provider method get_contexts_for_userid() would report the context of any courses the user is in.
* Then, we'd export data for those contexts. This won't include courses the user has favourited, but is not a member of.
*
* To report the full list, the course provider needs to be made aware of the contexts of any courses the user may have marked
* as favourites. Course will need to ask th favourites subsystem for this - a call to add_contexts_for_userid($userid).
*
* Once called, if a course has been marked as a favourite, at site context, then we'd return the site context. During export,
* the consumer (course), just looks at all contexts and decides whether to export favourite courses for each one.
*
* @param \core_privacy\local\request\contextlist $contextlist
* @param int $userid The id of the user in scope.
* @param string $component the frankenstyle component name.
* @param string $itemtype the type of the favourited items.
*/
public static function add_contexts_for_userid(\core_privacy\local\request\contextlist $contextlist, int $userid,
string $component, string $itemtype = null) {
$sql = "SELECT contextid
FROM {favourite} f
WHERE userid = :userid
AND component = :component";
$params = ['userid' => $userid, 'component' => $component];
if (!is_null($itemtype)) {
$sql .= " AND itemtype = :itemtype";
$params['itemtype'] = $itemtype;
}
$contextlist->add_from_sql($sql, $params);
}
/**
* Add users to a userlist who have favourites within the specified context.
*
* @param \core_privacy\local\request\userlist $userlist The userlist to add the users to.
* @param string $itemtype the type of the favourited items.
* @return void
*/
public static function add_userids_for_context(\core_privacy\local\request\userlist $userlist,
string $itemtype = null) {
if (empty($userlist)) {
return;
}
$params = [
'contextid' => $userlist->get_context()->id,
'component' => $userlist->get_component()
];
$sql = "SELECT userid
FROM {favourite}
WHERE contextid = :contextid
AND component = :component";
if (!is_null($itemtype)) {
$sql .= " AND itemtype = :itemtype";
$params['itemtype'] = $itemtype;
}
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Get favourites data for the specified user in the specified component, item type and item ID.
*
* @param int $userid The id of the user in scope.
* @param \context $context The context to which data is scoped.
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's item type.
* @param int $itemid The favourite's item ID.
* @return array|null
*/
public static function get_favourites_info_for_user(int $userid, \context $context,
string $component, string $itemtype, int $itemid) {
global $DB;
$params = [
'userid' => $userid,
'component' => $component,
'itemtype' => $itemtype,
'itemid' => $itemid,
'contextid' => $context->id
];
if (!$favourited = $DB->get_record('favourite', $params)) {
return;
}
return [
'starred' => transform::yesno(true),
'ordering' => $favourited->ordering,
'timecreated' => transform::datetime($favourited->timecreated),
'timemodified' => transform::datetime($favourited->timemodified)
];
}
/**
* Delete all favourites for all users in the specified contexts, and component area.
*
* @param \context $context The context to which deletion is scoped.
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_all_users(\context $context, string $component, string $itemtype,
int $itemid = 0) {
global $DB;
$params = [
'component' => $component,
'itemtype' => $itemtype,
'contextid' => $context->id
];
$select = "component = :component AND itemtype =:itemtype AND contextid = :contextid";
if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}
$DB->delete_records_select('favourite', $select, $params);
}
/**
* Delete all favourites for the specified users in the specified context, component area and item type.
*
* @param \core_privacy\local\request\approved_userlist $userlist The approved contexts and user information
* to delete information for.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_userlist(\core_privacy\local\request\approved_userlist $userlist,
string $itemtype, int $itemid = 0) {
global $DB;
$userids = $userlist->get_userids();
if (empty($userids)) {
return;
}
$context = $userlist->get_context();
list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$params = [
'component' => $userlist->get_component(),
'itemtype' => $itemtype,
'contextid' => $context->id
];
$params += $userparams;
$select = "component = :component AND itemtype = :itemtype AND contextid = :contextid AND userid $usersql";
if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}
$DB->delete_records_select('favourite', $select, $params);
}
/**
* Delete all favourites for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \coding_exception
* @throws \dml_exception
*/
public static function delete_favourites_for_user(approved_contextlist $contextlist, string $component, string $itemtype,
int $itemid = 0) {
global $DB;
$userid = $contextlist->get_user()->id;
list($insql, $inparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
$params = [
'userid' => $userid,
'component' => $component,
'itemtype' => $itemtype,
];
$params += $inparams;
$select = "userid = :userid AND component = :component AND itemtype =:itemtype AND contextid $insql";
if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}
$DB->delete_records_select('favourite', $select, $params);
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the service_factory, a locator for services for the favourites subsystem.
*
* Services encapsulate the business logic, and any data manipulation code, and are what clients should interact with.
*
* @package core_favourites
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_favourites;
defined('MOODLE_INTERNAL') || die();
/**
* Class service_factory, providing functions for location of service objects for the favourites subsystem.
*
* This class is responsible for providing service objects to clients only.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_factory {
/**
* Returns a basic service object providing operations for user favourites.
*
* @param \context_user $context the context of the user to which the service should be scoped.
* @return \core_favourites\local\service\user_favourite_service the service object.
*/
public static function get_service_for_user_context(\context_user $context): local\service\user_favourite_service {
return new local\service\user_favourite_service($context, new local\repository\favourite_repository());
}
/**
* Returns a basic service object providing operations for favourites belonging to a given component.
*
* @param string $component frankenstyle component name.
* @return local\service\component_favourite_service the service object.
*/
public static function get_service_for_component(string $component): local\service\component_favourite_service {
return new local\service\component_favourite_service($component, new local\repository\favourite_repository());
}
}