diff --git a/mod/forum/backup/moodle2/restore_forum_stepslib.php b/mod/forum/backup/moodle2/restore_forum_stepslib.php index 855e25c2c6f..1c344a3ded5 100644 --- a/mod/forum/backup/moodle2/restore_forum_stepslib.php +++ b/mod/forum/backup/moodle2/restore_forum_stepslib.php @@ -116,6 +116,7 @@ class restore_forum_activity_structure_step extends restore_activity_structure_s $data->parent = $this->get_mappingid('forum_post', $data->parent); } + $data = \mod_forum\local\entities\post::add_message_counts($data); $newitemid = $DB->insert_record('forum_posts', $data); $this->set_mapping('forum_post', $oldid, $newitemid, true); diff --git a/mod/forum/classes/local/data_mappers/legacy/post.php b/mod/forum/classes/local/data_mappers/legacy/post.php index fcced5f71a7..09742c1ed5c 100644 --- a/mod/forum/classes/local/data_mappers/legacy/post.php +++ b/mod/forum/classes/local/data_mappers/legacy/post.php @@ -61,6 +61,8 @@ class post { 'mailnow' => $post->should_mail_now(), 'deleted' => $post->is_deleted(), 'privatereplyto' => $post->get_private_reply_recipient_id(), + 'wordcount' => $post->get_wordcount(), + 'charcount' => $post->get_charcount(), ]; }, $posts); } diff --git a/mod/forum/classes/local/entities/post.php b/mod/forum/classes/local/entities/post.php index f84053bd4ea..accf789dd8b 100644 --- a/mod/forum/classes/local/entities/post.php +++ b/mod/forum/classes/local/entities/post.php @@ -67,6 +67,10 @@ class post { private $deleted; /** @var int $privatereplyto The user being privately replied to */ private $privatereplyto; + /** @var int $wordcount Number of words in the message */ + private $wordcount; + /** @var int $charcount Number of chars in the message */ + private $charcount; /** * Constructor. @@ -104,7 +108,9 @@ class post { int $totalscore, bool $mailnow, bool $deleted, - int $privatereplyto + int $privatereplyto, + ?int $wordcount, + ?int $charcount ) { $this->id = $id; $this->discussionid = $discussionid; @@ -122,6 +128,8 @@ class post { $this->mailnow = $mailnow; $this->deleted = $deleted; $this->privatereplyto = $privatereplyto; + $this->wordcount = $wordcount; + $this->charcount = $charcount; } /** @@ -315,4 +323,36 @@ class post { public function is_private_reply_intended_for_user(stdClass $user) : bool { return $this->get_private_reply_recipient_id() == $user->id; } + + /** + * Returns the word count. + * + * @return int|null + */ + public function get_wordcount() : ?int { + return $this->wordcount; + } + + /** + * Returns the char count. + * + * @return int|null + */ + public function get_charcount() : ?int { + return $this->charcount; + } + + /** + * This methods adds/updates forum posts' word count and char count attributes based on $data->message. + * + * @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 { + if (!empty($record->message)) { + $record->wordcount = count_words($record->message); + $record->charcount = count_letters($record->message); + } + return $record; + } } diff --git a/mod/forum/classes/local/exporters/post.php b/mod/forum/classes/local/exporters/post.php index f854e4613f3..c10749dae50 100644 --- a/mod/forum/classes/local/exporters/post.php +++ b/mod/forum/classes/local/exporters/post.php @@ -120,6 +120,12 @@ class post extends exporter { 'default' => null, 'null' => NULL_ALLOWED ], + 'charcount' => [ + 'type' => PARAM_INT, + 'optional' => true, + 'default' => null, + 'null' => NULL_ALLOWED + ], 'capabilities' => [ 'type' => [ 'view' => [ @@ -410,6 +416,11 @@ class post extends exporter { $replysubject = "{$strre} {$replysubject}"; } + $showwordcount = $forum->should_display_word_count(); + if ($showwordcount) { + $wordcount = $post->get_wordcount() ?? count_words($message); + } + return [ 'id' => $post->get_id(), 'subject' => $subject, @@ -424,8 +435,9 @@ class post extends exporter { 'unread' => ($loadcontent && $readreceiptcollection) ? !$readreceiptcollection->has_user_read_post($user, $post) : null, 'isdeleted' => $isdeleted, 'isprivatereply' => $isprivatereply, - 'haswordcount' => $forum->should_display_word_count(), - 'wordcount' => $forum->should_display_word_count() ? count_words($message) : null, + 'haswordcount' => $showwordcount, + 'wordcount' => $showwordcount ? $wordcount : null, + 'charcount' => $post->get_charcount(), 'capabilities' => [ 'view' => $canview, 'edit' => $canedit, diff --git a/mod/forum/classes/local/factories/entity.php b/mod/forum/classes/local/factories/entity.php index e40e59634fe..8406b99aac7 100644 --- a/mod/forum/classes/local/factories/entity.php +++ b/mod/forum/classes/local/factories/entity.php @@ -154,7 +154,9 @@ class entity { $record->totalscore, $record->mailnow, $record->deleted, - $record->privatereplyto + $record->privatereplyto, + $record->wordcount, + $record->charcount ); } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index db0eb65cfb0..d7d62bfd824 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -2948,6 +2948,7 @@ function forum_add_new_post($post, $mform, $unused = null) { $post->mailnow = 0; } + $post = \mod_forum\local\entities\post::add_message_counts($post); $post->id = $DB->insert_record("forum_posts", $post); $post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, mod_forum_post_form::editor_options($context, null), $post->message); @@ -3018,6 +3019,7 @@ function forum_update_post($newpost, $mform, $unused = null) { } $post->message = file_save_draft_area_files($newpost->itemid, $context->id, 'mod_forum', 'post', $post->id, mod_forum_post_form::editor_options($context, $post->id), $post->message); + $post = \mod_forum\local\entities\post::add_message_counts($post); $DB->update_record('forum_posts', $post); // Note: Discussion modified time/user are intentionally not updated, to enable them to track the latest new post. $DB->update_record('forum_discussions', $discussion); @@ -3080,6 +3082,7 @@ function forum_add_discussion($discussion, $mform=null, $unused=null, $userid=nu $post->course = $forum->course; // speedup $post->mailnow = $discussion->mailnow; + $post = \mod_forum\local\entities\post::add_message_counts($post); $post->id = $DB->insert_record("forum_posts", $post); // TODO: Fix the calling code so that there always is a $cm when this function is called diff --git a/mod/forum/tests/entities_discussion_summary_test.php b/mod/forum/tests/entities_discussion_summary_test.php index 5475d5251db..adc051ae9c4 100644 --- a/mod/forum/tests/entities_discussion_summary_test.php +++ b/mod/forum/tests/entities_discussion_summary_test.php @@ -93,7 +93,9 @@ class mod_forum_entities_discussion_summary_testcase extends advanced_testcase { 0, false, false, - false + false, + null, + null ); $discussionsummary = new discussion_summary_entity($discussion, $firstpost, $firstauthor, $lastauthor); diff --git a/mod/forum/tests/entities_discussion_test.php b/mod/forum/tests/entities_discussion_test.php index c3bb6f8f674..b4779010fc5 100644 --- a/mod/forum/tests/entities_discussion_test.php +++ b/mod/forum/tests/entities_discussion_test.php @@ -75,7 +75,9 @@ class mod_forum_entities_discussion_testcase extends advanced_testcase { 0, false, false, - false + false, + null, + null ); $notfirstpost = new post_entity( 1, @@ -93,7 +95,9 @@ class mod_forum_entities_discussion_testcase extends advanced_testcase { 0, false, false, - false + false, + null, + null ); $this->assertEquals(1, $discussion->get_id()); diff --git a/mod/forum/tests/entities_post_read_receipt_collection_test.php b/mod/forum/tests/entities_post_read_receipt_collection_test.php index 567d66a793f..f1dc18927ee 100644 --- a/mod/forum/tests/entities_post_read_receipt_collection_test.php +++ b/mod/forum/tests/entities_post_read_receipt_collection_test.php @@ -58,7 +58,9 @@ class mod_forum_entities_post_read_receipt_collection_testcase extends advanced_ 0, false, false, - false + false, + null, + null ); $post = new post_entity( 1, @@ -76,7 +78,9 @@ class mod_forum_entities_post_read_receipt_collection_testcase extends advanced_ 0, false, false, - false + false, + null, + null ); $collection = new collection_entity([ (object) [ diff --git a/mod/forum/tests/entities_post_test.php b/mod/forum/tests/entities_post_test.php index 62bdd8bc565..01ecf6ee4ea 100644 --- a/mod/forum/tests/entities_post_test.php +++ b/mod/forum/tests/entities_post_test.php @@ -59,7 +59,9 @@ class mod_forum_entities_post_testcase extends advanced_testcase { 0, false, false, - false + false, + null, + null ); $this->assertEquals(4, $post->get_id()); diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 84428a5a590..b4ea6be0d59 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -689,6 +689,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'isprivatereply' => false, 'haswordcount' => false, 'wordcount' => null, + 'charcount' => 17, 'author'=> $exporteduser3, 'attachments' => [], 'tags' => [], @@ -744,6 +745,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'isprivatereply' => false, 'haswordcount' => false, 'wordcount' => null, + 'charcount' => 17, 'author'=> $exporteduser2, 'attachments' => [], 'tags' => [], diff --git a/mod/forum/tests/generator/lib.php b/mod/forum/tests/generator/lib.php index d8a9a7cb2c9..3705db81d86 100644 --- a/mod/forum/tests/generator/lib.php +++ b/mod/forum/tests/generator/lib.php @@ -313,6 +313,7 @@ class mod_forum_generator extends testing_module_generator { } $record = (object) $record; + $record = \mod_forum\local\entities\post::add_message_counts($record); // Add the post. $record->id = $DB->insert_record('forum_posts', $record);