#252 Privacy API update

Add support to get_users_in_context and delete_data_for_users methods
added since 3.4.6 and 3.5.3 and 3.6.
This commit is contained in:
Sara Arjona 2018-12-13 13:34:32 +01:00 committed by Mark Nelson
parent 17934659b6
commit c8ab936b34
2 changed files with 157 additions and 0 deletions

View file

@ -25,9 +25,11 @@ namespace mod_customcert\privacy;
use core_privacy\local\metadata\collection; use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist; use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist; use core_privacy\local\request\contextlist;
use core_privacy\local\request\helper; use core_privacy\local\request\helper;
use core_privacy\local\request\transform; use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer; use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -96,6 +98,37 @@ class provider implements
return $contextlist; return $contextlist;
} }
/**
* Get the list of users who have data within a context.
*
* @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 instanceof \context_module) {
return;
}
// Fetch all users who have a custom certificate.
$sql = "SELECT customcertissues.userid
FROM {course_modules} cm
JOIN {modules} m
ON m.id = cm.module AND m.name = :modname
JOIN {customcert} customcert
ON customcert.id = cm.instance
JOIN {customcert_issues} customcertissues
ON customcertissues.customcertid = customcert.id
WHERE cm.id = :cmid";
$params = [
'cmid' => $context->instanceid,
'modname' => 'customcert',
];
$userlist->add_from_sql('userid', $sql, $params);
}
/** /**
* Export personal data for the given approved_contextlist. User and context information is contained within the contextlist. * Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
* *
@ -182,6 +215,33 @@ 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 (!$context instanceof \context_module) {
return;
}
$cm = get_coursemodule_from_id('customcert', $context->instanceid);
if (!$cm) {
// Only customcert module will be handled.
return;
}
$userids = $userlist->get_userids();
list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$select = "customcertid = :customcertid AND userid $usersql";
$params = ['customcertid' => $cm->instance] + $userparams;
$DB->delete_records_select('customcert_issues', $select, $params);
}
/** /**
* Return a list of Customcert IDs mapped to their course module ID. * Return a list of Customcert IDs mapped to their course module ID.
* *

View file

@ -64,6 +64,57 @@ class mod_customcert_privacy_provider_testcase extends \core_privacy\tests\provi
$this->assertEquals($cmcontext->id, $contextformodule->id); $this->assertEquals($cmcontext->id, $contextformodule->id);
} }
/**
* Test for provider::get_users_in_context().
*/
public function test_get_users_in_context() {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
// The customcert activity the user will have an issue from.
$customcert1 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
$customcert2 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
// Call get_users_in_context() when the customcert hasn't any user.
$cm = get_coursemodule_from_instance('customcert', $customcert1->id);
$cmcontext = context_module::instance($cm->id);
$userlist = new \core_privacy\local\request\userlist($cmcontext, 'mod_customcert');
provider::get_users_in_context($userlist);
// Check no user has been returned.
$this->assertCount(0, $userlist->get_userids());
// Create some users who will be issued a certificate.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$this->create_certificate_issue($customcert1->id, $user1->id);
$this->create_certificate_issue($customcert1->id, $user2->id);
$this->create_certificate_issue($customcert2->id, $user3->id);
// Call get_users_in_context() again.
provider::get_users_in_context($userlist);
// Check this time there are 2 users.
$this->assertCount(2, $userlist->get_userids());
$this->assertContains($user1->id, $userlist->get_userids());
$this->assertContains($user2->id, $userlist->get_userids());
$this->assertNotContains($user3->id, $userlist->get_userids());
}
/**
* Test for provider::get_users_in_context() with invalid context type.
*/
public function test_get_users_in_context_invalid_context_type() {
$systemcontext = context_system::instance();
$userlist = new \core_privacy\local\request\userlist($systemcontext, 'mod_customcert');
\mod_customcert\privacy\provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist->get_userids());
}
/** /**
* Test for provider::export_user_data(). * Test for provider::export_user_data().
*/ */
@ -179,6 +230,52 @@ class mod_customcert_privacy_provider_testcase extends \core_privacy\tests\provi
$this->assertEquals($user2->id, $lastissue->userid); $this->assertEquals($user2->id, $lastissue->userid);
} }
/**
* Test for provider::delete_data_for_users().
*/
public function test_delete_data_for_users() {
global $DB;
$this->resetAfterTest();
// Create course, customcert and users who will be issued a certificate.
$course = $this->getDataGenerator()->create_course();
$customcert1 = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id));
$customcert2 = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id));
$cm1 = get_coursemodule_from_instance('customcert', $customcert1->id);
$cm2 = get_coursemodule_from_instance('customcert', $customcert2->id);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$this->create_certificate_issue($customcert1->id, $user1->id);
$this->create_certificate_issue($customcert1->id, $user2->id);
$this->create_certificate_issue($customcert1->id, $user3->id);
$this->create_certificate_issue($customcert2->id, $user1->id);
$this->create_certificate_issue($customcert2->id, $user2->id);
// Before deletion we should have 3 + 2 issued certificates.
$count = $DB->count_records('customcert_issues', ['customcertid' => $customcert1->id]);
$this->assertEquals(3, $count);
$count = $DB->count_records('customcert_issues', ['customcertid' => $customcert2->id]);
$this->assertEquals(2, $count);
$context1 = context_module::instance($cm1->id);
$approveduserlist = new \core_privacy\local\request\approved_userlist($context1, 'customcert',
[$user1->id, $user2->id]);
provider::delete_data_for_users($approveduserlist);
// After deletion, the customcert of the 2 students provided above should have been deleted
// from the activity. So there should only remain 1 certificate which is for $user3.
$customcertissues1 = $DB->get_records('customcert_issues', ['customcertid' => $customcert1->id]);
$this->assertCount(1, $customcertissues1);
$lastissue = reset($customcertissues1);
$this->assertEquals($user3->id, $lastissue->userid);
// Confirm that the certificates issues in the other activity are intact.
$customcertissues1 = $DB->get_records('customcert_issues', ['customcertid' => $customcert2->id]);
$this->assertCount(2, $customcertissues1);
}
/** /**
* Mimicks the creation of a customcert issue. * Mimicks the creation of a customcert issue.
* *