2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Privacy Subsystem implementation for mod_choice.
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace mod_choice\privacy
;
27 use core_privacy\local\metadata\collection
;
28 use core_privacy\local\request\approved_contextlist
;
29 use core_privacy\local\request\contextlist
;
30 use core_privacy\local\request\deletion_criteria
;
31 use core_privacy\local\request\helper
;
32 use core_privacy\local\request\writer
;
34 defined('MOODLE_INTERNAL') ||
die();
37 * Implementation of the privacy subsystem plugin provider for the choice activity module.
39 * @copyright 2018 Jun Pataleta
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class provider
implements
43 // This plugin stores personal data.
44 \core_privacy\local\metadata\provider
,
46 // This plugin is a core_user_data_provider.
47 \core_privacy\local\request\plugin\provider
{
49 * Return the fields which contain personal data.
51 * @param collection $items a reference to the collection to use to store the metadata.
52 * @return collection the updated collection of metadata items.
54 public static function get_metadata(collection
$items) : collection
{
55 $items->add_database_table(
58 'choiceid' => 'privacy:metadata:choice_answers:choiceid',
59 'optionid' => 'privacy:metadata:choice_answers:optionid',
60 'userid' => 'privacy:metadata:choice_answers:userid',
61 'timemodified' => 'privacy:metadata:choice_answers:timemodified',
63 'privacy:metadata:choice_answers'
70 * Get the list of contexts that contain user information for the specified user.
72 * @param int $userid the userid.
73 * @return contextlist the list of contexts containing user info for the user.
75 public static function get_contexts_for_userid(int $userid) : contextlist
{
76 // Fetch all choice answers.
79 INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
80 INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname
81 INNER JOIN {choice} ch ON ch.id = cm.instance
82 INNER JOIN {choice_options} co ON co.choiceid = ch.id
83 INNER JOIN {choice_answers} ca ON ca.optionid = co.id AND ca.choiceid = ch.id
84 WHERE ca.userid = :userid";
87 'modname' => 'choice',
88 'contextlevel' => CONTEXT_MODULE
,
91 $contextlist = new contextlist();
92 $contextlist->add_from_sql($sql, $params);
98 * Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
100 * @param approved_contextlist $contextlist a list of contexts approved for export.
102 public static function export_user_data(approved_contextlist
$contextlist) {
105 if (empty($contextlist->count())) {
109 $user = $contextlist->get_user();
111 list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED
);
113 $sql = "SELECT cm.id AS cmid,
117 INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
118 INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname
119 INNER JOIN {choice} ch ON ch.id = cm.instance
120 INNER JOIN {choice_options} co ON co.choiceid = ch.id
121 INNER JOIN {choice_answers} ca ON ca.optionid = co.id AND ca.choiceid = ch.id
122 WHERE c.id {$contextsql}
123 AND ca.userid = :userid
126 $params = ['modname' => 'choice', 'contextlevel' => CONTEXT_MODULE
, 'userid' => $user->id
] +
$contextparams;
128 // Reference to the choice activity seen in the last iteration of the loop. By comparing this with the current record, and
129 // because we know the results are ordered, we know when we've moved to the answers for a new choice activity and therefore
130 // when we can export the complete data for the last activity.
133 $choiceanswers = $DB->get_recordset_sql($sql, $params);
134 foreach ($choiceanswers as $choiceanswer) {
135 // If we've moved to a new choice, then write the last choice data and reinit the choice data array.
136 if ($lastcmid != $choiceanswer->cmid
) {
137 if (!empty($choicedata)) {
138 $context = \context_module
::instance($lastcmid);
139 self
::export_choice_data_for_user($choicedata, $context, $user);
143 'timemodified' => \core_privacy\local\request\transform
::datetime($choiceanswer->timemodified
),
146 $choicedata['answer'][] = $choiceanswer->answer
;
147 $lastcmid = $choiceanswer->cmid
;
149 $choiceanswers->close();
151 // The data for the last activity won't have been written yet, so make sure to write it now!
152 if (!empty($choicedata)) {
153 $context = \context_module
::instance($lastcmid);
154 self
::export_choice_data_for_user($choicedata, $context, $user);
159 * Export the supplied personal data for a single choice activity, along with any generic data or area files.
161 * @param array $choicedata the personal data to export for the choice.
162 * @param \context_module $context the context of the choice.
163 * @param \stdClass $user the user record
165 protected static function export_choice_data_for_user(array $choicedata, \context_module
$context, \stdClass
$user) {
166 // Fetch the generic module data for the choice.
167 $contextdata = helper
::get_context_data($context, $user);
169 // Merge with choice data and write it.
170 $contextdata = (object)array_merge((array)$contextdata, $choicedata);
171 writer
::with_context($context)->export_data([], $contextdata);
173 // Write generic module intro files.
174 helper
::export_context_files($context, $user);
178 * Delete all data for all users in the specified context.
180 * @param \context $context the context to delete in.
182 public static function delete_data_for_all_users_in_context(\context
$context) {
185 if (!$context instanceof \context_module
) {
189 if ($cm = get_coursemodule_from_id('choice', $context->instanceid
)) {
190 $DB->delete_records('choice_answers', ['choiceid' => $cm->instance
]);
195 * Delete all user data for the specified user, in the specified contexts.
197 * @param approved_contextlist $contextlist a list of contexts approved for deletion.
199 public static function delete_data_for_user(approved_contextlist
$contextlist) {
202 if (empty($contextlist->count())) {
206 $userid = $contextlist->get_user()->id
;
207 foreach ($contextlist->get_contexts() as $context) {
209 if (!$context instanceof \context_module
) {
212 $instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid
], MUST_EXIST
);
213 $DB->delete_records('choice_answers', ['choiceid' => $instanceid, 'userid' => $userid]);