|
31 | 31 | use mod_moodleoverflow\review; |
32 | 32 | use mod_moodleoverflow\readtracking; |
33 | 33 | use mod_moodleoverflow\discussion\discussion; |
| 34 | +use mod_moodleoverflow_post_form; |
34 | 35 | use moodle_exception; |
35 | 36 |
|
36 | 37 | defined('MOODLE_INTERNAL') || die(); |
@@ -103,8 +104,8 @@ class post { |
103 | 104 | /** @var string The subject/title of the Discussion */ |
104 | 105 | public $subject; |
105 | 106 |
|
106 | | - /** @var object The discussion where the post is located */ |
107 | | - public $discussionobject; |
| 107 | + /** @var discussion The discussion where the post is located */ |
| 108 | + public discussion $discussionobject; |
108 | 109 |
|
109 | 110 | /** @var object The Moodleoverflow where the post is located*/ |
110 | 111 | public $moodleoverflowobject; |
@@ -154,9 +155,9 @@ public function __construct($id, $discussion, $parent, $userid, $created, $modif |
154 | 155 | * Builds a Post from a DB record. |
155 | 156 | * Look up database structure for standard values. |
156 | 157 | * @param object $record Data object. |
157 | | - * @return object post instance |
| 158 | + * @return post post instance |
158 | 159 | */ |
159 | | - public static function from_record($record) { |
| 160 | + public static function from_record($record): post { |
160 | 161 | $id = null; |
161 | 162 | if (object_property_exists($record, 'id') && $record->id) { |
162 | 163 | $id = $record->id; |
@@ -259,6 +260,15 @@ public function moodleoverflow_add_new_post() { |
259 | 260 |
|
260 | 261 | // Add post to the database. |
261 | 262 | $this->id = $DB->insert_record('moodleoverflow_posts', $this->build_db_object()); |
| 263 | + |
| 264 | + // Save draft files to permanent file area. |
| 265 | + $context = \context_module::instance($this->get_coursemodule()->id); |
| 266 | + $draftid = file_get_submitted_draft_itemid('introeditor'); |
| 267 | + $this->message = file_save_draft_area_files($draftid, $context->id, 'mod_moodleoverflow', 'post', |
| 268 | + $this->id, mod_moodleoverflow_post_form::editor_options($context, $this->id), $this->message); |
| 269 | + $DB->update_record('moodleoverflow_posts', $this->build_db_object()); |
| 270 | + |
| 271 | + // Update the attachments. This happens after the DB update call, as this function changes the DB record as well. |
262 | 272 | $this->moodleoverflow_add_attachment(); |
263 | 273 |
|
264 | 274 | if ($this->reviewed) { |
@@ -375,10 +385,15 @@ public function moodleoverflow_edit_post($time, $postmessage, $messageformat, $f |
375 | 385 |
|
376 | 386 | // Update the attributes. |
377 | 387 | $this->modified = $time; |
378 | | - $this->message = $postmessage; |
379 | 388 | $this->messageformat = $messageformat; |
380 | 389 | $this->formattachments = $formattachments; |
381 | 390 |
|
| 391 | + // Update the message and save draft files to permanent file area. |
| 392 | + $context = \context_module::instance($this->get_coursemodule()->id); |
| 393 | + $draftid = file_get_submitted_draft_itemid('introeditor'); |
| 394 | + $this->message = file_save_draft_area_files($draftid, $context->id, 'mod_moodleoverflow', 'post', |
| 395 | + $this->id, mod_moodleoverflow_post_form::editor_options($context, $this->id), $postmessage); |
| 396 | + |
382 | 397 | // Update the record in the database. |
383 | 398 | $DB->update_record('moodleoverflow_posts', $this->build_db_object()); |
384 | 399 |
|
@@ -546,9 +561,9 @@ public function get_moodleoverflow() { |
546 | 561 | /** |
547 | 562 | * Returns the discussion where the post is located. |
548 | 563 | * |
549 | | - * @return object $discussionobject. |
| 564 | + * @return discussion $discussionobject. |
550 | 565 | */ |
551 | | - public function get_discussion() { |
| 566 | + public function get_discussion(): discussion { |
552 | 567 | global $DB; |
553 | 568 | $this->existence_check(); |
554 | 569 |
|
@@ -631,7 +646,7 @@ public function get_db_object() { |
631 | 646 | public function moodleoverflow_get_post_ratings() { |
632 | 647 | $this->existence_check(); |
633 | 648 |
|
634 | | - $discussionid = $this->get_discussion()->id; |
| 649 | + $discussionid = $this->get_discussion()->get_id(); |
635 | 650 | $postratings = \mod_moodleoverflow\ratings::moodleoverflow_get_ratings_by_discussion($discussionid, $this->id); |
636 | 651 |
|
637 | 652 | $ratingsobject = new \stdClass(); |
|
0 commit comments