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 / Question / BuilderListQuestionsTemplate.php

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