| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Question_Post_Type |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 4.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
use LearnPress\Models\Question\QuestionPostModel; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit(); |
| 13 |
|
| 14 |
if ( ! class_exists( 'LP_Question_Post_Type' ) ) { |
| 15 |
|
| 16 |
/** |
| 17 |
* Class LP_Question_Post_Type |
| 18 |
*/ |
| 19 |
class LP_Question_Post_Type extends LP_Abstract_Post_Type { |
| 20 |
/** |
| 21 |
* @var null |
| 22 |
*/ |
| 23 |
protected static $_instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $_post_type = LP_QUESTION_CPT; |
| 29 |
|
| 30 |
/** |
| 31 |
* LP_Question_Post_Type constructor. |
| 32 |
* |
| 33 |
* @param $post_type |
| 34 |
* @param mixed |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); |
| 38 |
add_action( 'admin_head', array( $this, 'init' ) ); |
| 39 |
add_action( 'learn-press/admin/after-enqueue-scripts', array( $this, 'data_question_editor' ) ); |
| 40 |
|
| 41 |
add_filter( 'views_edit-' . LP_QUESTION_CPT, array( $this, 'views_pages' ), 11 ); |
| 42 |
add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ), 10 ); |
| 43 |
|
| 44 |
// $this->add_map_method( 'before_delete', 'before_delete_question' ); |
| 45 |
|
| 46 |
parent::__construct(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Add question types support answer options |
| 51 |
* |
| 52 |
* @since 3.3.0 |
| 53 |
*/ |
| 54 |
public function wp_loaded() { |
| 55 |
$default_support_options = apply_filters( |
| 56 |
'learn-press/default-question-types-support-answer-options', |
| 57 |
array( |
| 58 |
'true_or_false', |
| 59 |
'single_choice', |
| 60 |
'multi_choice', |
| 61 |
'fill_in_blanks', |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
foreach ( $default_support_options as $type ) { |
| 66 |
LP_Global::add_object_feature( 'question.' . $type, 'answer-options', 'yes' ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add filters to lesson view. |
| 72 |
* |
| 73 |
* @param array $views |
| 74 |
* |
| 75 |
* @return array |
| 76 |
* @since 3.0.1 |
| 77 |
* @version 1.0.1 |
| 78 |
* @editor tungnx |
| 79 |
*/ |
| 80 |
public function views_pages( array $views ): array { |
| 81 |
$lp_question_db = LP_Question_DB::getInstance(); |
| 82 |
|
| 83 |
try { |
| 84 |
$filter = new LP_Question_Filter(); |
| 85 |
/*if ( ! current_user_can( 'administrator' ) ) { |
| 86 |
$filter->where[] = $lp_question_db->wpdb->prepare( 'AND post_author = %d', get_current_user_id() ); |
| 87 |
}*/ |
| 88 |
|
| 89 |
$count_unassigned_questions = $lp_question_db->get_total_question_unassigned( $filter ); |
| 90 |
|
| 91 |
if ( $count_unassigned_questions > 0 ) { |
| 92 |
$views['unassigned'] = sprintf( |
| 93 |
'<a href="%s" class="%s">%s <span class="count">(%d)</span></a>', |
| 94 |
admin_url( 'edit.php?post_type=' . LP_QUESTION_CPT . '&unassigned=yes' ), |
| 95 |
isset( $_GET['unassigned'] ) ? 'current' : '', |
| 96 |
__( 'Unassigned', 'learnpress' ), |
| 97 |
$count_unassigned_questions |
| 98 |
); |
| 99 |
} |
| 100 |
} catch ( Throwable $e ) { |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
return $views; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Load data for question editor. |
| 109 |
* |
| 110 |
* @since 3.0.0 |
| 111 |
*/ |
| 112 |
public function data_question_editor() { |
| 113 |
global $post; |
| 114 |
|
| 115 |
if ( LP_QUESTION_CPT !== get_post_type() ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
$question = LP_Question::get_question( $post->ID ); |
| 120 |
$type = $question->get_type(); |
| 121 |
$answers = ( $question->get_data( 'answer_options' ) ? array_values( $question->get_data( 'answer_options' ) ) : array() ); |
| 122 |
|
| 123 |
if ( empty( $answers ) ) { |
| 124 |
$answers = array( |
| 125 |
array( |
| 126 |
'order' => 1, |
| 127 |
'question_answer_id' => 0, |
| 128 |
'is_true' => 'yes', |
| 129 |
'title' => esc_html__( 'Correct', 'learnpress' ), |
| 130 |
), |
| 131 |
array( |
| 132 |
'order' => 2, |
| 133 |
'question_answer_id' => 0, |
| 134 |
'is_true' => '', |
| 135 |
'title' => esc_html__( 'Incorrect', 'learnpress' ), |
| 136 |
), |
| 137 |
); |
| 138 |
|
| 139 |
$type = 'true_or_false'; |
| 140 |
} |
| 141 |
|
| 142 |
wp_localize_script( |
| 143 |
'learn-press-admin-question-editor', |
| 144 |
'lp_question_editor', |
| 145 |
apply_filters( |
| 146 |
'learn-press/question-editor/localize-script', |
| 147 |
array( |
| 148 |
'root' => array( |
| 149 |
'id' => $post->ID, |
| 150 |
'auto_draft' => get_post_status( $post->ID ) == 'auto-draft', |
| 151 |
'open' => false, |
| 152 |
'title' => get_the_title( $post->ID ), |
| 153 |
'type' => array( |
| 154 |
'key' => $type, |
| 155 |
'label' => learn_press_question_types()[ $type ], |
| 156 |
), |
| 157 |
'answers' => apply_filters( 'learn-press/question-editor/question-answers-data', $answers, $post->ID, 0 ), |
| 158 |
'ajax' => admin_url( '' ), |
| 159 |
'action' => 'admin_question_editor', |
| 160 |
'nonce' => wp_create_nonce( 'learnpress_admin_question_editor' ), |
| 161 |
'questionTypes' => LP_Question::get_types(), |
| 162 |
'supportAnswerOptions' => learn_press_get_question_support_answer_options(), |
| 163 |
), |
| 164 |
'i18n' => apply_filters( |
| 165 |
'learn-press/question-editor/i18n', |
| 166 |
array( |
| 167 |
'new_option_label' => esc_html__( 'New Option', 'learnpress' ), |
| 168 |
'confirm_remove_blanks' => esc_html__( 'Are you sure to remove all the blanks?', 'learnpress' ), |
| 169 |
) |
| 170 |
), |
| 171 |
) |
| 172 |
) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Register question post type. |
| 178 |
*/ |
| 179 |
public function args_register_post_type(): array { |
| 180 |
register_taxonomy( |
| 181 |
'question_tag', |
| 182 |
array( LP_QUESTION_CPT ), |
| 183 |
array( |
| 184 |
'labels' => array( |
| 185 |
'name' => esc_html__( 'Question Tag', 'learnpress' ), |
| 186 |
'menu_name' => esc_html__( 'Tag', 'learnpress' ), |
| 187 |
'singular_name' => esc_html__( 'Tag', 'learnpress' ), |
| 188 |
'add_new_item' => esc_html__( 'Add A New Tag', 'learnpress' ), |
| 189 |
'all_items' => esc_html__( 'All Tags', 'learnpress' ), |
| 190 |
), |
| 191 |
'public' => true, |
| 192 |
'hierarchical' => false, |
| 193 |
'show_ui' => true, |
| 194 |
'show_admin_column' => 'true', |
| 195 |
'show_in_nav_menus' => true, |
| 196 |
'rewrite' => array( |
| 197 |
'slug' => 'question-tag', |
| 198 |
'hierarchical' => false, |
| 199 |
'with_front' => false, |
| 200 |
), |
| 201 |
) |
| 202 |
); |
| 203 |
add_post_type_support( 'question', 'comments' ); |
| 204 |
|
| 205 |
return array( |
| 206 |
'labels' => array( |
| 207 |
'name' => esc_html__( 'Question Bank', 'learnpress' ), |
| 208 |
'menu_name' => esc_html__( 'Question Bank', 'learnpress' ), |
| 209 |
'singular_name' => esc_html__( 'Question', 'learnpress' ), |
| 210 |
'all_items' => esc_html__( 'Questions', 'learnpress' ), |
| 211 |
'view_item' => esc_html__( 'View Question', 'learnpress' ), |
| 212 |
'add_new_item' => esc_html__( 'Add A New Question', 'learnpress' ), |
| 213 |
'add_new' => esc_html__( 'Add New', 'learnpress' ), |
| 214 |
'edit_item' => esc_html__( 'Edit Question', 'learnpress' ), |
| 215 |
'update_item' => esc_html__( 'Update Question', 'learnpress' ), |
| 216 |
'search_items' => esc_html__( 'Search Questions', 'learnpress' ), |
| 217 |
'not_found' => esc_html__( 'No questions found', 'learnpress' ), |
| 218 |
'not_found_in_trash' => esc_html__( 'There was no questions found in the trash', 'learnpress' ), |
| 219 |
), |
| 220 |
'public' => true, |
| 221 |
'publicly_queryable' => true, |
| 222 |
'show_ui' => true, |
| 223 |
'has_archive' => false, |
| 224 |
'capability_type' => LP_LESSON_CPT, |
| 225 |
'map_meta_cap' => true, |
| 226 |
'show_in_menu' => 'learn_press', |
| 227 |
'show_in_admin_bar' => true, |
| 228 |
'show_in_nav_menus' => true, |
| 229 |
'show_in_rest' => learn_press_user_maybe_is_a_teacher(), |
| 230 |
'supports' => array( 'title', 'editor', 'revisions' ), |
| 231 |
'hierarchical' => false, |
| 232 |
'rewrite' => array( |
| 233 |
'slug' => 'questions', |
| 234 |
'hierarchical' => true, |
| 235 |
'with_front' => false, |
| 236 |
), |
| 237 |
'exclude_from_search' => true, |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Init question. |
| 243 |
* |
| 244 |
* @since 3.0.0 |
| 245 |
*/ |
| 246 |
public function init() { |
| 247 |
$hidden = get_user_meta( get_current_user_id(), 'manageedit-lp_questioncolumnshidden', true ); |
| 248 |
|
| 249 |
if ( ! is_array( $hidden ) && empty( $hidden ) ) { |
| 250 |
update_user_meta( get_current_user_id(), 'manageedit-lp_questioncolumnshidden', array( 'taxonomy-question-tag' ) ); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Remove question from quiz items. |
| 256 |
* |
| 257 |
* @param $question_id |
| 258 |
* |
| 259 |
* @since 3.0.0 |
| 260 |
*/ |
| 261 |
public function before_delete_question( int $question_id = 0 ) { |
| 262 |
$curd = new LP_Question_CURD(); |
| 263 |
|
| 264 |
$curd->delete( $question_id ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Handle when save post. |
| 269 |
* |
| 270 |
* @param int $post_id |
| 271 |
* @param WP_Post|null $post |
| 272 |
* @param bool $is_update |
| 273 |
* |
| 274 |
* @return void |
| 275 |
* @since 4.2.7.6 |
| 276 |
* @version 1.0.0 |
| 277 |
*/ |
| 278 |
public function save_post( int $post_id, ?WP_Post $post = null, bool $is_update = false ) { |
| 279 |
// Clear cache get question by id |
| 280 |
$lpCache = new LP_Cache(); |
| 281 |
$lpCache->clear( "questionPostModel/find/{$post_id}" ); |
| 282 |
$lpCache->clear( "questionModel/find/{$post_id}" ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Admin editor |
| 287 |
* |
| 288 |
* @since 3.3.0 |
| 289 |
* |
| 290 |
* @return void |
| 291 |
*/ |
| 292 |
public function admin_editor() { |
| 293 |
global $post; |
| 294 |
|
| 295 |
$question_id = $post->ID; |
| 296 |
$questionPostModel = QuestionPostModel::find( $question_id, true ); |
| 297 |
if ( ! $questionPostModel instanceof QuestionPostModel ) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
do_action( 'learn-press/admin/edit-question/layout', $questionPostModel ); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Add columns to admin manage question page |
| 306 |
* |
| 307 |
* @param array $columns |
| 308 |
* |
| 309 |
* @return array |
| 310 |
*/ |
| 311 |
public function columns_head( $columns ) { |
| 312 |
$pos = array_search( 'title', array_keys( $columns ) ); |
| 313 |
$new_columns = array( |
| 314 |
'instructor' => esc_html__( 'Author', 'learnpress' ), |
| 315 |
LP_QUIZ_CPT => esc_html__( 'Quiz', 'learnpress' ), |
| 316 |
'type' => esc_html__( 'Type', 'learnpress' ), |
| 317 |
); |
| 318 |
|
| 319 |
if ( false !== $pos && ! array_key_exists( LP_QUIZ_CPT, $columns ) ) { |
| 320 |
$columns = array_merge( |
| 321 |
array_slice( $columns, 0, $pos + 1 ), |
| 322 |
$new_columns, |
| 323 |
array_slice( $columns, $pos + 1 ) |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
$user = wp_get_current_user(); |
| 328 |
|
| 329 |
if ( in_array( LP_TEACHER_ROLE, $user->roles ) ) { |
| 330 |
unset( $columns['instructor'] ); |
| 331 |
} |
| 332 |
|
| 333 |
if ( ! empty( $columns['author'] ) ) { |
| 334 |
unset( $columns['author'] ); |
| 335 |
} |
| 336 |
|
| 337 |
return $columns; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Displaying the content of extra columns |
| 342 |
* |
| 343 |
* @param $name |
| 344 |
* @param $post_id |
| 345 |
*/ |
| 346 |
public function columns_content( $name, $post_id = 0 ) { |
| 347 |
switch ( $name ) { |
| 348 |
case 'instructor': |
| 349 |
$this->column_instructor( $post_id ); |
| 350 |
break; |
| 351 |
case 'lp_quiz': |
| 352 |
$curd = new LP_Question_CURD(); |
| 353 |
// get quiz |
| 354 |
$quiz = $curd->get_quiz( $post_id ); |
| 355 |
|
| 356 |
if ( $quiz ) { |
| 357 |
echo '<div><a href="' . esc_url_raw( add_query_arg( array( 'filter_quiz' => $quiz->ID ) ) ) . '">' . get_the_title( $quiz->ID ) . '</a>'; |
| 358 |
echo '<div class="row-actions">'; |
| 359 |
printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $quiz->ID ) ), esc_html__( 'Edit', 'learnpress' ) ); |
| 360 |
echo ' | '; |
| 361 |
printf( '<a href="%s">%s</a>', get_the_permalink( $quiz->ID ), esc_html__( 'View', 'learnpress' ) ); |
| 362 |
echo '</div></div>'; |
| 363 |
} else { |
| 364 |
esc_html_e( 'Not assigned yet', 'learnpress' ); |
| 365 |
} |
| 366 |
break; |
| 367 |
case 'type': |
| 368 |
echo learn_press_question_name_from_slug( get_post_meta( $post_id, '_lp_type', true ) ); |
| 369 |
break; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Posts_join_paged. |
| 375 |
* |
| 376 |
* @param $join |
| 377 |
* |
| 378 |
* @return string |
| 379 |
*/ |
| 380 |
public function posts_join_paged( $join ): string { |
| 381 |
if ( ! $this->is_page_list_posts_on_backend() ) { |
| 382 |
return $join; |
| 383 |
} |
| 384 |
|
| 385 |
global $wpdb; |
| 386 |
|
| 387 |
$quiz_id = $this->_filter_quiz(); |
| 388 |
if ( $quiz_id || $this->get_order_by() == 'quiz-name' ) { |
| 389 |
$join .= " LEFT JOIN {$wpdb->prefix}learnpress_quiz_questions qq ON {$wpdb->posts}.ID = qq.question_id"; |
| 390 |
$join .= " LEFT JOIN {$wpdb->posts} q ON q.ID = qq.quiz_id"; |
| 391 |
} |
| 392 |
|
| 393 |
return $join; |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* @param $where |
| 398 |
* |
| 399 |
* @return mixed|string |
| 400 |
*/ |
| 401 |
public function posts_where_paged( $where ) { |
| 402 |
static $posts_where_paged = false; |
| 403 |
|
| 404 |
if ( $posts_where_paged || ! $this->is_page_list_posts_on_backend() ) { |
| 405 |
return $where; |
| 406 |
} |
| 407 |
|
| 408 |
global $wpdb; |
| 409 |
$quiz_id = $this->_filter_quiz(); |
| 410 |
|
| 411 |
if ( $quiz_id ) { |
| 412 |
$where .= $wpdb->prepare( ' AND (q.ID = %d)', $quiz_id ); |
| 413 |
} |
| 414 |
|
| 415 |
if ( 'yes' === LP_Request::get( 'unassigned' ) ) { |
| 416 |
global $wpdb; |
| 417 |
$where .= " AND {$wpdb->posts}.ID NOT IN( |
| 418 |
SELECT qq.question_id |
| 419 |
FROM {$wpdb->learnpress_quiz_questions} qq |
| 420 |
) |
| 421 |
"; |
| 422 |
} |
| 423 |
|
| 424 |
return $where; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* @param $order_by_statement |
| 429 |
* |
| 430 |
* @return string |
| 431 |
*/ |
| 432 |
public function posts_orderby( $order_by_statement ): string { |
| 433 |
if ( ! $this->is_page_list_posts_on_backend() ) { |
| 434 |
return $order_by_statement; |
| 435 |
} |
| 436 |
|
| 437 |
$orderby = $this->get_order_by(); |
| 438 |
$order = $this->get_order_sort(); |
| 439 |
|
| 440 |
if ( $orderby && $order ) { |
| 441 |
switch ( $orderby ) { |
| 442 |
case 'quiz-name': |
| 443 |
$order_by_statement = "q.post_title {$order}"; |
| 444 |
break; |
| 445 |
case 'date': |
| 446 |
$order_by_statement = "post_date {$order}"; |
| 447 |
break; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
return $order_by_statement; |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* @param $columns |
| 456 |
* |
| 457 |
* @return mixed |
| 458 |
*/ |
| 459 |
public function sortable_columns( $columns ) { |
| 460 |
$columns['author'] = 'author'; |
| 461 |
$columns[ LP_QUIZ_CPT ] = 'quiz-name'; |
| 462 |
|
| 463 |
return $columns; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* @return bool|int |
| 468 |
*/ |
| 469 |
private function _filter_quiz() { |
| 470 |
return LP_Request::get_int( 'filter_quiz' ); |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Quiz assigned view. |
| 475 |
* |
| 476 |
* @since 3.0.0 |
| 477 |
*/ |
| 478 |
public static function question_assigned() { |
| 479 |
learn_press_admin_view( 'meta-boxes/quiz/assigned.php' ); |
| 480 |
} |
| 481 |
|
| 482 |
public function meta_boxes() { |
| 483 |
return array( |
| 484 |
'question_assigned' => array( |
| 485 |
'title' => esc_html__( 'Assigned', 'learnpress' ), |
| 486 |
'callback' => function ( $post ) { |
| 487 |
learn_press_admin_view( 'meta-boxes/quiz/assigned.php' ); |
| 488 |
}, |
| 489 |
'context' => 'side', |
| 490 |
'priority' => 'high', |
| 491 |
), |
| 492 |
'question-editor' => array( |
| 493 |
'title' => esc_html__( 'Questions Options', 'learnpress' ), |
| 494 |
'callback' => array( $this, 'admin_editor' ), |
| 495 |
'context' => 'normal', |
| 496 |
'priority' => 'high', |
| 497 |
), |
| 498 |
); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* @return LP_Question_Post_Type|null |
| 503 |
* |
| 504 |
* @editor tungnx |
| 505 |
*/ |
| 506 |
public static function instance() { |
| 507 |
if ( ! self::$_instance ) { |
| 508 |
/* |
| 509 |
$args = array( |
| 510 |
'default_meta' => array( |
| 511 |
'_lp_mark' => 1, |
| 512 |
'_lp_type' => 'true_or_false', |
| 513 |
), |
| 514 |
);*/ |
| 515 |
self::$_instance = new self(); |
| 516 |
} |
| 517 |
|
| 518 |
return self::$_instance; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
$question_post_type = LP_Question_Post_Type::instance(); |
| 523 |
} |
| 524 |
|