PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.2
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.2, at inc/Ajax/EditCurriculumAjax.php

527 lines 15.1 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.1
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 $response->data->section_item = $courseSectionItemModel;
239
240 /**
241 * @var $itemModel PostModel
242 */
243 $itemModel = $courseModel->get_item_model(
244 $courseSectionItemModel->item_id,
245 $courseSectionItemModel->item_type
246 );
247 $response->data->item_link = $itemModel ? $itemModel->get_edit_link() : '';
248
249 $response->status = 'success';
250 $response->message = __( 'Item added to section successfully', 'learnpress' );
251 } catch ( Throwable $e ) {
252 $response->message = $e->getMessage();
253 }
254
255 wp_send_json( $response );
256 }
257
258 /**
259 * Add items selected to section
260 *
261 * $data['course_id'] => ID of course
262 * $data['section_id'] => ID of section
263 * $data['items'] => [ item_id => 0, item_type => '' ]
264 *
265 * JS file edit-section-item.js: function addItemsSelectedToSection call this method.
266 *
267 * @since 4.2.8.6
268 * @version 1.0.1
269 */
270 public static function add_items_to_section() {
271 $response = new LP_REST_Response();
272
273 try {
274 $data = self::check_valid();
275 $course_id = $data['course_id'] ?? 0;
276 $section_id = $data['section_id'] ?? 0;
277
278 $courseModel = CourseModel::find( $course_id, true );
279 if ( ! $courseModel ) {
280 throw new Exception( __( 'Course not found', 'learnpress' ) );
281 }
282
283 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
284 if ( ! $courseSectionModel ) {
285 throw new Exception( __( 'Section not found', 'learnpress' ) );
286 }
287
288 $courseSectionItems = $courseSectionModel->add_items( $data );
289 if ( empty( $courseSectionItems ) ) {
290 throw new Exception( __( 'No items were added to the section', 'learnpress' ) );
291 }
292
293 // Set context_data for Course Builder context
294 $is_course_builder = ! empty( $data['is_course_builder'] );
295 AdminEditCurriculumTemplate::instance()->context_data = [
296 'is_course_builder' => $is_course_builder,
297 ];
298
299 $response->data->html = '';
300 /**
301 * @var $courseSectionItem CourseSectionItemModel
302 */
303 foreach ( $courseSectionItems as $courseSectionItem ) {
304 $courseSectionItemAlias = (object) get_object_vars( $courseSectionItem );
305 $itemModel = $courseModel->get_item_model(
306 $courseSectionItem->item_id,
307 $courseSectionItem->item_type
308 );
309 $courseSectionItemAlias->title = $itemModel ? $itemModel->get_the_title() : '';
310 $response->data->html .= AdminEditCurriculumTemplate::instance()->html_section_item(
311 $courseModel,
312 $courseSectionItemAlias
313 );
314 }
315
316 $response->status = 'success';
317 $response->message = __( 'Items added to section successfully', 'learnpress' );
318 } catch ( Throwable $e ) {
319 $response->message = $e->getMessage();
320 }
321
322 wp_send_json( $response );
323 }
324
325 /**
326 * Delete item from section
327 *
328 * $data['course_id'] => ID of course
329 * $data['section_id'] => ID of section
330 * $data['item_id'] => ID of item to delete
331 *
332 * JS file edit-section-item.js: function deleteItem call this method.
333 */
334 public static function delete_item_from_section() {
335 $response = new LP_REST_Response();
336
337 try {
338 $data = self::check_valid();
339 $course_id = $data['course_id'] ?? 0;
340 $section_id = $data['section_id'] ?? 0;
341 $item_id = $data['item_id'] ?? 0;
342
343 $courseModel = CourseModel::find( $course_id, true );
344 if ( ! $courseModel ) {
345 throw new Exception( __( 'Course not found', 'learnpress' ) );
346 }
347
348 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
349 if ( ! $courseSectionModel ) {
350 throw new Exception( __( 'Section not found', 'learnpress' ) );
351 }
352
353 // Find item of section id
354 $courseSectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
355 if ( ! $courseSectionItemModel ) {
356 throw new Exception( __( 'Item not found in section', 'learnpress' ) );
357 }
358
359 // Delete item from section
360 $courseSectionItemModel->section_course_id = $course_id;
361 $courseSectionItemModel->delete();
362
363 $response->status = 'success';
364 $response->message = __( 'Item deleted from section successfully', 'learnpress' );
365 } catch ( Throwable $e ) {
366 $response->message = $e->getMessage();
367 }
368
369 wp_send_json( $response );
370 }
371
372 /**
373 * Update item on new section and position
374 *
375 * $data['course_id'] => ID of course
376 * $data['items_position'] => list of item id by order on section new
377 * $data['item_id_change'] => ID of item to change section
378 * $data['section_id_new_of_item'] => ID of new section of item
379 * $data['section_id_old_of_item'] => ID of old section of item
380 *
381 * JS file edit-section-item.js: function sortAbleItem call this method.
382 *
383 * @since 4.2.8.6
384 * @version 1.0.1
385 */
386 public static function update_item_section_and_position() {
387 $response = new LP_REST_Response();
388
389 try {
390 $data = self::check_valid();
391 $course_id = $data['course_id'] ?? 0;
392
393 $courseModel = CourseModel::find( $course_id, true );
394 if ( ! $courseModel ) {
395 throw new Exception( __( 'Course not found', 'learnpress' ) );
396 }
397
398 $coursePostModel = new CoursePostModel( $courseModel );
399 $coursePostModel->update_items_position( $data );
400
401 $response->status = 'success';
402 $response->message = __( 'Item position updated successfully', 'learnpress' );
403 } catch ( Throwable $e ) {
404 $response->message = $e->getMessage();
405 }
406
407 wp_send_json( $response );
408 }
409
410 /**
411 * Update data of item in section
412 *
413 * $data['course_id'] => ID of course
414 * $data['section_id'] => ID of section
415 * $data['item_id'] => ID of item to update
416 * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
417 * $data['item_title'] => New title of the item
418 *
419 * JS file edit-section-item.js: function updateTitle call this method.
420 *
421 * @since 4.2.8.6
422 * @version 1.0.0
423 */
424 public static function update_item_of_section() {
425 $response = new LP_REST_Response();
426
427 try {
428 $data = self::check_valid();
429 $course_id = $data['course_id'] ?? 0;
430 $section_id = $data['section_id'] ?? 0;
431 $item_id = $data['item_id'] ?? 0;
432 $item_type = $data['item_type'] ?? '';
433 $item_title = $data['item_title'] ?? '';
434
435 if ( empty( $item_title ) ) {
436 throw new Exception( __( 'Item title is required', 'learnpress' ) );
437 }
438
439 $courseModel = CourseModel::find( $course_id, true );
440 if ( ! $courseModel ) {
441 throw new Exception( __( 'Course not found', 'learnpress' ) );
442 }
443
444 $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
445 if ( ! $courseSectionModel ) {
446 throw new Exception( __( 'Section not found', 'learnpress' ) );
447 }
448
449 $sectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
450 if ( ! $sectionItemModel ) {
451 throw new Exception( __( 'Item not found in section', 'learnpress' ) );
452 }
453
454 /**
455 * @var $itemModel PostModel
456 */
457 $itemModel = $courseModel->get_item_model( $item_id, $item_type );
458 if ( ! $itemModel ) {
459 throw new Exception( __( 'Item not found', 'learnpress' ) );
460 }
461
462 $itemModel->post_title = $item_title;
463 $itemModel->save();
464
465 $courseModel->sections_items = null;
466 $courseModel->save();
467
468 $response->status = 'success';
469 $response->message = __( 'Item updated successfully', 'learnpress' );
470 } catch ( Throwable $e ) {
471 $response->message = $e->getMessage();
472 }
473
474 wp_send_json( $response );
475 }
476
477 /**
478 * Update item preview.
479 *
480 * JS file edit-section-item.js: function updatePreviewItem call this method.
481 *
482 * @since 4.2.8.6
483 * @version 1.0.2
484 */
485 public static function update_item_preview() {
486 $response = new LP_REST_Response();
487
488 try {
489 $data = self::check_valid();
490 $course_id = $data['course_id'] ?? 0;
491 $item_id = $data['item_id'] ?? 0;
492 $item_type = $data['item_type'] ?? '';
493 $enable_preview = $data['enable_preview'] ?? 0;
494
495 $courseModel = CourseModel::find( $course_id, true );
496 if ( ! $courseModel ) {
497 throw new Exception( __( 'Course not found', 'learnpress' ) );
498 }
499
500 if ( $item_type !== LP_LESSON_CPT ) {
501 throw new Exception( __( 'Only lesson can be set preview', 'learnpress' ) );
502 }
503
504 /**
505 * @var $itemModel LessonPostModel
506 */
507 $itemModel = $courseModel->get_item_model( $item_id, $item_type );
508 if ( ! $itemModel ) {
509 throw new Exception( __( 'Item not found', 'learnpress' ) );
510 }
511
512 $itemModel->set_preview( $enable_preview == 1 );
513
514 // Save course to update preview data of item in the curriculum.
515 $coursePostModel = new CoursePostModel( $courseModel );
516 $coursePostModel->save();
517
518 $response->status = 'success';
519 $response->message = __( 'Item updated successfully', 'learnpress' );
520 } catch ( Throwable $e ) {
521 $response->message = $e->getMessage();
522 }
523
524 wp_send_json( $response );
525 }
526 }
527