Skip to content

Commit

Permalink
MDL-61937 comment: do not use API in provider
Browse files Browse the repository at this point in the history
When exporting comments we can not use comments API because:
- we don't need to call component validate, comments may be disabled right now but
  are still stored
- we don't need to call display callback
- we need to export all comments and not only first 15 of them
  • Loading branch information
marinaglancy committed May 4, 2018
1 parent a7a6be9 commit 70703d4
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 27 deletions.
83 changes: 57 additions & 26 deletions comment/classes/privacy/provider.php
Expand Up @@ -29,8 +29,6 @@
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\transform;

require_once($CFG->dirroot . '/comment/lib.php');

/**
* Privacy class for requesting user data.
*
Expand Down Expand Up @@ -67,33 +65,46 @@ public static function get_metadata(collection $collection) : collection {
* @param bool $onlyforthisuser Only return the comments this user made.
*/
public static function export_comments(\context $context, string $component, string $commentarea, int $itemid,
array $subcontext, bool $onlyforthisuser = true) {
$data = new \stdClass;
$data->context = $context;
$data->area = $commentarea;
$data->itemid = $itemid;
$data->component = $component;

$commentobject = new \comment($data);
$commentobject->set_view_permission(true);
$comments = $commentobject->get_comments(0);
$subcontext[] = get_string('commentsubcontext', 'core_comment');

$comments = array_filter($comments, function($comment) use ($onlyforthisuser) {
global $USER;

return (!$onlyforthisuser || $comment->userid == $USER->id);
});

$comments = array_map(function($comment) {
return (object) [
'content' => $comment->content,
'time' => transform::datetime($comment->timecreated),
'userid' => transform::user($comment->userid),
array $subcontext, bool $onlyforthisuser = true) {
global $USER, $DB;
$params = [
'contextid' => $context->id,
'component' => $component,
'commentarea' => $commentarea,
'itemid' => $itemid
];
$sql = "SELECT c.id, c.content, c.format, c.timecreated, c.userid
FROM {comments} c
WHERE c.contextid = :contextid AND
c.commentarea = :commentarea AND
c.itemid = :itemid AND
(c.component IS NULL OR c.component = :component)";
if ($onlyforthisuser) {
$sql .= " AND c.userid = :userid";
$params['userid'] = $USER->id;
}
$sql .= " ORDER BY c.timecreated DESC";

$rs = $DB->get_recordset_sql($sql, $params);
$comments = [];
foreach ($rs as $record) {
if ($record->userid != $USER->id) {
// Clean HTML in comments that were added by other users.
$comment = ['content' => format_text($record->content, $record->format, ['context' => $context])];
} else {
// Export comments made by this user as they are stored.
$comment = ['content' => $record->content, 'contentformat' => $record->format];
}
$comment += [
'time' => transform::datetime($record->timecreated),
'userid' => transform::user($record->userid),
];
}, $comments);
$comments[] = (object)$comment;
}
$rs->close();

if (!empty($comments)) {
$subcontext[] = get_string('commentsubcontext', 'core_comment');
\core_privacy\local\request\writer::with_context($context)
->export_data($subcontext, (object) [
'comments' => $comments,
Expand Down Expand Up @@ -125,6 +136,26 @@ public static function delete_comments_for_all_users(\context $context, string $
$DB->delete_records('comments', $params);
}

/**
* Deletes all comments for a specified context, component, and commentarea.
*
* @param \context $context Details about which context to delete comments for.
* @param string $component Component to delete.
* @param string $commentarea Comment area to delete.
* @param string $itemidstest an SQL fragment that the itemid must match. Used
* in the query like WHERE itemid $itemidstest. Must use named parameters,
* and may not use named parameters called contextid, component or commentarea.
* @param array $params any query params used by $itemidstest.
*/
public static function delete_comments_for_all_users_select(\context $context, string $component, string $commentarea,
$itemidstest, $params = []) {
global $DB;
$params += ['contextid' => $context->id, 'component' => $component, 'commentarea' => $commentarea];
$DB->delete_records_select('comments',
'contextid = :contextid AND component = :component AND commentarea = :commentarea AND itemid ' . $itemidstest,
$params);
}

/**
* Deletes all records for a user from a list of approved contexts.
*
Expand Down
72 changes: 71 additions & 1 deletion comment/tests/privacy_test.php
Expand Up @@ -160,6 +160,76 @@ public function test_delete_comments_for_all_users() {
$this->assertEquals('tool_dataprivacy', $data->component);
}

/**
* Tests the deletion of all comments in a context.
*/
public function test_delete_comments_for_all_users_select() {
global $DB;

$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();

$coursecontext1 = context_course::instance($course1->id);
$coursecontext2 = context_course::instance($course2->id);

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();

$comment1 = $this->get_comment_object($coursecontext1, $course1);
$comment2 = $this->get_comment_object($coursecontext2, $course2);

$this->setUser($user1);
$comment1->add('First comment for user 1 on comment 1');
$comment2->add('First comment for user 1 on comment 2');
$this->setUser($user2);
$comment1->add('First comment for user 2 on comment 1');
$comment2->add('First comment for user 2 on comment 2');

// Because of the way things are set up with validation, creating an entry with the same context in a different component
// or comment area is a huge pain. We're just going to jam entries into the table instead.
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'block_comments',
'commentarea' => 'other_comments',
'itemid' => 2,
'content' => 'Comment user 1 different comment area',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'tool_dataprivacy',
'commentarea' => 'page_comments',
'itemid' => 2,
'content' => 'Comment user 1 different component',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);

// Delete only for the first context. All records in the comments table for this context should be removed.
list($sql, $params) = $DB->get_in_or_equal([0, 1, 2, 3], SQL_PARAMS_NAMED);
\core_comment\privacy\provider::delete_comments_for_all_users_select($coursecontext1,
'block_comments', 'page_comments', $sql, $params);
// No records left here.
$this->assertCount(0, $comment1->get_comments());
// All of the records are left intact here.
$this->assertCount(2, $comment2->get_comments());
// Check the other comment area.
$result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('other_comments', $data->commentarea);
// Check the different component, same commentarea.
$result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('tool_dataprivacy', $data->component);
}

/**
* Tests deletion of comments for a specified user and contexts.
*/
Expand Down Expand Up @@ -229,7 +299,7 @@ public function test_delete_comments_for_user() {
// Nothing changed here as user 1 did not leave a comment.
$comment3comments = $comment3->get_comments();
$this->assertCount(1, $comment3comments);
$data = array_shift(($comment3comments));
$data = array_shift($comment3comments);
$this->assertEquals($user2->id, $data->userid);
// Check the other comment area.
$result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
Expand Down

0 comments on commit 70703d4

Please sign in to comment.