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 * Provides the class {@link workshopform_comments\privacy\provider}
19 *
20 * @package     workshopform_comments
21 * @category    privacy
22 * @copyright   2018 David Mudrák <david@moodle.com>
23 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace workshopform_comments\privacy;
27
28use core_privacy\local\request\writer;
29
30defined('MOODLE_INTERNAL') || die();
31
32/**
33 * Privacy API implementation for the Comments grading strategy.
34 *
35 * @copyright 2018 David Mudrák <david@moodle.com>
36 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class provider implements \core_privacy\local\metadata\null_provider, \mod_workshop\privacy\workshopform_provider {
39
40    /**
41     * Explain that this plugin stores no personal data.
42     *
43     * @return string
44     */
45    public static function get_reason() : string {
46        return 'privacy:metadata';
47    }
48
49    /**
50     * Return details of the filled assessment form.
51     *
52     * @param stdClass $user User we are exporting data for
53     * @param context $context The workshop activity context
54     * @param array $subcontext Subcontext within the context to export to
55     * @param int $assessmentid ID of the assessment
56     */
57    public static function export_assessment_form(\stdClass $user, \context $context, array $subcontext, int $assessmentid) {
58        global $DB;
59
60        if ($context->contextlevel != CONTEXT_MODULE) {
61            throw new \coding_exception('Unexpected context provided');
62        }
63
64        $sql = "SELECT dim.id, dim.description, dim.descriptionformat, wg.peercomment, wg.peercommentformat
65                  FROM {course_modules} cm
66                  JOIN {context} ctx ON ctx.contextlevel = :contextlevel AND ctx.instanceid = cm.id
67                  JOIN {workshop} w ON cm.instance = w.id
68                  JOIN {workshopform_comments} dim ON dim.workshopid = w.id
69             LEFT JOIN {workshop_grades} wg ON wg.strategy = :strategy
70                       AND wg.dimensionid = dim.id AND wg.assessmentid = :assessmentid
71                 WHERE ctx.id = :contextid
72              ORDER BY dim.sort";
73
74        $params = [
75            'strategy' => 'comments',
76            'contextlevel' => CONTEXT_MODULE,
77            'contextid' => $context->id,
78            'assessmentid' => $assessmentid,
79        ];
80
81        $writer = \core_privacy\local\request\writer::with_context($context);
82        $data = [];
83        $hasdata = false;
84        $dimensionids = [];
85
86        foreach ($DB->get_records_sql($sql, $params) as $record) {
87            if ($record->peercomment !== null) {
88                $hasdata = true;
89            }
90            $record->description = $writer->rewrite_pluginfile_urls($subcontext, 'workshopform_comments',
91                'description', $record->id, $record->description);
92            $dimensionids[] = $record->id;
93            unset($record->id);
94            $data[] = $record;
95        }
96
97        if ($hasdata) {
98            $writer->export_data($subcontext, (object) ['aspects' => $data]);
99            foreach ($dimensionids as $dimensionid) {
100                $writer->export_area_files($subcontext, 'workshopform_comments', 'description', $dimensionid);
101            }
102        }
103    }
104}
105