@@ -135,10 +135,11 @@ public function detect_interaction(stdClass $urlparameter): void {
135135 /**
136136 * Controls the execution of an interaction.
137137 * @param object $form The results from the post_form.
138+ * @return object An object with information for the redirect
138139 * @throws coding_exception
139140 * @throws moodle_exception
140141 */
141- public function execute_interaction (object $ form ): void {
142+ public function execute_interaction (object $ form ): object {
142143 global $ SESSION ;
143144 // Redirect url in case of occurring errors.
144145 $ SESSION ->errorreturnurl = new moodle_url ('/mod/moodleoverflow/view.php? ' , ['m ' => $ this ->prepost ->moodleoverflowid ]);
@@ -154,11 +155,11 @@ public function execute_interaction(object $form): void {
154155
155156 // Execute the right function.
156157 if ($ this ->interaction == 'create ' && $ form ->moodleoverflow == $ this ->prepost ->moodleoverflowid ) {
157- $ this ->execute_create ($ form );
158+ return $ this ->execute_create ($ form );
158159 } else if ($ this ->interaction == 'reply ' && $ form ->reply == $ this ->prepost ->parentid ) {
159- $ this ->execute_reply ($ form );
160+ return $ this ->execute_reply ($ form );
160161 } else if ($ this ->interaction == 'edit ' && $ form ->edit == $ this ->prepost ->postid ) {
161- $ this ->execute_edit ($ form );
162+ return $ this ->execute_edit ($ form );
162163 } else {
163164 throw new coding_exception (get_string ('errorunexpectedinteraction ' , 'moodleoverflow ' ));
164165 }
@@ -354,8 +355,9 @@ private function build_prepost_delete(int $deletepostid): void {
354355 *
355356 * @param object $form The results from the post_form.
356357 * @throws moodle_exception if the discussion could not be added.
358+ * @return object An object with all information to redirect.
357359 */
358- private function execute_create (object $ form ): void {
360+ private function execute_create (object $ form ): object {
359361 global $ USER ;
360362 // Check if the user is allowed to post.
361363 $ this ->check_user_can_create_discussion ();
@@ -388,9 +390,6 @@ private function execute_create(object $form): void {
388390 throw new moodle_exception ('couldnotadd ' , 'moodleoverflow ' );
389391 }
390392
391- // The creation was successful.
392- $ redirectmessage = html_writer::tag ('p ' , get_string ("postaddedsuccess " , "moodleoverflow " ));
393-
394393 // Trigger the discussion created event.
395394 $ params = ['context ' => $ this ->info ->modulecontext , 'objectid ' => $ discussion ->get_id ()];
396395 $ event = discussion_created::create ($ params );
@@ -407,7 +406,8 @@ private function execute_create(object $form): void {
407406
408407 // Define the location to redirect the user after successfully posting.
409408 $ redirectto = new moodle_url ('/mod/moodleoverflow/view.php ' , ['m ' => $ form ->moodleoverflow ]);
410- redirect (moodleoverflow_go_back_to ($ redirectto ), $ redirectmessage , null , \core \output \notification::NOTIFY_SUCCESS );
409+ $ redirectmessage = html_writer::tag ('p ' , get_string ("postaddedsuccess " , "moodleoverflow " ));
410+ return (object ) ['redirectto ' => $ redirectto , 'redirectmessage ' => $ redirectmessage ];
411411 }
412412
413413 /**
@@ -416,7 +416,7 @@ private function execute_create(object $form): void {
416416 * @param object $form The results from the post_form.
417417 * @throws moodle_exception if the reply could not be added.
418418 */
419- private function execute_reply (object $ form ): void {
419+ private function execute_reply (object $ form ): object {
420420 // Check if the user has the capability to write a reply.
421421 $ this ->check_user_can_create_reply ();
422422
@@ -435,14 +435,6 @@ private function execute_reply(object $form): void {
435435 throw new moodle_exception ('couldnotadd ' , 'moodleoverflow ' );
436436 }
437437
438- // The creation was successful.
439- $ redirectmessage = html_writer::tag ('p ' , get_string ("postaddedsuccess " , "moodleoverflow " ));
440- $ redirectmessage .= html_writer::tag ('p ' , get_string (
441- "postaddedtimeleft " ,
442- "moodleoverflow " ,
443- format_time (get_config ('moodleoverflow ' , 'maxeditingtime ' ))
444- ));
445-
446438 // Trigger the post created event.
447439 $ params = ['context ' => $ this ->info ->modulecontext , 'objectid ' => $ newpostid ,
448440 'other ' => ['discussionid ' => $ this ->prepost ->discussionid ,
@@ -466,7 +458,13 @@ private function execute_reply(object $form): void {
466458 '/mod/moodleoverflow/discussion.php ' ,
467459 ['d ' => $ this ->prepost ->discussionid , 'p ' => $ newpostid ]
468460 );
469- redirect (\moodleoverflow_go_back_to ($ redirectto ), $ redirectmessage , null , \core \output \notification::NOTIFY_SUCCESS );
461+ $ redirectmessage = html_writer::tag ('p ' , get_string ("postaddedsuccess " , "moodleoverflow " ));
462+ $ redirectmessage .= html_writer::tag ('p ' , get_string (
463+ "postaddedtimeleft " ,
464+ "moodleoverflow " ,
465+ format_time (get_config ('moodleoverflow ' , 'maxeditingtime ' ))
466+ ));
467+ return (object ) ['redirectto ' => $ redirectto , 'redirectmessage ' => $ redirectmessage ];
470468 }
471469
472470 /**
@@ -475,7 +473,7 @@ private function execute_reply(object $form): void {
475473 * @param object $form The results from the post_form.
476474 * @throws moodle_exception if the post could not be updated.
477475 */
478- private function execute_edit (object $ form ): void {
476+ private function execute_edit (object $ form ): object {
479477 global $ USER , $ DB ;
480478 // Check if the user has the capability to edit his post.
481479 $ this ->check_user_can_edit_post ();
@@ -490,18 +488,6 @@ private function execute_edit(object $form): void {
490488 throw new moodle_exception ('couldnotupdate ' , 'moodleoverflow ' );
491489 }
492490
493- // The edit was successful.
494- $ redirectmessage = get_string ('postupdated ' , 'moodleoverflow ' );
495- if ($ this ->prepost ->userid != $ USER ->id ) {
496- if (anonymous::is_post_anonymous ($ this ->info ->discussion , $ this ->info ->moodleoverflow , $ this ->prepost ->userid )) {
497- $ name = get_string ('anonymous ' , 'moodleoverflow ' );
498- } else {
499- $ realuser = $ DB ->get_record ('user ' , ['id ' => $ this ->prepost ->userid ]);
500- $ name = fullname ($ realuser );
501- }
502- $ redirectmessage = get_string ('editedpostupdated ' , 'moodleoverflow ' , $ name );
503- }
504-
505491 // Trigger the post updated event.
506492 $ params = ['context ' => $ this ->info ->modulecontext , 'objectid ' => $ form ->edit ,
507493 'other ' => ['discussionid ' => $ this ->prepost ->discussionid ,
@@ -517,15 +503,28 @@ private function execute_edit(object $form): void {
517503 '/mod/moodleoverflow/discussion.php ' ,
518504 ['d ' => $ this ->prepost ->discussionid , 'p ' => $ form ->edit ]
519505 );
520- redirect (moodleoverflow_go_back_to ($ redirectto ), $ redirectmessage , null , \core \output \notification::NOTIFY_SUCCESS );
506+ // The edit was successful.
507+ $ redirectmessage = get_string ('postupdated ' , 'moodleoverflow ' );
508+ if ($ this ->prepost ->userid != $ USER ->id ) {
509+ if (anonymous::is_post_anonymous ($ this ->info ->discussion , $ this ->info ->moodleoverflow , $ this ->prepost ->userid )) {
510+ $ name = get_string ('anonymous ' , 'moodleoverflow ' );
511+ } else {
512+ $ realuser = $ DB ->get_record ('user ' , ['id ' => $ this ->prepost ->userid ]);
513+ $ name = fullname ($ realuser );
514+ }
515+ $ redirectmessage = get_string ('editedpostupdated ' , 'moodleoverflow ' , $ name );
516+ }
517+
518+ return (object ) ['redirectto ' => $ redirectto , 'redirectmessage ' => $ redirectmessage ];
521519 }
522520
523521 /**
524522 * Executes the deletion of a post.
525523 *
524+ * @return mixed redirect information
526525 * @throws moodle_exception if the post could not be deleted.
527526 */
528- public function execute_delete (): void {
527+ public function execute_delete (): mixed {
529528 global $ SESSION ;
530529 $ this ->check_interaction ('delete ' );
531530
@@ -551,11 +550,11 @@ public function execute_delete(): void {
551550 $ this ->info ->discussion ->moodleoverflow_delete_discussion ($ this ->prepost );
552551
553552 // Redirect the user back to the start page of the moodleoverflow instance.
554- redirect ( 'view.php?m= ' . $ moodleoverflowid) ;
553+ return 'view.php?m= ' . $ moodleoverflowid ;
555554 } else {
556555 $ this ->info ->discussion ->moodleoverflow_delete_post_from_discussion ($ this ->prepost );
557556 $ discussionurl = new moodle_url ('/mod/moodleoverflow/discussion.php ' , ['d ' => $ this ->info ->discussion ->get_id ()]);
558- redirect ( moodleoverflow_go_back_to ($ discussionurl) );
557+ return moodleoverflow_go_back_to ($ discussionurl );
559558 }
560559 }
561560
0 commit comments