MDL-61307 core_rating: Add implementation of Privacy API
[moodle.git] / rating / classes / privacy / provider.php
CommitLineData
cbc63252
AN
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 * Privacy Subsystem implementation for core_ratings.
19 *
20 * @package core_rating
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_rating\privacy;
26
27use \core_privacy\local\metadata\collection;
28
29defined('MOODLE_INTERNAL') || die();
30
31require_once($CFG->dirroot . '/rating/lib.php');
32
33/**
34 * Privacy Subsystem implementation for core_ratings.
35 *
36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class provider implements
40 // The ratings subsystem contains data.
41 \core_privacy\local\metadata\provider,
42
43 // The ratings subsystem is only ever used to store data for other components.
44 // It does not store any data of its own and does not need to implement the \core_privacy\local\request\subsystem\provider
45 // as a result.
46
47 // The ratings subsystem provides a data service to other components.
48 \core_privacy\local\request\subsystem\plugin_provider {
49
50 /**
51 * Returns metadata about the ratings subsystem.
52 *
53 * @param collection $collection The initialised collection to add items to.
54 * @return collection A listing of user data stored through the subsystem.
55 */
56 public static function get_metadata(collection $collection) : collection {
57 // The table 'rating' cotains data that a user has entered.
58 // It stores the user-entered rating alongside a mapping to describe what was mapped.
59 $collection->add_database_table('rating', [
60 'rating' => 'privacy:metadata:rating:rating',
61 'userid' => 'privacy:metadata:rating:userid',
62 'timecreated' => 'privacy:metadata:rating:timecreated',
63 'timemodified' => 'privacy:metadata:rating:timemodified',
64 ], 'privacy:metadata:rating');
65
66 return $collection;
67 }
68
69 /**
70 * Export all ratings which match the specified component, areaid, and itemid.
71 *
72 * If requesting ratings for a users own content, and you wish to include all ratings of that content, specify
73 * $onlyuser as false.
74 *
75 * When requesting ratings for another users content, you should only export the ratings that the specified user
76 * made themselves.
77 *
78 * @param int $userid The user whose information is to be exported
79 * @param \context $context The context being stored.
80 * @param array $subcontext The subcontext within the context to export this information
81 * @param string $component The component to fetch data from
82 * @param string $ratingarea The ratingarea that the data was stored in within the component
83 * @param int $itemid The itemid within that ratingarea
84 * @param bool $onlyuser Whether to only export ratings that the current user has made, or all ratings
85 */
86 public static function export_area_ratings(
87 int $userid,
88 \context $context,
89 array $subcontext,
90 string $component,
91 string $ratingarea,
92 int $itemid,
93 bool $onlyuser = true
94 ) {
95 global $DB;
96
97 $rm = new \rating_manager();
98 $ratings = $rm->get_all_ratings_for_item((object) [
99 'context' => $context,
100 'component' => $component,
101 'ratingarea' => $ratingarea,
102 'itemid' => $itemid,
103 ]);
104
105 if ($onlyuser) {
106 $ratings = array_filter($ratings, function($rating) use ($userid){
107 return ($rating->userid == $userid);
108 });
109 }
110
111 if (empty($ratings)) {
112 return;
113 }
114
115 $toexport = array_map(function($rating) {
116 return (object) [
117 'rating' => $rating->rating,
118 'author' => $rating->userid,
119 ];
120 }, $ratings);
121
122 $writer = \core_privacy\local\request\writer::with_context($context)
123 ->export_related_data($subcontext, 'rating', $toexport);
124 }
125
126 /**
127 * Get the SQL required to find all submission items where this user has had any involvements.
128 *
129 * @param string $alias The name of the table alias to use.
130 * @param string $component The na eof the component to fetch ratings for.
131 * @param string $ratingarea The rating area to fetch results for.
132 * @param string $itemidjoin The right-hand-side of the JOIN ON clause.
133 * @param int $userid The ID of the user being stored.
134 * @return \stdClass
135 */
136 public static function get_sql_join($alias, $component, $ratingarea, $itemidjoin, $userid) {
137 static $count = 0;
138 $count++;
139
140 // Join the rating table with the specified alias and the relevant join params.
141 $join = "LEFT JOIN {rating} {$alias} ON ";
142 $join .= "{$alias}.component = :ratingcomponent{$count} AND ";
143 $join .= "{$alias}.ratingarea = :ratingarea{$count} AND ";
144 $join .= "{$alias}.itemid = {$itemidjoin}";
145
146 // Match against the specified user.
147 $userwhere = "{$alias}.userid = :ratinguserid{$count}";
148
149 $params = [
150 'ratingcomponent' . $count => $component,
151 'ratingarea' . $count => $ratingarea,
152 'ratinguserid' . $count => $userid,
153 ];
154
155 $return = (object) [
156 'join' => $join,
157 'params' => $params,
158 'userwhere' => $userwhere,
159 ];
160 return $return;
161 }
162}