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 class for requesting user data.
19 *
20 * @package    assignfeedback_comments
21 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace assignfeedback_comments\privacy;
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/mod/assign/locallib.php');
30
31use \core_privacy\local\metadata\collection;
32use \core_privacy\local\request\writer;
33use \core_privacy\local\request\contextlist;
34use \mod_assign\privacy\assign_plugin_request_data;
35use \mod_assign\privacy\useridlist;
36
37/**
38 * Privacy class for requesting user data.
39 *
40 * @package    assignfeedback_comments
41 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
42 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class provider implements
45        \core_privacy\local\metadata\provider,
46        \mod_assign\privacy\assignfeedback_provider,
47        \mod_assign\privacy\assignfeedback_user_provider {
48
49    /**
50     * Return meta data about this plugin.
51     *
52     * @param  collection $collection A list of information to add to.
53     * @return collection Return the collection after adding to it.
54     */
55    public static function get_metadata(collection $collection) : collection {
56        $data = [
57            'assignment' => 'privacy:metadata:assignmentid',
58            'grade' => 'privacy:metadata:gradepurpose',
59            'commenttext' => 'privacy:metadata:commentpurpose'
60        ];
61        $collection->add_database_table('assignfeedback_comments', $data, 'privacy:metadata:tablesummary');
62        $collection->link_subsystem('core_files', 'privacy:metadata:filepurpose');
63
64        return $collection;
65    }
66
67    /**
68     * No need to fill in this method as all information can be acquired from the assign_grades table in the mod assign
69     * provider.
70     *
71     * @param  int $userid The user ID.
72     * @param  contextlist $contextlist The context list.
73     */
74    public static function get_context_for_userid_within_feedback(int $userid, contextlist $contextlist) {
75        // This uses the assign_grades table.
76    }
77
78    /**
79     * This also does not need to be filled in as this is already collected in the mod assign provider.
80     *
81     * @param  useridlist $useridlist A list of user IDs
82     */
83    public static function get_student_user_ids(useridlist $useridlist) {
84        // Not required.
85    }
86
87    /**
88     * If you have tables that contain userids and you can generate entries in your tables without creating an
89     * entry in the assign_grades table then please fill in this method.
90     *
91     * @param  \core_privacy\local\request\userlist $userlist The userlist object
92     */
93    public static function get_userids_from_context(\core_privacy\local\request\userlist $userlist) {
94        // Not required.
95    }
96
97    /**
98     * Export all user data for this plugin.
99     *
100     * @param  assign_plugin_request_data $exportdata Data used to determine which context and user to export and other useful
101     * information to help with exporting.
102     */
103    public static function export_feedback_user_data(assign_plugin_request_data $exportdata) {
104        // Get that comment information and jam it into that exporter.
105        $assign = $exportdata->get_assign();
106        $plugin = $assign->get_plugin_by_type('assignfeedback', 'comments');
107        $gradeid = $exportdata->get_pluginobject()->id;
108        $comments = $plugin->get_feedback_comments($gradeid);
109        if ($comments && !empty($comments->commenttext)) {
110            $currentpath = array_merge(
111                $exportdata->get_subcontext(),
112                [get_string('privacy:commentpath', 'assignfeedback_comments')]
113            );
114
115            $comments->commenttext = writer::with_context($assign->get_context())->rewrite_pluginfile_urls(
116                $currentpath,
117                ASSIGNFEEDBACK_COMMENTS_COMPONENT,
118                ASSIGNFEEDBACK_COMMENTS_FILEAREA,
119                $gradeid,
120                $comments->commenttext
121            );
122            $data = (object)
123            [
124                'commenttext' => format_text($comments->commenttext, $comments->commentformat,
125                    ['context' => $exportdata->get_context()])
126            ];
127            writer::with_context($exportdata->get_context())->export_data($currentpath, $data);
128            writer::with_context($exportdata->get_context())->export_area_files($currentpath,
129                ASSIGNFEEDBACK_COMMENTS_COMPONENT, ASSIGNFEEDBACK_COMMENTS_FILEAREA, $gradeid);
130        }
131    }
132
133    /**
134     * Any call to this method should delete all user data for the context defined in the deletion_criteria.
135     *
136     * @param  assign_plugin_request_data $requestdata Data useful for deleting user data from this sub-plugin.
137     */
138    public static function delete_feedback_for_context(assign_plugin_request_data $requestdata) {
139        $assign = $requestdata->get_assign();
140        $fs = get_file_storage();
141        $fs->delete_area_files($requestdata->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
142            ASSIGNFEEDBACK_COMMENTS_FILEAREA);
143
144        $plugin = $assign->get_plugin_by_type('assignfeedback', 'comments');
145        $plugin->delete_instance();
146    }
147
148    /**
149     * Calling this function should delete all user data associated with this grade entry.
150     *
151     * @param  assign_plugin_request_data $requestdata Data useful for deleting user data.
152     */
153    public static function delete_feedback_for_grade(assign_plugin_request_data $requestdata) {
154        global $DB;
155
156        $fs = new \file_storage();
157        $fs->delete_area_files($requestdata->get_context()->id, ASSIGNFEEDBACK_COMMENTS_COMPONENT,
158            ASSIGNFEEDBACK_COMMENTS_FILEAREA, $requestdata->get_pluginobject()->id);
159
160        $DB->delete_records('assignfeedback_comments', ['assignment' => $requestdata->get_assignid(),
161                'grade' => $requestdata->get_pluginobject()->id]);
162    }
163
164    /**
165     * Deletes all feedback for the grade ids / userids provided in a context.
166     * assign_plugin_request_data contains:
167     * - context
168     * - assign object
169     * - grade ids (pluginids)
170     * - user ids
171     * @param  assign_plugin_request_data $deletedata A class that contains the relevant information required for deletion.
172     */
173    public static function delete_feedback_for_grades(assign_plugin_request_data $deletedata) {
174        global $DB;
175        if (empty($deletedata->get_gradeids())) {
176            return;
177        }
178
179        list($sql, $params) = $DB->get_in_or_equal($deletedata->get_gradeids(), SQL_PARAMS_NAMED);
180
181        $fs = new \file_storage();
182        $fs->delete_area_files_select(
183                $deletedata->get_context()->id,
184                ASSIGNFEEDBACK_COMMENTS_COMPONENT,
185                ASSIGNFEEDBACK_COMMENTS_FILEAREA,
186                $sql,
187                $params
188            );
189
190        $params['assignment'] = $deletedata->get_assignid();
191        $DB->delete_records_select('assignfeedback_comments', "assignment = :assignment AND grade $sql", $params);
192    }
193}
194