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_rubric\privacy\provider}
19 *
20 * @package     workshopform_rubric
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_rubric\privacy;
27
28use core_privacy\local\request\writer;
29
30defined('MOODLE_INTERNAL') || die();
31
32/**
33 * Privacy API implementation for the Rubric 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 r.id, r.workshopid, r.description, r.descriptionformat,
65                       rl.id AS levelid, rl.grade AS levelgrade, rl.definition, rl.definitionformat,
66                       wg.grade
67                  FROM {course_modules} cm
68                  JOIN {context} ctx ON ctx.contextlevel = :contextlevel AND ctx.instanceid = cm.id
69                  JOIN {workshop} w ON cm.instance = w.id
70                  JOIN {workshopform_rubric} r ON r.workshopid = w.id
71                  JOIN {workshopform_rubric_levels} rl ON rl.dimensionid = r.id
72             LEFT JOIN {workshop_grades} wg ON wg.strategy = :strategy AND wg.dimensionid = r.id AND wg.assessmentid = :assessmentid
73                 WHERE ctx.id = :contextid
74              ORDER BY r.sort, rl.grade DESC";
75
76        $params = [
77            'strategy' => 'rubric',
78            'contextlevel' => CONTEXT_MODULE,
79            'contextid' => $context->id,
80            'assessmentid' => $assessmentid,
81        ];
82
83        $writer = \core_privacy\local\request\writer::with_context($context);
84        $criteria = [];
85        $workshopid = null;
86        $hasdata = false;
87
88        $rs = $DB->get_recordset_sql($sql, $params);
89
90        foreach ($rs as $record) {
91            if (empty($criteria[$record->id])) {
92                $criteria[$record->id] = (object) [
93                    'description' => $writer->rewrite_pluginfile_urls($subcontext, 'workshopform_rubric', 'description',
94                        $record->id, $record->description),
95                    'descriptionformat' => $record->descriptionformat,
96                    'grade' => $record->grade,
97                    'levels' => [],
98                ];
99                $workshopid = $record->workshopid;
100            }
101            $criteria[$record->id]->levels[] = (object) [
102                'grade' => $record->levelgrade,
103                'definition' => $record->definition,
104                'definitionformat' => $record->definitionformat,
105            ];
106            if ($record->grade !== null) {
107                $hasdata = true;
108            }
109        }
110
111        $rs->close();
112
113        if ($hasdata) {
114            $data = (object) [
115                'criteria' => array_values($criteria),
116            ];
117            $layout = $DB->get_field('workshopform_rubric_config', 'layout', ['workshopid' => $workshopid]);
118
119            foreach (array_keys($criteria) as $dimensionid) {
120                $writer->export_area_files($subcontext, 'workshopform_rubric', 'description', $dimensionid);
121            }
122
123            $writer->export_data($subcontext, $data);
124            $writer->export_metadata($subcontext, 'layout', $layout, get_string('layout', 'workshopform_rubric'));
125        }
126    }
127}
128