tab_list_questions();
$tab = [
'wrapper' => '
',
'header' => $this->html_header(),
'filter_bar' => $this->html_filter_bar(),
'questions' => $list_question,
'wrapper_end' => '
',
];
echo Template::combine_components( $tab );
}
public function html_header(): string {
$section = [
'wrapper' => '',
];
return Template::combine_components( $section );
}
public function html_filter_bar(): string {
$section = [
'wrapper_action' => '',
'search_question' => $this->html_search(),
'wrapper_action_end' => '
',
];
return Template::combine_components( $section );
}
public function html_search() {
$args = lp_archive_skeleton_get_args();
$link_tab = CourseBuilder::get_tab_link( 'questions' );
$search = [
'wrapper' => sprintf( '',
];
return Template::combine_components( $search );
}
public function tab_list_questions(): string {
$content = '';
try {
// Query questions of user
$param = lp_archive_skeleton_get_args();
$param['id_url'] = 'tab-list-questions';
$query_args = array(
'post_type' => LP_QUESTION_CPT,
'post_status' => array( 'publish', 'private', 'draft', 'pending', 'trash' ),
'posts_per_page' => 12,
'paged' => $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1,
's' => ! empty( $param['c_search'] ) ? sanitize_text_field( $param['c_search'] ) : '',
);
$user_id = get_current_user_id();
$userModel = UserModel::find( $user_id, true );
if ( ! $userModel instanceof UserModel ) {
return '';
}
if ( ! current_user_can( ADMIN_ROLE ) ) {
$query_args['author'] = $user_id;
}
$query = new WP_Query();
$result = $query->query( $query_args );
$total_questions = $query->found_posts ?? 0;
if ( $total_questions < 1 ) {
unset( $query_args['paged'] );
$count_query = new WP_Query();
$count_query->query( $query_args );
$total_questions = $count_query->found_posts;
}
$questions = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$question_model = QuestionPostModel::find( get_the_ID(), true );
$questions[] = $question_model;
}
}
wp_reset_postdata();
if ( ! empty( $questions ) ) {
$html_questions = $this->list_questions( $questions );
} else {
$html_questions = Template::print_message(
sprintf( __( 'No questions found', 'learnpress' ) ),
'info',
false
);
}
$total_pages = \LP_Database::get_total_pages( $query_args['posts_per_page'], $total_questions );
$link_tab = CourseBuilder::get_tab_link( 'questions' );
$data_pagination = [
'paged' => max( 1, $query_args['paged'] ?? 1 ),
'total_pages' => $total_pages,
'base' => trailingslashit( $link_tab ) . 'page/%#%',
'format' => '',
];
$pagination = Template::instance()->html_pagination( $data_pagination );
$sections = apply_filters(
'learn-press/course-builder/questions/sections',
[
'wrapper' => '',
'questions' => $html_questions,
'wrapper_end' => '
',
'pagination' => $pagination,
],
$questions,
$userModel
);
$content = Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
/**
* Display list questions.
*
* @param $questions
*
* @return string
*/
public function list_questions( $questions ): string {
$content = '';
try {
$html_list_question = '';
foreach ( $questions as $question_model ) {
$html_list_question .= self::render_question( $question_model );
}
$header = '';
$sections = [
'header' => $header,
'wrapper' => '',
'list_question' => $html_list_question,
'wrapper_end' => '
',
];
$content = Template::combine_components( $sections );
} catch ( Throwable $e ) {
error_log( __METHOD__ . ': ' . $e->getMessage() );
}
return $content;
}
/**
* Render question in course builder
*
* @param $question
* @param array $settings
*
* @return string
* @since 4.3.0
* @version 1.0.0
*/
public static function render_question( QuestionPostModel $question_model, array $settings = [] ): string {
$edit_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-edit.svg' );
$more_actions_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-more.svg' );
$types = LP_Question::get_types();
$type = get_post_meta( $question_model->get_id(), '_lp_type', true );
$question_type = $types[ $type ] ?? '';
$author = get_user_by( 'ID', $question_model->post_author );
$author_name = $author && isset( $author->display_name ) ? $author->display_name : '--';
$question = array(
'id' => $question_model->get_id(),
'title' => $question_model->post_title,
'status' => $question_model->post_status,
'quizzes' => BuilderEditQuestionTemplate::instance()->get_assigned_question( $question_model->get_id() ),
'author' => $author_name,
'type' => $question_type,
'date_modified' => lp_jwt_prepare_date_response( $question_model->post_date_gmt ),
);
try {
$edit_link = BuilderQuestionTemplate::instance()->get_link_edit( $question['id'] );
$html_quizzes = '';
$assigned = '--';
if ( ! empty( $question['quizzes'] ) ) {
$quizzes = is_array( $question['quizzes'] ) && isset( $question['quizzes']['id'] )
? array( $question['quizzes'] )
: $question['quizzes'];
$quiz_htmls = array();
foreach ( $quizzes as $quiz ) {
$quiz_id = $quiz['id'] ?? 0;
$quiz_title = $quiz['title'] ?? '';
if ( $quiz_id && $quiz_title ) {
$quiz_link = BuilderQuizTemplate::instance()->get_link_edit( $quiz_id );
$quiz_htmls[] = sprintf(
'%s',
esc_url( $quiz_link ),
esc_html( $quiz_title )
);
}
}
if ( ! empty( $quiz_htmls ) ) {
$assigned = implode( ', ', $quiz_htmls );
}
}
$html_quizzes = sprintf(
'%s: %s
',
__( 'Assigned', 'learnpress' ),
$assigned
);
$status = $question_model->post_status ?? '';
$html_content = apply_filters(
'learn-press/course-builder/list-questions/item/section/bottom',
[
'title' => sprintf(
'',
esc_url( $edit_link ),
esc_html( $question['title'] )
),
'quizzes' => $html_quizzes,
'date' => sprintf( '%s', ! empty( $question['date_modified'] ) ? date_i18n( 'm/d/Y', strtotime( $question['date_modified'] ) ) : '--' ),
'question_status' => ! empty( $status ) ? sprintf( '%1$s', $status ) : '',
'type' => sprintf( '%s', $question['type'] ),
],
$question,
$settings
);
$html_action = apply_filters(
'learn-press/course-builder/list-questions/item/action',
[
'wrapper' => '',
'edit' => $status !== PostModel::STATUS_TRASH ? sprintf(
'
',
esc_url( $edit_link ),
$edit_icon,
__( 'Edit', 'learnpress' )
) : '',
'action_expanded_button' => sprintf(
'
',
$more_actions_icon
),
'action_expanded_wrapper' => '
',
'action_expanded_duplicate' => sprintf( '%s', __( 'Duplicate', 'learnpress' ) ),
'action_expanded_publish' => sprintf( '%s', __( 'Publish', 'learnpress' ) ),
'action_expanded_trash' => sprintf(
'%s',
$status === 'trash' ? ' style="display:none"' : '',
__( 'Trash', 'learnpress' )
),
'action_expanded_restore' => sprintf(
'%s',
$status !== 'trash' ? ' style="display:none"' : '',
__( 'Restore', 'learnpress' )
),
'action_expanded_delete' => sprintf( '%s', __( 'Delete', 'learnpress' ) ),
'action_expanded_wrapper_end' => '
',
'wrapper_end' => '
',
],
$question,
$settings
);
$section = apply_filters(
'learn-press/course-builder/list-questions/item-li',
[
'wrapper_li' => '',
'wrapper_div' => sprintf( '', $question['id'], $status ),
'question_info' => Template::combine_components( $html_content ),
'question_action' => Template::combine_components( $html_action ),
'wrapper_div_end' => '
',
'wrapper_li_end' => '',
],
$question,
$settings
);
$html_item = Template::combine_components( $section );
} catch ( Throwable $e ) {
$html_item = $e->getMessage();
}
return $html_item;
}
}