MDL-62535 grading methods: Fix for multiple intances.
[moodle.git] / grade / grading / form / rubric / classes / privacy / provider.php
CommitLineData
6416e87f
SA
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 gradingform_rubric
21 * @copyright 2018 Sara Arjona <sara@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace gradingform_rubric\privacy;
26
27defined('MOODLE_INTERNAL') || die();
28
f3a6d9bb
AG
29use \core_privacy\local\metadata\collection;
30
6416e87f
SA
31/**
32 * Privacy class for requesting user data.
33 *
34 * @copyright 2018 Sara Arjona <sara@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class provider implements
f3a6d9bb
AG
38 \core_privacy\local\metadata\provider,
39 \core_grading\privacy\gradingform_provider_v2 {
40
41 /**
42 * Returns meta data about this system.
43 *
44 * @param collection $collection The initialised collection to add items to.
45 * @return collection A listing of user data stored through this system.
46 */
47 public static function get_metadata(collection $collection) : collection {
48 $collection->add_database_table('gradingform_rubric_fillings', [
49 'instanceid' => 'privacy:metadata:instanceid',
50 'criterionid' => 'privacy:metadata:criterionid',
51 'levelid' => 'privacy:metadata:levelid',
52 'remark' => 'privacy:metadata:remark'
53 ], 'privacy:metadata:fillingssummary');
54 return $collection;
55 }
56
57 /**
58 * Export user data relating to an instance ID.
59 *
60 * @param \context $context Context to use with the export writer.
61 * @param int $instanceid The instance ID to export data for.
62 * @param array $subcontext The directory to export this data to.
63 */
64 public static function export_gradingform_instance_data(\context $context, int $instanceid, array $subcontext) {
65 global $DB;
66 // Get records from the provided params.
67 $params = ['instanceid' => $instanceid];
68 $sql = "SELECT rc.description, rl.definition, rl.score, rf.remark
69 FROM {gradingform_rubric_fillings} rf
70 JOIN {gradingform_rubric_criteria} rc ON rc.id = rf.criterionid
71 JOIN {gradingform_rubric_levels} rl ON rf.levelid = rl.id
72 WHERE rf.instanceid = :instanceid";
73 $records = $DB->get_records_sql($sql, $params);
74 if ($records) {
fe7d6e31 75 $subcontext = array_merge($subcontext, [get_string('rubric', 'gradingform_rubric'), $instanceid]);
f3a6d9bb
AG
76 \core_privacy\local\request\writer::with_context($context)->export_data($subcontext, (object) $records);
77 }
78 }
6416e87f
SA
79
80 /**
f3a6d9bb 81 * Deletes all user data related to the provided instance IDs.
6416e87f 82 *
f3a6d9bb 83 * @param array $instanceids The instance IDs to delete information from.
6416e87f 84 */
f3a6d9bb
AG
85 public static function delete_gradingform_for_instances(array $instanceids) {
86 global $DB;
87 $DB->delete_records_list('gradingform_rubric_fillings', 'instanceid', $instanceids);
6416e87f
SA
88 }
89}