@@ -159,6 +159,7 @@ public function thematicSave($id, $title, $content, Course $course, Session $ses
159159 ->setContent ($ content )
160160 ->setParent ($ course )
161161 ->addCourseLink ($ course , $ session )
162+ ->setActive (true )
162163 ;
163164
164165 $ repo ->create ($ thematic );
@@ -198,55 +199,66 @@ public function delete(int|array $thematicId): void
198199 }
199200
200201 /**
201- * @param int $thematicId
202+ * Duplicate a thematic (title, content, plans and advances) into the same Course/Session.
202203 */
203- public function copy ($ thematicId)
204+ public function copy (int $ thematicId, ? Course $ course = null , ? Session $ session = null ): ? CThematic
204205 {
205206 $ repo = Container::getThematicRepository ();
206- /** @var CThematic $thematic */
207- $ thematic = $ repo ->find ($ thematicId );
208- if (null === $ thematic ) {
209- return false ;
207+ /** @var CThematic|null $source */
208+ $ source = $ repo ->find ($ thematicId );
209+ if (! $ source ) {
210+ return null ;
210211 }
211212
212- $ thematicManager = new Thematic ();
213+ // Resolve context if not provided
214+ $ course = $ course ?: api_get_course_entity ();
215+ $ session = $ session ?: api_get_session_entity ();
213216
214- $ newThematic = $ thematicManager ->thematicSave (
217+ // Create the new thematic using the existing helper (keeps linking logic consistent)
218+ $ new = $ this ->thematicSave (
215219 null ,
216- $ thematic ->getTitle (). ' - ' . get_lang ( ' Copy ' ),
217- $ thematic ->getContent (),
218- api_get_course_entity () ,
219- api_get_session_entity ()
220+ ( string ) $ source ->getTitle (),
221+ ( string ) $ source ->getContent (),
222+ $ course ,
223+ $ session
220224 );
221225
222- if (!empty ($ newThematic ->getIid ())) {
223- $ thematic_advanced = $ thematic ->getAdvances ();
224- if (!empty ($ thematic_advanced )) {
225- foreach ($ thematic_advanced as $ item ) {
226- $ thematic = new Thematic ();
227- $ thematic ->thematicAdvanceSave (
228- $ newThematic ,
229- $ item ->getAttendance (),
230- null ,
231- $ item ->getContent (),
232- $ item ->getStartDate ()->format ('Y-m-d H:i:s ' ),
233- $ item ->getDuration ()
234- );
235- }
236- }
226+ if (!$ new ) {
227+ return null ;
228+ }
237229
238- $ thematic_plan = $ thematic ->getPlans ();
239- if (!empty ($ thematic_plan )) {
240- foreach ($ thematic_plan as $ item ) {
241- $ thematic ->thematicPlanSave (
242- $ newThematic ,
243- $ item ->getTitle (),
244- $ item ->getDescription (),
245- $ item ->getDescriptionType ()
246- );
247- }
230+ // Copy advances
231+ foreach ($ source ->getAdvances () as $ adv ) {
232+ // Normalize start date to string Y-m-d H:i:s for thematicAdvanceSave
233+ $ startDate = $ adv ->getStartDate ();
234+ if ($ startDate instanceof \DateTimeInterface) {
235+ $ startDate = $ startDate ->format ('Y-m-d H:i:s ' );
236+ } else {
237+ $ startDate = (string ) $ startDate ;
248238 }
239+
240+ // Keep the same attendance relation if any (same course/session context)
241+ $ this ->thematicAdvanceSave (
242+ $ new ,
243+ $ adv ->getAttendance (),
244+ null ,
245+ (string ) $ adv ->getContent (),
246+ $ startDate ,
247+ (float ) $ adv ->getDuration ()
248+ );
249+ }
250+
251+ // Copy plans
252+ foreach ($ source ->getPlans () as $ plan ) {
253+ $ this ->thematicPlanSave (
254+ $ new ,
255+ (string ) $ plan ->getTitle (),
256+ (string ) $ plan ->getDescription (),
257+ (int ) $ plan ->getDescriptionType ()
258+ );
249259 }
260+
261+ return $ new ;
250262 }
251263
252264 /**
0 commit comments