| @@ -1,654 +1,654 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Class LP_Admin_Editor_Quiz | |
| 5 | - * | |
| 6 | - * @since 3.0.2 | |
| 7 | - */ | |
| 8 | -class LP_Admin_Editor_Quiz extends LP_Admin_Editor { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * @var LP_Quiz_CURD | |
| 12 | - */ | |
| 13 | - protected $quiz_curd = null; | |
| 14 | - | |
| 15 | - /** | |
| 16 | - * @var LP_Question_CURD | |
| 17 | - */ | |
| 18 | - protected $question_curd = null; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * @var LP_Quiz | |
| 22 | - */ | |
| 23 | - protected $quiz = null; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * LP_Admin_Editor_Quiz constructor. | |
| 27 | - */ | |
| 28 | - public function __construct() { | |
| 29 | - } | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Do the action depending on ajax calls with params | |
| 33 | - * | |
| 34 | - * @return bool|mixed|WP_Error | |
| 35 | - */ | |
| 36 | - public function dispatch() { | |
| 37 | - check_ajax_referer( 'learnpress_admin_quiz_editor', 'nonce' ); | |
| 38 | - $args = wp_parse_args( | |
| 39 | - $_REQUEST, | |
| 40 | - array( | |
| 41 | - 'id' => false, | |
| 42 | - 'type' => '', | |
| 43 | - ) | |
| 44 | - ); | |
| 45 | - | |
| 46 | - // get quiz | |
| 47 | - $quiz_id = $args['id']; | |
| 48 | - $quiz = learn_press_get_quiz( $quiz_id ); | |
| 49 | - | |
| 50 | - if ( ! $quiz ) { | |
| 51 | - return new WP_Error( 'INVALID_QUIZ', __( 'Invalid quiz', 'learnpress' ) ); | |
| 52 | - } | |
| 53 | - | |
| 54 | - $this->quiz = $quiz; | |
| 55 | - $this->quiz_curd = new LP_Quiz_CURD(); | |
| 56 | - $this->question_curd = new LP_Question_CURD(); | |
| 57 | - $this->result = array( 'status' => false ); | |
| 58 | - | |
| 59 | - $this->call( $args['type'], array( $args ) ); | |
| 60 | - | |
| 61 | - return $this->get_result(); | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * Get question data in admin quiz editor. | |
| 66 | - * | |
| 67 | - * @since 3.0.0 | |
| 68 | - * | |
| 69 | - * @param $question | |
| 70 | - * @param $object | if true, input in question object, do not need init LP_Question::get_question() | |
| 71 | - * @param array $args | |
| 72 | - * | |
| 73 | - * @return array | |
| 74 | - */ | |
| 75 | - public function get_question_data_to_quiz_editor( $question, $object = false, $args = array() ) { | |
| 76 | - | |
| 77 | - if ( ! $object || ! $question ) { | |
| 78 | - if ( get_post_type( $question ) !== LP_QUESTION_CPT ) { | |
| 79 | - return array(); | |
| 80 | - } | |
| 81 | - | |
| 82 | - // get question | |
| 83 | - $question = LP_Question::get_question( $question ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - // question id | |
| 87 | - $question_id = $question->get_id(); | |
| 88 | - // question answer | |
| 89 | - $answer_options = $question->get_data( 'answer_options' ); | |
| 90 | - $answer = array(); | |
| 91 | - foreach ( $answer_options as $answer_option ) { | |
| 92 | - if ( ! isset( $answer[ $answer_option['question_answer_id'] ] ) ) { | |
| 93 | - $answer[ $answer_option['question_answer_id'] ] = $answer_option; | |
| 94 | - } | |
| 95 | - } | |
| 96 | - $answers = array_values( $answer ); | |
| 97 | - $data = wp_parse_args( | |
| 98 | - $args, | |
| 99 | - array( | |
| 100 | - 'id' => $question_id, | |
| 101 | - 'open' => false, | |
| 102 | - 'title' => get_the_title( $question_id ), | |
| 103 | - 'type' => array( | |
| 104 | - 'key' => $question->get_type(), | |
| 105 | - 'label' => $question->get_type_label(), | |
| 106 | - ), | |
| 107 | - 'answers' => $answers, | |
| 108 | - 'settings' => array( | |
| 109 | - 'mark' => get_post_meta( $question_id, '_lp_mark', true ), | |
| 110 | - 'explanation' => get_post_meta( $question_id, '_lp_explanation', true ), | |
| 111 | - 'hint' => get_post_meta( $question_id, '_lp_hint', true ), | |
| 112 | - ), | |
| 113 | - 'order' => count( $answers ), | |
| 114 | - ) | |
| 115 | - ); | |
| 116 | - | |
| 117 | - return $data; | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * @param array $args | |
| 122 | - * | |
| 123 | - * @return bool | |
| 124 | - */ | |
| 125 | - public function hidden_questions( $args = array() ) { | |
| 126 | - $hidden = ! empty( $args['hidden'] ) ? $args['hidden'] : false; | |
| 127 | - update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden ); | |
| 128 | - | |
| 129 | - return true; | |
| 130 | - } | |
| 131 | - | |
| 132 | - /** | |
| 133 | - * @param array $args | |
| 134 | - * | |
| 135 | - * @return bool | |
| 136 | - */ | |
| 137 | - public function new_question( $args = array() ) { | |
| 138 | - $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 139 | - $question = json_decode( wp_unslash( $question ), true ); | |
| 140 | - | |
| 141 | - if ( ! $question ) { | |
| 142 | - return false; | |
| 143 | - } | |
| 144 | - | |
| 145 | - $quiz_id = $this->quiz->get_id(); | |
| 146 | - | |
| 147 | - // draft quiz | |
| 148 | - if ( get_post_status( $quiz_id ) == 'auto-draft' ) { | |
| 149 | - | |
| 150 | - $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : ''; | |
| 151 | - $draft_quiz = (array) ( json_decode( wp_unslash( $draft_quiz ), '' ) ); | |
| 152 | - | |
| 153 | - $quiz_args = array( | |
| 154 | - 'id' => $this->quiz->get_id(), | |
| 155 | - 'title' => $draft_quiz['title'] ? $draft_quiz['title'] : __( 'New Quiz', 'learnpress' ), | |
| 156 | - 'content' => $draft_quiz['content'], | |
| 157 | - 'status' => 'draft', | |
| 158 | - ); | |
| 159 | - | |
| 160 | - $quiz_id = $this->quiz_curd->create( $quiz_args ); | |
| 161 | - } | |
| 162 | - | |
| 163 | - if ( ! isset( $quiz_id ) ) { | |
| 164 | - $this->result = new WP_Error( 'CREATE_QUIZ_FAILED', __( 'Quiz creation failed.', 'learnpress' ) ); | |
| 165 | - | |
| 166 | - return false; | |
| 167 | - } | |
| 168 | - | |
| 169 | - $args = array( | |
| 170 | - 'quiz_id' => $quiz_id, | |
| 171 | - 'title' => $question['title'], | |
| 172 | - 'type' => $question['type'], | |
| 173 | - ); | |
| 174 | - | |
| 175 | - $new_question = $this->question_curd->create( $args ); | |
| 176 | - $this->clear_cache_of_quiz_questions( $quiz_id ); | |
| 177 | - | |
| 178 | - if ( ! is_wp_error( $new_question ) ) { | |
| 179 | - // update hidden questions in quiz meta | |
| 180 | - $quiz = LP_Quiz::get_quiz( $quiz_id ); | |
| 181 | - $hidden_questions = $quiz->get_question_ids(); | |
| 182 | - | |
| 183 | - if ( $hidden_questions ) { | |
| 184 | - $index = array_search( $new_question->get_id(), $hidden_questions ); | |
| 185 | - if ( $index !== false ) { | |
| 186 | - unset( $hidden_questions[ $index ] ); | |
| 187 | - } | |
| 188 | - //$hidden_questions = array_keys( $hidden_questions ); | |
| 189 | - } | |
| 190 | - | |
| 191 | - update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions ); | |
| 192 | - | |
| 193 | - // get new question data | |
| 194 | - $this->result = $this->get_question_data_to_quiz_editor( $new_question, true, array( 'open' => true ) ); | |
| 195 | - | |
| 196 | - if ( isset( $question['id'] ) ) { | |
| 197 | - $this->result['temp_id'] = $question['id']; | |
| 198 | - } | |
| 199 | - | |
| 200 | - return true; | |
| 201 | - } | |
| 202 | - | |
| 203 | - return false; | |
| 204 | - } | |
| 205 | - | |
| 206 | - /** | |
| 207 | - * @param array $args | |
| 208 | - * | |
| 209 | - * @return bool | |
| 210 | - */ | |
| 211 | - public function sort_questions( $args = array() ) { | |
| 212 | - $order = ! empty( $args['order'] ) ? $args['order'] : false; | |
| 213 | - $order = json_decode( wp_unslash( $order ), true ); | |
| 214 | - | |
| 215 | - if ( ! $order ) { | |
| 216 | - return false; | |
| 217 | - } | |
| 218 | - | |
| 219 | - $this->result = $this->quiz_curd->sort_questions( $order ); | |
| 220 | - | |
| 221 | - return true; | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * @param array $args | |
| 226 | - * | |
| 227 | - * @return bool | |
| 228 | - */ | |
| 229 | - public function update_question_title( $args = array() ) { | |
| 230 | - $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 231 | - $question = json_decode( wp_unslash( $question ), true ); | |
| 232 | - | |
| 233 | - if ( ! $question ) { | |
| 234 | - return false; | |
| 235 | - } | |
| 236 | - | |
| 237 | - wp_update_post( | |
| 238 | - array( | |
| 239 | - 'ID' => $question['id'], | |
| 240 | - 'post_title' => $question['title'], | |
| 241 | - ) | |
| 242 | - ); | |
| 243 | - | |
| 244 | - $this->result['status'] = true; | |
| 245 | - | |
| 246 | - return true; | |
| 247 | - } | |
| 248 | - | |
| 249 | - /** | |
| 250 | - * @param array $args | |
| 251 | - * | |
| 252 | - * @return bool | |
| 253 | - */ | |
| 254 | - public function change_question_type( $args = array() ) { | |
| 255 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 256 | - $type = ! empty( $args['question_type'] ) ? $args['question_type'] : false; | |
| 257 | - | |
| 258 | - if ( ! ( $question_id || $type ) ) { | |
| 259 | - return false; | |
| 260 | - } | |
| 261 | - | |
| 262 | - $question = LP_Question::get_question( $question_id ); | |
| 263 | - | |
| 264 | - $this->question_curd->change_question_type( $question, $type ); | |
| 265 | - | |
| 266 | - $this->result = $this->get_question_data_to_quiz_editor( $question, true ); | |
| 267 | - | |
| 268 | - return true; | |
| 269 | - } | |
| 270 | - | |
| 271 | - /** | |
| 272 | - * @param array $args | |
| 273 | - * | |
| 274 | - * @return bool | |
| 275 | - */ | |
| 276 | - public function clone_question( $args = array() ) { | |
| 277 | - $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 278 | - $question = json_decode( wp_unslash( $question ), true ); | |
| 279 | - | |
| 280 | - if ( ! $question ) { | |
| 281 | - return false; | |
| 282 | - } | |
| 283 | - | |
| 284 | - // duplicate question | |
| 285 | - $new_question_id = $this->question_curd->duplicate( $question['id'], array( 'post_status' => 'publish' ) ); | |
| 286 | - $this->clear_cache_of_quiz_questions( $this->quiz->get_id() ); | |
| 287 | - | |
| 288 | - if ( ! is_wp_error( $new_question_id ) ) { | |
| 289 | - | |
| 290 | - // add question to hidden questions in quiz meta | |
| 291 | - $hidden_questions = get_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', true ); | |
| 292 | - if ( ! $hidden_questions ) { | |
| 293 | - $hidden_questions = array(); | |
| 294 | - } | |
| 295 | - $hidden_questions[] = $new_question_id;// add question to hidden questions in quiz meta | |
| 296 | - update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden_questions ); | |
| 297 | - | |
| 298 | - // add question to quiz | |
| 299 | - $this->quiz_curd->add_question( $this->quiz->get_id(), $new_question_id ); | |
| 300 | - | |
| 301 | - $this->result = $this->get_question_data_to_quiz_editor( $new_question_id ); | |
| 302 | - | |
| 303 | - return true; | |
| 304 | - } | |
| 305 | - | |
| 306 | - return false; | |
| 307 | - } | |
| 308 | - | |
| 309 | - /** | |
| 310 | - * @param array $args | |
| 311 | - * | |
| 312 | - * @return bool | |
| 313 | - */ | |
| 314 | - public function remove_question( $args = array() ) { | |
| 315 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 316 | - | |
| 317 | - if ( ! $question_id ) { | |
| 318 | - return false; | |
| 319 | - } | |
| 320 | - | |
| 321 | - $this->result = $this->quiz_curd->remove_questions( $this->quiz->get_id(), $question_id ); | |
| 322 | - $this->clear_cache_of_quiz_questions( $this->quiz->get_id() ); | |
| 323 | - | |
| 324 | - return true; | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** | |
| 328 | - * @param array $args | |
| 329 | - * | |
| 330 | - * @return bool | |
| 331 | - */ | |
| 332 | - public function delete_question( $args = array() ) { | |
| 333 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 334 | - | |
| 335 | - if ( ! $question_id ) { | |
| 336 | - return false; | |
| 337 | - } | |
| 338 | - | |
| 339 | - $this->result = wp_trash_post( $question_id ); | |
| 340 | - $this->clear_cache_of_quiz_questions( $this->quiz->get_id() ); | |
| 341 | - | |
| 342 | - return true; | |
| 343 | - } | |
| 344 | - | |
| 345 | - /** | |
| 346 | - * @param array $args | |
| 347 | - * | |
| 348 | - * @return bool | |
| 349 | - */ | |
| 350 | - public function sort_question_answers( $args = array() ) { | |
| 351 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 352 | - | |
| 353 | - $order = ! empty( $args['order'] ) ? $args['order'] : false; | |
| 354 | - $order = json_decode( wp_unslash( $order ), true ); | |
| 355 | - | |
| 356 | - if ( ! ( $question_id && $order ) ) { | |
| 357 | - return false; | |
| 358 | - } | |
| 359 | - | |
| 360 | - // sort answer | |
| 361 | - $this->result = $this->question_curd->sort_answers( $question_id, $order ); | |
| 362 | - | |
| 363 | - return true; | |
| 364 | - } | |
| 365 | - | |
| 366 | - /** | |
| 367 | - * @param array $args | |
| 368 | - * | |
| 369 | - * @return bool | |
| 370 | - */ | |
| 371 | - public function update_question_answer_title( $args = array() ) { | |
| 372 | - // question id | |
| 373 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 374 | - | |
| 375 | - // answers | |
| 376 | - $answer = ! empty( $args['answer'] ) ? $args['answer'] : false; | |
| 377 | - $answer = json_decode( wp_unslash( $answer ), true ); | |
| 378 | - | |
| 379 | - if ( ! ( $question_id && $answer ) ) { | |
| 380 | - return false; | |
| 381 | - } | |
| 382 | - | |
| 383 | - // update answer title | |
| 384 | - $this->result = $this->question_curd->update_answer_title( $question_id, $answer ); | |
| 385 | - | |
| 386 | - return true; | |
| 387 | - } | |
| 388 | - | |
| 389 | - /** | |
| 390 | - * @param array $args | |
| 391 | - * | |
| 392 | - * @return bool | |
| 393 | - */ | |
| 394 | - public function change_question_correct_answer( $args = array() ) { | |
| 395 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 396 | - | |
| 397 | - // correct answer | |
| 398 | - $correct = ! empty( $args['correct'] ) ? $args['correct'] : false; | |
| 399 | - $correct = json_decode( wp_unslash( $correct ), true ); | |
| 400 | - | |
| 401 | - if ( ! ( $question_id && $correct ) ) { | |
| 402 | - return false; | |
| 403 | - } | |
| 404 | - | |
| 405 | - $question = LP_Question::get_question( $question_id ); | |
| 406 | - // update correct answer, get new question | |
| 407 | - $question = $this->question_curd->change_correct_answer( $question, $correct ); | |
| 408 | - | |
| 409 | - $this->result = $this->get_question_data_to_quiz_editor( $question, true ); | |
| 410 | - | |
| 411 | - return true; | |
| 412 | - } | |
| 413 | - | |
| 414 | - /** | |
| 415 | - * @param array $args | |
| 416 | - * | |
| 417 | - * @return bool | |
| 418 | - */ | |
| 419 | - public function delete_question_answer( $args = array() ) { | |
| 420 | - $question_id = LP_Helper::sanitize_params_submitted( $_POST['question_id'] ?? 0 ); | |
| 421 | - $answer_id = LP_Helper::sanitize_params_submitted( $_POST['answer_id'] ?? 0 ); | |
| 422 | - | |
| 423 | - if ( ! $question_id || ! $answer_id ) { | |
| 424 | - return false; | |
| 425 | - } | |
| 426 | - | |
| 427 | - $this->result = $this->question_curd->delete_answer( $question_id, $answer_id ); | |
| 428 | - | |
| 429 | - return true; | |
| 430 | - } | |
| 431 | - | |
| 432 | - /** | |
| 433 | - * @param array $args | |
| 434 | - * | |
| 435 | - * @return bool | |
| 436 | - */ | |
| 437 | - public function new_question_answer( $args = array() ) { | |
| 438 | - $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 439 | - | |
| 440 | - if ( ! $question_id ) { | |
| 441 | - return false; | |
| 442 | - } | |
| 443 | - | |
| 444 | - $answer = LP_Question::get_default_answer(); | |
| 445 | - $new_answer_id = $this->question_curd->new_answer( $question_id, $answer ); | |
| 446 | - $question = LP_Question::get_question( $question_id ); | |
| 447 | - | |
| 448 | - if ( $new_answer_id ) { | |
| 449 | - $this->result = array_merge( | |
| 450 | - $answer, | |
| 451 | - array( | |
| 452 | - 'temp_id' => isset( $args['question_answer_id'] ) ? $args['question_answer_id'] : 0, | |
| 453 | - 'question_answer_id' => $new_answer_id, | |
| 454 | - 'question_id' => $question_id, | |
| 455 | - 'order' => count( $question->get_data( 'answer_options' ) ), | |
| 456 | - ) | |
| 457 | - ); | |
| 458 | - | |
| 459 | - return true; | |
| 460 | - } | |
| 461 | - | |
| 462 | - return false; | |
| 463 | - } | |
| 464 | - | |
| 465 | - public function update_quiz_questions_hidden( $args = array() ) { | |
| 466 | - $id = $args['id']; | |
| 467 | - $questions = isset( $args['hidden'] ) ? $args['hidden'] : false; | |
| 468 | - | |
| 469 | - if ( $questions ) { | |
| 470 | - update_post_meta( $id, '_hidden_questions_settings', $questions ); | |
| 471 | - } else { | |
| 472 | - delete_post_meta( $id, '_hidden_questions_settings' ); | |
| 473 | - } | |
| 474 | - | |
| 475 | - return true; | |
| 476 | - } | |
| 477 | - | |
| 478 | - /** | |
| 479 | - * @param array $args | |
| 480 | - * | |
| 481 | - * @return bool | |
| 482 | - */ | |
| 483 | - public function update_question_content( $args = array() ) { | |
| 484 | - $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 485 | - $question = json_decode( wp_unslash( $question ), true ); | |
| 486 | - | |
| 487 | - if ( ! $question ) { | |
| 488 | - return false; | |
| 489 | - } | |
| 490 | - | |
| 491 | - wp_update_post( | |
| 492 | - array( | |
| 493 | - 'ID' => $question['id'], | |
| 494 | - 'post_content' => $question['settings']['content'], | |
| 495 | - ) | |
| 496 | - ); | |
| 497 | - | |
| 498 | - $this->result['status'] = true; | |
| 499 | - | |
| 500 | - return true; | |
| 501 | - } | |
| 502 | - | |
| 503 | - /** | |
| 504 | - * @param array $args | |
| 505 | - * | |
| 506 | - * @return bool | |
| 507 | - */ | |
| 508 | - public function update_question_meta( $args = array() ) { | |
| 509 | - $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 510 | - $question = json_decode( wp_unslash( $question ), true ); | |
| 511 | - | |
| 512 | - $meta_key = ! empty( $args['meta_key'] ) ? $args['meta_key'] : false; | |
| 513 | - | |
| 514 | - if ( ! ( $question && $meta_key ) ) { | |
| 515 | - return false; | |
| 516 | - } | |
| 517 | - | |
| 518 | - update_post_meta( $question['id'], '_lp_' . $meta_key, $question['settings'][ $meta_key ] ); | |
| 519 | - | |
| 520 | - $this->result['status'] = true; | |
| 521 | - | |
| 522 | - return true; | |
| 523 | - } | |
| 524 | - | |
| 525 | - /** | |
| 526 | - * @param array $args | |
| 527 | - * | |
| 528 | - * @return bool | |
| 529 | - */ | |
| 530 | - public function search_items( $args = array() ) { | |
| 531 | - $query = ! empty( $args['query'] ) ? $args['query'] : ''; | |
| 532 | - $page = ! empty( $args['page'] ) ? intval( $args['page'] ) : 1; | |
| 533 | - $exclude = ! empty( $args['exclude'] ) ? intval( $args['exclude'] ) : ''; | |
| 534 | - | |
| 535 | - if ( $exclude ) { | |
| 536 | - $exclude = json_decode( $exclude, true ); | |
| 537 | - } | |
| 538 | - | |
| 539 | - $ids_exclude = array(); | |
| 540 | - if ( is_array( $exclude ) ) { | |
| 541 | - foreach ( $exclude as $item ) { | |
| 542 | - $ids_exclude[] = $item['id']; | |
| 543 | - } | |
| 544 | - } | |
| 545 | - | |
| 546 | - $search = new LP_Modal_Search_Items( | |
| 547 | - array( | |
| 548 | - 'type' => 'lp_question', | |
| 549 | - 'context' => 'quiz', | |
| 550 | - 'context_id' => $this->quiz->get_id(), | |
| 551 | - 'term' => $query, | |
| 552 | - 'limit' => apply_filters( 'learn-press/quiz-editor/choose-items-limit', 10 ), | |
| 553 | - 'paged' => $page, | |
| 554 | - 'exclude' => $ids_exclude, | |
| 555 | - ) | |
| 556 | - ); | |
| 557 | - | |
| 558 | - $ids_item = $search->get_items(); | |
| 559 | - | |
| 560 | - $items = array(); | |
| 561 | - foreach ( $ids_item as $id ) { | |
| 562 | - $post = get_post( $id ); | |
| 563 | - | |
| 564 | - $items[] = array( | |
| 565 | - 'id' => $post->ID, | |
| 566 | - 'title' => $post->post_title, | |
| 567 | - 'type' => $post->post_type, | |
| 568 | - ); | |
| 569 | - } | |
| 570 | - | |
| 571 | - $this->result = array( | |
| 572 | - 'items' => $items, | |
| 573 | - 'pagination' => $search->get_pagination( false ), | |
| 574 | - ); | |
| 575 | - | |
| 576 | - return true; | |
| 577 | - } | |
| 578 | - | |
| 579 | - /** | |
| 580 | - * @param array $args | |
| 581 | - * | |
| 582 | - * @return bool | |
| 583 | - */ | |
| 584 | - public function add_questions_to_quiz( $args = array() ) { | |
| 585 | - $questions = LP_Helper::sanitize_params_submitted( $_POST['items'] ?? '' ); | |
| 586 | - | |
| 587 | - if ( ! $questions ) { | |
| 588 | - return false; | |
| 589 | - } | |
| 590 | - | |
| 591 | - $questions = json_decode( $questions, true ); | |
| 592 | - | |
| 593 | - $quiz_id = $this->quiz->get_id(); | |
| 594 | - | |
| 595 | - if ( get_post_status( $quiz_id ) == 'auto-draft' ) { | |
| 596 | - $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : ''; | |
| 597 | - $draft_quiz = (array) json_decode( $draft_quiz, '' ); | |
| 598 | - | |
| 599 | - $quiz_args = array( | |
| 600 | - 'id' => $quiz_id, | |
| 601 | - 'title' => $draft_quiz['title'], | |
| 602 | - 'content' => $draft_quiz['content'], | |
| 603 | - 'status' => 'draft', | |
| 604 | - ); | |
| 605 | - | |
| 606 | - $quiz_id = $this->quiz_curd->create( $quiz_args ); | |
| 607 | - } | |
| 608 | - | |
| 609 | - if ( ! isset( $quiz_id ) ) { | |
| 610 | - $this->result = new WP_Error( __( 'Quiz creation failed.', 'learnpress' ) ); | |
| 611 | - | |
| 612 | - return false; | |
| 613 | - } | |
| 614 | - | |
| 615 | - if ( $questions ) { | |
| 616 | - $hidden_questions = get_post_meta( $quiz_id, '_lp_hidden_questions', true ); | |
| 617 | - | |
| 618 | - if ( ! $hidden_questions ) { | |
| 619 | - $hidden_questions = array(); | |
| 620 | - } | |
| 621 | - | |
| 622 | - foreach ( $questions as $key => $question ) { | |
| 623 | - $hidden_questions[] = $question['id']; | |
| 624 | - $this->quiz_curd->add_question( $quiz_id, $question['id'] ); | |
| 625 | - } | |
| 626 | - | |
| 627 | - $hidden_questions = array_unique( $hidden_questions ); | |
| 628 | - | |
| 629 | - update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions ); | |
| 630 | - | |
| 631 | - $this->clear_cache_of_quiz_questions( $quiz_id ); | |
| 632 | - $this->result = $this->quiz->quiz_editor_get_questions(); | |
| 633 | - | |
| 634 | - return true; | |
| 635 | - } | |
| 636 | - | |
| 637 | - return false; | |
| 638 | - } | |
| 639 | - | |
| 640 | - /** | |
| 641 | - * Clear cache when add/remove question to quiz | |
| 642 | - * | |
| 643 | - * @param $quiz_id | |
| 644 | - * | |
| 645 | - * @return void | |
| 646 | - * @since 4.2.5.8 | |
| 647 | - * @version 1.0.0 | |
| 648 | - */ | |
| 649 | - public function clear_cache_of_quiz_questions( $quiz_id ) { | |
| 650 | - $lp_quiz_cache = LP_Quiz_Cache::instance(); | |
| 651 | - $key_cache = "$quiz_id/question_ids"; | |
| 652 | - $lp_quiz_cache->clear( $key_cache ); | |
| 653 | - } | |
| 654 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Class LP_Admin_Editor_Quiz | |
| 5 | + * | |
| 6 | + * @since 3.0.2 | |
| 7 | + */ | |
| 8 | +class LP_Admin_Editor_Quiz extends LP_Admin_Editor { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * @var LP_Quiz_CURD | |
| 12 | + */ | |
| 13 | + protected $quiz_curd = null; | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * @var LP_Question_CURD | |
| 17 | + */ | |
| 18 | + protected $question_curd = null; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * @var LP_Quiz | |
| 22 | + */ | |
| 23 | + protected $quiz = null; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * LP_Admin_Editor_Quiz constructor. | |
| 27 | + */ | |
| 28 | + public function __construct() { | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Do the action depending on ajax calls with params | |
| 33 | + * | |
| 34 | + * @return bool|mixed|WP_Error | |
| 35 | + */ | |
| 36 | + public function dispatch() { | |
| 37 | + check_ajax_referer( 'learnpress_admin_quiz_editor', 'nonce' ); | |
| 38 | + $args = wp_parse_args( | |
| 39 | + $_REQUEST, | |
| 40 | + array( | |
| 41 | + 'id' => false, | |
| 42 | + 'type' => '', | |
| 43 | + ) | |
| 44 | + ); | |
| 45 | + | |
| 46 | + // get quiz | |
| 47 | + $quiz_id = $args['id']; | |
| 48 | + $quiz = learn_press_get_quiz( $quiz_id ); | |
| 49 | + | |
| 50 | + if ( ! $quiz ) { | |
| 51 | + return new WP_Error( 'INVALID_QUIZ', __( 'Invalid quiz', 'learnpress' ) ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + $this->quiz = $quiz; | |
| 55 | + $this->quiz_curd = new LP_Quiz_CURD(); | |
| 56 | + $this->question_curd = new LP_Question_CURD(); | |
| 57 | + $this->result = array( 'status' => false ); | |
| 58 | + | |
| 59 | + $this->call( $args['type'], array( $args ) ); | |
| 60 | + | |
| 61 | + return $this->get_result(); | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Get question data in admin quiz editor. | |
| 66 | + * | |
| 67 | + * @since 3.0.0 | |
| 68 | + * | |
| 69 | + * @param $question | |
| 70 | + * @param $object | if true, input in question object, do not need init LP_Question::get_question() | |
| 71 | + * @param array $args | |
| 72 | + * | |
| 73 | + * @return array | |
| 74 | + */ | |
| 75 | + public function get_question_data_to_quiz_editor( $question, $object = false, $args = array() ) { | |
| 76 | + | |
| 77 | + if ( ! $object || ! $question ) { | |
| 78 | + if ( get_post_type( $question ) !== LP_QUESTION_CPT ) { | |
| 79 | + return array(); | |
| 80 | + } | |
| 81 | + | |
| 82 | + // get question | |
| 83 | + $question = LP_Question::get_question( $question ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + // question id | |
| 87 | + $question_id = $question->get_id(); | |
| 88 | + // question answer | |
| 89 | + $answer_options = $question->get_data( 'answer_options' ); | |
| 90 | + $answer = array(); | |
| 91 | + foreach ( $answer_options as $answer_option ) { | |
| 92 | + if ( ! isset( $answer[ $answer_option['question_answer_id'] ] ) ) { | |
| 93 | + $answer[ $answer_option['question_answer_id'] ] = $answer_option; | |
| 94 | + } | |
| 95 | + } | |
| 96 | + $answers = array_values( $answer ); | |
| 97 | + $data = wp_parse_args( | |
| 98 | + $args, | |
| 99 | + array( | |
| 100 | + 'id' => $question_id, | |
| 101 | + 'open' => false, | |
| 102 | + 'title' => get_the_title( $question_id ), | |
| 103 | + 'type' => array( | |
| 104 | + 'key' => $question->get_type(), | |
| 105 | + 'label' => $question->get_type_label(), | |
| 106 | + ), | |
| 107 | + 'answers' => $answers, | |
| 108 | + 'settings' => array( | |
| 109 | + 'mark' => get_post_meta( $question_id, '_lp_mark', true ), | |
| 110 | + 'explanation' => get_post_meta( $question_id, '_lp_explanation', true ), | |
| 111 | + 'hint' => get_post_meta( $question_id, '_lp_hint', true ), | |
| 112 | + ), | |
| 113 | + 'order' => count( $answers ), | |
| 114 | + ) | |
| 115 | + ); | |
| 116 | + | |
| 117 | + return $data; | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * @param array $args | |
| 122 | + * | |
| 123 | + * @return bool | |
| 124 | + */ | |
| 125 | + public function hidden_questions( $args = array() ) { | |
| 126 | + $hidden = ! empty( $args['hidden'] ) ? $args['hidden'] : false; | |
| 127 | + update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden ); | |
| 128 | + | |
| 129 | + return true; | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * @param array $args | |
| 134 | + * | |
| 135 | + * @return bool | |
| 136 | + */ | |
| 137 | + public function new_question( $args = array() ) { | |
| 138 | + $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 139 | + $question = json_decode( wp_unslash( $question ), true ); | |
| 140 | + | |
| 141 | + if ( ! $question ) { | |
| 142 | + return false; | |
| 143 | + } | |
| 144 | + | |
| 145 | + $quiz_id = $this->quiz->get_id(); | |
| 146 | + | |
| 147 | + // draft quiz | |
| 148 | + if ( get_post_status( $quiz_id ) == 'auto-draft' ) { | |
| 149 | + | |
| 150 | + $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : ''; | |
| 151 | + $draft_quiz = (array) ( json_decode( wp_unslash( $draft_quiz ), '' ) ); | |
| 152 | + | |
| 153 | + $quiz_args = array( | |
| 154 | + 'id' => $this->quiz->get_id(), | |
| 155 | + 'title' => $draft_quiz['title'] ? $draft_quiz['title'] : __( 'New Quiz', 'learnpress' ), | |
| 156 | + 'content' => $draft_quiz['content'], | |
| 157 | + 'status' => 'draft', | |
| 158 | + ); | |
| 159 | + | |
| 160 | + $quiz_id = $this->quiz_curd->create( $quiz_args ); | |
| 161 | + } | |
| 162 | + | |
| 163 | + if ( ! isset( $quiz_id ) ) { | |
| 164 | + $this->result = new WP_Error( 'CREATE_QUIZ_FAILED', __( 'Quiz creation failed.', 'learnpress' ) ); | |
| 165 | + | |
| 166 | + return false; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $args = array( | |
| 170 | + 'quiz_id' => $quiz_id, | |
| 171 | + 'title' => $question['title'], | |
| 172 | + 'type' => $question['type'], | |
| 173 | + ); | |
| 174 | + | |
| 175 | + $new_question = $this->question_curd->create( $args ); | |
| 176 | + $this->clear_cache_of_quiz_questions( $quiz_id ); | |
| 177 | + | |
| 178 | + if ( ! is_wp_error( $new_question ) ) { | |
| 179 | + // update hidden questions in quiz meta | |
| 180 | + $quiz = LP_Quiz::get_quiz( $quiz_id ); | |
| 181 | + $hidden_questions = $quiz->get_question_ids(); | |
| 182 | + | |
| 183 | + if ( $hidden_questions ) { | |
| 184 | + $index = array_search( $new_question->get_id(), $hidden_questions ); | |
| 185 | + if ( $index !== false ) { | |
| 186 | + unset( $hidden_questions[ $index ] ); | |
| 187 | + } | |
| 188 | + //$hidden_questions = array_keys( $hidden_questions ); | |
| 189 | + } | |
| 190 | + | |
| 191 | + update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions ); | |
| 192 | + | |
| 193 | + // get new question data | |
| 194 | + $this->result = $this->get_question_data_to_quiz_editor( $new_question, true, array( 'open' => true ) ); | |
| 195 | + | |
| 196 | + if ( isset( $question['id'] ) ) { | |
| 197 | + $this->result['temp_id'] = $question['id']; | |
| 198 | + } | |
| 199 | + | |
| 200 | + return true; | |
| 201 | + } | |
| 202 | + | |
| 203 | + return false; | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * @param array $args | |
| 208 | + * | |
| 209 | + * @return bool | |
| 210 | + */ | |
| 211 | + public function sort_questions( $args = array() ) { | |
| 212 | + $order = ! empty( $args['order'] ) ? $args['order'] : false; | |
| 213 | + $order = json_decode( wp_unslash( $order ), true ); | |
| 214 | + | |
| 215 | + if ( ! $order ) { | |
| 216 | + return false; | |
| 217 | + } | |
| 218 | + | |
| 219 | + $this->result = $this->quiz_curd->sort_questions( $order ); | |
| 220 | + | |
| 221 | + return true; | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * @param array $args | |
| 226 | + * | |
| 227 | + * @return bool | |
| 228 | + */ | |
| 229 | + public function update_question_title( $args = array() ) { | |
| 230 | + $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 231 | + $question = json_decode( wp_unslash( $question ), true ); | |
| 232 | + | |
| 233 | + if ( ! $question ) { | |
| 234 | + return false; | |
| 235 | + } | |
| 236 | + | |
| 237 | + wp_update_post( | |
| 238 | + array( | |
| 239 | + 'ID' => $question['id'], | |
| 240 | + 'post_title' => $question['title'], | |
| 241 | + ) | |
| 242 | + ); | |
| 243 | + | |
| 244 | + $this->result['status'] = true; | |
| 245 | + | |
| 246 | + return true; | |
| 247 | + } | |
| 248 | + | |
| 249 | + /** | |
| 250 | + * @param array $args | |
| 251 | + * | |
| 252 | + * @return bool | |
| 253 | + */ | |
| 254 | + public function change_question_type( $args = array() ) { | |
| 255 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 256 | + $type = ! empty( $args['question_type'] ) ? $args['question_type'] : false; | |
| 257 | + | |
| 258 | + if ( ! ( $question_id || $type ) ) { | |
| 259 | + return false; | |
| 260 | + } | |
| 261 | + | |
| 262 | + $question = LP_Question::get_question( $question_id ); | |
| 263 | + | |
| 264 | + $this->question_curd->change_question_type( $question, $type ); | |
| 265 | + | |
| 266 | + $this->result = $this->get_question_data_to_quiz_editor( $question, true ); | |
| 267 | + | |
| 268 | + return true; | |
| 269 | + } | |
| 270 | + | |
| 271 | + /** | |
| 272 | + * @param array $args | |
| 273 | + * | |
| 274 | + * @return bool | |
| 275 | + */ | |
| 276 | + public function clone_question( $args = array() ) { | |
| 277 | + $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 278 | + $question = json_decode( wp_unslash( $question ), true ); | |
| 279 | + | |
| 280 | + if ( ! $question ) { | |
| 281 | + return false; | |
| 282 | + } | |
| 283 | + | |
| 284 | + // duplicate question | |
| 285 | + $new_question_id = $this->question_curd->duplicate( $question['id'], array( 'post_status' => 'publish' ) ); | |
| 286 | + $this->clear_cache_of_quiz_questions( $this->quiz->get_id() ); | |
| 287 | + | |
| 288 | + if ( ! is_wp_error( $new_question_id ) ) { | |
| 289 | + | |
| 290 | + // add question to hidden questions in quiz meta | |
| 291 | + $hidden_questions = get_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', true ); | |
| 292 | + if ( ! $hidden_questions ) { | |
| 293 | + $hidden_questions = array(); | |
| 294 | + } | |
| 295 | + $hidden_questions[] = $new_question_id;// add question to hidden questions in quiz meta | |
| 296 | + update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden_questions ); | |
| 297 | + | |
| 298 | + // add question to quiz | |
| 299 | + $this->quiz_curd->add_question( $this->quiz->get_id(), $new_question_id ); | |
| 300 | + | |
| 301 | + $this->result = $this->get_question_data_to_quiz_editor( $new_question_id ); | |
| 302 | + | |
| 303 | + return true; | |
| 304 | + } | |
| 305 | + | |
| 306 | + return false; | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * @param array $args | |
| 311 | + * | |
| 312 | + * @return bool | |
| 313 | + */ | |
| 314 | + public function remove_question( $args = array() ) { | |
| 315 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 316 | + | |
| 317 | + if ( ! $question_id ) { | |
| 318 | + return false; | |
| 319 | + } | |
| 320 | + | |
| 321 | + $this->result = $this->quiz_curd->remove_questions( $this->quiz->get_id(), $question_id ); | |
| 322 | + $this->clear_cache_of_quiz_questions( $this->quiz->get_id() ); | |
| 323 | + | |
| 324 | + return true; | |
| 325 | + } | |
| 326 | + | |
| 327 | + /** | |
| 328 | + * @param array $args | |
| 329 | + * | |
| 330 | + * @return bool | |
| 331 | + */ | |
| 332 | + public function delete_question( $args = array() ) { | |
| 333 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 334 | + | |
| 335 | + if ( ! $question_id ) { | |
| 336 | + return false; | |
| 337 | + } | |
| 338 | + | |
| 339 | + $this->result = wp_trash_post( $question_id ); | |
| 340 | + $this->clear_cache_of_quiz_questions( $this->quiz->get_id() ); | |
| 341 | + | |
| 342 | + return true; | |
| 343 | + } | |
| 344 | + | |
| 345 | + /** | |
| 346 | + * @param array $args | |
| 347 | + * | |
| 348 | + * @return bool | |
| 349 | + */ | |
| 350 | + public function sort_question_answers( $args = array() ) { | |
| 351 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 352 | + | |
| 353 | + $order = ! empty( $args['order'] ) ? $args['order'] : false; | |
| 354 | + $order = json_decode( wp_unslash( $order ), true ); | |
| 355 | + | |
| 356 | + if ( ! ( $question_id && $order ) ) { | |
| 357 | + return false; | |
| 358 | + } | |
| 359 | + | |
| 360 | + // sort answer | |
| 361 | + $this->result = $this->question_curd->sort_answers( $question_id, $order ); | |
| 362 | + | |
| 363 | + return true; | |
| 364 | + } | |
| 365 | + | |
| 366 | + /** | |
| 367 | + * @param array $args | |
| 368 | + * | |
| 369 | + * @return bool | |
| 370 | + */ | |
| 371 | + public function update_question_answer_title( $args = array() ) { | |
| 372 | + // question id | |
| 373 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 374 | + | |
| 375 | + // answers | |
| 376 | + $answer = ! empty( $args['answer'] ) ? $args['answer'] : false; | |
| 377 | + $answer = json_decode( wp_unslash( $answer ), true ); | |
| 378 | + | |
| 379 | + if ( ! ( $question_id && $answer ) ) { | |
| 380 | + return false; | |
| 381 | + } | |
| 382 | + | |
| 383 | + // update answer title | |
| 384 | + $this->result = $this->question_curd->update_answer_title( $question_id, $answer ); | |
| 385 | + | |
| 386 | + return true; | |
| 387 | + } | |
| 388 | + | |
| 389 | + /** | |
| 390 | + * @param array $args | |
| 391 | + * | |
| 392 | + * @return bool | |
| 393 | + */ | |
| 394 | + public function change_question_correct_answer( $args = array() ) { | |
| 395 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 396 | + | |
| 397 | + // correct answer | |
| 398 | + $correct = ! empty( $args['correct'] ) ? $args['correct'] : false; | |
| 399 | + $correct = json_decode( wp_unslash( $correct ), true ); | |
| 400 | + | |
| 401 | + if ( ! ( $question_id && $correct ) ) { | |
| 402 | + return false; | |
| 403 | + } | |
| 404 | + | |
| 405 | + $question = LP_Question::get_question( $question_id ); | |
| 406 | + // update correct answer, get new question | |
| 407 | + $question = $this->question_curd->change_correct_answer( $question, $correct ); | |
| 408 | + | |
| 409 | + $this->result = $this->get_question_data_to_quiz_editor( $question, true ); | |
| 410 | + | |
| 411 | + return true; | |
| 412 | + } | |
| 413 | + | |
| 414 | + /** | |
| 415 | + * @param array $args | |
| 416 | + * | |
| 417 | + * @return bool | |
| 418 | + */ | |
| 419 | + public function delete_question_answer( $args = array() ) { | |
| 420 | + $question_id = LP_Helper::sanitize_params_submitted( $_POST['question_id'] ?? 0 ); | |
| 421 | + $answer_id = LP_Helper::sanitize_params_submitted( $_POST['answer_id'] ?? 0 ); | |
| 422 | + | |
| 423 | + if ( ! $question_id || ! $answer_id ) { | |
| 424 | + return false; | |
| 425 | + } | |
| 426 | + | |
| 427 | + $this->result = $this->question_curd->delete_answer( $question_id, $answer_id ); | |
| 428 | + | |
| 429 | + return true; | |
| 430 | + } | |
| 431 | + | |
| 432 | + /** | |
| 433 | + * @param array $args | |
| 434 | + * | |
| 435 | + * @return bool | |
| 436 | + */ | |
| 437 | + public function new_question_answer( $args = array() ) { | |
| 438 | + $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false; | |
| 439 | + | |
| 440 | + if ( ! $question_id ) { | |
| 441 | + return false; | |
| 442 | + } | |
| 443 | + | |
| 444 | + $answer = LP_Question::get_default_answer(); | |
| 445 | + $new_answer_id = $this->question_curd->new_answer( $question_id, $answer ); | |
| 446 | + $question = LP_Question::get_question( $question_id ); | |
| 447 | + | |
| 448 | + if ( $new_answer_id ) { | |
| 449 | + $this->result = array_merge( | |
| 450 | + $answer, | |
| 451 | + array( | |
| 452 | + 'temp_id' => isset( $args['question_answer_id'] ) ? $args['question_answer_id'] : 0, | |
| 453 | + 'question_answer_id' => $new_answer_id, | |
| 454 | + 'question_id' => $question_id, | |
| 455 | + 'order' => count( $question->get_data( 'answer_options' ) ), | |
| 456 | + ) | |
| 457 | + ); | |
| 458 | + | |
| 459 | + return true; | |
| 460 | + } | |
| 461 | + | |
| 462 | + return false; | |
| 463 | + } | |
| 464 | + | |
| 465 | + public function update_quiz_questions_hidden( $args = array() ) { | |
| 466 | + $id = $args['id']; | |
| 467 | + $questions = isset( $args['hidden'] ) ? $args['hidden'] : false; | |
| 468 | + | |
| 469 | + if ( $questions ) { | |
| 470 | + update_post_meta( $id, '_hidden_questions_settings', $questions ); | |
| 471 | + } else { | |
| 472 | + delete_post_meta( $id, '_hidden_questions_settings' ); | |
| 473 | + } | |
| 474 | + | |
| 475 | + return true; | |
| 476 | + } | |
| 477 | + | |
| 478 | + /** | |
| 479 | + * @param array $args | |
| 480 | + * | |
| 481 | + * @return bool | |
| 482 | + */ | |
| 483 | + public function update_question_content( $args = array() ) { | |
| 484 | + $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 485 | + $question = json_decode( wp_unslash( $question ), true ); | |
| 486 | + | |
| 487 | + if ( ! $question ) { | |
| 488 | + return false; | |
| 489 | + } | |
| 490 | + | |
| 491 | + wp_update_post( | |
| 492 | + array( | |
| 493 | + 'ID' => $question['id'], | |
| 494 | + 'post_content' => $question['settings']['content'], | |
| 495 | + ) | |
| 496 | + ); | |
| 497 | + | |
| 498 | + $this->result['status'] = true; | |
| 499 | + | |
| 500 | + return true; | |
| 501 | + } | |
| 502 | + | |
| 503 | + /** | |
| 504 | + * @param array $args | |
| 505 | + * | |
| 506 | + * @return bool | |
| 507 | + */ | |
| 508 | + public function update_question_meta( $args = array() ) { | |
| 509 | + $question = ! empty( $args['question'] ) ? $args['question'] : false; | |
| 510 | + $question = json_decode( wp_unslash( $question ), true ); | |
| 511 | + | |
| 512 | + $meta_key = ! empty( $args['meta_key'] ) ? $args['meta_key'] : false; | |
| 513 | + | |
| 514 | + if ( ! ( $question && $meta_key ) ) { | |
| 515 | + return false; | |
| 516 | + } | |
| 517 | + | |
| 518 | + update_post_meta( $question['id'], '_lp_' . $meta_key, $question['settings'][ $meta_key ] ); | |
| 519 | + | |
| 520 | + $this->result['status'] = true; | |
| 521 | + | |
| 522 | + return true; | |
| 523 | + } | |
| 524 | + | |
| 525 | + /** | |
| 526 | + * @param array $args | |
| 527 | + * | |
| 528 | + * @return bool | |
| 529 | + */ | |
| 530 | + public function search_items( $args = array() ) { | |
| 531 | + $query = ! empty( $args['query'] ) ? $args['query'] : ''; | |
| 532 | + $page = ! empty( $args['page'] ) ? intval( $args['page'] ) : 1; | |
| 533 | + $exclude = ! empty( $args['exclude'] ) ? intval( $args['exclude'] ) : ''; | |
| 534 | + | |
| 535 | + if ( $exclude ) { | |
| 536 | + $exclude = json_decode( $exclude, true ); | |
| 537 | + } | |
| 538 | + | |
| 539 | + $ids_exclude = array(); | |
| 540 | + if ( is_array( $exclude ) ) { | |
| 541 | + foreach ( $exclude as $item ) { | |
| 542 | + $ids_exclude[] = $item['id']; | |
| 543 | + } | |
| 544 | + } | |
| 545 | + | |
| 546 | + $search = new LP_Modal_Search_Items( | |
| 547 | + array( | |
| 548 | + 'type' => 'lp_question', | |
| 549 | + 'context' => 'quiz', | |
| 550 | + 'context_id' => $this->quiz->get_id(), | |
| 551 | + 'term' => $query, | |
| 552 | + 'limit' => apply_filters( 'learn-press/quiz-editor/choose-items-limit', 10 ), | |
| 553 | + 'paged' => $page, | |
| 554 | + 'exclude' => $ids_exclude, | |
| 555 | + ) | |
| 556 | + ); | |
| 557 | + | |
| 558 | + $ids_item = $search->get_items(); | |
| 559 | + | |
| 560 | + $items = array(); | |
| 561 | + foreach ( $ids_item as $id ) { | |
| 562 | + $post = get_post( $id ); | |
| 563 | + | |
| 564 | + $items[] = array( | |
| 565 | + 'id' => $post->ID, | |
| 566 | + 'title' => $post->post_title, | |
| 567 | + 'type' => $post->post_type, | |
| 568 | + ); | |
| 569 | + } | |
| 570 | + | |
| 571 | + $this->result = array( | |
| 572 | + 'items' => $items, | |
| 573 | + 'pagination' => $search->get_pagination( false ), | |
| 574 | + ); | |
| 575 | + | |
| 576 | + return true; | |
| 577 | + } | |
| 578 | + | |
| 579 | + /** | |
| 580 | + * @param array $args | |
| 581 | + * | |
| 582 | + * @return bool | |
| 583 | + */ | |
| 584 | + public function add_questions_to_quiz( $args = array() ) { | |
| 585 | + $questions = LP_Helper::sanitize_params_submitted( $_POST['items'] ?? '' ); | |
| 586 | + | |
| 587 | + if ( ! $questions ) { | |
| 588 | + return false; | |
| 589 | + } | |
| 590 | + | |
| 591 | + $questions = json_decode( $questions, true ); | |
| 592 | + | |
| 593 | + $quiz_id = $this->quiz->get_id(); | |
| 594 | + | |
| 595 | + if ( get_post_status( $quiz_id ) == 'auto-draft' ) { | |
| 596 | + $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : ''; | |
| 597 | + $draft_quiz = (array) json_decode( $draft_quiz, '' ); | |
| 598 | + | |
| 599 | + $quiz_args = array( | |
| 600 | + 'id' => $quiz_id, | |
| 601 | + 'title' => $draft_quiz['title'], | |
| 602 | + 'content' => $draft_quiz['content'], | |
| 603 | + 'status' => 'draft', | |
| 604 | + ); | |
| 605 | + | |
| 606 | + $quiz_id = $this->quiz_curd->create( $quiz_args ); | |
| 607 | + } | |
| 608 | + | |
| 609 | + if ( ! isset( $quiz_id ) ) { | |
| 610 | + $this->result = new WP_Error( __( 'Quiz creation failed.', 'learnpress' ) ); | |
| 611 | + | |
| 612 | + return false; | |
| 613 | + } | |
| 614 | + | |
| 615 | + if ( $questions ) { | |
| 616 | + $hidden_questions = get_post_meta( $quiz_id, '_lp_hidden_questions', true ); | |
| 617 | + | |
| 618 | + if ( ! $hidden_questions ) { | |
| 619 | + $hidden_questions = array(); | |
| 620 | + } | |
| 621 | + | |
| 622 | + foreach ( $questions as $key => $question ) { | |
| 623 | + $hidden_questions[] = $question['id']; | |
| 624 | + $this->quiz_curd->add_question( $quiz_id, $question['id'] ); | |
| 625 | + } | |
| 626 | + | |
| 627 | + $hidden_questions = array_unique( $hidden_questions ); | |
| 628 | + | |
| 629 | + update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions ); | |
| 630 | + | |
| 631 | + $this->clear_cache_of_quiz_questions( $quiz_id ); | |
| 632 | + $this->result = $this->quiz->quiz_editor_get_questions(); | |
| 633 | + | |
| 634 | + return true; | |
| 635 | + } | |
| 636 | + | |
| 637 | + return false; | |
| 638 | + } | |
| 639 | + | |
| 640 | + /** | |
| 641 | + * Clear cache when add/remove question to quiz | |
| 642 | + * | |
| 643 | + * @param $quiz_id | |
| 644 | + * | |
| 645 | + * @return void | |
| 646 | + * @since 4.2.5.8 | |
| 647 | + * @version 1.0.0 | |
| 648 | + */ | |
| 649 | + public function clear_cache_of_quiz_questions( $quiz_id ) { | |
| 650 | + $lp_quiz_cache = LP_Quiz_Cache::instance(); | |
| 651 | + $key_cache = "$quiz_id/question_ids"; | |
| 652 | + $lp_quiz_cache->clear( $key_cache ); | |
| 653 | + } | |
| 654 | +} | |