Merge branch 'MDL-61984-master' of git://github.com/FMCorz/moodle

This commit is contained in:
Andrew Nicols
2018-04-24 09:23:35 +08:00
3 changed files with 576 additions and 0 deletions
+260
View File
@@ -0,0 +1,260 @@
<?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/>.
/**
* Data provider.
*
* @package mod_chat
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_chat\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use context_helper;
use context_module;
use moodle_recordset;
use stdClass;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\helper;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
/**
* Data provider class.
*
* @package mod_chat
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <[email protected]>
* @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 {
/**
* Returns metadata.
*
* @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('chat_messages', [
'userid' => 'privacy:metadata:messages:userid',
'message' => 'privacy:metadata:messages:message',
'issystem' => 'privacy:metadata:messages:issystem',
'timestamp' => 'privacy:metadata:messages:timestamp',
], 'privacy:metadata:messages');
// The tables chat_messages_current and chat_users are not reported here
// because they are considered as short-lived data and are deleted on a
// regular basis by cron, or during normal requests. MDL-62006 was raised
// to discuss and/or implement support for those tables.
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) : \core_privacy\local\request\contextlist {
$contextlist = new \core_privacy\local\request\contextlist();
$sql = "
SELECT DISTINCT ctx.id
FROM {chat} c
JOIN {modules} m
ON m.name = :chat
JOIN {course_modules} cm
ON cm.instance = c.id
AND cm.module = m.id
JOIN {context} ctx
ON ctx.instanceid = cm.id
AND ctx.contextlevel = :modulelevel
JOIN {chat_messages} chm
ON chm.chatid = c.id
WHERE chm.userid = :userid";
$params = [
'chat' => 'chat',
'modulelevel' => CONTEXT_MODULE,
'userid' => $userid,
];
$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;
$user = $contextlist->get_user();
$userid = $user->id;
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_MODULE) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($cmids)) {
return;
}
$chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
$chatids = array_keys($chatidstocmids);
// Export the messages.
list($insql, $inparams) = $DB->get_in_or_equal($chatids, SQL_PARAMS_NAMED);
$params = array_merge($inparams, ['userid' => $userid]);
$recordset = $DB->get_recordset_select('chat_messages', "chatid $insql AND userid = :userid", $params, 'timestamp, id');
static::recordset_loop_and_export($recordset, 'chatid', [], function($carry, $record) use ($user, $chatidstocmids) {
$message = $record->message;
if ($record->issystem) {
$message = get_string('message' . $record->message, 'mod_chat', fullname($user));
}
$carry[] = [
'message' => $message,
'sent_at' => transform::datetime($record->timestamp),
'is_system_generated' => transform::yesno($record->issystem),
];
return $carry;
}, function($chatid, $data) use ($user, $chatidstocmids) {
$context = context_module::instance($chatidstocmids[$chatid]);
$contextdata = helper::get_context_data($context, $user);
$finaldata = (object) array_merge((array) $contextdata, ['messages' => $data]);
helper::export_context_files($context, $user);
writer::with_context($context)->export_data([], $finaldata);
});
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
global $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return;
}
$chatid = get_coursemodule_from_id('chat', $context->instanceid, 0, false, MUST_EXIST)->instance;
$DB->delete_records_select('chat_messages', 'chatid = :chatid', ['chatid' => $chatid]);
$DB->delete_records_select('chat_messages_current', 'chatid = :chatid', ['chatid' => $chatid]);
$DB->delete_records_select('chat_users', 'chatid = :chatid', ['chatid' => $chatid]);
}
/**
* 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) {
global $DB;
$userid = $contextlist->get_user()->id;
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_MODULE) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($cmids)) {
return;
}
$chatidstocmids = static::get_chat_ids_to_cmids_from_cmids($cmids);
$chatids = array_keys($chatidstocmids);
list($insql, $inparams) = $DB->get_in_or_equal($chatids, SQL_PARAMS_NAMED);
$sql = "chatid $insql AND userid = :userid";
$params = array_merge($inparams, ['userid' => $userid]);
$DB->delete_records_select('chat_messages', $sql, $params);
$DB->delete_records_select('chat_messages_current', $sql, $params);
$DB->delete_records_select('chat_users', $sql, $params);
}
/**
* Return a dict of chat IDs mapped to their course module ID.
*
* @param array $cmids The course module IDs.
* @return array In the form of [$chatid => $cmid].
*/
protected static function get_chat_ids_to_cmids_from_cmids(array $cmids) {
global $DB;
list($insql, $inparams) = $DB->get_in_or_equal($cmids, SQL_PARAMS_NAMED);
$sql = "
SELECT c.id, cm.id AS cmid
FROM {chat} c
JOIN {modules} m
ON m.name = :chat
JOIN {course_modules} cm
ON cm.instance = c.id
AND cm.module = m.id
WHERE cm.id $insql";
$params = array_merge($inparams, ['chat' => 'chat']);
return $DB->get_records_sql_menu($sql, $params);
}
/**
* Loop and export from a recordset.
*
* @param moodle_recordset $recordset The recordset.
* @param string $splitkey The record key to determine when to export.
* @param mixed $initial The initial data to reduce from.
* @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
* @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
* @return void
*/
protected static function recordset_loop_and_export(moodle_recordset $recordset, $splitkey, $initial,
callable $reducer, callable $export) {
$data = $initial;
$lastid = null;
foreach ($recordset as $record) {
if ($lastid && $record->{$splitkey} != $lastid) {
$export($lastid, $data);
$data = $initial;
}
$data = $reducer($data, $record);
$lastid = $record->{$splitkey};
}
$recordset->close();
if (!empty($lastid)) {
$export($lastid, $data);
}
}
}
+5
View File
@@ -118,6 +118,11 @@ $string['page-mod-chat-x'] = 'Any chat module page';
$string['pastchats'] = 'Past chat sessions';
$string['pluginadministration'] = 'Chat administration';
$string['pluginname'] = 'Chat';
$string['privacy:metadata:messages'] = 'A record of the messages sent during a chat session';
$string['privacy:metadata:messages:issystem'] = 'Whether the message is a system-generated message';
$string['privacy:metadata:messages:message'] = 'The message';
$string['privacy:metadata:messages:timestamp'] = 'The time at which the message was sent';
$string['privacy:metadata:messages:userid'] = 'The user ID of the author of the message';
$string['refreshroom'] = 'Refresh room';
$string['refreshuserlist'] = 'Refresh user list';
$string['removemessages'] = 'Remove all messages';
+311
View File
@@ -0,0 +1,311 @@
<?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/>.
/**
* Data provider tests.
*
* @package mod_chat
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
use core_privacy\tests\provider_testcase;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use mod_chat\privacy\provider;
require_once($CFG->dirroot . '/mod/chat/lib.php');
/**
* Data provider testcase class.
*
* @package mod_chat
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_chat_privacy_testcase extends provider_testcase {
public function setUp() {
global $PAGE;
$this->resetAfterTest();
$PAGE->get_renderer('core');
}
public function test_get_contexts_for_userid() {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$c2 = $dg->create_course();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$chat1a = $dg->create_module('chat', ['course' => $c1]);
$chat1b = $dg->create_module('chat', ['course' => $c1]);
$chat2a = $dg->create_module('chat', ['course' => $c2]);
// Logins but no message.
$chatuser = $this->login_user_in_course_chat($u1, $c1, $chat1a);
// Logins and messages.
$chatuser = $this->login_user_in_course_chat($u1, $c1, $chat1b);
chat_send_chatmessage($chatuser, 'Hello world!');
// Silent login (no system message).
$chatuser = $this->login_user_in_course_chat($u1, $c2, $chat2a, 0, true);
// Silent login and messages.
$chatuser = $this->login_user_in_course_chat($u2, $c1, $chat1b, 0, true);
chat_send_chatmessage($chatuser, 'Ça va ?');
chat_send_chatmessage($chatuser, 'Moi, ça va.');
// Silent login and messages.
$chatuser = $this->login_user_in_course_chat($u2, $c2, $chat2a);
chat_send_chatmessage($chatuser, 'What\'s happening here?');
// Check contexts for user 1.
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
$this->assertCount(2, $contextids);
$this->assertTrue(in_array(context_module::instance($chat1a->cmid)->id, $contextids));
$this->assertTrue(in_array(context_module::instance($chat1b->cmid)->id, $contextids));
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
$this->assertCount(2, $contextids);
$this->assertTrue(in_array(context_module::instance($chat1b->cmid)->id, $contextids));
$this->assertTrue(in_array(context_module::instance($chat2a->cmid)->id, $contextids));
}
public function test_delete_data_for_all_users_in_context() {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$chat1a = $dg->create_module('chat', ['course' => $c1]);
$chat1b = $dg->create_module('chat', ['course' => $c1]);
$chat1actx = context_module::instance($chat1a->cmid);
$chat1bctx = context_module::instance($chat1b->cmid);
$u1chat1a = $this->login_user_in_course_chat($u1, $c1, $chat1a);
$u2chat1a = $this->login_user_in_course_chat($u2, $c1, $chat1a);
chat_send_chatmessage($u1chat1a, 'Ça va ?');
chat_send_chatmessage($u2chat1a, 'Oui, et toi ?');
chat_send_chatmessage($u1chat1a, 'Bien merci.');
chat_send_chatmessage($u2chat1a, 'Pourquoi ils disent omelette "du" fromage ?!');
chat_send_chatmessage($u1chat1a, 'Aucune idée');
$this->assert_has_data_in_chat($u1, $chat1a);
$this->assert_has_data_in_chat($u2, $chat1a);
$u1chat1b = $this->login_user_in_course_chat($u1, $c1, $chat1b);
$u2chat1b = $this->login_user_in_course_chat($u2, $c1, $chat1b);
chat_send_chatmessage($u1chat1b, 'How are you going?');
chat_send_chatmessage($u2chat1b, 'Alright, you?');
chat_send_chatmessage($u1chat1b, 'Good, thanks.');
chat_send_chatmessage($u2chat1b, 'Sacre bleu!');
chat_send_chatmessage($u1chat1b, '\ö/');
$this->assert_has_data_in_chat($u1, $chat1b);
$this->assert_has_data_in_chat($u2, $chat1b);
// No change.
provider::delete_data_for_all_users_in_context(context_course::instance($c1->id));
$this->assert_has_data_in_chat($u1, $chat1a);
$this->assert_has_data_in_chat($u2, $chat1a);
$this->assert_has_data_in_chat($u1, $chat1b);
$this->assert_has_data_in_chat($u2, $chat1b);
// Deletinge first chat does not affect other chat.
provider::delete_data_for_all_users_in_context($chat1actx);
$this->assert_has_no_data_in_chat($u1, $chat1a);
$this->assert_has_no_data_in_chat($u2, $chat1a);
$this->assert_has_data_in_chat($u1, $chat1b);
$this->assert_has_data_in_chat($u2, $chat1b);
}
public function test_delete_data_for_user() {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$chat1a = $dg->create_module('chat', ['course' => $c1]);
$chat1b = $dg->create_module('chat', ['course' => $c1]);
$chat1actx = context_module::instance($chat1a->cmid);
$chat1bctx = context_module::instance($chat1b->cmid);
$u1chat1a = $this->login_user_in_course_chat($u1, $c1, $chat1a);
$u2chat1a = $this->login_user_in_course_chat($u2, $c1, $chat1a);
chat_send_chatmessage($u1chat1a, 'Ça va ?');
chat_send_chatmessage($u2chat1a, 'Oui, et toi ?');
chat_send_chatmessage($u1chat1a, 'Bien merci.');
chat_send_chatmessage($u2chat1a, 'Pourquoi ils disent omelette "du" fromage ?!');
chat_send_chatmessage($u1chat1a, 'Aucune idée');
$this->assert_has_data_in_chat($u1, $chat1a);
$this->assert_has_data_in_chat($u2, $chat1a);
$u1chat1b = $this->login_user_in_course_chat($u1, $c1, $chat1b);
$u2chat1b = $this->login_user_in_course_chat($u2, $c1, $chat1b);
chat_send_chatmessage($u1chat1b, 'How are you going?');
chat_send_chatmessage($u2chat1b, 'Alright, you?');
chat_send_chatmessage($u1chat1b, 'Good, thanks.');
chat_send_chatmessage($u2chat1b, 'Sacre bleu!');
chat_send_chatmessage($u1chat1b, '\ö/');
$this->assert_has_data_in_chat($u1, $chat1b);
$this->assert_has_data_in_chat($u2, $chat1b);
provider::delete_data_for_user(new approved_contextlist($u1, 'mod_chat', [$chat1actx->id]));
$this->assert_has_no_data_in_chat($u1, $chat1a);
$this->assert_has_data_in_chat($u2, $chat1a);
$this->assert_has_data_in_chat($u1, $chat1b);
$this->assert_has_data_in_chat($u2, $chat1b);
provider::delete_data_for_user(new approved_contextlist($u2, 'mod_chat', [$chat1actx->id, $chat1bctx->id]));
$this->assert_has_no_data_in_chat($u1, $chat1a);
$this->assert_has_no_data_in_chat($u2, $chat1a);
$this->assert_has_data_in_chat($u1, $chat1b);
$this->assert_has_no_data_in_chat($u2, $chat1b);
}
public function test_export_data_for_user() {
global $DB;
$dg = $this->getDataGenerator();
$c1 = $dg->create_course();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$chat1a = $dg->create_module('chat', ['course' => $c1]);
$chat1b = $dg->create_module('chat', ['course' => $c1]);
$chat1actx = context_module::instance($chat1a->cmid);
$chat1bctx = context_module::instance($chat1b->cmid);
$u1chat1a = $this->login_user_in_course_chat($u1, $c1, $chat1a);
$u2chat1a = $this->login_user_in_course_chat($u2, $c1, $chat1a);
chat_send_chatmessage($u1chat1a, 'Ça va ?');
chat_send_chatmessage($u2chat1a, 'Oui, et toi ?');
chat_send_chatmessage($u1chat1a, 'Bien merci.');
chat_send_chatmessage($u2chat1a, 'Pourquoi ils disent omelette "du" fromage ?!');
chat_send_chatmessage($u1chat1a, 'Aucune idée');
chat_send_chatmessage($u1chat1a, 'exit', true);
$u1chat1b = $this->login_user_in_course_chat($u1, $c1, $chat1b);
$u2chat1b = $this->login_user_in_course_chat($u2, $c1, $chat1b);
chat_send_chatmessage($u1chat1b, 'How are you going?');
chat_send_chatmessage($u2chat1b, 'Alright, you?');
chat_send_chatmessage($u1chat1b, 'Good, thanks.');
chat_send_chatmessage($u2chat1b, 'Sacre bleu!');
chat_send_chatmessage($u1chat1b, '\ö/');
// Export for user 1 in chat 1.
provider::export_user_data(new approved_contextlist($u1, 'mod_chat', [$chat1actx->id]));
$data = writer::with_context($chat1actx)->get_data([]);
$this->assertNotEmpty($data);
$this->assertCount(5, $data->messages);
$this->assertEquals(get_string('messageenter', 'mod_chat', fullname($u1)), $data->messages[0]['message']);
$this->assertEquals(transform::yesno(true), $data->messages[0]['is_system_generated']);
$this->assertEquals('Ça va ?', $data->messages[1]['message']);
$this->assertEquals(transform::yesno(false), $data->messages[1]['is_system_generated']);
$this->assertEquals('Bien merci.', $data->messages[2]['message']);
$this->assertEquals(transform::yesno(false), $data->messages[2]['is_system_generated']);
$this->assertEquals('Aucune idée', $data->messages[3]['message']);
$this->assertEquals(transform::yesno(false), $data->messages[3]['is_system_generated']);
$this->assertEquals(get_string('messageexit', 'mod_chat', fullname($u1)), $data->messages[4]['message']);
$this->assertEquals(transform::yesno(true), $data->messages[4]['is_system_generated']);
$data = writer::with_context($chat1bctx)->get_data([]);
$this->assertEmpty($data);
// Export for user2 in chat 1 and 2.
writer::reset();
provider::export_user_data(new approved_contextlist($u2, 'mod_chat', [$chat1actx->id, $chat1bctx->id]));
$data = writer::with_context($chat1actx)->get_data([]);
$this->assertNotEmpty($data);
$this->assertCount(3, $data->messages);
$this->assertEquals(get_string('messageenter', 'mod_chat', fullname($u2)), $data->messages[0]['message']);
$this->assertEquals('Oui, et toi ?', $data->messages[1]['message']);
$this->assertEquals('Pourquoi ils disent omelette "du" fromage ?!', $data->messages[2]['message']);
$data = writer::with_context($chat1bctx)->get_data([]);
$this->assertNotEmpty($data);
$this->assertCount(3, $data->messages);
$this->assertEquals(get_string('messageenter', 'mod_chat', fullname($u2)), $data->messages[0]['message']);
$this->assertEquals('Alright, you?', $data->messages[1]['message']);
$this->assertEquals('Sacre bleu!', $data->messages[2]['message']);
}
/**
* Assert that there is data for a user in a chat.
*
* @param object $user The user.
* @param object $chat The chat.
* @return void
*/
protected function assert_has_data_in_chat($user, $chat) {
$this->assertTrue($this->has_data_in_chat($user, $chat));
}
/**
* Assert that there isn't any data for a user in a chat.
*
* @param object $user The user.
* @param object $chat The chat.
* @return void
*/
protected function assert_has_no_data_in_chat($user, $chat) {
$this->assertFalse($this->has_data_in_chat($user, $chat));
}
/**
* Check whether a user has data in a chat.
*
* @param object $user The user.
* @param object $chat The chat.
* @return bool
*/
protected function has_data_in_chat($user, $chat) {
global $DB;
return $DB->record_exists('chat_messages', ['chatid' => $chat->id, 'userid' => $user->id]);
}
/**
* Login a user in a chat.
*
* @param object $user The user.
* @param object $course The course.
* @param object $chat The chat.
* @param int $group The group number.
* @param bool $silent Whether we should advertise that the user logs in.
* @return object The chat user.
*/
protected function login_user_in_course_chat($user, $course, $chat, $group = 0, $silent = false) {
global $DB, $USER;
$origuser = $USER;
$this->setUser($user);
chat_login_user($chat->id, $silent ? 'sockets' : 'basic', 0, $course);
$chatuser = $DB->get_record('chat_users', ['userid' => $user->id, 'chatid' => $chat->id, 'groupid' => 0]);
$this->setUser($origuser);
return $chatuser;
}
}