PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / Models / WPTables / QuizzesTable.php

QuizzesTable.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/Models/WPTables/QuizzesTable.php

239 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Models\WPTables;
4
5 use LearnPress\Helpers\Response;
6 use LearnPress\Helpers\Template;
7 use LearnPress\Models\CourseSectionItemModel;
8 use LearnPress\Models\QuizPostModel;
9 use LP_Abstract_Post_Type;
10 use LP_Datetime;
11 use Throwable;
12 use WP_Post;
13 use WP_Posts_List_Table;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * LearnPress Quizzes Table class.
19 *
20 * @since 4.4.8
21 * @version 1.0.0
22 */
23 class QuizzesTable extends WP_Posts_List_Table {
24 /**
25 * Get the table columns.
26 *
27 * @return array
28 */
29 public function get_columns() {
30 $columns = parent::get_columns();
31
32 $pos = array_search( 'title', array_keys( $columns ), true );
33 if ( false !== $pos ) {
34 $insert = array(
35 LP_COURSE_CPT => esc_html__( 'Course', 'learnpress' ),
36 'instructor' => esc_html__( 'Author', 'learnpress' ),
37 'num_of_question' => esc_html__( 'Questions', 'learnpress' ),
38 'duration' => esc_html__( 'Duration', 'learnpress' ),
39 );
40
41 $columns = array_merge(
42 array_slice( $columns, 0, $pos + 1 ),
43 $insert,
44 array_slice( $columns, $pos + 1 )
45 );
46 }
47
48 unset( $columns['taxonomy-lesson-tag'] );
49
50 if ( ! empty( $columns['author'] ) ) {
51 unset( $columns['author'] );
52 }
53
54 $user = wp_get_current_user();
55 if ( in_array( LP_TEACHER_ROLE, $user->roles, true ) ) {
56 unset( $columns['instructor'] );
57 }
58
59 return $columns;
60 }
61
62 /**
63 * Set columns can be sortable.
64 *
65 * @return array
66 */
67 public function get_sortable_columns() {
68 $sortable_columns = parent::get_sortable_columns();
69 $sortable_columns['instructor'] = array( 'author', 'asc' );
70 $sortable_columns[ LP_COURSE_CPT ] = array( 'course-name', 'asc' );
71 $sortable_columns['num_of_question'] = array( 'question-count', 'asc' );
72
73 return $sortable_columns;
74 }
75
76 /**
77 * Column instructor.
78 *
79 * @param WP_Post $post
80 *
81 * @return void
82 */
83 public function column_instructor( $post ) {
84 try {
85 LP_Abstract_Post_Type::column_author( $post );
86 } catch ( Throwable $e ) {
87 Template::print_message( $e->getMessage(), Response::STATUS_ERROR );
88 }
89 }
90
91 /**
92 * Column course.
93 *
94 * @param WP_Post $post
95 *
96 * @return void
97 */
98 public function column_lp_course( $post ) {
99 try {
100 $this->render_courses_of_item( $post->ID );
101 } catch ( Throwable $e ) {
102 Template::print_message( $e->getMessage(), Response::STATUS_ERROR );
103 }
104 }
105
106 /**
107 * Column number of questions.
108 *
109 * @param WP_Post $post
110 *
111 * @return void
112 */
113 public function column_num_of_question( $post ) {
114 try {
115 $quizPostModel = QuizPostModel::find( $post->ID, true );
116 if ( ! $quizPostModel ) {
117 return;
118 }
119
120 $count = $quizPostModel->count_questions();
121
122 printf(
123 '<span class="lp-label-counter %s" title="%s">%s</span>',
124 esc_attr( ! $count ? 'disabled' : '' ),
125 $count ?
126 sprintf( /* translators: %d: question count */ _n( '%d question', '%d questions', $count, 'learnpress' ), $count ) :
127 __( 'This quiz has no questions', 'learnpress' ),
128 esc_html( $count )
129 );
130 } catch ( Throwable $e ) {
131 Template::print_message( $e->getMessage(), Response::STATUS_ERROR );
132 }
133 }
134
135 /**
136 * Column duration.
137 *
138 * @param WP_Post $post
139 *
140 * @return void
141 */
142 public function column_duration( $post ) {
143 try {
144 $quizPostModel = QuizPostModel::find( $post->ID, true );
145 if ( ! $quizPostModel ) {
146 return;
147 }
148
149 $duration_str = $quizPostModel->get_duration();
150 $duration_arr = explode( ' ', $duration_str );
151 $duration = $duration_arr[0];
152 $duration_type = $duration_arr[1];
153
154 if ( $duration > 0 ) {
155 $duration_str = LP_Datetime::get_string_plural_duration( $duration, $duration_type );
156 } else {
157 $duration_str = __( 'Unlimited', 'learnpress' );
158 }
159
160 echo esc_html( $duration_str );
161 } catch ( Throwable $e ) {
162 Template::print_message( $e->getMessage(), Response::STATUS_ERROR );
163 }
164 }
165
166 /**
167 * Column preview.
168 *
169 * @param WP_Post $post
170 *
171 * @return void
172 */
173 public function column_preview( $post ) {
174 try {
175 printf(
176 '<input type="checkbox" class="learn-press-checkbox learn-press-toggle-item-preview" %s value="%s" data-nonce="%s" />',
177 checked( get_post_meta( $post->ID, '_lp_preview', true ), 'yes', false ),
178 esc_attr( $post->ID ),
179 esc_attr( wp_create_nonce( 'learn-press-toggle-item-preview' ) )
180 );
181 } catch ( Throwable $e ) {
182 Template::print_message( $e->getMessage(), Response::STATUS_ERROR );
183 }
184 }
185
186 /**
187 * Get the course id currently filtering by.
188 *
189 * @return int|false
190 */
191 protected function get_filter_course_id() {
192 $course_id = ! empty( $_REQUEST['course'] ) ? absint( $_REQUEST['course'] ) : false;
193
194 if ( ! $course_id && learn_press_is_support_course_item_type( LP_QUIZ_CPT ) ) {
195 $course_id = false;
196 }
197
198 return $course_id;
199 }
200
201 /**
202 * Render the courses an item is assigned to.
203 *
204 * @param int $post_id
205 *
206 * @return void
207 */
208 protected function render_courses_of_item( int $post_id ) {
209 try {
210 $courses = CourseSectionItemModel::get_courses_from_item_id( $post_id, LP_QUIZ_CPT );
211
212 if ( $courses ) {
213 foreach ( $courses as $course ) {
214 $course_id = $course->section_course_id;
215 echo '<div><a href="' . esc_url_raw( remove_query_arg( 'orderby', add_query_arg( array( 'course' => $course_id ) ) ) ) . '">' . get_the_title( $course_id ) . '</a>';
216 echo '<div class="row-actions">';
217 printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $course_id ) ), __( 'Edit', 'learnpress' ) );
218 echo '&nbsp;|&nbsp;';
219 printf( '<a href="%s">%s</a>', get_the_permalink( $course_id ), __( 'View', 'learnpress' ) );
220
221 if ( $this->get_filter_course_id() ) {
222 echo '&nbsp;|&nbsp;';
223 printf(
224 '<a href="%s">%s</a>',
225 esc_url_raw( remove_query_arg( array( 'course', 'orderby' ) ) ),
226 __( 'Remove Filter', 'learnpress' )
227 );
228 }
229 echo '</div></div>';
230 }
231 } else {
232 _e( 'Not assigned yet', 'learnpress' );
233 }
234 } catch ( Throwable $e ) {
235 Template::print_message( $e->getMessage(), Response::STATUS_ERROR );
236 }
237 }
238 }
239