Merge branch 'MDL-61998-master' of git://github.com/sarjona/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2018-04-26 02:39:17 +02:00
commit 7bff0ee145
3 changed files with 416 additions and 0 deletions

View File

@ -0,0 +1,194 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy class for requesting user data.
*
* @package core_cohort
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_cohort\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
/**
* Privacy class for requesting user data.
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider {
/**
* Return the fields which contain personal data.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) : collection {
$collection->add_database_table('cohort_members', [
'cohortid' => 'privacy:metadata:cohort_members:cohortid',
'userid' => 'privacy:metadata:cohort_members:userid',
'timeadded' => 'privacy:metadata:cohort_members:timeadded'
], 'privacy:metadata:cohort_members');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
$sql = "SELECT ctx.id
FROM {context} ctx
INNER JOIN {cohort} c ON c.contextid = ctx.id
INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
WHERE cm.userid = :userid AND (ctx.contextlevel = :contextlevel1 OR ctx.contextlevel = :contextlevel2)";
$params = [
'userid' => $userid,
'contextlevel1' => CONTEXT_SYSTEM,
'contextlevel2' => CONTEXT_COURSECAT,
];
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
// Remove contexts different from SYSTEM or COURSECAT.
$contexts = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_SYSTEM || $context->contextlevel == CONTEXT_COURSECAT) {
$carry[] = $context->id;
}
return $carry;
}, []);
if (empty($contexts)) {
return;
}
// Get cohort data.
$userid = $contextlist->get_user()->id;
list($contextsql, $contextparams) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED);
$sql = "SELECT c.name,
c.idnumber,
c.description,
c.visible,
cm.timeadded,
ctx.id as contextid
FROM {context} ctx
INNER JOIN {cohort} c ON c.contextid = ctx.id
INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
WHERE ctx.id {$contextsql}
AND cm.userid = :userid";
$params = [
'userid' => $userid
] + $contextparams;
$cohorts = $DB->get_recordset_sql($sql, $params);
foreach ($cohorts as $cohort) {
$alldata[$cohort->contextid][] = (object)[
'name' => $cohort->name,
'idnumber' => $cohort->idnumber,
'visible' => transform::yesno($cohort->visible),
'timeadded' => transform::datetime($cohort->timeadded),
];
}
$cohorts->close();
// Export cohort data.
array_walk($alldata, function($data, $contextid) {
$context = \context::instance_by_id($contextid);
writer::with_context($context)->export_related_data([], 'cohort', $data);
});
}
/**
* Delete all use data which matches the specified context.
*
* @param context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
if (empty($context)) {
return;
}
if (!$context instanceof \context_system && !$context instanceof \context_coursecat) {
return;
}
static::delete_data($context);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
if (empty($contextlist->count())) {
return;
}
$userid = $contextlist->get_user()->id;
foreach ($contextlist->get_contexts() as $context) {
if (!$context instanceof \context_system && !$context instanceof \context_coursecat) {
return;
}
static::delete_data($context, $userid);
}
}
/**
* Delete data related to a context and user (if defined).
*
* @param context $context A context.
* @param int $userid The user ID.
*/
protected static function delete_data(\context $context, int $userid = null) {
global $DB;
$cohortids = $DB->get_fieldset_select('cohort', 'id', 'contextid = :contextid', ['contextid' => $context->id]);
foreach ($cohortids as $cohortid) {
$params = ['cohortid' => $cohortid];
if (!empty($userid)) {
$params['userid'] = $userid;
}
$DB->delete_records('cohort_members', $params);
}
}
}

View File

@ -0,0 +1,218 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for unit tests for core_cohort.
*
* @package core_cohort
* @category test
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_cohort\privacy\provider;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\writer;
use core_privacy\tests\provider_testcase;
/**
* Unit tests for cohort\classes\privacy\provider.php
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_cohort_testcase extends provider_testcase {
/**
* Basic setup for these tests.
*/
public function setUp() {
$this->resetAfterTest(true);
}
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid() {
// Create system cohort and category cohort.
$coursecategory = $this->getDataGenerator()->create_category();
$coursecategoryctx = \context_coursecat::instance($coursecategory->id);
$systemctx = \context_system::instance();
$categorycohort = $this->getDataGenerator()->create_cohort([
'contextid' => $coursecategoryctx->id,
'name' => 'Category cohort 1',
]);
$systemcohort = $this->getDataGenerator()->create_cohort([
'contextid' => $systemctx->id,
'name' => 'System cohort 1'
]);
// Create user and add to the system and category cohorts.
$user = $this->getDataGenerator()->create_user();
cohort_add_member($categorycohort->id, $user->id);
cohort_add_member($systemcohort->id, $user->id);
// User is member of 2 cohorts.
$contextlist = provider::get_contexts_for_userid($user->id);
$this->assertCount(2, (array) $contextlist->get_contextids());
$this->assertContains($coursecategoryctx->id, $contextlist->get_contextids());
$this->assertContains($systemctx->id, $contextlist->get_contextids());
}
/**
* Test that data is exported correctly for this plugin.
*/
public function test_export_user_data() {
// Create system cohort and category cohort.
$coursecategory = $this->getDataGenerator()->create_category();
$coursecategoryctx = \context_coursecat::instance($coursecategory->id);
$systemctx = \context_system::instance();
$categorycohort = $this->getDataGenerator()->create_cohort([
'contextid' => $coursecategoryctx->id,
'name' => 'Category cohort 1',
]);
$systemcohort1 = $this->getDataGenerator()->create_cohort([
'contextid' => $systemctx->id,
'name' => 'System cohort 1'
]);
$systemcohort2 = $this->getDataGenerator()->create_cohort([
'contextid' => $systemctx->id,
'name' => 'System cohort 2'
]);
// Create user and add to the system and category cohorts.
$user = $this->getDataGenerator()->create_user();
cohort_add_member($categorycohort->id, $user->id);
cohort_add_member($systemcohort1->id, $user->id);
cohort_add_member($systemcohort2->id, $user->id);
// Validate system cohort exported data.
$writer = writer::with_context($systemctx);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($user->id, $systemctx, 'core_cohort');
$data = $writer->get_related_data([], 'cohort');
$this->assertCount(2, $data);
// Validate category cohort exported data.
$writer = writer::with_context($coursecategoryctx);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($user->id, $coursecategoryctx, 'core_cohort');
$data = $writer->get_related_data([], 'cohort');
$this->assertCount(1, $data);
$this->assertEquals($categorycohort->name, reset($data)->name);
}
/**
* Test for provider::delete_data_for_all_users_in_context().
*/
public function test_delete_data_for_all_users_in_context() {
global $DB;
// Create system cohort and category cohort.
$coursecategory = $this->getDataGenerator()->create_category();
$coursecategoryctx = \context_coursecat::instance($coursecategory->id);
$systemctx = \context_system::instance();
$categorycohort = $this->getDataGenerator()->create_cohort([
'contextid' => $coursecategoryctx->id,
'name' => 'Category cohort 1',
'idnumber' => '',
'description' => ''
]);
$systemcohort = $this->getDataGenerator()->create_cohort([
'contextid' => $systemctx->id,
'name' => 'System cohort 1'
]);
// Create user and add to the system and category cohorts.
$user = $this->getDataGenerator()->create_user();
cohort_add_member($categorycohort->id, $user->id);
cohort_add_member($systemcohort->id, $user->id);
// Before deletion, we should have 2 entries in the cohort_members table.
$count = $DB->count_records('cohort_members');
$this->assertEquals(2, $count);
// Delete data based on system context.
provider::delete_data_for_all_users_in_context($systemctx);
// After deletion, the cohort_members entries should have been deleted.
$count = $DB->count_records('cohort_members');
$this->assertEquals(1, $count);
// Delete data based on category context.
provider::delete_data_for_all_users_in_context($coursecategoryctx);
// After deletion, the cohort_members entries should have been deleted.
$count = $DB->count_records('cohort_members');
$this->assertEquals(0, $count);
}
/**
* Test for provider::delete_data_for_user().
*/
public function test_delete_data_for_user() {
global $DB;
// Create system cohort and category cohort.
$coursecategory = $this->getDataGenerator()->create_category();
$coursecategoryctx = \context_coursecat::instance($coursecategory->id);
$systemctx = \context_system::instance();
$categorycohort = $this->getDataGenerator()->create_cohort([
'contextid' => $coursecategoryctx->id,
'name' => 'Category cohort 1',
'idnumber' => '',
'description' => ''
]);
$systemcohort = $this->getDataGenerator()->create_cohort([
'contextid' => $systemctx->id,
'name' => 'System cohort 1'
]);
// Create user and add to the system and category cohorts.
$user1 = $this->getDataGenerator()->create_user();
cohort_add_member($categorycohort->id, $user1->id);
cohort_add_member($systemcohort->id, $user1->id);
// Create another user and add to the system and category cohorts.
$user2 = $this->getDataGenerator()->create_user();
cohort_add_member($categorycohort->id, $user2->id);
cohort_add_member($systemcohort->id, $user2->id);
// Create another user and add to the system cohort.
$user3 = $this->getDataGenerator()->create_user();
cohort_add_member($systemcohort->id, $user3->id);
// Before deletion, we should have 5 entries in the cohort_members table.
$count = $DB->count_records('cohort_members');
$this->assertEquals(5, $count);
$contextlist = provider::get_contexts_for_userid($user1->id);
$approvedcontextlist = new approved_contextlist($user1, 'cohort', $contextlist->get_contextids());
provider::delete_data_for_user($approvedcontextlist);
// After deletion, the cohort_members entries for the first student should have been deleted.
$count = $DB->count_records('cohort_members', ['userid' => $user1->id]);
$this->assertEquals(0, $count);
$count = $DB->count_records('cohort_members');
$this->assertEquals(3, $count);
// Confirm that the cohorts hasn't been removed.
$cohortscount = $DB->get_records('cohort');
$this->assertCount(2, (array) $cohortscount);
}
}

View File

@ -70,6 +70,10 @@ $string['nocomponent'] = 'Created manually';
$string['potusers'] = 'Potential users';
$string['potusersmatching'] = 'Potential matching users';
$string['preview'] = 'Preview';
$string['privacy:metadata:cohort_members'] = 'Information about the user\'s cohort.';
$string['privacy:metadata:cohort_members:cohortid'] = 'The ID of the cohort';
$string['privacy:metadata:cohort_members:timeadded'] = 'The timestamp indicating when the user was added to the cohort';
$string['privacy:metadata:cohort_members:userid'] = 'The ID of the user which is associated to the cohort';
$string['removeuserwarning'] = 'Removing users from a cohort may result in unenrolling of users from multiple courses which includes deleting of user settings, grades, group membership and other user information from affected courses.';
$string['selectfromcohort'] = 'Select members from cohort';
$string['systemcohorts'] = 'System cohorts';