PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.5
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.5
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / Ajax / EditCurriculumAjax.php

EditCurriculumAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.5, at inc/Ajax/EditCurriculumAjax.php

533 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class EditCurriculumAjax
4 *
5 * This class handles the AJAX request to edit the curriculum of a course.
6 *
7 * @since 4.2.8.6
8 * @version 1.0.1
9 */
10
11 namespace LearnPress\Ajax;
12
13 use Exception;
14 use LearnPress\Models\CourseModel;
15 use LearnPress\Models\CoursePostModel;
16 use LearnPress\Models\CourseSectionItemModel;
17 use LearnPress\Models\CourseSectionModel;
18 use LearnPress\Models\LessonPostModel;
19 use LearnPress\Models\PostModel;
20 use LearnPress\TemplateHooks\Course\AdminEditCurriculumTemplate;
21 use LP_Helper;
22 use LP_REST_Response;
23 use Throwable;
24
25 class EditCurriculumAjax extends AbstractAjax {
26 /**
27 * Check permissions and validate parameters.
28 *
29 * @throws Exception
30 *
31 * @since 4.2.8.6
32 * @version 1.0.1
33 */
34 public static function check_valid() {
35 $params = wp_unslash( $_REQUEST['data'] ?? '' );
36 if ( empty( $params ) ) {
37 throw new Exception( 'Error: params invalid!' );
38 }
39
40 return LP_Helper::json_decode( $params, true );
41 }
42
43 /**
44 * Add section
45 *
46 * JS file edit-section.js: function addSection call this method to update the section description.
47 *
48 * @since 4.2.8.6
49 * @version 1.0.1
50 */
51 public static function course_add_section() {
52 $response = new LP_REST_Response();
53
54 try {
55 $data = self::check_valid();
56
57 $course_id = $data['course_id'] ?? 0;
58 $courseModel = CourseModel::find( $course_id, true );
59 if ( ! $courseModel ) {
60 throw new Exception( __( 'Course not found', 'learnpress' ) );
61 }
62
63 $section_name = trim( $data['section_name'] ?? '' );
64 if ( empty( $section_name ) ) {
65 throw new Exception( __( 'Section title is required', 'learnpress' ) );
66 }
67
68 // Add section to course.
69 $coursePostModel = new CoursePostModel( $courseModel );
70 $sectionNew = $coursePostModel->add_section(
71 [
72 'section_name' => $section_name,
73 'section_description' => $data['section_description'] ?? '',
74 ]
75 );
76
77 $response->data->section = $sectionNew;
78 $response->status = 'success';
79 $response->message = __( 'Section added successfully', 'learnpress' );
80 } catch ( Throwable $e ) {
81 $response->message = $e->getMessage();
82 }
83
84 wp_send_json( $response );
85 }
86
87 /**
88 * Update section
89 *
90 * JS file edit-section.js: function updateSectionTitle call this method to update the section title.
91 * JS file edit-section.js: function updateSectionDescription call this method to update the section description.
92 *
93 * @since 4.2.8.6
94 * @version 1.0.1
95 */
96 public static function course_update_section() {
97 $response = new LP_REST_Response();
98
99 try {
100 $data = self::check_valid();
101 $course_id = $data['course_id'] ?? 0;
102 $section_id = $data['section_id'] ?? 0;
103
104 $courseModel = CourseModel::find( $course_id, true );
105 if ( ! $courseModel ) {
106 throw new Exception( __( 'Course not found', 'learnpress' ) );
107 }
108
109 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id, true );
110 if ( ! $courseSectionModel ) {
111 throw new Exception( __( 'Section not found', 'learnpress' ) );
112 }
113
114 // Update section of course
115 $coursePostModel = new CoursePostModel( $courseModel );
116 $coursePostModel->update_section( $courseSectionModel, $data );
117
118 $response->status = 'success';
119 $response->message = __( 'Section updated successfully', 'learnpress' );
120 } catch ( Throwable $e ) {
121 $response->message = $e->getMessage();
122 }
123
124 wp_send_json( $response );
125 }
126
127 /**
128 * Delete section
129 *
130 * JS file edit-section.js: function deleteSection call this method to update the section title.
131 *
132 * @since 4.2.8.6
133 * @version 1.0.0
134 */
135 public static function course_delete_section() {
136 $response = new LP_REST_Response();
137
138 try {
139 $data = self::check_valid();
140 $course_id = $data['course_id'] ?? 0;
141 $section_id = $data['section_id'] ?? 0;
142
143 $courseModel = CourseModel::find( $course_id, true );
144 if ( ! $courseModel ) {
145 throw new Exception( __( 'Course not found', 'learnpress' ) );
146 }
147
148 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
149 if ( ! $courseSectionModel ) {
150 throw new Exception( __( 'Section not found', 'learnpress' ) );
151 }
152
153 $courseSectionModel->delete();
154
155 $response->status = 'success';
156 $response->message = __( 'Section updated successfully', 'learnpress' );
157 } catch ( Throwable $e ) {
158 $response->message = $e->getMessage();
159 }
160
161 wp_send_json( $response );
162 }
163
164 /**
165 * Update sections position
166 * new_position => list of section id by order
167 *
168 * JS file edit-section.js: function sortAbleSection call this method.
169 *
170 * @since 4.2.8.6
171 * @version 1.0.1
172 */
173 public static function course_update_section_position() {
174 $response = new LP_REST_Response();
175
176 try {
177 $data = self::check_valid();
178 $course_id = $data['course_id'] ?? 0;
179 $new_position = $data['new_position'] ?? [];
180 if ( ! is_array( $new_position ) ) {
181 throw new Exception( __( 'Invalid section position', 'learnpress' ) );
182 }
183
184 $courseModel = CourseModel::find( $course_id, true );
185 if ( ! $courseModel ) {
186 throw new Exception( __( 'Course not found', 'learnpress' ) );
187 }
188
189 // Update all sections position
190 $coursePostMoel = new CoursePostModel( $courseModel );
191 $coursePostMoel->update_sections_position( [ 'new_position' => $new_position ] );
192
193 $courseModel->sections_items = null;
194 $courseModel->save();
195
196 $response->status = 'success';
197 $response->message = __( 'Section updated successfully', 'learnpress' );
198 } catch ( Throwable $e ) {
199 $response->message = $e->getMessage();
200 }
201
202 wp_send_json( $response );
203 }
204
205 /**
206 * Create item and add to section
207 *
208 * $data['course_id'] => ID of course
209 * $data['section_id'] => ID of section
210 * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
211 * $data['item_title'] => Title of the item
212 *
213 * JS file edit-section-item.js: function addItemToSection call this method.
214 *
215 * @since 4.2.8.6
216 * @version 1.0.2
217 */
218 public static function create_item_add_to_section() {
219 $response = new LP_REST_Response();
220
221 try {
222 $data = self::check_valid();
223 $course_id = $data['course_id'] ?? 0;
224 $section_id = $data['section_id'] ?? 0;
225
226 $courseModel = CourseModel::find( $course_id, true );
227 if ( ! $courseModel ) {
228 throw new Exception( __( 'Course not found', 'learnpress' ) );
229 }
230
231 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
232 if ( ! $courseSectionModel ) {
233 throw new Exception( __( 'Section not found', 'learnpress' ) );
234 }
235
236 $courseSectionItemModel = $courseSectionModel->create_item_and_add( $data );
237
238 // Reload course model to get updated sections
239 $courseModel = CourseModel::find( $course_id, true );
240
241 $response->data->section_item = $courseSectionItemModel;
242
243 /**
244 * @var $itemModel PostModel
245 */
246 $itemModel = $courseModel->get_item_model(
247 $courseSectionItemModel->item_id,
248 $courseSectionItemModel->item_type
249 );
250 $response->data->item_link = $itemModel ? $itemModel->get_edit_link() : '';
251
252 $response->status = 'success';
253 $response->message = __( 'Item added to section successfully', 'learnpress' );
254 } catch ( Throwable $e ) {
255 $response->message = $e->getMessage();
256 }
257
258 wp_send_json( $response );
259 }
260
261 /**
262 * Add items selected to section
263 *
264 * $data['course_id'] => ID of course
265 * $data['section_id'] => ID of section
266 * $data['items'] => [ item_id => 0, item_type => '' ]
267 *
268 * JS file edit-section-item.js: function addItemsSelectedToSection call this method.
269 *
270 * @since 4.2.8.6
271 * @version 1.0.2
272 */
273 public static function add_items_to_section() {
274 $response = new LP_REST_Response();
275
276 try {
277 $data = self::check_valid();
278 $course_id = $data['course_id'] ?? 0;
279 $section_id = $data['section_id'] ?? 0;
280
281 $courseModel = CourseModel::find( $course_id, true );
282 if ( ! $courseModel ) {
283 throw new Exception( __( 'Course not found', 'learnpress' ) );
284 }
285
286 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
287 if ( ! $courseSectionModel ) {
288 throw new Exception( __( 'Section not found', 'learnpress' ) );
289 }
290
291 $courseSectionItems = $courseSectionModel->add_items( $data );
292 if ( empty( $courseSectionItems ) ) {
293 throw new Exception( __( 'No items were added to the section', 'learnpress' ) );
294 }
295
296 // Reload course model to get updated sections
297 $courseModel = CourseModel::find( $course_id, true );
298
299 // Set context_data for Course Builder context
300 $is_course_builder = ! empty( $data['is_course_builder'] );
301 AdminEditCurriculumTemplate::instance()->context_data = [
302 'is_course_builder' => $is_course_builder,
303 ];
304
305 $response->data->html = '';
306 /**
307 * @var $courseSectionItem CourseSectionItemModel
308 */
309 foreach ( $courseSectionItems as $courseSectionItem ) {
310 $courseSectionItemAlias = (object) get_object_vars( $courseSectionItem );
311 $itemModel = $courseModel->get_item_model(
312 $courseSectionItem->item_id,
313 $courseSectionItem->item_type
314 );
315 $courseSectionItemAlias->title = $itemModel ? $itemModel->get_the_title() : '';
316 $response->data->html .= AdminEditCurriculumTemplate::instance()->html_section_item(
317 $courseModel,
318 $courseSectionItemAlias
319 );
320 }
321
322 $response->status = 'success';
323 $response->message = __( 'Items added to section successfully', 'learnpress' );
324 } catch ( Throwable $e ) {
325 $response->message = $e->getMessage();
326 }
327
328 wp_send_json( $response );
329 }
330
331 /**
332 * Delete item from section
333 *
334 * $data['course_id'] => ID of course
335 * $data['section_id'] => ID of section
336 * $data['item_id'] => ID of item to delete
337 *
338 * JS file edit-section-item.js: function deleteItem call this method.
339 */
340 public static function delete_item_from_section() {
341 $response = new LP_REST_Response();
342
343 try {
344 $data = self::check_valid();
345 $course_id = $data['course_id'] ?? 0;
346 $section_id = $data['section_id'] ?? 0;
347 $item_id = $data['item_id'] ?? 0;
348
349 $courseModel = CourseModel::find( $course_id, true );
350 if ( ! $courseModel ) {
351 throw new Exception( __( 'Course not found', 'learnpress' ) );
352 }
353
354 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
355 if ( ! $courseSectionModel ) {
356 throw new Exception( __( 'Section not found', 'learnpress' ) );
357 }
358
359 // Find item of section id
360 $courseSectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
361 if ( ! $courseSectionItemModel ) {
362 throw new Exception( __( 'Item not found in section', 'learnpress' ) );
363 }
364
365 // Delete item from section
366 $courseSectionItemModel->section_course_id = $course_id;
367 $courseSectionItemModel->delete();
368
369 $response->status = 'success';
370 $response->message = __( 'Item deleted from section successfully', 'learnpress' );
371 } catch ( Throwable $e ) {
372 $response->message = $e->getMessage();
373 }
374
375 wp_send_json( $response );
376 }
377
378 /**
379 * Update item on new section and position
380 *
381 * $data['course_id'] => ID of course
382 * $data['items_position'] => list of item id by order on section new
383 * $data['item_id_change'] => ID of item to change section
384 * $data['section_id_new_of_item'] => ID of new section of item
385 * $data['section_id_old_of_item'] => ID of old section of item
386 *
387 * JS file edit-section-item.js: function sortAbleItem call this method.
388 *
389 * @since 4.2.8.6
390 * @version 1.0.1
391 */
392 public static function update_item_section_and_position() {
393 $response = new LP_REST_Response();
394
395 try {
396 $data = self::check_valid();
397 $course_id = $data['course_id'] ?? 0;
398
399 $courseModel = CourseModel::find( $course_id, true );
400 if ( ! $courseModel ) {
401 throw new Exception( __( 'Course not found', 'learnpress' ) );
402 }
403
404 $coursePostModel = new CoursePostModel( $courseModel );
405 $coursePostModel->update_items_position( $data );
406
407 $response->status = 'success';
408 $response->message = __( 'Item position updated successfully', 'learnpress' );
409 } catch ( Throwable $e ) {
410 $response->message = $e->getMessage();
411 }
412
413 wp_send_json( $response );
414 }
415
416 /**
417 * Update data of item in section
418 *
419 * $data['course_id'] => ID of course
420 * $data['section_id'] => ID of section
421 * $data['item_id'] => ID of item to update
422 * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
423 * $data['item_title'] => New title of the item
424 *
425 * JS file edit-section-item.js: function updateTitle call this method.
426 *
427 * @since 4.2.8.6
428 * @version 1.0.0
429 */
430 public static function update_item_of_section() {
431 $response = new LP_REST_Response();
432
433 try {
434 $data = self::check_valid();
435 $course_id = $data['course_id'] ?? 0;
436 $section_id = $data['section_id'] ?? 0;
437 $item_id = $data['item_id'] ?? 0;
438 $item_type = $data['item_type'] ?? '';
439 $item_title = $data['item_title'] ?? '';
440
441 if ( empty( $item_title ) ) {
442 throw new Exception( __( 'Item title is required', 'learnpress' ) );
443 }
444
445 $courseModel = CourseModel::find( $course_id, true );
446 if ( ! $courseModel ) {
447 throw new Exception( __( 'Course not found', 'learnpress' ) );
448 }
449
450 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
451 if ( ! $courseSectionModel ) {
452 throw new Exception( __( 'Section not found', 'learnpress' ) );
453 }
454
455 $sectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
456 if ( ! $sectionItemModel ) {
457 throw new Exception( __( 'Item not found in section', 'learnpress' ) );
458 }
459
460 /**
461 * @var $itemModel PostModel
462 */
463 $itemModel = $courseModel->get_item_model( $item_id, $item_type );
464 if ( ! $itemModel ) {
465 throw new Exception( __( 'Item not found', 'learnpress' ) );
466 }
467
468 $itemModel->post_title = $item_title;
469 $itemModel->save();
470
471 $courseModel->sections_items = null;
472 $courseModel->save();
473
474 $response->status = 'success';
475 $response->message = __( 'Item updated successfully', 'learnpress' );
476 } catch ( Throwable $e ) {
477 $response->message = $e->getMessage();
478 }
479
480 wp_send_json( $response );
481 }
482
483 /**
484 * Update item preview.
485 *
486 * JS file edit-section-item.js: function updatePreviewItem call this method.
487 *
488 * @since 4.2.8.6
489 * @version 1.0.2
490 */
491 public static function update_item_preview() {
492 $response = new LP_REST_Response();
493
494 try {
495 $data = self::check_valid();
496 $course_id = $data['course_id'] ?? 0;
497 $item_id = $data['item_id'] ?? 0;
498 $item_type = $data['item_type'] ?? '';
499 $enable_preview = $data['enable_preview'] ?? 0;
500
501 $courseModel = CourseModel::find( $course_id, true );
502 if ( ! $courseModel ) {
503 throw new Exception( __( 'Course not found', 'learnpress' ) );
504 }
505
506 if ( $item_type !== LP_LESSON_CPT ) {
507 throw new Exception( __( 'Only lesson can be set preview', 'learnpress' ) );
508 }
509
510 /**
511 * @var $itemModel LessonPostModel
512 */
513 $itemModel = $courseModel->get_item_model( $item_id, $item_type );
514 if ( ! $itemModel ) {
515 throw new Exception( __( 'Item not found', 'learnpress' ) );
516 }
517
518 $itemModel->set_preview( $enable_preview == 1 );
519
520 // Save course to update preview data of item in the curriculum.
521 $coursePostModel = new CoursePostModel( $courseModel );
522 $coursePostModel->save();
523
524 $response->status = 'success';
525 $response->message = __( 'Item updated successfully', 'learnpress' );
526 } catch ( Throwable $e ) {
527 $response->message = $e->getMessage();
528 }
529
530 wp_send_json( $response );
531 }
532 }
533