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