@@ -186,46 +186,57 @@ public static function construct_without_id(
186186 */
187187 public function moodleoverflow_add_discussion (object $ prepost ): bool |int |null {
188188 global $ DB ;
189+ try {
190+ $ transaction = $ DB ->start_delegated_transaction ();
189191
190- // Add the discussion to the Database.
191- $ this ->id = $ DB ->insert_record ('moodleoverflow_discussions ' , $ this ->build_db_object ());
192-
193- // Create the first/parent post for the new discussion and add it do the DB.
194- $ post = post::construct_without_id (
195- $ this ->id ,
196- 0 ,
197- $ prepost ->userid ,
198- $ prepost ->timenow ,
199- $ prepost ->timenow ,
200- $ prepost ->message ,
201- $ prepost ->messageformat ,
202- "" ,
203- 0 ,
204- $ prepost ->reviewed ,
205- null ,
206- $ prepost ->formattachments
207- );
208- // Add it to the DB and save the id of the first/parent post.
209- $ this ->firstpost = $ post ->moodleoverflow_add_new_post ();
192+ // Add the discussion to the Database.
193+ $ this ->id = $ DB ->insert_record ('moodleoverflow_discussions ' , $ this ->build_db_object ());
194+
195+ // Create the first/parent post for the new discussion and add it to the DB.
196+ $ post = post::construct_without_id (
197+ $ this ->id ,
198+ 0 ,
199+ $ prepost ->userid ,
200+ $ prepost ->timenow ,
201+ $ prepost ->timenow ,
202+ $ prepost ->message ,
203+ $ prepost ->messageformat ,
204+ "" ,
205+ 0 ,
206+ $ prepost ->reviewed ,
207+ null ,
208+ $ prepost ->formattachments
209+ );
210+
211+ // Add it to the DB and save the id of the first/parent post.
212+ $ this ->firstpost = $ post ->moodleoverflow_add_new_post ();
213+ $ DB ->set_field ('moodleoverflow_discussions ' , 'firstpost ' , $ this ->firstpost , ['id ' => $ this ->id ]);
214+
215+ // Add the parent post to the $posts array.
216+ $ this ->posts [$ this ->firstpost ] = $ post ;
217+ $ this ->postsbuild = true ;
210218
211- // Save the id of the first/parent post in the DB.
212- $ DB ->set_field ('moodleoverflow_discussions ' , 'firstpost ' , $ this ->firstpost , ['id ' => $ this ->id ]);
219+ // Trigger event.
220+ $ params = [
221+ 'context ' => $ prepost ->modulecontext ,
222+ 'objectid ' => $ this ->id ,
223+ ];
224+ // LEARNWEB-TODO: check if the event functions.
225+ $ event = discussion_viewed::create ($ params );
226+ $ event ->trigger ();
213227
214- // Add the parent post to the $posts array.
215- $ this ->posts [$ this ->firstpost ] = $ post ;
216- $ this ->postsbuild = true ;
228+ // Everything worked, commit the transaction.
229+ $ transaction ->allow_commit ();
217230
218- // Trigger event.
219- $ params = [
220- 'context ' => $ prepost ->modulecontext ,
221- 'objectid ' => $ this ->id ,
222- ];
223- // LEARNWEB-TODO: check if the event functions.
224- $ event = discussion_viewed::create ($ params );
225- $ event ->trigger ();
231+ // Return the id of the discussion.
232+ return $ this ->id ;
233+ } catch (Exception $ e ) {
234+ // Something went wrong, roll back every change so no half-created discussion remains.
235+ $ transaction ->rollback ($ e );
236+ }
226237
227- // Return the id of the discussion .
228- return $ this -> id ;
238+ // Adding the discussion has failed .
239+ return false ;
229240 }
230241
231242 /**
0 commit comments