← All changes
|
inc/TemplateHooks/CourseBuilder/Quiz/BuilderListQuizzesTemplate.php
+351
-351
4.3.9
→
4.4.9
View file →
| @@ -1,351 +1,351 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Template hooks Tab Course in Course Builder. | |
| 4 | - * | |
| 5 | - * @since 4.3.0 | |
| 6 | - * @version 1.0.0 | |
| 7 | - */ | |
| 8 | - | |
| 9 | -namespace LearnPress\TemplateHooks\CourseBuilder\Quiz; | |
| 10 | - | |
| 11 | -use LearnPress\CourseBuilder\CourseBuilder; | |
| 12 | -use LearnPress\Helpers\Singleton; | |
| 13 | -use LearnPress\Helpers\Template; | |
| 14 | -use LearnPress\Models\PostModel; | |
| 15 | -use LearnPress\Models\QuizPostModel; | |
| 16 | -use LearnPress\Models\UserModel; | |
| 17 | -use LearnPress\TemplateHooks\CourseBuilder\Course\BuilderCourseTemplate; | |
| 18 | -use LearnPress\TemplateHooks\CourseBuilder\CourseBuilderTemplate; | |
| 19 | -use LP_WP_Filesystem; | |
| 20 | -use Throwable; | |
| 21 | -use WP_Query; | |
| 22 | - | |
| 23 | -class BuilderListQuizzesTemplate { | |
| 24 | - use Singleton; | |
| 25 | - | |
| 26 | - public function init() { | |
| 27 | - add_action( 'learn-press/course-builder/list-quizzes/layout', [ $this, 'layout' ] ); | |
| 28 | - } | |
| 29 | - | |
| 30 | - public function layout( array $data = [] ) { | |
| 31 | - $list_quiz = $this->tab_list_quizzes(); | |
| 32 | - | |
| 33 | - $tab = [ | |
| 34 | - 'wrapper' => '<div class="cb-tab-quiz">', | |
| 35 | - 'header' => $this->html_header(), | |
| 36 | - 'filter_bar' => $this->html_filter_bar(), | |
| 37 | - 'quizzes' => $list_quiz, | |
| 38 | - 'wrapper_end' => '</div>', | |
| 39 | - ]; | |
| 40 | - | |
| 41 | - echo Template::combine_components( $tab ); | |
| 42 | - } | |
| 43 | - | |
| 44 | - public function html_header(): string { | |
| 45 | - $section = [ | |
| 46 | - 'wrapper' => '<div class="cb-tab-header">', | |
| 47 | - 'title' => sprintf( '<h2 class="lp-cb-tab__title">%s</h2>', __( 'Quizzes', 'learnpress' ) ), | |
| 48 | - 'actions' => sprintf( | |
| 49 | - '<div class="cb-tab-header-actions" style="display:flex;align-items:center;gap:8px;">%s</div>', | |
| 50 | - CourseBuilderTemplate::instance()->html_btn_add_new() | |
| 51 | - ), | |
| 52 | - 'wrapper_end' => '</div>', | |
| 53 | - ]; | |
| 54 | - | |
| 55 | - return Template::combine_components( $section ); | |
| 56 | - } | |
| 57 | - | |
| 58 | - public function html_filter_bar(): string { | |
| 59 | - $section = [ | |
| 60 | - 'wrapper_action' => '<div class="cb-tab-quiz__action">', | |
| 61 | - 'search_quiz' => $this->html_search(), | |
| 62 | - 'wrapper_action_end' => '</div>', | |
| 63 | - ]; | |
| 64 | - | |
| 65 | - return Template::combine_components( $section ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - public function html_search() { | |
| 69 | - $args = lp_archive_skeleton_get_args(); | |
| 70 | - $link_tab = CourseBuilder::get_tab_link( 'quizzes' ); | |
| 71 | - | |
| 72 | - $search = [ | |
| 73 | - 'wrapper' => sprintf( '<form class="cb-search-form" method="get" action="%s">', $link_tab ), | |
| 74 | - 'search_quiz' => '<button class="lp-button cb-search-btn" type="submit"> <i class="lp-icon-search"> </i></button>', | |
| 75 | - 'input' => sprintf( '<input class="cb-input-search-quiz" type="search" placeholder="%s" name="c_search" value="%s">', __( 'Search', 'learnpress' ), $args['c_search'] ?? '' ), | |
| 76 | - 'wrapper_end' => '</form>', | |
| 77 | - ]; | |
| 78 | - | |
| 79 | - return Template::combine_components( $search ); | |
| 80 | - } | |
| 81 | - | |
| 82 | - public function tab_list_quizzes(): string { | |
| 83 | - $content = ''; | |
| 84 | - | |
| 85 | - try { | |
| 86 | - // Query quizzes of user | |
| 87 | - $param = lp_archive_skeleton_get_args(); | |
| 88 | - $param['id_url'] = 'tab-list-quizzes'; | |
| 89 | - | |
| 90 | - $query_args = array( | |
| 91 | - 'post_type' => LP_QUIZ_CPT, | |
| 92 | - 'post_status' => array( 'publish', 'private', 'draft', 'pending', 'trash' ), | |
| 93 | - 'posts_per_page' => 12, | |
| 94 | - 'paged' => $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1, | |
| 95 | - 's' => ! empty( $param['c_search'] ) ? sanitize_text_field( $param['c_search'] ) : '', | |
| 96 | - ); | |
| 97 | - | |
| 98 | - $user_id = get_current_user_id(); | |
| 99 | - $userModel = UserModel::find( $user_id, true ); | |
| 100 | - if ( ! $userModel instanceof UserModel ) { | |
| 101 | - return ''; | |
| 102 | - } | |
| 103 | - | |
| 104 | - if ( ! current_user_can( ADMIN_ROLE ) ) { | |
| 105 | - $query_args['author'] = $user_id; | |
| 106 | - } | |
| 107 | - | |
| 108 | - $query = new WP_Query(); | |
| 109 | - $result = $query->query( $query_args ); | |
| 110 | - $total_quizzes = $query->found_posts ?? 0; | |
| 111 | - | |
| 112 | - if ( $total_quizzes < 1 ) { | |
| 113 | - unset( $query_args['paged'] ); | |
| 114 | - $count_query = new WP_Query(); | |
| 115 | - $count_query->query( $query_args ); | |
| 116 | - $total_quizzes = $count_query->found_posts; | |
| 117 | - } | |
| 118 | - | |
| 119 | - $quizzes = array(); | |
| 120 | - | |
| 121 | - if ( $query->have_posts() ) { | |
| 122 | - while ( $query->have_posts() ) { | |
| 123 | - $query->the_post(); | |
| 124 | - | |
| 125 | - $quiz_model = QuizPostModel::find( get_the_ID(), true ); | |
| 126 | - $quizzes[] = $quiz_model; | |
| 127 | - } | |
| 128 | - } | |
| 129 | - wp_reset_postdata(); | |
| 130 | - | |
| 131 | - if ( ! empty( $quizzes ) ) { | |
| 132 | - $html_quizzes = $this->list_quizzes( $quizzes ); | |
| 133 | - } else { | |
| 134 | - $html_quizzes = Template::print_message( | |
| 135 | - sprintf( __( 'No quizzes found', 'learnpress' ) ), | |
| 136 | - 'info', | |
| 137 | - false | |
| 138 | - ); | |
| 139 | - } | |
| 140 | - | |
| 141 | - $total_pages = \LP_Database::get_total_pages( $query_args['posts_per_page'], $total_quizzes ); | |
| 142 | - $link_tab = CourseBuilder::get_tab_link( 'quizzes' ); | |
| 143 | - $data_pagination = [ | |
| 144 | - 'paged' => max( 1, $query_args['paged'] ?? 1 ), | |
| 145 | - 'total_pages' => $total_pages, | |
| 146 | - 'base' => trailingslashit( $link_tab ) . 'page/%#%', | |
| 147 | - 'format' => '', | |
| 148 | - ]; | |
| 149 | - | |
| 150 | - $pagination = Template::instance()->html_pagination( $data_pagination ); | |
| 151 | - | |
| 152 | - $sections = apply_filters( | |
| 153 | - 'learn-press/course-builder/quizzes/sections', | |
| 154 | - [ | |
| 155 | - 'wrapper' => '<div class="courses-builder__quiz-tab learn-press-quizzes">', | |
| 156 | - 'quizzes' => $html_quizzes, | |
| 157 | - 'wrapper_end' => '</div>', | |
| 158 | - 'pagination' => $pagination, | |
| 159 | - ], | |
| 160 | - $quizzes, | |
| 161 | - $userModel | |
| 162 | - ); | |
| 163 | - | |
| 164 | - $content = Template::combine_components( $sections ); | |
| 165 | - } catch ( Throwable $e ) { | |
| 166 | - error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 167 | - } | |
| 168 | - | |
| 169 | - return $content; | |
| 170 | - } | |
| 171 | - | |
| 172 | - /** | |
| 173 | - * Display list quizzes. | |
| 174 | - * | |
| 175 | - * @param $quizzes | |
| 176 | - * | |
| 177 | - * @return string | |
| 178 | - */ | |
| 179 | - public function list_quizzes( $quizzes ): string { | |
| 180 | - $content = ''; | |
| 181 | - | |
| 182 | - try { | |
| 183 | - $html_list_quiz = ''; | |
| 184 | - foreach ( $quizzes as $quiz_model ) { | |
| 185 | - $html_list_quiz .= self::render_quiz( $quiz_model ); | |
| 186 | - } | |
| 187 | - | |
| 188 | - $header = '<div class="cb-list-table-header">'; | |
| 189 | - $header .= sprintf( '<span>%s</span>', __( 'Quiz Title', 'learnpress' ) ); | |
| 190 | - $header .= sprintf( '<span>%s</span>', __( 'Courses', 'learnpress' ) ); | |
| 191 | - $header .= sprintf( '<span>%s</span>', __( 'Questions', 'learnpress' ) ); | |
| 192 | - $header .= sprintf( '<span>%s</span>', __( 'Duration', 'learnpress' ) ); | |
| 193 | - $header .= sprintf( '<span>%s</span>', __( 'Create Date', 'learnpress' ) ); | |
| 194 | - $header .= sprintf( '<span>%s</span>', __( 'Status', 'learnpress' ) ); | |
| 195 | - $header .= sprintf( '<span>%s</span>', __( 'Actions', 'learnpress' ) ); | |
| 196 | - $header .= '</div>'; | |
| 197 | - | |
| 198 | - $sections = [ | |
| 199 | - 'header' => $header, | |
| 200 | - 'wrapper' => '<ul class="cb-list-quiz">', | |
| 201 | - 'list_quiz' => $html_list_quiz, | |
| 202 | - 'wrapper_end' => '</ul>', | |
| 203 | - ]; | |
| 204 | - | |
| 205 | - $content = Template::combine_components( $sections ); | |
| 206 | - } catch ( Throwable $e ) { | |
| 207 | - error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 208 | - } | |
| 209 | - | |
| 210 | - return $content; | |
| 211 | - } | |
| 212 | - | |
| 213 | - /** | |
| 214 | - * Render quiz in course builder | |
| 215 | - * | |
| 216 | - * @param $quiz | |
| 217 | - * @param array $settings | |
| 218 | - * | |
| 219 | - * @return string | |
| 220 | - * @since 4.3.0 | |
| 221 | - * @version 1.0.0 | |
| 222 | - */ | |
| 223 | - public static function render_quiz( QuizPostModel $quiz_model, array $settings = [] ): string { | |
| 224 | - $edit_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-edit.svg' ); | |
| 225 | - $more_actions_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-more.svg' ); | |
| 226 | - | |
| 227 | - $author = get_user_by( 'ID', $quiz_model->post_author ); | |
| 228 | - $author_name = $author && isset( $author->display_name ) ? $author->display_name : '--'; | |
| 229 | - | |
| 230 | - $quiz = array( | |
| 231 | - 'id' => $quiz_model->get_id(), | |
| 232 | - 'title' => $quiz_model->post_title, | |
| 233 | - 'status' => $quiz_model->post_status, | |
| 234 | - 'courses' => BuilderEditQuizTemplate::instance()->get_assigned( $quiz_model->get_id() ), | |
| 235 | - 'author' => $author_name, | |
| 236 | - 'duration' => learn_press_get_post_translated_duration( $quiz_model->get_id(), esc_html__( 'Lifetime', 'learnpress' ) ), | |
| 237 | - 'date_modified' => lp_jwt_prepare_date_response( $quiz_model->post_date_gmt ), | |
| 238 | - ); | |
| 239 | - | |
| 240 | - try { | |
| 241 | - $edit_link = BuilderQuizTemplate::instance()->get_link_edit( $quiz['id'] ); | |
| 242 | - | |
| 243 | - $html_courses = ''; | |
| 244 | - $assigned = '--'; | |
| 245 | - if ( ! empty( $quiz['courses'] ) ) { | |
| 246 | - $courses = is_array( $quiz['courses'] ) && isset( $quiz['courses']['id'] ) | |
| 247 | - ? array( $quiz['courses'] ) | |
| 248 | - : $quiz['courses']; | |
| 249 | - | |
| 250 | - $course_htmls = array(); | |
| 251 | - foreach ( $courses as $course ) { | |
| 252 | - $course_id = $course['id'] ?? 0; | |
| 253 | - $course_title = $course['title'] ?? ''; | |
| 254 | - | |
| 255 | - if ( $course_id && $course_title ) { | |
| 256 | - $course_link = BuilderCourseTemplate::instance()->get_link_edit( $course_id ); | |
| 257 | - $course_htmls[] = sprintf( | |
| 258 | - '<a href="%s" target="_blank">%s</a>', | |
| 259 | - esc_url( $course_link ), | |
| 260 | - esc_html( $course_title ) | |
| 261 | - ); | |
| 262 | - } | |
| 263 | - } | |
| 264 | - | |
| 265 | - if ( ! empty( $course_htmls ) ) { | |
| 266 | - $assigned = implode( ', ', $course_htmls ); | |
| 267 | - } | |
| 268 | - } | |
| 269 | - | |
| 270 | - $html_courses = sprintf( | |
| 271 | - '<div class="lesson-assigned-courses"><span class="label">%s:</span> %s</div>', | |
| 272 | - __( 'Assigned', 'learnpress' ), | |
| 273 | - $assigned | |
| 274 | - ); | |
| 275 | - | |
| 276 | - $status = $quiz_model->post_status ?? ''; | |
| 277 | - $html_content = apply_filters( | |
| 278 | - 'learn-press/course-builder/list-quizzes/item/section/bottom', | |
| 279 | - [ | |
| 280 | - 'title' => sprintf( | |
| 281 | - '<h3 class="wap-quiz-title"><a href="%s">%s</a></h3>', | |
| 282 | - esc_url( $edit_link ), | |
| 283 | - esc_html( $quiz['title'] ) | |
| 284 | - ), | |
| 285 | - 'courses' => $html_courses, | |
| 286 | - 'total_questions' => sprintf( '<span class="quiz__total-questions">%d</span>', $quiz_model->count_questions() ), | |
| 287 | - 'duration' => sprintf( '<span class="quiz__duration">%s</span>', $quiz['duration'] ), | |
| 288 | - 'date' => sprintf( '<span class="quiz__date">%s</span>', ! empty( $quiz['date_modified'] ) ? date_i18n( 'm/d/Y', strtotime( $quiz['date_modified'] ) ) : '--' ), | |
| 289 | - 'quiz_status' => ! empty( $status ) ? sprintf( '<span class="quiz-status %1$s">%1$s</span>', $status ) : '<span></span>', | |
| 290 | - ], | |
| 291 | - $quiz, | |
| 292 | - $settings | |
| 293 | - ); | |
| 294 | - | |
| 295 | - $html_action = apply_filters( | |
| 296 | - 'learn-press/course-builder/list-quizzes/item/action', | |
| 297 | - [ | |
| 298 | - 'wrapper' => '<div class="quiz-action">', | |
| 299 | - 'edit' => $status !== PostModel::STATUS_TRASH ? sprintf( | |
| 300 | - '<div class="quiz-action-editor"><a class="lp-button btn-edit-quiz quiz-edit-permalink" href="%s">%s %s</a></div>', | |
| 301 | - esc_url( $edit_link ), | |
| 302 | - $edit_icon, | |
| 303 | - __( 'Edit', 'learnpress' ) | |
| 304 | - ) : '', | |
| 305 | - 'action_expanded_button' => sprintf( | |
| 306 | - '<button type="button" class="lp-button quiz-action-expanded">%s</button>', | |
| 307 | - $more_actions_icon | |
| 308 | - ), | |
| 309 | - 'action_expanded_wrapper' => '<div style="display:none;" class="quiz-action-expanded__items">', | |
| 310 | - 'action_expanded_duplicate' => sprintf( '<span class="lp-button quiz-action-expanded__duplicate" data-title="%s" data-content="%s">%s</span>', __( 'Are you sure?', 'learnpress' ), __( 'Are you sure you want to duplicate this quiz?', 'learnpress' ), __( 'Duplicate', 'learnpress' ) ), | |
| 311 | - 'action_expanded_publish' => sprintf( '<span class="lp-button quiz-action-expanded__publish">%s</span>', __( 'Publish', 'learnpress' ) ), | |
| 312 | - 'action_expanded_trash' => sprintf( | |
| 313 | - '<span class="lp-button quiz-action-expanded__trash"%s>%s</span>', | |
| 314 | - $status === 'trash' ? ' style="display:none"' : '', | |
| 315 | - __( 'Trash', 'learnpress' ) | |
| 316 | - ), | |
| 317 | - 'action_expanded_restore' => sprintf( | |
| 318 | - '<span class="lp-button quiz-action-expanded__restore"%s>%s</span>', | |
| 319 | - $status !== 'trash' ? ' style="display:none"' : '', | |
| 320 | - __( 'Restore', 'learnpress' ) | |
| 321 | - ), | |
| 322 | - 'action_expanded_delete' => sprintf( '<span class="lp-button quiz-action-expanded__delete">%s</span>', __( 'Delete', 'learnpress' ) ), | |
| 323 | - 'action_expanded_wrapper_end' => '</div>', | |
| 324 | - 'wrapper_end' => '</div>', | |
| 325 | - ], | |
| 326 | - $quiz, | |
| 327 | - $settings | |
| 328 | - ); | |
| 329 | - | |
| 330 | - $section = apply_filters( | |
| 331 | - 'learn-press/course-builder/list-quizzes/item-li', | |
| 332 | - [ | |
| 333 | - 'wrapper_li' => '<li class="quiz">', | |
| 334 | - 'wrapper_div' => sprintf( '<div class="quiz-item" data-quiz-id="%s" data-status="%s">', $quiz['id'], $status ), | |
| 335 | - 'quiz_info' => Template::combine_components( $html_content ), | |
| 336 | - 'quiz_action' => Template::combine_components( $html_action ), | |
| 337 | - 'wrapper_div_end' => '</div>', | |
| 338 | - 'wrapper_li_end' => '</li>', | |
| 339 | - ], | |
| 340 | - $quiz, | |
| 341 | - $settings | |
| 342 | - ); | |
| 343 | - | |
| 344 | - $html_item = Template::combine_components( $section ); | |
| 345 | - } catch ( Throwable $e ) { | |
| 346 | - $html_item = $e->getMessage(); | |
| 347 | - } | |
| 348 | - | |
| 349 | - return $html_item; | |
| 350 | - } | |
| 351 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Template hooks Tab Course in Course Builder. | |
| 4 | + * | |
| 5 | + * @since 4.3.0 | |
| 6 | + * @version 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace LearnPress\TemplateHooks\CourseBuilder\Quiz; | |
| 10 | + | |
| 11 | +use LearnPress\CourseBuilder\CourseBuilder; | |
| 12 | +use LearnPress\Helpers\Singleton; | |
| 13 | +use LearnPress\Helpers\Template; | |
| 14 | +use LearnPress\Models\PostModel; | |
| 15 | +use LearnPress\Models\QuizPostModel; | |
| 16 | +use LearnPress\Models\UserModel; | |
| 17 | +use LearnPress\TemplateHooks\CourseBuilder\Course\BuilderCourseTemplate; | |
| 18 | +use LearnPress\TemplateHooks\CourseBuilder\CourseBuilderTemplate; | |
| 19 | +use LP_WP_Filesystem; | |
| 20 | +use Throwable; | |
| 21 | +use WP_Query; | |
| 22 | + | |
| 23 | +class BuilderListQuizzesTemplate { | |
| 24 | + use Singleton; | |
| 25 | + | |
| 26 | + public function init() { | |
| 27 | + add_action( 'learn-press/course-builder/list-quizzes/layout', [ $this, 'layout' ] ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public function layout( array $data = [] ) { | |
| 31 | + $list_quiz = $this->tab_list_quizzes(); | |
| 32 | + | |
| 33 | + $tab = [ | |
| 34 | + 'wrapper' => '<div class="cb-tab-quiz">', | |
| 35 | + 'header' => $this->html_header(), | |
| 36 | + 'filter_bar' => $this->html_filter_bar(), | |
| 37 | + 'quizzes' => $list_quiz, | |
| 38 | + 'wrapper_end' => '</div>', | |
| 39 | + ]; | |
| 40 | + | |
| 41 | + echo Template::combine_components( $tab ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function html_header(): string { | |
| 45 | + $section = [ | |
| 46 | + 'wrapper' => '<div class="cb-tab-header">', | |
| 47 | + 'title' => sprintf( '<h2 class="lp-cb-tab__title">%s</h2>', __( 'Quizzes', 'learnpress' ) ), | |
| 48 | + 'actions' => sprintf( | |
| 49 | + '<div class="cb-tab-header-actions" style="display:flex;align-items:center;gap:8px;">%s</div>', | |
| 50 | + CourseBuilderTemplate::instance()->html_btn_add_new() | |
| 51 | + ), | |
| 52 | + 'wrapper_end' => '</div>', | |
| 53 | + ]; | |
| 54 | + | |
| 55 | + return Template::combine_components( $section ); | |
| 56 | + } | |
| 57 | + | |
| 58 | + public function html_filter_bar(): string { | |
| 59 | + $section = [ | |
| 60 | + 'wrapper_action' => '<div class="cb-tab-quiz__action">', | |
| 61 | + 'search_quiz' => $this->html_search(), | |
| 62 | + 'wrapper_action_end' => '</div>', | |
| 63 | + ]; | |
| 64 | + | |
| 65 | + return Template::combine_components( $section ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + public function html_search() { | |
| 69 | + $args = lp_archive_skeleton_get_args(); | |
| 70 | + $link_tab = CourseBuilder::get_tab_link( 'quizzes' ); | |
| 71 | + | |
| 72 | + $search = [ | |
| 73 | + 'wrapper' => sprintf( '<form class="cb-search-form" method="get" action="%s">', $link_tab ), | |
| 74 | + 'search_quiz' => '<button class="lp-button cb-search-btn" type="submit"> <i class="lp-icon-search"> </i></button>', | |
| 75 | + 'input' => sprintf( '<input class="cb-input-search-quiz" type="search" placeholder="%s" name="c_search" value="%s">', __( 'Search', 'learnpress' ), esc_attr( $args['c_search'] ?? '' ) ), | |
| 76 | + 'wrapper_end' => '</form>', | |
| 77 | + ]; | |
| 78 | + | |
| 79 | + return Template::combine_components( $search ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + public function tab_list_quizzes(): string { | |
| 83 | + $content = ''; | |
| 84 | + | |
| 85 | + try { | |
| 86 | + // Query quizzes of user | |
| 87 | + $param = lp_archive_skeleton_get_args(); | |
| 88 | + $param['id_url'] = 'tab-list-quizzes'; | |
| 89 | + | |
| 90 | + $query_args = array( | |
| 91 | + 'post_type' => LP_QUIZ_CPT, | |
| 92 | + 'post_status' => array( 'publish', 'private', 'draft', 'pending', 'trash' ), | |
| 93 | + 'posts_per_page' => 12, | |
| 94 | + 'paged' => $GLOBALS['wp_query']->get( 'paged', 1 ) ? $GLOBALS['wp_query']->get( 'paged', 1 ) : 1, | |
| 95 | + 's' => ! empty( $param['c_search'] ) ? sanitize_text_field( $param['c_search'] ) : '', | |
| 96 | + ); | |
| 97 | + | |
| 98 | + $user_id = get_current_user_id(); | |
| 99 | + $userModel = UserModel::find( $user_id, true ); | |
| 100 | + if ( ! $userModel instanceof UserModel ) { | |
| 101 | + return ''; | |
| 102 | + } | |
| 103 | + | |
| 104 | + if ( ! current_user_can( ADMIN_ROLE ) ) { | |
| 105 | + $query_args['author'] = $user_id; | |
| 106 | + } | |
| 107 | + | |
| 108 | + $query = new WP_Query(); | |
| 109 | + $result = $query->query( $query_args ); | |
| 110 | + $total_quizzes = $query->found_posts ?? 0; | |
| 111 | + | |
| 112 | + if ( $total_quizzes < 1 ) { | |
| 113 | + unset( $query_args['paged'] ); | |
| 114 | + $count_query = new WP_Query(); | |
| 115 | + $count_query->query( $query_args ); | |
| 116 | + $total_quizzes = $count_query->found_posts; | |
| 117 | + } | |
| 118 | + | |
| 119 | + $quizzes = array(); | |
| 120 | + | |
| 121 | + if ( $query->have_posts() ) { | |
| 122 | + while ( $query->have_posts() ) { | |
| 123 | + $query->the_post(); | |
| 124 | + | |
| 125 | + $quiz_model = QuizPostModel::find( get_the_ID(), true ); | |
| 126 | + $quizzes[] = $quiz_model; | |
| 127 | + } | |
| 128 | + } | |
| 129 | + wp_reset_postdata(); | |
| 130 | + | |
| 131 | + if ( ! empty( $quizzes ) ) { | |
| 132 | + $html_quizzes = $this->list_quizzes( $quizzes ); | |
| 133 | + } else { | |
| 134 | + $html_quizzes = Template::print_message( | |
| 135 | + sprintf( __( 'No quizzes found', 'learnpress' ) ), | |
| 136 | + 'info', | |
| 137 | + false | |
| 138 | + ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + $total_pages = \LP_Database::get_total_pages( $query_args['posts_per_page'], $total_quizzes ); | |
| 142 | + $link_tab = CourseBuilder::get_tab_link( 'quizzes' ); | |
| 143 | + $data_pagination = [ | |
| 144 | + 'paged' => max( 1, $query_args['paged'] ?? 1 ), | |
| 145 | + 'total_pages' => $total_pages, | |
| 146 | + 'base' => trailingslashit( $link_tab ) . 'page/%#%', | |
| 147 | + 'format' => '', | |
| 148 | + ]; | |
| 149 | + | |
| 150 | + $pagination = Template::instance()->html_pagination( $data_pagination ); | |
| 151 | + | |
| 152 | + $sections = apply_filters( | |
| 153 | + 'learn-press/course-builder/quizzes/sections', | |
| 154 | + [ | |
| 155 | + 'wrapper' => '<div class="courses-builder__quiz-tab learn-press-quizzes">', | |
| 156 | + 'quizzes' => $html_quizzes, | |
| 157 | + 'wrapper_end' => '</div>', | |
| 158 | + 'pagination' => $pagination, | |
| 159 | + ], | |
| 160 | + $quizzes, | |
| 161 | + $userModel | |
| 162 | + ); | |
| 163 | + | |
| 164 | + $content = Template::combine_components( $sections ); | |
| 165 | + } catch ( Throwable $e ) { | |
| 166 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + return $content; | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Display list quizzes. | |
| 174 | + * | |
| 175 | + * @param $quizzes | |
| 176 | + * | |
| 177 | + * @return string | |
| 178 | + */ | |
| 179 | + public function list_quizzes( $quizzes ): string { | |
| 180 | + $content = ''; | |
| 181 | + | |
| 182 | + try { | |
| 183 | + $html_list_quiz = ''; | |
| 184 | + foreach ( $quizzes as $quiz_model ) { | |
| 185 | + $html_list_quiz .= self::render_quiz( $quiz_model ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + $header = '<div class="cb-list-table-header">'; | |
| 189 | + $header .= sprintf( '<span>%s</span>', __( 'Quiz Title', 'learnpress' ) ); | |
| 190 | + $header .= sprintf( '<span>%s</span>', __( 'Courses', 'learnpress' ) ); | |
| 191 | + $header .= sprintf( '<span>%s</span>', __( 'Questions', 'learnpress' ) ); | |
| 192 | + $header .= sprintf( '<span>%s</span>', __( 'Duration', 'learnpress' ) ); | |
| 193 | + $header .= sprintf( '<span>%s</span>', __( 'Create Date', 'learnpress' ) ); | |
| 194 | + $header .= sprintf( '<span>%s</span>', __( 'Status', 'learnpress' ) ); | |
| 195 | + $header .= sprintf( '<span>%s</span>', __( 'Actions', 'learnpress' ) ); | |
| 196 | + $header .= '</div>'; | |
| 197 | + | |
| 198 | + $sections = [ | |
| 199 | + 'header' => $header, | |
| 200 | + 'wrapper' => '<ul class="cb-list-quiz">', | |
| 201 | + 'list_quiz' => $html_list_quiz, | |
| 202 | + 'wrapper_end' => '</ul>', | |
| 203 | + ]; | |
| 204 | + | |
| 205 | + $content = Template::combine_components( $sections ); | |
| 206 | + } catch ( Throwable $e ) { | |
| 207 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + return $content; | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Render quiz in course builder | |
| 215 | + * | |
| 216 | + * @param $quiz | |
| 217 | + * @param array $settings | |
| 218 | + * | |
| 219 | + * @return string | |
| 220 | + * @since 4.3.0 | |
| 221 | + * @version 1.0.0 | |
| 222 | + */ | |
| 223 | + public static function render_quiz( QuizPostModel $quiz_model, array $settings = [] ): string { | |
| 224 | + $edit_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-edit.svg' ); | |
| 225 | + $more_actions_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-more.svg' ); | |
| 226 | + | |
| 227 | + $author = get_user_by( 'ID', $quiz_model->post_author ); | |
| 228 | + $author_name = $author && isset( $author->display_name ) ? $author->display_name : '--'; | |
| 229 | + | |
| 230 | + $quiz = array( | |
| 231 | + 'id' => $quiz_model->get_id(), | |
| 232 | + 'title' => $quiz_model->post_title, | |
| 233 | + 'status' => $quiz_model->post_status, | |
| 234 | + 'courses' => BuilderEditQuizTemplate::instance()->get_assigned( $quiz_model->get_id() ), | |
| 235 | + 'author' => $author_name, | |
| 236 | + 'duration' => learn_press_get_post_translated_duration( $quiz_model->get_id(), esc_html__( 'Lifetime', 'learnpress' ) ), | |
| 237 | + 'date_modified' => lp_jwt_prepare_date_response( $quiz_model->post_date_gmt ), | |
| 238 | + ); | |
| 239 | + | |
| 240 | + try { | |
| 241 | + $edit_link = BuilderQuizTemplate::instance()->get_link_edit( $quiz['id'] ); | |
| 242 | + | |
| 243 | + $html_courses = ''; | |
| 244 | + $assigned = '--'; | |
| 245 | + if ( ! empty( $quiz['courses'] ) ) { | |
| 246 | + $courses = is_array( $quiz['courses'] ) && isset( $quiz['courses']['id'] ) | |
| 247 | + ? array( $quiz['courses'] ) | |
| 248 | + : $quiz['courses']; | |
| 249 | + | |
| 250 | + $course_htmls = array(); | |
| 251 | + foreach ( $courses as $course ) { | |
| 252 | + $course_id = $course['id'] ?? 0; | |
| 253 | + $course_title = $course['title'] ?? ''; | |
| 254 | + | |
| 255 | + if ( $course_id && $course_title ) { | |
| 256 | + $course_link = BuilderCourseTemplate::instance()->get_link_edit( $course_id ); | |
| 257 | + $course_htmls[] = sprintf( | |
| 258 | + '<a href="%s" target="_blank">%s</a>', | |
| 259 | + esc_url( $course_link ), | |
| 260 | + esc_html( $course_title ) | |
| 261 | + ); | |
| 262 | + } | |
| 263 | + } | |
| 264 | + | |
| 265 | + if ( ! empty( $course_htmls ) ) { | |
| 266 | + $assigned = implode( ', ', $course_htmls ); | |
| 267 | + } | |
| 268 | + } | |
| 269 | + | |
| 270 | + $html_courses = sprintf( | |
| 271 | + '<div class="lesson-assigned-courses"><span class="label">%s:</span> %s</div>', | |
| 272 | + __( 'Assigned', 'learnpress' ), | |
| 273 | + $assigned | |
| 274 | + ); | |
| 275 | + | |
| 276 | + $status = $quiz_model->post_status ?? ''; | |
| 277 | + $html_content = apply_filters( | |
| 278 | + 'learn-press/course-builder/list-quizzes/item/section/bottom', | |
| 279 | + [ | |
| 280 | + 'title' => sprintf( | |
| 281 | + '<h3 class="wap-quiz-title"><a href="%s">%s</a></h3>', | |
| 282 | + esc_url( $edit_link ), | |
| 283 | + esc_html( $quiz['title'] ) | |
| 284 | + ), | |
| 285 | + 'courses' => $html_courses, | |
| 286 | + 'total_questions' => sprintf( '<span class="quiz__total-questions">%d</span>', $quiz_model->count_questions() ), | |
| 287 | + 'duration' => sprintf( '<span class="quiz__duration">%s</span>', $quiz['duration'] ), | |
| 288 | + 'date' => sprintf( '<span class="quiz__date">%s</span>', ! empty( $quiz['date_modified'] ) ? date_i18n( 'm/d/Y', strtotime( $quiz['date_modified'] ) ) : '--' ), | |
| 289 | + 'quiz_status' => ! empty( $status ) ? sprintf( '<span class="quiz-status %1$s">%1$s</span>', $status ) : '<span></span>', | |
| 290 | + ], | |
| 291 | + $quiz, | |
| 292 | + $settings | |
| 293 | + ); | |
| 294 | + | |
| 295 | + $html_action = apply_filters( | |
| 296 | + 'learn-press/course-builder/list-quizzes/item/action', | |
| 297 | + [ | |
| 298 | + 'wrapper' => '<div class="quiz-action">', | |
| 299 | + 'edit' => $status !== PostModel::STATUS_TRASH ? sprintf( | |
| 300 | + '<div class="quiz-action-editor"><a class="lp-button btn-edit-quiz quiz-edit-permalink" href="%s">%s %s</a></div>', | |
| 301 | + esc_url( $edit_link ), | |
| 302 | + $edit_icon, | |
| 303 | + __( 'Edit', 'learnpress' ) | |
| 304 | + ) : '', | |
| 305 | + 'action_expanded_button' => sprintf( | |
| 306 | + '<button type="button" class="lp-button quiz-action-expanded">%s</button>', | |
| 307 | + $more_actions_icon | |
| 308 | + ), | |
| 309 | + 'action_expanded_wrapper' => '<div style="display:none;" class="quiz-action-expanded__items">', | |
| 310 | + 'action_expanded_duplicate' => sprintf( '<span class="lp-button quiz-action-expanded__duplicate" data-title="%s" data-content="%s">%s</span>', __( 'Are you sure?', 'learnpress' ), __( 'Are you sure you want to duplicate this quiz?', 'learnpress' ), __( 'Duplicate', 'learnpress' ) ), | |
| 311 | + 'action_expanded_publish' => sprintf( '<span class="lp-button quiz-action-expanded__publish">%s</span>', __( 'Publish', 'learnpress' ) ), | |
| 312 | + 'action_expanded_trash' => sprintf( | |
| 313 | + '<span class="lp-button quiz-action-expanded__trash"%s>%s</span>', | |
| 314 | + $status === 'trash' ? ' style="display:none"' : '', | |
| 315 | + __( 'Trash', 'learnpress' ) | |
| 316 | + ), | |
| 317 | + 'action_expanded_restore' => sprintf( | |
| 318 | + '<span class="lp-button quiz-action-expanded__restore"%s>%s</span>', | |
| 319 | + $status !== 'trash' ? ' style="display:none"' : '', | |
| 320 | + __( 'Restore', 'learnpress' ) | |
| 321 | + ), | |
| 322 | + 'action_expanded_delete' => sprintf( '<span class="lp-button quiz-action-expanded__delete">%s</span>', __( 'Delete', 'learnpress' ) ), | |
| 323 | + 'action_expanded_wrapper_end' => '</div>', | |
| 324 | + 'wrapper_end' => '</div>', | |
| 325 | + ], | |
| 326 | + $quiz, | |
| 327 | + $settings | |
| 328 | + ); | |
| 329 | + | |
| 330 | + $section = apply_filters( | |
| 331 | + 'learn-press/course-builder/list-quizzes/item-li', | |
| 332 | + [ | |
| 333 | + 'wrapper_li' => '<li class="quiz">', | |
| 334 | + 'wrapper_div' => sprintf( '<div class="quiz-item" data-quiz-id="%s" data-status="%s">', $quiz['id'], $status ), | |
| 335 | + 'quiz_info' => Template::combine_components( $html_content ), | |
| 336 | + 'quiz_action' => Template::combine_components( $html_action ), | |
| 337 | + 'wrapper_div_end' => '</div>', | |
| 338 | + 'wrapper_li_end' => '</li>', | |
| 339 | + ], | |
| 340 | + $quiz, | |
| 341 | + $settings | |
| 342 | + ); | |
| 343 | + | |
| 344 | + $html_item = Template::combine_components( $section ); | |
| 345 | + } catch ( Throwable $e ) { | |
| 346 | + $html_item = $e->getMessage(); | |
| 347 | + } | |
| 348 | + | |
| 349 | + return $html_item; | |
| 350 | + } | |
| 351 | +} | |