PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.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 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / TemplateHooks / CourseBuilder / Question / BuilderListQuestionsTemplate.php

BuilderListQuestionsTemplate.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/TemplateHooks/CourseBuilder/Question/BuilderListQuestionsTemplate.php

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