' => '', ); $profile = LP_Profile::instance(); if ( ! $profile->get_user() ) { throw new Exception( __( 'Invalid User Profile', 'learnpress' ) ); } $user_id = $profile->get_user()->get_id(); if ( ! $user_id ) { throw new Exception( __( 'User is not exist', 'learnpress' ) ); } /** * @uses ProfileQuizzesTemplate::renderContent() */ $callback = array( 'class' => self::class, 'method' => 'renderContent', ); $args = array( 'id_url' => 'profile_quizzes', 'user_id' => $user_id, 'paged' => 1, 'type' => 'all', ); $content = TemplateAJAX::load_content_via_ajax( $args, $callback ); $html = Template::instance()->nest_elements( $html_wrapper, $content ); echo $html; } /** * Render the content for the quiz tab. * * @param array $args * * @return stdClass */ public static function renderContent( array $args ): stdClass { $content = new stdClass(); $html = ''; try { $userModel = UserModel::find( $args['user_id'], true ); if ( ! $userModel ) { throw new Exception( __( 'Invalid User', 'learnpress' ) ); } // Check permission, self user or admin can view $user_current_id = get_current_user_id(); if ( $user_current_id !== $userModel->get_id() && ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) ) { throw new Exception( __( 'You do not have permission to view this profile.', 'learnpress' ) ); } $total_rows = 0; $limit = apply_filters( 'learnpress/profile/user-quizzes/limit', get_option( 'posts_per_page', 10 ) ); $filter = new UserItemsFilter(); $filter->user_id = $userModel->get_id(); $filter->limit = $limit; $filter->page = $args['paged']; switch ( $args['type'] ) { case UserItemModel::STATUS_COMPLETED: $filter->status = LP_ITEM_COMPLETED; break; case UserItemModel::GRADUATION_PASSED: $filter->graduation = LP_GRADUATION_PASSED; break; case UserItemModel::GRADUATION_FAILED: $filter->graduation = LP_GRADUATION_FAILED; break; } $user_quizzes = $userModel->get_quizzes_attend( $filter, $total_rows ); $total_pages = LP_Database::get_total_pages( $limit, $total_rows ); $header = [ [ 'title' => __( 'Quiz', 'learnpress' ), ], [ 'title' => __( 'Result', 'learnpress' ), ], [ 'title' => __( 'Time spent', 'learnpress' ), ], [ 'title' => __( 'Started Date', 'learnpress' ), ], ]; $body = []; if ( ! empty( $user_quizzes ) ) { foreach ( $user_quizzes as $user_quiz ) { $userQuizModel = UserQuizModel::find_user_item( $user_quiz->user_id, $user_quiz->item_id, $user_quiz->item_type, $user_quiz->ref_id, $user_quiz->ref_type, true ); if ( ! $userQuizModel ) { continue; } $quizPostModel = $userQuizModel->get_quiz_post_model(); if ( ! $quizPostModel ) { continue; } $courseModel = $userQuizModel->get_course_model(); $item_body = [ sprintf( '%s', esc_url( $courseModel->get_item_link( $quizPostModel->get_id() ) ), esc_html( $quizPostModel->get_the_title() ) ), sprintf( '%s%% %s', esc_html( $userQuizModel->get_result()['result'] ), esc_attr( $userQuizModel->get_graduation() ), $userQuizModel->get_graduation_label() ), $userQuizModel->get_time_spend(), ( new LP_Datetime( $userQuizModel->get_start_time() ) )->format( LP_Datetime::I18N_FORMAT ), ]; $body[] = $item_body; } $section_footer = [ 'page_result' => TableListTemplate::instance()->html_page_result( [ 'paged' => $args['paged'], 'per_page' => $limit, 'total_rows' => $total_rows, ] ), 'pagination' => ListCoursesTemplate::instance()->html_pagination_number( [ 'total_pages' => $total_pages, 'paged' => $args['paged'], ] ), ]; $table_args = array( 'header' => $header, 'body' => $body, 'footer' => Template::combine_components( $section_footer ), 'class_table' => 'profile-list-table profile-list-quizzes', ); $html = TableListTemplate::instance()->html_table( $table_args ); } else { $html = Template::print_message( __( 'No quizzes found', 'learnpress' ), 'info', false ); } $section = [ 'header' => self::instance()->html_tabs_header( $args['type'] ), 'content' => $html, ]; $content->content = Template::combine_components( $section ); $content->total_pages = $total_pages; $content->paged = $args['paged']; } catch ( Throwable $e ) { $content->content = Template::print_message( $e->getMessage(), 'error', false ); } return $content; } /** * Render the header for the quiz tab. * * @param string $tab_active The active tab. * * @return string. */ public static function html_tabs_header( string $tab_active = 'all' ): string { $filter_types = apply_filters( 'learnpress/profile/quiz-tab/header/filter-types', array( 'all' => __( 'All', 'learnpress' ), 'completed' => __( 'Finished', 'learnpress' ), 'passed' => __( 'Passed', 'learnpress' ), 'failed' => __( 'Failed', 'learnpress' ), ) ); $html_li = ''; foreach ( $filter_types as $type => $label ) { $html_li .= sprintf( '