@@ -271,18 +271,7 @@ public function moodleoverflow_add_new_post(): bool|int|null {
271271 $ this ->id = $ DB ->insert_record ('moodleoverflow_posts ' , $ this ->build_db_object ());
272272
273273 // Save draft files to permanent file area.
274- $ context = \context_module::instance ($ this ->get_coursemodule ()->id );
275- $ draftid = file_get_submitted_draft_itemid ('message ' );
276- $ this ->message = file_save_draft_area_files (
277- $ draftid ,
278- $ context ->id ,
279- 'mod_moodleoverflow ' ,
280- 'post ' ,
281- $ this ->id ,
282- mod_moodleoverflow_post_form::editor_options ($ context , $ this ->id ),
283- $ this ->message
284- );
285- $ DB ->update_record ('moodleoverflow_posts ' , $ this ->build_db_object ());
274+ $ this ->save_draft_files ();
286275
287276 // Update the attachments. This happens after the DB update call, as this function changes the DB record as well.
288277 $ this ->moodleoverflow_add_attachment ();
@@ -344,7 +333,7 @@ public function moodleoverflow_delete_post(bool $deletechildren): bool {
344333
345334 // Delete the attachments.
346335 $ fs = get_file_storage ();
347- $ context = \ context_module::instance ($ coursemoduleid );
336+ $ context = context_module::instance ($ coursemoduleid );
348337 $ attachments = $ fs ->get_area_files (
349338 $ context ->id ,
350339 'mod_moodleoverflow ' ,
@@ -410,29 +399,16 @@ public function moodleoverflow_delete_post(bool $deletechildren): bool {
410399 * @throws moodle_exception
411400 */
412401 public function moodleoverflow_edit_post (int $ time , string $ postmessage , int $ messageformat , ?int $ formattachments ): bool {
413- global $ DB ;
414402 $ this ->existence_check ();
415403
416404 // Update the attributes.
417405 $ this ->modified = $ time ;
406+ $ this ->message = $ postmessage ;
418407 $ this ->messageformat = $ messageformat ;
419408 $ this ->formattachments = $ formattachments ;
420409
421410 // Update the message and save draft files to permanent file area.
422- $ context = \context_module::instance ($ this ->get_coursemodule ()->id );
423- $ draftid = file_get_submitted_draft_itemid ('message ' );
424- $ this ->message = file_save_draft_area_files (
425- $ draftid ,
426- $ context ->id ,
427- 'mod_moodleoverflow ' ,
428- 'post ' ,
429- $ this ->id ,
430- mod_moodleoverflow_post_form::editor_options ($ context , $ this ->id ),
431- $ postmessage
432- );
433-
434- // Update the record in the database.
435- $ DB ->update_record ('moodleoverflow_posts ' , $ this ->build_db_object ());
411+ $ this ->save_draft_files ();
436412
437413 // Update the attachments. This happens after the DB update call, as this function changes the DB record as well.
438414 $ this ->moodleoverflow_add_attachment ();
@@ -485,7 +461,7 @@ public function moodleoverflow_add_attachment(): void {
485461 return ; // Nothing to do.
486462 }
487463
488- $ context = \ context_module::instance ($ this ->get_coursemodule ()->id );
464+ $ context = context_module::instance ($ this ->get_coursemodule ()->id );
489465 $ info = file_get_draft_area_info ($ this ->formattachments );
490466 $ present = ($ info ['filecount ' ] > 0 ) ? '1 ' : '' ;
491467 file_save_draft_area_files (
@@ -509,7 +485,7 @@ public function moodleoverflow_get_attachments(): array {
509485 global $ OUTPUT ;
510486 $ this ->existence_check ();
511487
512- if (empty ($ this ->attachment ) || (!$ context = \ context_module::instance ($ this ->get_coursemodule ()->id ))) {
488+ if (empty ($ this ->attachment ) || (!$ context = context_module::instance ($ this ->get_coursemodule ()->id ))) {
513489 return [];
514490 }
515491
@@ -554,6 +530,27 @@ public function moodleoverflow_get_attachments(): array {
554530 return $ attachments ;
555531 }
556532
533+ /**
534+ * Saves draft files from the message form permanently and updates the post message.
535+ * @return void
536+ * @throws moodle_exception
537+ */
538+ public function save_draft_files (): void {
539+ global $ DB ;
540+ $ this ->existence_check ();
541+ $ context = context_module::instance ($ this ->get_coursemodule ()->id );
542+ $ this ->message = file_save_draft_area_files (
543+ file_get_submitted_draft_itemid ('message ' ),
544+ $ context ->id ,
545+ 'mod_moodleoverflow ' ,
546+ 'post ' ,
547+ $ this ->id ,
548+ mod_moodleoverflow_post_form::editor_options ($ context , $ this ->id ),
549+ $ this ->message
550+ );
551+ $ DB ->update_record ('moodleoverflow_posts ' , $ this ->build_db_object ());
552+ }
553+
557554 /**
558555 * Get a link to the users profile.
559556 * Returns a html link embedded in the users name.
0 commit comments