From e345bd89204b32d182fe0a667c507b3d03d562f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Mudr=C3=A1k?= <david@moodle.com>
Date: Tue, 8 May 2018 10:40:11 +0200
Subject: [PATCH 1/2] core

---
 mod/workshop/classes/privacy/provider.php    |  31 ++++----
 mod/workshop/tests/privacy_provider_test.php | 111 ++++++++++++++++++++++++++-
 2 files changed, 122 insertions(+), 20 deletions(-)

diff --git a/mod/workshop/classes/privacy/provider.php b/mod/workshop/classes/privacy/provider.php
index cffe2feb99..e2f3e6a7dc 100644
--- a/mod/workshop/classes/privacy/provider.php
+++ b/mod/workshop/classes/privacy/provider.php
@@ -298,7 +298,7 @@ class provider implements
         list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
         $user = $contextlist->get_user();
 
-        $sql = "SELECT ws.id, ws.workshopid, ws.authorid, ws.example, ws.timecreated, ws.timemodified, ws.title,
+        $sql = "SELECT ws.id, ws.authorid, ws.example, ws.timecreated, ws.timemodified, ws.title,
                        ws.content, ws.contentformat, ws.grade, ws.gradeover, ws.feedbackauthor, ws.feedbackauthorformat,
                        ws.published, ws.late,
                        w.phase, w.course, cm.id AS cmid, ".\context_helper::get_preload_record_columns_sql('ctx')."
@@ -335,9 +335,6 @@ class provider implements
             $courseid = $record->course;
 
             $data = (object) [
-                'id' => $record->id,
-                'workshopid' => $record->workshopid,
-                'authorid' => $record->authorid,
                 'example' => transform::yesno($record->example),
                 'timecreated' => transform::datetime($record->timecreated),
                 'timemodified' => $record->timemodified ? transform::datetime($record->timemodified) : null,
@@ -364,7 +361,7 @@ class provider implements
             // user that are always exported.
             if ($mysubmission && $phase == \workshop::PHASE_CLOSED) {
                 $assessments = $DB->get_records('workshop_assessments', ['submissionid' => $mysubmission->id], '',
-                    'id, weight, timecreated, timemodified, grade, feedbackauthor, feedbackauthorformat');
+                    'id, reviewerid, weight, timecreated, timemodified, grade, feedbackauthor, feedbackauthorformat');
 
                 foreach ($assessments as $assessment) {
                     // Skip unfinished allocated assessments as they do not affect user's grade.
@@ -372,20 +369,24 @@ class provider implements
                         continue;
                     }
 
+                    $assid = $assessment->id;
+                    $assessment->selfassessment = transform::yesno($assessment->reviewerid == $user->id);
                     $assessment->timecreated = transform::datetime($assessment->timecreated);
                     $assessment->timemodified = $assessment->timemodified ? transform::datetime($assessment->timemodified) : null;
                     $assessment->feedbackauthor = $writer->rewrite_pluginfile_urls($subcontext,
-                        'mod_workshop', 'overallfeedback_content', $assessment->id, $assessment->feedbackauthor);
+                        'mod_workshop', 'overallfeedback_content', $assid, $assessment->feedbackauthor);
 
-                    $assessmentsubcontext = array_merge($subcontext, [get_string('assessments', 'mod_workshop'), $assessment->id]);
+                    $assessmentsubcontext = array_merge($subcontext, [get_string('assessments', 'mod_workshop'), $assid]);
+
+                    unset($assessment->id);
+                    unset($assessment->reviewerid);
 
                     $writer->export_data($assessmentsubcontext, $assessment);
-                    $writer->export_area_files($assessmentsubcontext, 'mod_workshop', 'overallfeedback_content', $assessment->id);
-                    $writer->export_area_files($assessmentsubcontext, 'mod_workshop', 'overallfeedback_attachment',
-                        $assessment->id);
+                    $writer->export_area_files($assessmentsubcontext, 'mod_workshop', 'overallfeedback_content', $assid);
+                    $writer->export_area_files($assessmentsubcontext, 'mod_workshop', 'overallfeedback_attachment', $assid);
 
                     // Export details of how the assessment forms were filled.
-                    static::export_assessment_forms($user, $context, $assessmentsubcontext, $assessment->id);
+                    static::export_assessment_forms($user, $context, $assessmentsubcontext, $assid);
                 }
             }
 
@@ -413,7 +414,7 @@ class provider implements
         list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
         $user = $contextlist->get_user();
 
-        $sql = "SELECT ws.workshopid, ws.example, ws.timecreated, ws.timemodified, ws.title, ws.content, ws.contentformat,
+        $sql = "SELECT ws.authorid, ws.example, ws.timecreated, ws.timemodified, ws.title, ws.content, ws.contentformat,
                        wa.id, wa.submissionid, wa.reviewerid, wa.weight, wa.timecreated, wa.timemodified, wa.grade,
                        wa.gradinggrade, wa.gradinggradeover, wa.feedbackauthor, wa.feedbackauthorformat, wa.feedbackreviewer,
                        wa.feedbackreviewerformat, cm.id AS cmid, ".\context_helper::get_preload_record_columns_sql('ctx')."
@@ -441,9 +442,6 @@ class provider implements
             $subcontext = [get_string('myassessments', 'mod_workshop'), $record->id];
 
             $data = (object) [
-                'id' => $record->id,
-                'submissionid' => $record->submissionid,
-                'reviewerid' => $record->reviewerid,
                 'weight' => $record->weight,
                 'timecreated' => transform::datetime($record->timecreated),
                 'timemodified' => $record->timemodified ? transform::datetime($record->timemodified) : null,
@@ -458,8 +456,7 @@ class provider implements
             ];
 
             $submission = (object) [
-                'id' => $record->submissionid,
-                'workshopid' => $record->workshopid,
+                'myownsubmission' => transform::yesno($record->authorid == $user->id),
                 'example' => transform::yesno($record->example),
                 'timecreated' => transform::datetime($record->timecreated),
                 'timemodified' => $record->timemodified ? transform::datetime($record->timemodified) : null,
diff --git a/mod/workshop/tests/privacy_provider_test.php b/mod/workshop/tests/privacy_provider_test.php
index 9adc84bd08..6c6b0ecaca 100644
--- a/mod/workshop/tests/privacy_provider_test.php
+++ b/mod/workshop/tests/privacy_provider_test.php
@@ -37,8 +37,88 @@ use core_privacy\local\request\writer;
  */
 class mod_workshop_privacy_provider_testcase extends advanced_testcase {
 
+    /** @var testing_data_generator */
+    protected $generator;
+
+    /** @var mod_workshop_generator */
+    protected $workshopgenerator;
+
+    /** @var stdClass */
+    protected $course1;
+
+    /** @var stdClass */
+    protected $course2;
+
+    /** @var stdClass */
+    protected $student1;
+
+    /** @var stdClass */
+    protected $student2;
+
+    /** @var stdClass */
+    protected $student3;
+
+    /** @var stdClass */
+    protected $teacher4;
+
+    /** @var stdClass first workshop in course1 */
+    protected $workshop11;
+
+    /** @var stdClass second workshop in course1 */
+    protected $workshop12;
+
+    /** @var stdClass first workshop in course2 */
+    protected $workshop21;
+
+    /** @var int ID of the submission in workshop11 by student1 */
+    protected $submission111;
+
+    /** @var int ID of the submission in workshop12 by student1 */
+    protected $submission121;
+
+    /** @var int ID of the submission in workshop12 by student2 */
+    protected $submission122;
+
+    /** @var int ID of the submission in workshop21 by student2 */
+    protected $submission212;
+
+    /** @var int ID of the assessment of submission111 by student1 */
+    protected $assessment1111;
+
+    /** @var int ID of the assessment of submission111 by student2 */
+    protected $assessment1112;
+
+    /** @var int ID of the assessment of submission111 by student3 */
+    protected $assessment1113;
+
+    /** @var int ID of the assessment of submission121 by student2 */
+    protected $assessment1212;
+
+    /** @var int ID of the assessment of submission212 by student1 */
+    protected $assessment2121;
+
     /**
-     * {@inheritdoc}
+     * Set up the test environment.
+     *
+     * course1
+     *  |
+     *  +--workshop11 (first digit matches the course, second is incremental)
+     *  |   |
+     *  |   +--submission111 (first two digits match the workshop, last one matches the author)
+     *  |       |
+     *  |       +--assessment1111 (first three digits match the submission, last one matches the reviewer)
+     *  |       +--assessment1112
+     *  |       +--assessment1113
+     *  |
+     *  +--workshop12
+     *      |
+     *      +--submission121
+     *      |   |
+     *      |   +--assessment1212
+     *      |
+     *      +--submission122
+     *
+     *  etc.
      */
     protected function setUp() {
         global $DB;
@@ -127,7 +207,7 @@ class mod_workshop_privacy_provider_testcase extends advanced_testcase {
     /**
      * Test {@link \mod_workshop\privacy\provider::export_user_data()} implementation.
      */
-    public function test_export_user_data() {
+    public function test_export_user_data_1() {
 
         $contextlist = new \core_privacy\local\request\approved_contextlist($this->student1, 'mod_workshop', [
             \context_module::instance($this->workshop11->cmid)->id,
@@ -145,7 +225,6 @@ class mod_workshop_privacy_provider_testcase extends advanced_testcase {
         $mysubmission = $writer->get_data([
             get_string('mysubmission', 'mod_workshop'),
         ]);
-        $this->assertEquals($this->student1->id, $mysubmission->authorid);
 
         $mysubmissionassessmentwithoutgrade = $writer->get_data([
             get_string('mysubmission', 'mod_workshop'),
@@ -160,6 +239,32 @@ class mod_workshop_privacy_provider_testcase extends advanced_testcase {
             $this->assessment1112,
         ]);
         $this->assertEquals(92, $mysubmissionassessmentwithgrade->grade);
+        $this->assertEquals(get_string('no'), $mysubmissionassessmentwithgrade->selfassessment);
+
+        $myassessments = $writer->get_data([
+            get_string('myassessments', 'mod_workshop'),
+        ]);
+        $this->assertEmpty($myassessments);
+    }
+
+    /**
+     * Test {@link \mod_workshop\privacy\provider::export_user_data()} implementation.
+     */
+    public function test_export_user_data_2() {
+
+        $contextlist = new \core_privacy\local\request\approved_contextlist($this->student2, 'mod_workshop', [
+            \context_module::instance($this->workshop11->cmid)->id,
+        ]);
+
+        \mod_workshop\privacy\provider::export_user_data($contextlist);
+
+        $writer = writer::with_context(\context_module::instance($this->workshop11->cmid));
+
+        $assessedsubmission = $writer->get_related_data([
+            get_string('myassessments', 'mod_workshop'),
+            $this->assessment1112,
+        ], 'submission');
+        $this->assertEquals(get_string('no'), $assessedsubmission->myownsubmission);
     }
 
     /**
-- 
2.16.1

