MDL-62535 grading methods: Fix for multiple intances.
[moodle.git] / grade / grading / form / guide / 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_guide
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_guide\privacy;
26
27defined('MOODLE_INTERNAL') || die();
28
29use \core_privacy\local\metadata\collection;
30use \core_privacy\local\request\transform;
31use \core_privacy\local\request\writer;
32
33/**
34 * Privacy class for requesting user data.
35 *
36 * @copyright 2018 Sara Arjona <sara@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class provider implements
61496b76 40 \core_privacy\local\metadata\provider,
f3a6d9bb 41 \core_grading\privacy\gradingform_provider_v2,
6416e87f
SA
42 \core_privacy\local\request\user_preference_provider {
43
44 /**
45 * Return the fields which contain personal data.
46 *
47 * @param collection $collection The initialised collection to add items to.
48 * @return collection A listing of user data stored through this system.
49 */
50 public static function get_metadata(collection $collection) : collection {
f3a6d9bb
AG
51 $collection->add_database_table('gradingform_guide_fillings', [
52 'instanceid' => 'privacy:metadata:instanceid',
53 'criterionid' => 'privacy:metadata:criterionid',
54 'remark' => 'privacy:metadata:remark',
55 'score' => 'privacy:metadata:score'
56 ], 'privacy:metadata:fillingssummary');
6416e87f
SA
57 $collection->add_user_preference(
58 'gradingform_guide-showmarkerdesc',
59 'privacy:metadata:preference:showmarkerdesc'
60 );
61 $collection->add_user_preference(
62 'gradingform_guide-showstudentdesc',
63 'privacy:metadata:preference:showstudentdesc'
64 );
65
66 return $collection;
67 }
68
f3a6d9bb
AG
69 /**
70 * Export user data relating to an instance ID.
71 *
72 * @param \context $context Context to use with the export writer.
73 * @param int $instanceid The instance ID to export data for.
74 * @param array $subcontext The directory to export this data to.
75 */
76 public static function export_gradingform_instance_data(\context $context, int $instanceid, array $subcontext) {
77 global $DB;
78 // Get records from the provided params.
79 $params = ['instanceid' => $instanceid];
80 $sql = "SELECT gc.shortname, gc.description, gc.maxscore, gf.score, gf.remark
81 FROM {gradingform_guide_fillings} gf
82 JOIN {gradingform_guide_criteria} gc ON gc.id = gf.criterionid
83 WHERE gf.instanceid = :instanceid";
84 $records = $DB->get_records_sql($sql, $params);
85 if ($records) {
fe7d6e31 86 $subcontext = array_merge($subcontext, [get_string('guide', 'gradingform_guide'), $instanceid]);
f3a6d9bb
AG
87 writer::with_context($context)->export_data($subcontext, (object) $records);
88 }
89 }
90
91 /**
92 * Deletes all user data related to the provided instance IDs.
93 *
94 * @param array $instanceids The instance IDs to delete information from.
95 */
96 public static function delete_gradingform_for_instances(array $instanceids) {
97 global $DB;
98 $DB->delete_records_list('gradingform_guide_fillings', 'instanceid', $instanceids);
99 }
100
6416e87f
SA
101 /**
102 * Store all user preferences for the plugin.
103 *
104 * @param int $userid The userid of the user whose data is to be exported.
105 */
106 public static function export_user_preferences(int $userid) {
107 $prefvalue = get_user_preferences('gradingform_guide-showmarkerdesc', null, $userid);
108 if ($prefvalue !== null) {
109 $transformedvalue = transform::yesno($prefvalue);
110 writer::export_user_preference(
111 'gradingform_guide',
112 'gradingform_guide-showmarkerdesc',
113 $transformedvalue,
114 get_string('privacy:metadata:preference:showmarkerdesc', 'gradingform_guide')
115 );
116 }
117
118 $prefvalue = get_user_preferences('gradingform_guide-showstudentdesc', null, $userid);
119 if ($prefvalue !== null) {
120 $transformedvalue = transform::yesno($prefvalue);
121 writer::export_user_preference(
122 'gradingform_guide',
123 'gradingform_guide-showstudentdesc',
124 $transformedvalue,
125 get_string('privacy:metadata:preference:showstudentdesc', 'gradingform_guide')
126 );
127 }
128 }
129}