@@ -76,80 +76,127 @@ public function migrate( WP_Post $post, int $parent_post_id ) {
7676 */
7777 public function migrate_assignment_files () {
7878 try {
79+
7980 $ assignments = get_posts ( array ( 'post_type ' => self ::LD_ASSIGNMENT ) );
80- array_map (
81- function ( $ assignment ) {
82- if ( ! $ this ->assign_file_to_tutor ( $ assignment ) ) {
83- ErrorHandler::set_error ( ContentTypes::ASSIGNMENT_FILES , 'Failed To Transfer Assignment For Assignment ID: ' . $ assignment ->ID );
81+ $ data = array ();
82+
83+ foreach ( $ assignments as $ assignment ) {
84+
85+ $ meta = get_post_meta ( $ assignment ->ID );
86+ $ lesson_id = $ meta ['lesson_id ' ][0 ] ?? 0 ;
87+ $ user_id = intval ( $ meta ['user_id ' ][0 ] ?? 0 );
88+ $ course_id = intval ( $ meta ['course_id ' ][0 ] ?? 0 );
89+
90+ if ( $ lesson_id && $ user_id && $ course_id ) {
91+
92+ if ( ! isset ( $ data [ $ user_id ][ $ course_id ][ $ lesson_id ] ) ) {
93+ $ data [ $ user_id ][ $ course_id ][ $ lesson_id ] = array (
94+ 'assignment_id ' => $ assignment ->ID ,
95+ 'comment_post_ID ' => intval ( $ lesson_id ),
96+ 'comment_date ' => $ assignment ->post_date ,
97+ 'comment_date_gmt ' => $ assignment ->post_date_gmt ,
98+ 'comment_author ' => $ meta ['disp_name ' ][0 ] ?? '' ,
99+ 'comment_content ' => $ meta ['post_content ' ][0 ] ?? '' ,
100+ 'comment_parent ' => $ course_id ,
101+ 'user_id ' => intval ( $ meta ['user_id ' ][0 ] ?? 0 ),
102+ );
103+ }
104+
105+ $ data [ $ user_id ][ $ course_id ][ $ lesson_id ]['attachment_files ' ][] = array (
106+ 'name ' => $ meta ['file_name ' ][0 ] ?? '' ,
107+ 'url ' => $ meta ['file_link ' ][0 ] ?? '' ,
108+ 'type ' => wp_check_filetype ( $ meta ['file_name ' ][0 ] ?? '' )['type ' ] ?? '' ,
109+ 'uploaded_path ' => $ meta ['file_path ' ][0 ] ? $ this ->get_uploaded_file_path ( $ meta ['file_path ' ][0 ] ) : '' ,
110+ );
111+
112+ // LearnDash allows individual point values per assignment file, while Tutor offers a single, unified point value for all submitted assignments file.
113+ $ data [ $ user_id ][ $ course_id ][ $ lesson_id ]['assignment_mark ' ] = max (
114+ intval ( $ meta ['points ' ][0 ] ) ?? 10 ,
115+ $ data [ $ user_id ][ $ course_id ][ $ lesson_id ]['assignment_mark ' ]
116+ );
117+
118+ if ( (int ) ( $ meta ['approval_status ' ][0 ] ?? 0 ) === 1 ) {
119+ $ data [ $ user_id ][ $ course_id ][ $ lesson_id ]['evaluate_time ' ] = $ assignment ->post_modified_gmt ;
84120 }
85- },
86- $ assignments
87- );
121+ }
122+ }
123+
124+ if ( ! empty ( $ data ) ) {
125+ $ this ->assign_file_to_tutor ( $ data );
126+ }
88127 } catch ( \Throwable $ th ) {
89128 throw $ th ;
90129 }
91130 }
92131
93132 /**
94- * Assigns a single LearnDash assignment file to TutorLMS .
133+ * Assigns uploaded assignment files to Tutor LMS by inserting comments .
95134 *
96135 * @since 2.3.0
97136 *
98- * @param WP_Post $assignment The assignment post object to migrate.
137+ * If insertion fails for any comment, the error is logged using the ErrorHandler.
138+ *
139+ * @param array $data A multi-dimensional array of data to insert as wp_comment.
99140 *
100- * @return bool True on success, false on failure.
141+ * @return void
101142 */
102- private function assign_file_to_tutor ( WP_Post $ assignment ) {
143+ private function assign_file_to_tutor ( $ data ) {
144+
145+ foreach ( $ data as $ user_info ) {
146+ foreach ( $ user_info as $ course_info ) {
147+ foreach ( $ course_info as $ ld_content ) {
148+ $ comment_id = wp_insert_comment (
149+ array (
150+ 'comment_post_ID ' => $ ld_content ['comment_post_ID ' ],
151+ 'comment_date ' => $ ld_content ['comment_date ' ],
152+ 'comment_date_gmt ' => $ ld_content ['comment_date_gmt ' ],
153+ 'comment_author ' => $ ld_content ['comment_author ' ],
154+ 'comment_content ' => $ ld_content ['comment_content ' ],
155+ 'comment_approved ' => 'submitted ' ,
156+ 'comment_agent ' => 'TutorLMSPlugin ' ,
157+ 'comment_type ' => self ::TUTOR_ASSIGNMENT ,
158+ 'comment_parent ' => $ ld_content ['comment_parent ' ],
159+ 'user_id ' => $ ld_content ['user_id ' ],
160+ )
161+ );
162+
163+ if ( ! $ comment_id ) {
164+ ErrorHandler::set_error (
165+ ContentTypes::ASSIGNMENT_FILES ,
166+ "Failed to insert comment for assignment ID: {$ ld_content ['assignment_id ' ]}"
167+ );
168+ continue ;
169+ }
103170
104- $ meta = get_post_meta ( $ assignment ->ID );
171+ add_comment_meta ( $ comment_id , 'assignment_mark ' , $ ld_content ['assignment_mark ' ] );
172+ add_comment_meta ( $ comment_id , 'uploaded_attachments ' , wp_json_encode ( $ ld_content ['attachment_files ' ] ) );
105173
106- if ( empty ( $ meta ) ) {
107- return false ;
174+ if ( $ ld_content ['evaluate_time ' ] ) {
175+ add_comment_meta ( $ comment_id , 'evaluate_time ' , $ ld_content ['evaluate_time ' ] );
176+ }
177+ }
178+ }
108179 }
180+ }
109181
110- $ comment_id = wp_insert_comment (
111- array (
112- 'comment_post_ID ' => intval ( $ meta ['lesson_id ' ][0 ] ?? 0 ),
113- 'comment_date ' => $ assignment ->post_date ,
114- 'comment_date_gmt ' => $ assignment ->post_date_gmt ,
115- 'comment_author ' => $ meta ['disp_name ' ][0 ] ?? '' ,
116- 'comment_content ' => $ meta ['post_content ' ][0 ] ?? '' ,
117- 'comment_approved ' => 'submitted ' ,
118- 'comment_agent ' => 'TutorLMSPlugin ' ,
119- 'comment_type ' => self ::TUTOR_ASSIGNMENT ,
120- 'comment_parent ' => intval ( $ meta ['course_id ' ][0 ] ?? 0 ),
121- 'user_id ' => intval ( $ meta ['user_id ' ][0 ] ?? 0 ),
122- )
123- );
182+ /**
183+ * Get the relative uploaded file path from the absolute file path.
184+ *
185+ * @since 2.3.0
186+ *
187+ * @param string $file_path The URL-encoded full file path.
188+ *
189+ * @return string|null The relative file path within the uploads directory, or null if the input is empty.
190+ */
191+ private function get_uploaded_file_path ( $ file_path ) {
124192
125- if ( ! $ comment_id ) {
126- ErrorHandler::set_error (
127- ContentTypes::ASSIGNMENT_FILES ,
128- "Failed to insert comment for assignment ID: {$ assignment ->ID }"
129- );
130- return false ;
193+ if ( empty ( $ file_path ) ) {
194+ return ;
131195 }
132196
133- $ uploaded_path = urldecode ( $ meta [ ' file_path ' ][ 0 ] ?? '' );
197+ $ uploaded_path = urldecode ( $ file_path ?? '' );
134198 $ upload_dir = wp_upload_dir ();
135- $ relative_path = str_replace ( $ upload_dir ['basedir ' ], '' , $ uploaded_path );
136-
137- $ attachments = array (
138- array (
139- 'url ' => $ meta ['file_link ' ][0 ] ?? '' ,
140- 'type ' => wp_check_filetype ( $ meta ['file_name ' ][0 ] ?? '' )['type ' ] ?? '' ,
141- 'uploaded_path ' => $ relative_path ?? '' ,
142- ),
143- );
144-
145- add_comment_meta ( $ comment_id , 'assignment_mark ' , $ meta ['points ' ][0 ] ?? 10 );
146- add_comment_meta ( $ comment_id , 'uploaded_attachments ' , wp_json_encode ( $ attachments ) );
147-
148- // If approved, set evaluation time.
149- if ( (int ) ( $ meta ['approval_status ' ][0 ] ?? 0 ) === 1 ) {
150- add_comment_meta ( $ comment_id , 'evaluate_time ' , $ assignment ->post_modified_gmt );
151- }
152199
153- return true ;
200+ return str_replace ( $ upload_dir [ ' basedir ' ], '' , $ uploaded_path ) ;
154201 }
155202}
0 commit comments