| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Ajax; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Databases\QuizQuestionsDB; |
| 7 |
use LearnPress\Models\CourseModel; |
| 8 |
use LearnPress\Models\CourseSectionItemModel; |
| 9 |
use LearnPress\Models\Question\QuestionAnswerModel; |
| 10 |
use LearnPress\Models\Question\QuestionPostFIBModel; |
| 11 |
use LearnPress\Models\Question\QuestionPostModel; |
| 12 |
use LearnPress\Models\Quiz\QuizQuestionModel; |
| 13 |
use LearnPress\Models\QuizPostModel; |
| 14 |
use LearnPress\TemplateHooks\Admin\AdminEditQizTemplate; |
| 15 |
use LearnPress\TemplateHooks\Admin\AdminEditQuestionTemplate; |
| 16 |
use LP_Helper; |
| 17 |
use LP_REST_Response; |
| 18 |
use Throwable; |
| 19 |
|
| 20 |
/** |
| 21 |
* class EditQuizAjax |
| 22 |
* |
| 23 |
* This class handles the AJAX request to edit the quiz. |
| 24 |
* |
| 25 |
* @since 4.2.9 |
| 26 |
* @version 1.0.1 |
| 27 |
*/ |
| 28 |
class EditQuizAjax extends AbstractAjax { |
| 29 |
/** |
| 30 |
* Check permissions and validate parameters. |
| 31 |
* |
| 32 |
* @throws Exception |
| 33 |
* |
| 34 |
* @since 4.2.9 |
| 35 |
* @version 1.0.0 |
| 36 |
*/ |
| 37 |
public static function check_valid() { |
| 38 |
$params = wp_unslash( $_REQUEST['data'] ?? '' ); |
| 39 |
if ( empty( $params ) ) { |
| 40 |
throw new Exception( 'Error: params invalid!' ); |
| 41 |
} |
| 42 |
|
| 43 |
$params = LP_Helper::json_decode( $params, true ); |
| 44 |
$quiz_id = $params['quiz_id'] ?? 0; |
| 45 |
$quizPostModel = QuizPostModel::find( $quiz_id, true ); |
| 46 |
if ( ! $quizPostModel ) { |
| 47 |
throw new Exception( __( 'Quiz not found', 'learnpress' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
$params['quizPostModel'] = $quizPostModel; |
| 51 |
|
| 52 |
return $params; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Create a new question and add it to the quiz. |
| 57 |
* |
| 58 |
* JS file edit-section.js: function addSection call this method to update the section description. |
| 59 |
* |
| 60 |
* @since 4.2.9 |
| 61 |
* @version 1.0.0 |
| 62 |
*/ |
| 63 |
public static function create_question_add_to_quiz() { |
| 64 |
$response = new LP_REST_Response(); |
| 65 |
|
| 66 |
try { |
| 67 |
$data = self::check_valid(); |
| 68 |
/** |
| 69 |
* @var QuizPostModel $quizPostModel |
| 70 |
*/ |
| 71 |
$quizPostModel = $data['quizPostModel']; |
| 72 |
|
| 73 |
$quizQuestionModel = $quizPostModel->create_question_and_add( $data ); |
| 74 |
$questionPostModel = $quizQuestionModel->get_question_post_model(); |
| 75 |
$html_edit_question = AdminEditQizTemplate::instance()->html_edit_question( $questionPostModel ); |
| 76 |
|
| 77 |
$response->data->question = $questionPostModel; |
| 78 |
$response->data->html_edit_question = $html_edit_question; |
| 79 |
$response->status = 'success'; |
| 80 |
$response->message = __( 'Question added successfully', 'learnpress' ); |
| 81 |
} catch ( Throwable $e ) { |
| 82 |
$response->message = $e->getMessage(); |
| 83 |
} |
| 84 |
|
| 85 |
wp_send_json( $response ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add questions exists (from Question Bank) to quiz. |
| 90 |
* |
| 91 |
* @since 4.2.9 |
| 92 |
* @version 1.0.1 |
| 93 |
*/ |
| 94 |
public static function add_questions_to_quiz() { |
| 95 |
$response = new LP_REST_Response(); |
| 96 |
|
| 97 |
try { |
| 98 |
$data = self::check_valid(); |
| 99 |
/** |
| 100 |
* @var QuizPostModel $quizPostModel |
| 101 |
*/ |
| 102 |
$quizPostModel = $data['quizPostModel']; |
| 103 |
$questions_added = $quizPostModel->add_questions_to_quiz( $data ); |
| 104 |
|
| 105 |
/** |
| 106 |
* @var QuizQuestionModel $quizQuestionModel |
| 107 |
*/ |
| 108 |
foreach ( $questions_added as $quizQuestionModel ) { |
| 109 |
$questionPostModel = $quizQuestionModel->get_question_post_model(); |
| 110 |
$response->data->html_edit_question[ $quizQuestionModel->question_id ] = AdminEditQizTemplate::instance()->html_edit_question( $questionPostModel ); |
| 111 |
} |
| 112 |
|
| 113 |
$response->status = 'success'; |
| 114 |
$response->message = __( 'Question added successfully', 'learnpress' ); |
| 115 |
} catch ( Throwable $e ) { |
| 116 |
$response->message = $e->getMessage(); |
| 117 |
} |
| 118 |
|
| 119 |
wp_send_json( $response ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Remove question from quiz. |
| 124 |
* |
| 125 |
* JS file edit-quiz.js: function removeQuestion call this method. |
| 126 |
* |
| 127 |
* @since 4.2.9 |
| 128 |
* @version 1.0.0 |
| 129 |
*/ |
| 130 |
public static function remove_question_from_quiz() { |
| 131 |
$response = new LP_REST_Response(); |
| 132 |
|
| 133 |
try { |
| 134 |
$data = self::check_valid(); |
| 135 |
$question_id = $data['question_id'] ?? 0; |
| 136 |
|
| 137 |
/** |
| 138 |
* @var QuizPostModel $quizPostModel |
| 139 |
*/ |
| 140 |
$quizPostModel = $data['quizPostModel']; |
| 141 |
$quizPostModel->remove_question_from_quiz( $question_id ); |
| 142 |
|
| 143 |
$response->status = 'success'; |
| 144 |
$response->message = __( 'Question removed successfully', 'learnpress' ); |
| 145 |
} catch ( Throwable $e ) { |
| 146 |
$response->message = $e->getMessage(); |
| 147 |
} |
| 148 |
|
| 149 |
wp_send_json( $response ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Update item on new section and position |
| 154 |
* |
| 155 |
* $data['course_id'] => ID of course |
| 156 |
* $data['items_position'] => list of item id by order on section new |
| 157 |
* $data['item_id_change'] => ID of item to change section |
| 158 |
* $data['section_id_new_of_item'] => ID of new section of item |
| 159 |
* $data['section_id_old_of_item'] => ID of old section of item |
| 160 |
* |
| 161 |
* JS file edit-section-item.js: function sortAbleItem call this method. |
| 162 |
* |
| 163 |
* @since 4.2.9 |
| 164 |
* @version 1.0.1 |
| 165 |
*/ |
| 166 |
public static function update_questions_position() { |
| 167 |
$response = new LP_REST_Response(); |
| 168 |
|
| 169 |
try { |
| 170 |
$data = self::check_valid(); |
| 171 |
/** |
| 172 |
* @var QuizPostModel $quizPostModel |
| 173 |
*/ |
| 174 |
$quizPostModel = $data['quizPostModel']; |
| 175 |
|
| 176 |
$quizPostModel->update_question_position( $data ); |
| 177 |
|
| 178 |
$response->status = 'success'; |
| 179 |
$response->message = __( 'Question position updated successfully', 'learnpress' ); |
| 180 |
} catch ( Throwable $e ) { |
| 181 |
$response->message = $e->getMessage(); |
| 182 |
} |
| 183 |
|
| 184 |
wp_send_json( $response ); |
| 185 |
} |
| 186 |
} |
| 187 |
|