mirror of
https://github.com/moodle/moodle.git
synced 2025-03-19 23:20:09 +01:00
MDL-66694 mod_forum: Update word & char counts on db save
This commit is contained in:
parent
68f2d677ae
commit
591dd68790
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -154,7 +154,9 @@ class entity {
|
||||
$record->totalscore,
|
||||
$record->mailnow,
|
||||
$record->deleted,
|
||||
$record->privatereplyto
|
||||
$record->privatereplyto,
|
||||
$record->wordcount,
|
||||
$record->charcount
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
|
@ -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) [
|
||||
|
@ -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());
|
||||
|
@ -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' => [],
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user