Skip to content

Commit

Permalink
MDL-64821 mod_forum: don't show name of deleted user
Browse files Browse the repository at this point in the history
When a user account is deleted don't render the user's name in
the forum. Instead we render "Deleted user".
  • Loading branch information
ryanwyllie committed Sep 25, 2019
1 parent b97622a commit 23e0cec
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 46 deletions.
1 change: 1 addition & 0 deletions mod/forum/classes/local/data_mappers/legacy/author.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function to_legacy_objects(array $authors) : array {
'lastname' => $author->get_last_name(),
'fullname' => $author->get_full_name(),
'email' => $author->get_email(),
'deleted' => $author->is_deleted(),
'middlename' => $author->get_middle_name(),
'firstnamephonetic' => $author->get_first_name_phonetic(),
'lastnamephonetic' => $author->get_last_name_phonetic(),
Expand Down
13 changes: 13 additions & 0 deletions mod/forum/classes/local/entities/author.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class author {
private $fullname;
/** @var string $email Email */
private $email;
/** @var bool $deleted Deleted */
private $deleted;
/** @var string $middlename Middle name */
private $middlename;
/** @var string $firstnamephonetic Phonetic spelling of first name */
Expand Down Expand Up @@ -78,6 +80,7 @@ public function __construct(
string $lastname,
string $fullname,
string $email,
bool $deleted,
string $middlename = null,
string $firstnamephonetic = null,
string $lastnamephonetic = null,
Expand All @@ -90,6 +93,7 @@ public function __construct(
$this->lastname = $lastname;
$this->fullname = $fullname;
$this->email = $email;
$this->deleted = $deleted;
$this->middlename = $middlename;
$this->firstnamephonetic = $firstnamephonetic;
$this->lastnamephonetic = $lastnamephonetic;
Expand Down Expand Up @@ -151,6 +155,15 @@ public function get_email() : string {
return $this->email;
}

/**
* Is the author deleted?
*
* @return bool
*/
public function is_deleted() : bool {
return !empty($this->deleted);
}

/**
* Return the middle name.
*
Expand Down
71 changes: 46 additions & 25 deletions mod/forum/classes/local/exporters/author.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ protected static function define_other_properties() {
'default' => null,
'null' => NULL_ALLOWED
],
'isdeleted' => [
'type' => PARAM_BOOL,
'optional' => true,
'default' => null,
'null' => NULL_ALLOWED
],
'groups' => [
'multiple' => true,
'optional' => true,
Expand Down Expand Up @@ -143,41 +149,56 @@ protected function get_other_values(renderer_base $output) {
$context = $this->related['context'];

if ($this->canview) {
$groups = array_map(function($group) use ($urlfactory, $context) {
$imageurl = null;
$groupurl = null;
if (!$group->hidepicture) {
$imageurl = get_group_picture_url($group, $group->courseid, true);
}
if (course_can_view_participants($context)) {
$groupurl = $urlfactory->get_author_group_url($group);
}

if ($author->is_deleted()) {
return [
'id' => $group->id,
'name' => $group->name,
'id' => $author->get_id(),
'fullname' => get_string('deleteduser', 'mod_forum'),
'isdeleted' => true,
'groups' => [],
'urls' => [
'image' => $imageurl ? $imageurl->out(false) : null,
'group' => $groupurl ? $groupurl->out(false) : null

'profile' => ($urlfactory->get_author_profile_url($author))->out(false),
'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
]
];
}, $this->authorgroups);
} else {
$groups = array_map(function($group) use ($urlfactory, $context) {
$imageurl = null;
$groupurl = null;
if (!$group->hidepicture) {
$imageurl = get_group_picture_url($group, $group->courseid, true);
}
if (course_can_view_participants($context)) {
$groupurl = $urlfactory->get_author_group_url($group);
}

return [
'id' => $author->get_id(),
'fullname' => $author->get_full_name(),
'groups' => $groups,
'urls' => [
'profile' => ($urlfactory->get_author_profile_url($author))->out(false),
'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
]
];
return [
'id' => $group->id,
'name' => $group->name,
'urls' => [
'image' => $imageurl ? $imageurl->out(false) : null,
'group' => $groupurl ? $groupurl->out(false) : null

]
];
}, $this->authorgroups);

return [
'id' => $author->get_id(),
'fullname' => $author->get_full_name(),
'isdeleted' => false,
'groups' => $groups,
'urls' => [
'profile' => ($urlfactory->get_author_profile_url($author))->out(false),
'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
]
];
}
} else {
// The author should be anonymised.
return [
'id' => null,
'fullname' => get_string('forumauthorhidden', 'mod_forum'),
'isdeleted' => null,
'groups' => [],
'urls' => [
'profile' => null,
Expand Down
6 changes: 1 addition & 5 deletions mod/forum/classes/local/exporters/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ protected function get_other_values(renderer_base $output) {
$author,
$authorcontextid,
$authorgroups,
($canview && !$isdeleted),
$canview,
$this->related
);
$exportedauthor = $authorexporter->export($output);
Expand All @@ -402,10 +402,6 @@ protected function get_other_values(renderer_base $output) {
$subject = $isdeleted ? get_string('forumsubjectdeleted', 'forum') : get_string('forumsubjecthidden', 'forum');
$message = $isdeleted ? get_string('forumbodydeleted', 'forum') : get_string('forumbodyhidden', 'forum');
$timecreated = null;

if ($isdeleted) {
$exportedauthor->fullname = null;
}
}

$replysubject = $subject;
Expand Down
1 change: 1 addition & 0 deletions mod/forum/classes/local/factories/entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public function get_author_from_stdclass(stdClass $record) : author_entity {
$record->lastname,
fullname($record),
$record->email,
$record->deleted,
$record->middlename,
$record->firstnamephonetic,
$record->lastnamephonetic,
Expand Down
4 changes: 2 additions & 2 deletions mod/forum/classes/local/vaults/discussion_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ protected function generate_get_records_sql(string $wheresql = null, ?string $so
// - Most recent editor.
$thistable = new dml_table(self::TABLE, $alias, $alias);
$posttable = new dml_table('forum_posts', 'fp', 'p_');
$firstauthorfields = \user_picture::fields('fa', null, self::FIRST_AUTHOR_ID_ALIAS, self::FIRST_AUTHOR_ALIAS);
$latestuserfields = \user_picture::fields('la', null, self::LATEST_AUTHOR_ID_ALIAS, self::LATEST_AUTHOR_ALIAS);
$firstauthorfields = \user_picture::fields('fa', ['deleted'], self::FIRST_AUTHOR_ID_ALIAS, self::FIRST_AUTHOR_ALIAS);
$latestuserfields = \user_picture::fields('la', ['deleted'], self::LATEST_AUTHOR_ID_ALIAS, self::LATEST_AUTHOR_ALIAS);

$fields = implode(', ', [
$thistable->get_field_select(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function execute(array $records) : array {
$alias = $this->alias;

return array_map(function($record) use ($idalias, $alias) {
return user_picture::unalias($record, null, $idalias, $alias);
return user_picture::unalias($record, ['deleted'], $idalias, $alias);
}, $records);
}
}
1 change: 1 addition & 0 deletions mod/forum/lang/en/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
$string['deleteddiscussion'] = 'The discussion topic has been deleted';
$string['deletedpost'] = 'The post has been deleted';
$string['deletedposts'] = 'Those posts have been deleted';
$string['deleteduser'] = 'Deleted user';
$string['deletesure'] = 'Are you sure you want to delete this post?';
$string['deletesureplural'] = 'Are you sure you want to delete this post and all replies? ({$a} posts)';
$string['digestmailheader'] = 'This is your daily digest of new posts from the {$a->sitename} forums. To change your default forum email preferences, go to {$a->userprefs}.';
Expand Down
14 changes: 7 additions & 7 deletions mod/forum/templates/forum_discussion_modern_first_post.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@
{{/readonly}}
</div>
</div>
{{$subject}}
<h2
class="h1 font-weight-bold post-subject mt-n2"
data-region-content="forum-post-core-subject"
data-reply-subject="{{replysubject}}"
>{{{subject}}}</h2>
{{/subject}}
{{/isdeleted}}
{{$subject}}
<h2
class="h1 font-weight-bold post-subject mt-n2"
data-region-content="forum-post-core-subject"
data-reply-subject="{{replysubject}}"
>{{{subject}}}</h2>
{{/subject}}
{{#hasreplycount}}
<span class="sr-only">{{#str}} numberofreplies, mod_forum, {{replycount}} {{/str}}</span>
{{/hasreplycount}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
}}
{{< mod_forum/forum_discussion_modern_first_post }}
{{$subject}}
<h3 class="sr-only" data-region-content="forum-post-core-subject">{{{subject}}}</h3>
<h3
{{#isdeleted}}class="h6 font-weight-bold"{{/isdeleted}}
{{^isdeleted}}class="sr-only"{{/isdeleted}}
data-region-content="forum-post-core-subject"
>{{{subject}}}</h3>
{{/subject}}
{{$footer}}
{{^isdeleted}}
Expand Down
2 changes: 2 additions & 0 deletions mod/forum/tests/entities_author_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function test_entity() {
'person',
'test person',
'[email protected]',
false,
'middle',
'tteeeeest',
'ppppeeerssson',
Expand All @@ -60,6 +61,7 @@ public function test_entity() {
$this->assertEquals('person', $author->get_last_name());
$this->assertEquals('test person', $author->get_full_name());
$this->assertEquals('[email protected]', $author->get_email());
$this->assertEquals(false, $author->is_deleted());
$this->assertEquals('middle', $author->get_middle_name());
$this->assertEquals('tteeeeest', $author->get_first_name_phonetic());
$this->assertEquals('ppppeeerssson', $author->get_last_name_phonetic());
Expand Down
6 changes: 4 additions & 2 deletions mod/forum/tests/entities_discussion_summary_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ public function test_entity() {
'test',
'person',
'test person',
'[email protected]'
'[email protected]',
false
);
$lastauthor = new author_entity(
2,
3,
'test 2',
'person 2',
'test 2 person 2',
'[email protected]'
'[email protected]',
false
);
$discussion = new discussion_entity(
1,
Expand Down
9 changes: 6 additions & 3 deletions mod/forum/tests/exporters_author_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function test_export_author() {
'test',
'user',
'test user',
'[email protected]'
'[email protected]',
false
);

$exporter = new author_exporter($author, 1, [], true, [
Expand Down Expand Up @@ -95,7 +96,8 @@ public function test_export_author_with_groups() {
'test',
'user',
'test user',
'[email protected]'
'[email protected]',
false
);

$group = $datagenerator->create_group(['courseid' => $course->id]);
Expand Down Expand Up @@ -132,7 +134,8 @@ public function test_export_author_no_view_capability() {
'test',
'user',
'test user',
'[email protected]'
'[email protected]',
false
);

$group = $datagenerator->create_group(['courseid' => $course->id]);
Expand Down
12 changes: 12 additions & 0 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ public function test_mod_forum_get_discussion_posts() {
$exporteduser2 = [
'id' => (int) $user2->id,
'fullname' => fullname($user2),
'isdeleted' => false,
'groups' => [],
'urls' => [
'profile' => $urlfactory->get_author_profile_url($user2entity),
Expand All @@ -562,6 +563,7 @@ public function test_mod_forum_get_discussion_posts() {
'id' => (int) $user3->id,
'fullname' => fullname($user3),
'groups' => [],
'isdeleted' => false,
'urls' => [
'profile' => $urlfactory->get_author_profile_url($user3entity),
'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
Expand Down Expand Up @@ -642,6 +644,16 @@ public function test_mod_forum_get_discussion_posts() {

// Delete one user, to test that we still receive posts by this user.
delete_user($user3);
$exporteduser3 = [
'id' => (int) $user3->id,
'fullname' => get_string('deleteduser', 'mod_forum'),
'groups' => [],
'isdeleted' => true,
'urls' => [
'profile' => $urlfactory->get_author_profile_url($user3entity),
'profileimage' => $urlfactory->get_author_profile_image_url($user3entity),
]
];

// Create what we expect to be returned when querying the discussion.
$expectedposts = array(
Expand Down

0 comments on commit 23e0cec

Please sign in to comment.