Merge branch 'MDL-62277-master' of git://github.com/bmbrands/moodle
[moodle.git] / mod / assign / classes / privacy / provider.php
CommitLineData
5c41cd77
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 mod_assign
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 mod_assign\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\contextlist;
34use \core_privacy\local\request\plugin\provider as pluginprovider;
35use \core_privacy\local\request\user_preference_provider as preference_provider;
36use \core_privacy\local\request\writer;
37use \core_privacy\local\request\approved_contextlist;
38use \core_privacy\local\request\transform;
39use \core_privacy\local\request\helper;
40use \core_privacy\manager;
41
42/**
43 * Privacy class for requesting user data.
44 *
45 * @package mod_assign
46 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
49class provider implements metadataprovider, pluginprovider, preference_provider {
50
51 /** Interface for all assign submission sub-plugins. */
52 const ASSIGNSUBMISSION_INTERFACE = 'mod_assign\privacy\assignsubmission_provider';
53
54 /** Interface for all assign feedback sub-plugins. */
55 const ASSIGNFEEDBACK_INTERFACE = 'mod_assign\privacy\assignfeedback_provider';
56
57 /**
58 * Provides meta data that is stored about a user with mod_assign
59 *
60 * @param collection $collection A collection of meta data items to be added to.
61 * @return collection Returns the collection of metadata.
62 */
63 public static function get_metadata(collection $collection) : collection {
64 $assigngrades = [
65 'userid' => 'privacy:metadata:userid',
66 'timecreated' => 'privacy:metadata:timecreated',
67 'timemodified' => 'timemodified',
68 'grader' => 'privacy:metadata:grader',
69 'grade' => 'privacy:metadata:grade',
70 'attemptnumber' => 'attemptnumber'
71 ];
72 $assignoverrides = [
73 'groupid' => 'privacy:metadata:groupid',
74 'userid' => 'privacy:metadata:userid',
75 'allowsubmissionsfromdate' => 'allowsubmissionsfromdate',
76 'duedate' => 'duedate',
77 'cutoffdate' => 'cutoffdate'
78 ];
79 $assignsubmission = [
80 'userid' => 'privacy:metadata:userid',
81 'timecreated' => 'privacy:metadata:timecreated',
82 'timemodified' => 'timemodified',
83 'status' => 'gradingstatus',
84 'groupid' => 'privacy:metadata:groupid',
85 'attemptnumber' => 'attemptnumber',
86 'latest' => 'privacy:metadata:latest'
87 ];
88 $assignuserflags = [
89 'userid' => 'privacy:metadata:userid',
90 'assignment' => 'privacy:metadata:assignmentid',
91 'locked' => 'locksubmissions',
92 'mailed' => 'privacy:metadata:mailed',
93 'extensionduedate' => 'extensionduedate',
94 'workflowstate' => 'markingworkflowstate',
95 'allocatedmarker' => 'allocatedmarker'
96 ];
97 $assignusermapping = [
98 'assignment' => 'privacy:metadata:assignmentid',
99 'userid' => 'privacy:metadata:userid'
100 ];
101 $collection->add_database_table('assign_grades', $assigngrades, 'privacy:metadata:assigngrades');
102 $collection->add_database_table('assign_overrides', $assignoverrides, 'privacy:metadata:assignoverrides');
103 $collection->add_database_table('assign_submission', $assignsubmission, 'privacy:metadata:assignsubmissiondetail');
104 $collection->add_database_table('assign_user_flags', $assignuserflags, 'privacy:metadata:assignuserflags');
105 $collection->add_database_table('assign_user_mapping', $assignusermapping, 'privacy:metadata:assignusermapping');
106 $collection->add_user_preference('assign_perpage', 'privacy:metadata:assignperpage');
107 $collection->add_user_preference('assign_filter', 'privacy:metadata:assignfilter');
108 $collection->add_user_preference('assign_markerfilter', 'privacy:metadata:assignmarkerfilter');
109 $collection->add_user_preference('assign_workflowfilter', 'privacy:metadata:assignworkflowfilter');
110 $collection->add_user_preference('assign_quickgrading', 'privacy:metadata:assignquickgrading');
111 $collection->add_user_preference('assign_downloadasfolders', 'privacy:metadata:assigndownloadasfolders');
112
113 // Link to subplugins.
114 $collection->add_plugintype_link('assignsubmission', [],'privacy:metadata:assignsubmissionpluginsummary');
115 $collection->add_plugintype_link('assignfeedback', [], 'privacy:metadata:assignfeedbackpluginsummary');
116 $collection->add_subsystem_link('core_message', [], 'privacy:metadata:assignmessageexplanation');
117
118 return $collection;
119 }
120
121 /**
122 * Returns all of the contexts that has information relating to the userid.
123 *
124 * @param int $userid The user ID.
125 * @return contextlist an object with the contexts related to a userid.
126 */
127 public static function get_contexts_for_userid(int $userid) : contextlist {
128 $params = ['modulename' => 'assign',
129 'contextlevel' => CONTEXT_MODULE,
130 'userid' => $userid,
131 'graderid' => $userid,
132 'aouserid' => $userid,
133 'asnuserid' => $userid,
134 'aufuserid' => $userid,
135 'aumuserid' => $userid];
136
137 $sql = "SELECT ctx.id
138 FROM {course_modules} cm
139 JOIN {modules} m ON cm.module = m.id AND m.name = :modulename
140 JOIN {assign} a ON cm.instance = a.id
141 JOIN {context} ctx ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
142 LEFT JOIN {assign_grades} ag ON a.id = ag.assignment
143 LEFT JOIN {assign_overrides} ao ON a.id = ao.assignid
144 LEFT JOIN {assign_submission} asn ON a.id = asn.assignment
145 LEFT JOIN {assign_user_flags} auf ON a.id = auf.assignment
146 LEFT JOIN {assign_user_mapping} aum ON a.id = aum.assignment
147 WHERE ag.userid = :userid OR ag.grader = :graderid OR ao.userid = :aouserid
148 OR asn.userid = :asnuserid OR auf.userid = :aufuserid OR aum.userid = :aumuserid";
149 $contextlist = new contextlist();
150 $contextlist->add_from_sql($sql, $params);
151 manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
152 'get_context_for_userid_within_feedback', [$userid, $contextlist]);
153 manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
154 'get_context_for_userid_within_submission', [$userid, $contextlist]);
155
156 return $contextlist;
157 }
158
159 /**
160 * Write out the user data filtered by contexts.
161 *
162 * @param approved_contextlist $contextlist contexts that we are writing data out from.
163 */
164 public static function export_user_data(approved_contextlist $contextlist) {
165 foreach ($contextlist->get_contexts() as $context) {
166 // Check that the context is a module context.
167 if ($context->contextlevel != CONTEXT_MODULE) {
168 continue;
169 }
170 $user = $contextlist->get_user();
171 $assigndata = helper::get_context_data($context, $user);
172 helper::export_context_files($context, $user);
173
174 writer::with_context($context)->export_data([], $assigndata);
175 $assign = new \assign($context, null, null);
176
177 // I need to find out if I'm a student or a teacher.
178 if ($userids = self::get_graded_users($user->id, $assign)) {
179 // Return teacher info.
180 $currentpath = [get_string('privacy:studentpath', 'mod_assign')];
181 foreach ($userids as $studentuserid) {
182 $studentpath = array_merge($currentpath, [$studentuserid->id]);
183 static::export_submission($assign, $studentuserid, $context, $studentpath, true);
184 }
185 }
186
187 static::export_overrides($context, $assign, $user);
188 static::export_submission($assign, $user, $context, []);
189 // Meta data.
190 self::store_assign_user_flags($context, $assign, $user->id);
191 if ($assign->is_blind_marking()) {
192 $uniqueid = $assign->get_uniqueid_for_user_static($assign->get_instance()->id, $contextlist->get_user()->id);
193 if ($uniqueid) {
194 writer::with_context($context)
195 ->export_metadata([get_string('blindmarking', 'mod_assign')], 'blindmarkingid', $uniqueid,
196 get_string('privacy:blindmarkingidentifier', 'mod_assign'));
197 }
198 }
199 }
200 }
201
202 /**
203 * Delete all use data which matches the specified context.
204 *
205 * @param context $context The module context.
206 */
207 public static function delete_data_for_all_users_in_context(\context $context) {
208 global $DB;
209
210 if ($context->contextlevel == CONTEXT_MODULE) {
211 // Apparently we can't trust anything that comes via the context.
212 // Go go mega query to find out it we have an assign context that matches an existing assignment.
213 $sql = "SELECT a.id
214 FROM {assign} a
215 JOIN {course_modules} cm ON a.id = cm.instance
216 JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
217 JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :contextmodule
218 WHERE ctx.id = :contextid";
219 $params = ['modulename' => 'assign', 'contextmodule' => CONTEXT_MODULE, 'contextid' => $context->id];
220 $count = $DB->get_field_sql($sql, $params);
221 // If we have a count over zero then we can proceed.
222 if ($count > 0) {
223 // Get the assignment related to this context.
224 $assign = new \assign($context, null, null);
225 // What to do first... Get sub plugins to delete their stuff.
226 $requestdata = new assign_plugin_request_data($context, $assign);
227 manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
228 'delete_submission_for_context', [$requestdata]);
229 $requestdata = new assign_plugin_request_data($context, $assign);
230 manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
231 'delete_feedback_for_context', [$requestdata]);
232 $DB->delete_records('assign_grades', ['assignment' => $assign->get_instance()->id]);
233
234 // Time to roll my own method for deleting overrides.
235 static::delete_user_overrides($assign);
236 $DB->delete_records('assign_submission', ['assignment' => $assign->get_instance()->id]);
237 $DB->delete_records('assign_user_flags', ['assignment' => $assign->get_instance()->id]);
238 $DB->delete_records('assign_user_mapping', ['assignment' => $assign->get_instance()->id]);
239 }
240 }
241 }
242
243 /**
244 * Delete all user data for the specified user, in the specified contexts.
245 *
246 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
247 */
248 public static function delete_data_for_user(approved_contextlist $contextlist) {
249 global $DB;
250
251 $user = $contextlist->get_user();
252
253 foreach ($contextlist as $context) {
254 if ($context->contextlevel != CONTEXT_MODULE) {
3d110a5f 255 continue;
5c41cd77
AG
256 }
257 // Get the assign object.
258 $assign = new \assign($context, null, null);
259 $assignid = $assign->get_instance()->id;
260
261 $submissions = $DB->get_records('assign_submission', ['assignment' => $assignid, 'userid' => $user->id]);
262 foreach ($submissions as $submission) {
263 $requestdata = new assign_plugin_request_data($context, $assign, $submission, [], $user);
264 manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
265 'delete_submission_for_userid', [$requestdata]);
266 }
267
268 $grades = $DB->get_records('assign_grades', ['assignment' => $assignid, 'userid' => $user->id]);
269 foreach ($grades as $grade) {
270 $requestdata = new assign_plugin_request_data($context, $assign, $grade, [], $user);
271 manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE,
272 'delete_feedback_for_grade', [$requestdata]);
273 }
274
275 static::delete_user_overrides($assign, $user);
276 $DB->delete_records('assign_user_flags', ['assignment' => $assignid, 'userid' => $user->id]);
277 $DB->delete_records('assign_user_mapping', ['assignment' => $assignid, 'userid' => $user->id]);
278 $DB->delete_records('assign_grades', ['assignment' => $assignid, 'userid' => $user->id]);
279 $DB->delete_records('assign_submission', ['assignment' => $assignid, 'userid' => $user->id]);
280 }
281 }
282
283 /**
284 * Deletes assignment overrides.
285 *
286 * @param \assign $assign The assignment object
287 * @param \stdClass $user The user object if we are deleting only the overrides for one user.
288 */
289 protected static function delete_user_overrides(\assign $assign, \stdClass $user = null) {
290 global $DB;
291
292 $assignid = $assign->get_instance()->id;
293 $params = (isset($user)) ? ['assignid' => $assignid, 'userid' => $user->id] : ['assignid' => $assignid];
294
295 $overrides = $DB->get_records('assign_overrides', $params);
296 if (!empty($overrides)) {
297 foreach ($overrides as $override) {
298
299 // First delete calendar events associated with this override.
300 $conditions = ['modulename' => 'assign', 'instance' => $assignid];
301 if (isset($user)) {
302 $conditions['userid'] = $user->id;
303 }
304 $DB->delete_records('event', $conditions);
305
306 // Next delete the overrides.
307 $DB->delete_records('assign_overrides', ['id' => $override->id]);
308 }
309 }
310 }
311
312 /**
313 * Find out if this user has graded any users.
314 *
315 * @param int $userid The user ID (potential teacher).
316 * @param assign $assign The assignment object.
317 * @return array If successful an array of objects with userids that this user graded, otherwise false.
318 */
319 protected static function get_graded_users(int $userid, \assign $assign) {
320 $params = ['grader' => $userid, 'assignid' => $assign->get_instance()->id];
321
322 $sql = "SELECT DISTINCT userid AS id
323 FROM {assign_grades}
324 WHERE grader = :grader AND assignment = :assignid";
325
326 $useridlist = new useridlist($userid, $assign->get_instance()->id);
327 $useridlist->add_from_sql($sql, $params);
328
329 // Call sub-plugins to see if they have information not already collected.
330 manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE, 'get_student_user_ids',
331 [$useridlist]);
332 manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE, 'get_student_user_ids', [$useridlist]);
333
334 $userids = $useridlist->get_userids();
335 return ($userids) ? $userids : false;
336 }
337
338 /**
339 * Writes out various user meta data about the assignment.
340 *
341 * @param \context $context The context of this assignment.
342 * @param \assign $assign The assignment object.
343 * @param int $userid The user ID
344 */
345 protected static function store_assign_user_flags(\context $context, \assign $assign, int $userid) {
346 $datatypes = ['locked' => get_string('locksubmissions', 'mod_assign'),
347 'mailed' => get_string('privacy:metadata:mailed', 'mod_assign'),
348 'extensionduedate' => get_string('extensionduedate', 'mod_assign'),
349 'workflowstate' => get_string('markingworkflowstate', 'mod_assign'),
350 'allocatedmarker' => get_string('allocatedmarker_help', 'mod_assign')];
351 $userflags = (array)$assign->get_user_flags($userid, false);
352
353 foreach ($datatypes as $key => $description) {
354 if (isset($userflags[$key]) && !empty($userflags[$key])) {
355 $value = $userflags[$key];
356 if ($key == 'locked' || $key == 'mailed') {
357 $value = transform::yesno($value);
358 } else if ($key == 'extensionduedate') {
359 $value = transform::datetime($value);
360 }
361 writer::with_context($context)->export_metadata([], $key, $value, $description);
362 }
363 }
364 }
365
366 /**
367 * Formats and then exports the user's grade data.
368 *
369 * @param \stdClass $grade The assign grade object
370 * @param \context $context The context object
371 * @param array $currentpath Current directory path that we are exporting to.
372 */
373 protected static function export_grade_data(\stdClass $grade, \context $context, array $currentpath) {
374 $gradedata = (object)[
375 'timecreated' => transform::datetime($grade->timecreated),
376 'timemodified' => transform::datetime($grade->timemodified),
377 'grader' => transform::user($grade->grader),
378 'grade' => $grade->grade,
0139b882 379 'attemptnumber' => ($grade->attemptnumber + 1)
5c41cd77
AG
380 ];
381 writer::with_context($context)
382 ->export_data(array_merge($currentpath, [get_string('privacy:gradepath', 'mod_assign')]), $gradedata);
383 }
384
385 /**
386 * Formats and then exports the user's submission data.
387 *
388 * @param \stdClass $submission The assign submission object
389 * @param \context $context The context object
390 * @param array $currentpath Current directory path that we are exporting to.
391 */
392 protected static function export_submission_data(\stdClass $submission, \context $context, array $currentpath) {
393 $submissiondata = (object)[
394 'timecreated' => transform::datetime($submission->timecreated),
395 'timemodified' => transform::datetime($submission->timemodified),
396 'status' => get_string('submissionstatus_' . $submission->status, 'mod_assign'),
397 'groupid' => $submission->groupid,
0139b882 398 'attemptnumber' => ($submission->attemptnumber + 1),
5c41cd77
AG
399 'latest' => transform::yesno($submission->latest)
400 ];
401 writer::with_context($context)
402 ->export_data(array_merge($currentpath, [get_string('privacy:submissionpath', 'mod_assign')]), $submissiondata);
403 }
404
405 /**
406 * Stores the user preferences related to mod_assign.
407 *
408 * @param int $userid The user ID that we want the preferences for.
409 */
410 public static function export_user_preferences(int $userid) {
411 $context = \context_system::instance();
412 $assignpreferences = [
413 'assign_perpage' => ['string' => get_string('privacy:metadata:assignperpage', 'mod_assign'), 'bool' => false],
414 'assign_filter' => ['string' => get_string('privacy:metadata:assignfilter', 'mod_assign'), 'bool' => false],
415 'assign_markerfilter' => ['string' => get_string('privacy:metadata:assignmarkerfilter', 'mod_assign'), 'bool' => true],
416 'assign_workflowfilter' => ['string' => get_string('privacy:metadata:assignworkflowfilter', 'mod_assign'),
417 'bool' => true],
418 'assign_quickgrading' => ['string' => get_string('privacy:metadata:assignquickgrading', 'mod_assign'), 'bool' => true],
419 'assign_downloadasfolders' => ['string' => get_string('privacy:metadata:assigndownloadasfolders', 'mod_assign'),
420 'bool' => true]
421 ];
422 foreach ($assignpreferences as $key => $preference) {
423 $value = get_user_preferences($key, null, $userid);
424 if ($preference['bool']) {
425 $value = transform::yesno($value);
426 }
427 if (isset($value)) {
428 writer::with_context($context)->export_user_preference('mod_assign', $key, $value, $preference['string']);
429 }
430 }
431 }
432
433 /**
434 * Export overrides for this assignment.
435 *
436 * @param \context $context Context
437 * @param \assign $assign The assign object.
438 * @param \stdClass $user The user object.
439 */
440 public static function export_overrides(\context $context, \assign $assign, \stdClass $user) {
441
442 $overrides = $assign->override_exists($user->id);
443 // Overrides returns an array with data in it, but an override with actual data will have the assign ID set.
444 if (isset($overrides->assignid)) {
445 $data = new \stdClass();
446 if (!empty($overrides->duedate)) {
447 $data->duedate = transform::datetime($overrides->duedate);
448 }
449 if (!empty($overrides->cutoffdate)) {
450 $overrides->cutoffdate = transform::datetime($overrides->cutoffdate);
451 }
452 if (!empty($overrides->allowsubmissionsfromdate)) {
453 $overrides->allowsubmissionsfromdate = transform::datetime($overrides->allowsubmissionsfromdate);
454 }
455 if (!empty($data)) {
456 writer::with_context($context)->export_data([get_string('overrides', 'mod_assign')], $data);
457 }
458 }
459 }
460
461 /**
462 * Exports assignment submission data for a user.
463 *
464 * @param \assign $assign The assignment object
465 * @param \stdClass $user The user object
466 * @param \context_module $context The context
467 * @param array $path The path for exporting data
468 * @param bool|boolean $exportforteacher A flag for if this is exporting data as a teacher.
469 */
470 protected static function export_submission(\assign $assign, \stdClass $user, \context_module $context, array $path,
471 bool $exportforteacher = false) {
472 $submissions = $assign->get_all_submissions($user->id);
473 $teacher = ($exportforteacher) ? $user : null;
474 foreach ($submissions as $submission) {
475 // Attempt numbers start at zero, which is fine for programming, but doesn't make as much sense
476 // for users.
477 $submissionpath = array_merge($path,
478 [get_string('privacy:attemptpath', 'mod_assign', ($submission->attemptnumber + 1))]);
479
480 $params = new assign_plugin_request_data($context, $assign, $submission, $submissionpath ,$teacher);
481 manager::plugintype_class_callback('assignsubmission', self::ASSIGNSUBMISSION_INTERFACE,
482 'export_submission_user_data', [$params]);
483 if (!isset($teacher)) {
484 self::export_submission_data($submission, $context, $submissionpath);
485 }
486 $grade = $assign->get_user_grade($user->id, false, $submission->attemptnumber);
487 if ($grade) {
488 $params = new assign_plugin_request_data($context, $assign, $grade, $submissionpath, $teacher);
489 manager::plugintype_class_callback('assignfeedback', self::ASSIGNFEEDBACK_INTERFACE, 'export_feedback_user_data',
490 [$params]);
491
492 self::export_grade_data($grade, $context, $submissionpath);
493 }
494 }
495 }
496}