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    assignsubmission_onlinetext
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 assignsubmission_onlinetext\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;
35
36/**
37 * Privacy class for requesting user data.
38 *
39 * @package    assignsubmission_onlinetext
40 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
41 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class provider implements
44        \core_privacy\local\metadata\provider,
45        \mod_assign\privacy\assignsubmission_provider,
46        \mod_assign\privacy\assignsubmission_user_provider {
47
48    /**
49     * Return meta data about this plugin.
50     *
51     * @param  collection $collection A list of information to add to.
52     * @return collection Return the collection after adding to it.
53     */
54    public static function get_metadata(collection $collection) : collection {
55        $detail = [
56                    'assignment' => 'privacy:metadata:assignmentid',
57                    'submission' => 'privacy:metadata:submissionpurpose',
58                    'onlinetext' => 'privacy:metadata:textpurpose'
59                  ];
60        $collection->add_database_table('assignsubmission_onlinetext', $detail, 'privacy:metadata:tablepurpose');
61        $collection->link_subsystem('core_files', 'privacy:metadata:filepurpose');
62        return $collection;
63    }
64
65    /**
66     * This is covered by mod_assign provider and the query on assign_submissions.
67     *
68     * @param  int $userid The user ID that we are finding contexts for.
69     * @param  contextlist $contextlist A context list to add sql and params to for contexts.
70     */
71    public static function get_context_for_userid_within_submission(int $userid, contextlist $contextlist) {
72        // This is already fetched from mod_assign.
73    }
74
75    /**
76     * This is also covered by the mod_assign provider and it's queries.
77     *
78     * @param  \mod_assign\privacy\useridlist $useridlist An object for obtaining user IDs of students.
79     */
80    public static function get_student_user_ids(\mod_assign\privacy\useridlist $useridlist) {
81        // No need.
82    }
83
84    /**
85     * If you have tables that contain userids and you can generate entries in your tables without creating an
86     * entry in the assign_submission table then please fill in this method.
87     *
88     * @param  \core_privacy\local\request\userlist $userlist The userlist object
89     */
90    public static function get_userids_from_context(\core_privacy\local\request\userlist $userlist) {
91        // Not required.
92    }
93
94    /**
95     * Export all user data for this plugin.
96     *
97     * @param  assign_plugin_request_data $exportdata Data used to determine which context and user to export and other useful
98     * information to help with exporting.
99     */
100    public static function export_submission_user_data(assign_plugin_request_data $exportdata) {
101        // We currently don't show submissions to teachers when exporting their data.
102        if ($exportdata->get_user() != null) {
103            return null;
104        }
105        // Retrieve text for this submission.
106        $assign = $exportdata->get_assign();
107        $plugin = $assign->get_plugin_by_type('assignsubmission', 'onlinetext');
108        $submission = $exportdata->get_pluginobject();
109        $editortext = $plugin->get_editor_text('onlinetext', $submission->id);
110        $context = $exportdata->get_context();
111        if (!empty($editortext)) {
112            $submissiontext = new \stdClass();
113            $currentpath = $exportdata->get_subcontext();
114            $currentpath[] = get_string('privacy:path', 'assignsubmission_onlinetext');
115            $submissiontext->text = writer::with_context($context)->rewrite_pluginfile_urls($currentpath,
116                    'assignsubmission_onlinetext', 'submissions_onlinetext', $submission->id, $editortext);
117            writer::with_context($context)
118                    ->export_area_files($currentpath, 'assignsubmission_onlinetext', 'submissions_onlinetext', $submission->id)
119                    // Add the text to the exporter.
120                    ->export_data($currentpath, $submissiontext);
121
122            // Handle plagiarism data.
123            $coursecontext = $context->get_course_context();
124            $userid = $submission->userid;
125            \core_plagiarism\privacy\provider::export_plagiarism_user_data($userid, $context, $currentpath, [
126                'cmid' => $context->instanceid,
127                'course' => $coursecontext->instanceid,
128                'userid' => $userid,
129                'content' => $editortext,
130                'assignment' => $submission->assignment
131            ]);
132        }
133    }
134
135    /**
136     * Any call to this method should delete all user data for the context defined in the deletion_criteria.
137     *
138     * @param  assign_plugin_request_data $requestdata Data useful for deleting user data from this sub-plugin.
139     */
140    public static function delete_submission_for_context(assign_plugin_request_data $requestdata) {
141        global $DB;
142
143        \core_plagiarism\privacy\provider::delete_plagiarism_for_context($requestdata->get_context());
144
145        // Delete related files.
146        $fs = get_file_storage();
147        $fs->delete_area_files($requestdata->get_context()->id, 'assignsubmission_onlinetext',
148                ASSIGNSUBMISSION_ONLINETEXT_FILEAREA);
149
150        // Delete the records in the table.
151        $DB->delete_records('assignsubmission_onlinetext', ['assignment' => $requestdata->get_assignid()]);
152    }
153
154    /**
155     * A call to this method should delete user data (where practicle) from the userid and context.
156     *
157     * @param  assign_plugin_request_data $deletedata Details about the user and context to focus the deletion.
158     */
159    public static function delete_submission_for_userid(assign_plugin_request_data $deletedata) {
160        global $DB;
161
162        \core_plagiarism\privacy\provider::delete_plagiarism_for_user($deletedata->get_user()->id, $deletedata->get_context());
163
164        $submissionid = $deletedata->get_pluginobject()->id;
165
166        // Delete related files.
167        $fs = get_file_storage();
168        $fs->delete_area_files($deletedata->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
169                $submissionid);
170
171        // Delete the records in the table.
172        $DB->delete_records('assignsubmission_onlinetext', ['assignment' => $deletedata->get_assignid(),
173                'submission' => $submissionid]);
174    }
175
176    /**
177     * Deletes all submissions for the submission ids / userids provided in a context.
178     * assign_plugin_request_data contains:
179     * - context
180     * - assign object
181     * - submission ids (pluginids)
182     * - user ids
183     * @param  assign_plugin_request_data $deletedata A class that contains the relevant information required for deletion.
184     */
185    public static function delete_submissions(assign_plugin_request_data $deletedata) {
186        global $DB;
187
188        \core_plagiarism\privacy\provider::delete_plagiarism_for_users($deletedata->get_userids(), $deletedata->get_context());
189        if (empty($deletedata->get_submissionids())) {
190            return;
191        }
192
193        $fs = get_file_storage();
194        list($sql, $params) = $DB->get_in_or_equal($deletedata->get_submissionids(), SQL_PARAMS_NAMED);
195        $fs->delete_area_files_select($deletedata->get_context()->id,
196                'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sql, $params);
197
198        $params['assignid'] = $deletedata->get_assignid();
199        $DB->delete_records_select('assignsubmission_onlinetext', "assignment = :assignid AND submission $sql", $params);
200    }
201}
202