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
learnpress / inc / TemplateHooks / CourseBuilder / Quiz / BuilderListQuizzesTemplate.php

BuilderListQuizzesTemplate.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.10, at inc/TemplateHooks/CourseBuilder/Quiz/BuilderListQuizzesTemplate.php

389 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
389