MDL-62491 mod_assign: Fill in the rewrite plugin urls properly.
[moodle.git] / mod / assign / submission / onlinetext / classes / privacy / provider.php
CommitLineData
58435433
AG
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\metadata\provider as metadataprovider;
33use \core_privacy\local\request\writer;
34use \core_privacy\local\request\contextlist;
35use \mod_assign\privacy\assign_plugin_request_data;
36
37/**
38 * Privacy class for requesting user data.
39 *
40 * @package assignsubmission_onlinetext
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 metadataprovider, \mod_assign\privacy\assignsubmission_provider {
45
46 /**
47 * Return meta data about this plugin.
48 *
49 * @param collection $collection A list of information to add to.
50 * @return collection Return the collection after adding to it.
51 */
52 public static function get_metadata(collection $collection) : collection {
53 $detail = [
54 'assignment' => 'privacy:metadata:assignmentid',
55 'submission' => 'privacy:metadata:submissionpurpose',
56 'onlinetext' => 'privacy:metadata:textpurpose'
57 ];
58 $collection->add_database_table('assignsubmission_onlinetext', $detail, 'privacy:metadata:tablepurpose');
59 $collection->link_subsystem('core_files', 'privacy:metadata:filepurpose');
60 return $collection;
61 }
62
63 /**
64 * This is covered by mod_assign provider and the query on assign_submissions.
65 *
66 * @param int $userid The user ID that we are finding contexts for.
67 * @param contextlist $contextlist A context list to add sql and params to for contexts.
68 */
69 public static function get_context_for_userid_within_submission(int $userid, contextlist $contextlist) {
70 // This is already fetched from mod_assign.
71 }
72
73 /**
74 * This is also covered by the mod_assign provider and it's queries.
75 *
76 * @param \mod_assign\privacy\useridlist $useridlist An object for obtaining user IDs of students.
77 */
78 public static function get_student_user_ids(\mod_assign\privacy\useridlist $useridlist) {
79 // No need.
80 }
81
82 /**
83 * Export all user data for this plugin.
84 *
85 * @param assign_plugin_request_data $exportdata Data used to determine which context and user to export and other useful
86 * information to help with exporting.
87 */
88 public static function export_submission_user_data(assign_plugin_request_data $exportdata) {
89 // We currently don't show submissions to teachers when exporting their data.
90 if ($exportdata->get_user() != null) {
91 return null;
92 }
93 // Retrieve text for this submission.
94 $assign = $exportdata->get_assign();
95 $plugin = $assign->get_plugin_by_type('assignsubmission', 'onlinetext');
96 $submission = $exportdata->get_pluginobject();
97 $editortext = $plugin->get_editor_text('onlinetext', $submission->id);
98 $context = $exportdata->get_context();
99 if (!empty($editortext)) {
100 $submissiontext = new \stdClass();
58435433
AG
101 $currentpath = $exportdata->get_subcontext();
102 $currentpath[] = get_string('privacy:path', 'assignsubmission_onlinetext');
812c4092
AG
103 $submissiontext->text = writer::with_context($context)->rewrite_pluginfile_urls($currentpath,
104 'assignsubmission_onlinetext', 'submissions_onlinetext', $submission->id, $editortext);
58435433
AG
105 writer::with_context($context)
106 ->export_area_files($currentpath, 'assignsubmission_onlinetext', 'submissions_onlinetext', $submission->id)
107 // Add the text to the exporter.
108 ->export_data($currentpath, $submissiontext);
109
110 // Handle plagiarism data.
111 $coursecontext = $context->get_course_context();
112 $userid = $submission->userid;
113 \core_plagiarism\privacy\provider::export_plagiarism_user_data($userid, $context, $currentpath, [
114 'cmid' => $context->instanceid,
115 'course' => $coursecontext->instanceid,
116 'userid' => $userid,
117 'content' => $editortext,
118 'assignment' => $submission->assignment
119 ]);
120 }
121 }
122
123 /**
124 * Any call to this method should delete all user data for the context defined in the deletion_criteria.
125 *
126 * @param assign_plugin_request_data $requestdata Data useful for deleting user data from this sub-plugin.
127 */
128 public static function delete_submission_for_context(assign_plugin_request_data $requestdata) {
129 global $DB;
130
131 \core_plagiarism\privacy\provider::delete_plagiarism_for_context($requestdata->get_context());
132
133 // Delete related files.
134 $fs = get_file_storage();
135 $fs->delete_area_files($requestdata->get_context()->id, 'assignsubmission_onlinetext',
136 ASSIGNSUBMISSION_ONLINETEXT_FILEAREA);
137
138 // Delete the records in the table.
139 $DB->delete_records('assignsubmission_onlinetext', ['assignment' => $requestdata->get_assign()->get_instance()->id]);
140 }
141
142 /**
143 * A call to this method should delete user data (where practicle) from the userid and context.
144 *
145 * @param assign_plugin_request_data $deletedata Details about the user and context to focus the deletion.
146 */
147 public static function delete_submission_for_userid(assign_plugin_request_data $deletedata) {
148 global $DB;
149
150 \core_plagiarism\privacy\provider::delete_plagiarism_for_user($deletedata->get_user()->id, $deletedata->get_context());
151
152 $submissionid = $deletedata->get_pluginobject()->id;
153
154 // Delete related files.
155 $fs = get_file_storage();
156 $fs->delete_area_files($deletedata->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
157 $submissionid);
158
159 // Delete the records in the table.
160 $DB->delete_records('assignsubmission_onlinetext', ['assignment' => $deletedata->get_assign()->get_instance()->id,
161 'submission' => $submissionid]);
162 }
163}