Skip to content

Commit

Permalink
MDL-66694 mod_forum: Word & char count update consistency fixes
Browse files Browse the repository at this point in the history
Charcount logic is now consistent with wordcount in the post exporter.
Counts update now called in forum_udpate_instance since it is updating
the data.
  • Loading branch information
mickhawkins committed Oct 17, 2019
1 parent 591dd68 commit 3eb0a82
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mod/forum/classes/local/entities/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public function get_charcount() : ?int {
* @param \stdClass $record A record ready to be inserted / updated in DB.
* @return \stdClass The same record with 'wordcount' and 'charcount' attributes.
*/
public static function add_message_counts(\stdClass $record): \stdClass {
public static function add_message_counts(\stdClass $record) : \stdClass {
if (!empty($record->message)) {
$record->wordcount = count_words($record->message);
$record->charcount = count_letters($record->message);
Expand Down
8 changes: 6 additions & 2 deletions mod/forum/classes/local/exporters/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ protected function get_other_values(renderer_base $output) {
$showwordcount = $forum->should_display_word_count();
if ($showwordcount) {
$wordcount = $post->get_wordcount() ?? count_words($message);
$charcount = $post->get_charcount() ?? count_letters($message);
} else {
$wordcount = null;
$charcount = null;
}

return [
Expand All @@ -436,8 +440,8 @@ protected function get_other_values(renderer_base $output) {
'isdeleted' => $isdeleted,
'isprivatereply' => $isprivatereply,
'haswordcount' => $showwordcount,
'wordcount' => $showwordcount ? $wordcount : null,
'charcount' => $post->get_charcount(),
'wordcount' => $wordcount,
'charcount' => $charcount,
'capabilities' => [
'view' => $canview,
'edit' => $canedit,
Expand Down
1 change: 1 addition & 0 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ function forum_update_instance($forum, $mform) {
$post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id, $options, $post->message);
}

\mod_forum\local\entities\post::add_message_counts($post);
$DB->update_record('forum_posts', $post);
$discussion->name = $forum->name;
$DB->update_record('forum_discussions', $discussion);
Expand Down

0 comments on commit 3eb0a82

Please sign in to comment.