| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Models\WPTables; |
| 4 |
|
| 5 |
use LearnPress\Databases\QuizQuestionsDB; |
| 6 |
use LearnPress\Filters\QuizQuestionsFilter; |
| 7 |
use LearnPress\Helpers\Response; |
| 8 |
use LearnPress\Helpers\Template; |
| 9 |
use LearnPress\Models\Question\QuestionPostModel; |
| 10 |
use LearnPress\Models\Quiz\QuizQuestionModel; |
| 11 |
use LP_Abstract_Post_Type; |
| 12 |
use Throwable; |
| 13 |
use WP_Post; |
| 14 |
use WP_Posts_List_Table; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* LearnPress Questions Table class. |
| 20 |
* |
| 21 |
* @since 4.4.8 |
| 22 |
* @version 1.0.0 |
| 23 |
*/ |
| 24 |
class QuestionsTable extends WP_Posts_List_Table { |
| 25 |
/** |
| 26 |
* Get the table columns. |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public function get_columns() { |
| 31 |
$columns = parent::get_columns(); |
| 32 |
|
| 33 |
$pos = array_search( 'title', array_keys( $columns ), true ); |
| 34 |
if ( false !== $pos ) { |
| 35 |
$insert = array( |
| 36 |
'instructor' => esc_html__( 'Author', 'learnpress' ), |
| 37 |
LP_QUIZ_CPT => esc_html__( 'Quiz', 'learnpress' ), |
| 38 |
'type' => esc_html__( 'Type', 'learnpress' ), |
| 39 |
); |
| 40 |
|
| 41 |
$columns = array_merge( |
| 42 |
array_slice( $columns, 0, $pos + 1 ), |
| 43 |
$insert, |
| 44 |
array_slice( $columns, $pos + 1 ) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! empty( $columns['author'] ) ) { |
| 49 |
unset( $columns['author'] ); |
| 50 |
} |
| 51 |
|
| 52 |
$user = wp_get_current_user(); |
| 53 |
if ( in_array( LP_TEACHER_ROLE, $user->roles, true ) ) { |
| 54 |
unset( $columns['instructor'] ); |
| 55 |
} |
| 56 |
|
| 57 |
return $columns; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Set columns can be sortable. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_sortable_columns() { |
| 66 |
$sortable_columns = parent::get_sortable_columns(); |
| 67 |
$sortable_columns['author'] = array( 'author', 'asc' ); |
| 68 |
$sortable_columns[ LP_QUIZ_CPT ] = array( 'quiz-name', 'asc' ); |
| 69 |
|
| 70 |
return $sortable_columns; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Column instructor. |
| 75 |
* |
| 76 |
* @param WP_Post $post |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function column_instructor( $post ) { |
| 81 |
try { |
| 82 |
LP_Abstract_Post_Type::column_author( $post ); |
| 83 |
} catch ( Throwable $e ) { |
| 84 |
Template::print_message( $e->getMessage(), Response::STATUS_ERROR ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Column quiz. |
| 90 |
* |
| 91 |
* @param WP_Post $post |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function column_lp_quiz( $post ) { |
| 96 |
try { |
| 97 |
$filter = new QuizQuestionsFilter(); |
| 98 |
$filter->question_id = $post->ID; |
| 99 |
$filter->query_count = false; |
| 100 |
$filter->run_query_count = false; |
| 101 |
$quiz_questions = QuizQuestionsDB::getInstance()->get_quiz_questions( $filter ); |
| 102 |
$quiz_items = array(); |
| 103 |
|
| 104 |
foreach ( $quiz_questions as $quiz_question ) { |
| 105 |
$quizPostModel = ( new QuizQuestionModel( $quiz_question ) )->get_quiz_post_model(); |
| 106 |
if ( ! $quizPostModel ) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
$quiz_items[] = sprintf( |
| 111 |
'<a href="%1$s">%2$s</a>', |
| 112 |
esc_url( add_query_arg( array( 'filter_quiz' => $quizPostModel->ID ) ) ), |
| 113 |
esc_html( $quizPostModel->get_the_title() ) |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
if ( $quiz_items ) { |
| 118 |
echo implode( ', ', $quiz_items ); |
| 119 |
} else { |
| 120 |
esc_html_e( 'Not assigned yet', 'learnpress' ); |
| 121 |
} |
| 122 |
} catch ( Throwable $e ) { |
| 123 |
esc_html_e( 'Not assigned yet', 'learnpress' ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Column type. |
| 129 |
* |
| 130 |
* @param WP_Post $post |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function column_type( $post ) { |
| 135 |
try { |
| 136 |
if ( empty( $post ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$questionPostModel = QuestionPostModel::find( $post->ID ?? 0, true ); |
| 141 |
if ( ! $questionPostModel instanceof QuestionPostModel ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$question_type_label = $questionPostModel->get_type_label(); |
| 146 |
if ( empty( $question_type_label ) ) { |
| 147 |
$question_type_label = esc_html__( 'Not set', 'learnpress' ); |
| 148 |
} |
| 149 |
|
| 150 |
echo esc_html( $question_type_label ); |
| 151 |
|
| 152 |
//echo esc_html( learn_press_question_name_from_slug( get_post_meta( $post->ID, '_lp_type', true ) ) ); |
| 153 |
} catch ( Throwable $e ) { |
| 154 |
Template::print_message( |
| 155 |
$e->getMessage(), |
| 156 |
Response::STATUS_ERROR |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|