MDL-63513 mod_assignment: Add removal of context users.
authorAdrian Greeve <abgreeve@gmail.com>
Wed, 3 Oct 2018 05:28:57 +0000 (13:28 +0800)
committerDavid Monllao <davidm@moodle.com>
Mon, 22 Oct 2018 10:48:25 +0000 (12:48 +0200)
This issue is part of the MDL-62560 Epic.

mod/assignment/classes/privacy/provider.php
mod/assignment/tests/privacy_test.php

index 6ed8802..9fbfa1e 100644 (file)
@@ -30,6 +30,8 @@ use core_privacy\local\request\contextlist;
 use core_privacy\local\request\transform;
 use core_privacy\local\request\writer;
 use core_privacy\local\request\helper;
+use core_privacy\local\request\approved_userlist;
+use core_privacy\local\request\userlist;
 
 defined('MOODLE_INTERNAL') || die();
 
@@ -45,7 +47,8 @@ require_once($CFG->dirroot . '/mod/assignment/lib.php');
 class provider implements
     \core_privacy\local\metadata\provider,
     \core_privacy\local\request\plugin\provider,
-    \core_privacy\local\request\user_preference_provider {
+    \core_privacy\local\request\user_preference_provider,
+    \core_privacy\local\request\core_userlist_provider {
 
     /**
      * Return the fields which contain personal data.
@@ -112,6 +115,43 @@ class provider implements
         return $contextlist;
     }
 
+    /**
+     * Get the list of contexts that contain user information for the specified user.
+     *
+     * @param   userlist    $userlist   The userlist containing the list of users who have data in this context/plugin combination.
+     */
+    public static function get_users_in_context(userlist $userlist) {
+        $context = $userlist->get_context();
+        if ($context->contextlevel != CONTEXT_MODULE) {
+            return;
+        }
+
+        $params = [
+            'modulename' => 'assignment',
+            'contextlevel' => CONTEXT_MODULE,
+            'contextid' => $context->id
+        ];
+        $sql = "SELECT s.userid
+                  FROM {assignment_submissions} s
+                  JOIN {assignment} a ON s.assignment = a.id
+                  JOIN {modules} m ON m.name = :modulename
+                  JOIN {course_modules} cm ON a.id = cm.instance AND cm.module = m.id
+                  JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :contextlevel
+                 WHERE ctx.id = :contextid
+        ";
+        $userlist->add_from_sql('userid', $sql, $params);
+
+        $sql = "SELECT s.teacher
+                  FROM {assignment_submissions} s
+                  JOIN {assignment} a ON s.assignment = a.id
+                  JOIN {modules} m ON m.name = :modulename
+                  JOIN {course_modules} cm ON a.id = cm.instance AND cm.module = m.id
+                  JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :contextlevel
+                 WHERE ctx.id = :contextid
+        ";
+        $userlist->add_from_sql('teacher', $sql, $params);
+    }
+
     /**
      * Export personal data for the given approved_contextlist.
      * User and context information is contained within the contextlist.
@@ -255,6 +295,43 @@ class provider implements
         }
     }
 
+    /**
+     * Delete multiple users within a single context.
+     *
+     * @param   approved_userlist       $userlist The approved context and user information to delete information for.
+     */
+    public static function delete_data_for_users(approved_userlist $userlist) {
+        global $DB;
+
+        $context = $userlist->get_context();
+        // If the context isn't for a module then return early.
+        if ($context->contextlevel != CONTEXT_MODULE) {
+            return;
+        }
+        // Fetch the assignment.
+        $assignment = self::get_assignment_by_context($context);
+        $userids = $userlist->get_userids();
+
+        list($inorequalsql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
+        $params['assignmentid'] = $assignment->id;
+
+        // Get submission ids.
+        $sql = "
+            SELECT s.id
+            FROM {assignment_submissions} s
+            JOIN {assignment} a ON s.assignment = a.id
+            WHERE a.id = :assignmentid
+            AND s.userid $inorequalsql
+        ";
+
+        $submissionids = $DB->get_records_sql($sql, $params);
+        list($submissionidsql, $submissionparams) = $DB->get_in_or_equal(array_keys($submissionids), SQL_PARAMS_NAMED);
+        $fs = get_file_storage();
+        $fs->delete_area_files_select($context->id, 'mod_assignment', 'submission', $submissionidsql, $submissionparams);
+        // Delete related tables.
+        $DB->delete_records_list('assignment_submissions', 'id', array_keys($submissionids));
+    }
+
     // Start of helper functions.
 
     /**
index 71c9509..18dbb41 100644 (file)
@@ -100,6 +100,76 @@ class mod_assignment_privacy_testcase extends advanced_testcase {
         $this->assertEquals(2, count($contextids->get_contextids()));
     }
 
+    /**
+     * Test that the correct userids are returned for a specific context.
+     */
+    public function test_get_users_in_context() {
+
+        $this->resetAfterTest(true);
+
+        $student1 = $this->getDataGenerator()->create_user(['username' => 'student1']);
+        $student2 = $this->getDataGenerator()->create_user(['username' => 'student2']);
+        // Student 3 should not turn up in the results of this test.
+        $student3 = $this->getDataGenerator()->create_user(['username' => 'student3']);
+        $teacher1 = $this->getDataGenerator()->create_user(['username' => 'teacher1']);
+
+        $course = $this->getDataGenerator()->create_course();
+        $course1assignment1 = $this->getDataGenerator()->create_module('assignment',
+            [
+                'course' => $course->id,
+                'name' => 'Course 1 - Assignment 1 (onlinetext)',
+                'assignmenttype' => 'onlinetext',
+            ]
+        );
+        // Create a second assignment in the same course.
+        $course1assignment2 = $this->getDataGenerator()->create_module('assignment',
+            [
+                'course' => $course->id,
+                'name' => 'Course 1 - Assignment 1 (onlinetext)',
+                'assignmenttype' => 'onlinetext',
+            ]
+        );
+
+        $this->add_assignment_submission(
+            $course1assignment1,
+            $student1,
+            "Course 1 - Ass 1: Student1 Test Submission"
+        );
+
+        $this->add_assignment_submission(
+            $course1assignment1,
+            $student2,
+            "Course 1 - Ass 1: Student2 Test Submission"
+        );
+        // Add a submission for the second assignment.
+        $this->add_assignment_submission(
+            $course1assignment2,
+            $student3,
+            "Course 1 - Ass 2: Student3 Test Submission"
+        );
+
+        $submissions = $this->get_course_assignment_submissions($course->id);
+        foreach ($submissions as $submission) {
+            $this->mark_assignment_submission($submission->assignment, $submission->id, $teacher1, 50);
+        }
+
+        $c1ass1ctx = context_module::instance($course1assignment1->cmid);
+        $userlist = new \core_privacy\local\request\userlist($c1ass1ctx, 'mod_assignment');
+
+        provider::get_users_in_context($userlist);
+        $userids = $userlist->get_userids();
+
+        // This is both students and the teacher who marked both assignments.
+        $this->assertCount(3, $userids);
+        // Make sure that student 3 is not in the returned userids.
+        $this->assertFalse(in_array($student3->id, $userids));
+        // Try with the course context.
+        $coursecontext = context_course::instance($course->id);
+        $userlist = new \core_privacy\local\request\userlist($coursecontext, 'mod_assignment');
+        provider::get_users_in_context($userlist);
+        $this->assertEmpty($userlist->get_userids());
+    }
+
     /**
      * Test for provider::export_user_data().
      *
@@ -363,6 +433,73 @@ class mod_assignment_privacy_testcase extends advanced_testcase {
         }
     }
 
+    /**
+     * Test the deletion of a data for users.
+     */
+    public function test_delete_data_for_users() {
+        global $DB;
+        $this->resetAfterTest();
+
+        $student1 = $this->getDataGenerator()->create_user(['username' => 'student1']);
+        $student2 = $this->getDataGenerator()->create_user(['username' => 'student2']);
+        $student3 = $this->getDataGenerator()->create_user(['username' => 'student3']);
+
+        $course = $this->getDataGenerator()->create_course();
+        $course1assignment = $this->getDataGenerator()->create_module('assignment',
+            [
+                'course' => $course->id,
+                'name' => 'Course 1 - Assignment 1 (single file upload)',
+                'assignmenttype' => 'uploadsingle'
+            ]
+        );
+        $context = context_module::instance($course1assignment->cmid);
+
+        // Student one submission.
+        $this->add_file_assignment_submission(
+            $course1assignment,
+            $student1,
+            "Course 1 - Ass 2: " . $student1->id,
+            'Student' . $student1->id . '-Course1-Ass2'
+        );
+        // Student two submission.
+        $this->add_file_assignment_submission(
+            $course1assignment,
+            $student2,
+            "Course 1 - Ass 2: " . $student2->id,
+            'Student' . $student2->id . '-Course1-Ass2'
+        );
+        // Student three submission to be retained.
+        $this->add_file_assignment_submission(
+            $course1assignment,
+            $student3,
+            "Course 1 - Ass 2: " . $student3->id,
+            'Student' . $student3->id . '-Course1-Ass2'
+        );
+
+        $files = $DB->get_records('files', [
+            'component' => 'mod_assignment',
+            'filearea' => 'submission',
+            'contextid' => $context->id
+        ]);
+        $this->assertCount(6, $files);
+
+        $submissions = $this->get_assignment_submissions($context->id);
+        $this->assertCount(3, $submissions);
+
+        $userlist = new \core_privacy\local\request\approved_userlist($context, 'mod_assignment', [$student1->id, $student2->id]);
+        provider::delete_data_for_users($userlist);
+
+        $files = $DB->get_records('files', [
+            'component' => 'mod_assignment',
+            'filearea' => 'submission',
+            'contextid' => $context->id
+        ]);
+        $this->assertCount(2, $files);
+
+        $submissions = $this->get_assignment_submissions($context->id);
+        $this->assertCount(1, $submissions);
+    }
+
     // Start of helper functions.
 
     /**