| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_REST_Users_Controller |
| 5 |
* |
| 6 |
* @since 3.3.0 |
| 7 |
*/ |
| 8 |
class LP_REST_Admin_Course_Controller extends LP_Abstract_REST_Controller { |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
$this->namespace = 'lp/v1/admin'; |
| 12 |
$this->rest_base = 'course'; |
| 13 |
|
| 14 |
parent::__construct(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Register rest routes. |
| 19 |
*/ |
| 20 |
public function register_routes() { |
| 21 |
$this->routes = array( |
| 22 |
'get_final_quiz' => array( |
| 23 |
array( |
| 24 |
'methods' => WP_REST_Server::CREATABLE, |
| 25 |
'callback' => array( $this, 'get_final_quiz' ), |
| 26 |
'permission_callback' => function() { |
| 27 |
return current_user_can( 'edit_posts' ); |
| 28 |
}, |
| 29 |
), |
| 30 |
), |
| 31 |
); |
| 32 |
|
| 33 |
parent::register_routes(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get Final Quiz in Course Settings. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function get_final_quiz( WP_REST_Request $request ) { |
| 42 |
$params = $request->get_params(); |
| 43 |
$course_id = $params['courseId'] ?? false; |
| 44 |
$response = new LP_REST_Response(); |
| 45 |
$response->data = ''; |
| 46 |
$final_quiz = ''; |
| 47 |
|
| 48 |
try { |
| 49 |
if ( empty( $course_id ) ) { |
| 50 |
throw new Exception( esc_html__( 'No Course ID available!', 'learnpress' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
$course = learn_press_get_course( $course_id ); |
| 54 |
|
| 55 |
if ( ! $course ) { |
| 56 |
throw new Exception( esc_html__( 'No Course available!', 'learnpress' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
$items = $course->get_item_ids(); |
| 60 |
|
| 61 |
if ( $items ) { |
| 62 |
foreach ( $items as $item ) { |
| 63 |
if ( learn_press_get_post_type( $item ) === LP_QUIZ_CPT ) { |
| 64 |
$final_quiz = $item; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! empty( $final_quiz ) ) { |
| 70 |
update_post_meta( $course_id, '_lp_final_quiz', $final_quiz ); |
| 71 |
$passing_grade = get_post_meta( $final_quiz, '_lp_passing_grade', true ); |
| 72 |
|
| 73 |
$post_type_object = get_post_type_object( LP_QUIZ_CPT ); |
| 74 |
$url = admin_url( sprintf( $post_type_object->_edit_link . '&action=edit#_lp_passing_grade', $final_quiz ) ); |
| 75 |
|
| 76 |
ob_start(); |
| 77 |
?> |
| 78 |
<div class="lp-metabox-evaluate-final_quiz__message"> |
| 79 |
<?php printf( esc_html__( 'Passing Grade: %s', 'learpress' ), $passing_grade . '%' ); ?> |
| 80 |
- |
| 81 |
<?php printf( esc_html__( 'Edit: %s', 'learnpress' ), '<a href="' . esc_url_raw( $url ) . '">' . get_the_title( $final_quiz ) . '</a>' ); ?> |
| 82 |
</div> |
| 83 |
<?php |
| 84 |
$response->status = 'success'; |
| 85 |
$response->data = ob_get_clean(); |
| 86 |
} else { |
| 87 |
delete_post_meta( $course_id, '_lp_final_quiz' ); |
| 88 |
$response->data = '<div class="lp-metabox-evaluate-final_quiz__message lp-metabox-evaluate-final_quiz__message-error">' . esc_html__( 'No Quiz in this course!', 'learnpress' ) . '</div>'; |
| 89 |
} |
| 90 |
} catch ( Exception $e ) { |
| 91 |
$response->message = $e->getMessage(); |
| 92 |
} |
| 93 |
|
| 94 |
return rest_ensure_response( $response ); |
| 95 |
} |
| 96 |
} |
| 97 |
|