PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 4.2.1 All 138 releases
learnpress / inc / custom-post-types / question.php

question.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.0, at inc/custom-post-types/question.php

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