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 / rest-api / v1 / admin / class-lp-admin-rest-statistics-controller.php

class-lp-admin-rest-statistics-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/rest-api/v1/admin/class-lp-admin-rest-statistics-controller.php

517 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Helpers\Template;
4
5 /**
6 * Class LP_REST_Admin_Statistics_Controller
7 *
8 * @since 4.2.5.5
9 */
10 class LP_REST_Admin_Statistics_Controller extends LP_Abstract_REST_Controller {
11 protected static $_instance = null;
12 public function __construct() {
13 $this->namespace = 'lp/v1';
14 $this->rest_base = 'statistics';
15
16 parent::__construct();
17 }
18
19 public function register_routes() {
20 $this->routes = array(
21 'overviews-statistics' => array(
22 array(
23 'methods' => WP_REST_Server::READABLE,
24 'callback' => array( $this, 'get_overviews_statistics' ),
25 'permission_callback' => array( $this, 'permission_check' ),
26 ),
27 ),
28 'order-statistics' => array(
29 array(
30 'methods' => WP_REST_Server::READABLE,
31 'callback' => array( $this, 'get_order_statistics' ),
32 'permission_callback' => array( $this, 'permission_check' ),
33 ),
34 ),
35 'course-statistics' => array(
36 array(
37 'methods' => WP_REST_Server::READABLE,
38 'callback' => array( $this, 'get_courses_statistics' ),
39 'permission_callback' => array( $this, 'permission_check' ),
40 ),
41 ),
42 'user-statistics' => array(
43 array(
44 'methods' => WP_REST_Server::READABLE,
45 'callback' => array( $this, 'get_users_statistics' ),
46 'permission_callback' => array( $this, 'permission_check' ),
47 ),
48 ),
49 );
50
51 parent::register_routes();
52 }
53
54 /**
55 * Gets the overviews statistics.
56 *
57 * @param WP_REST_Request $request
58 *
59 * @return LP_REST_Response.
60 */
61 public function get_overviews_statistics( WP_REST_Request $request ): LP_REST_Response {
62 $response = new LP_REST_Response();
63
64 try {
65 $params = $request->get_params();
66 $params = LP_Helper::sanitize_params_submitted( $params );
67 $filter = $this->get_statistics_filter( $params );
68
69 $lp_statistic_db = LP_Statistics_DB::getInstance();
70 $net_sales = $lp_statistic_db->get_net_sales_data( $filter['filter_type'], $filter['time'] );
71 $total_courses = $lp_statistic_db->get_total_course_created( $filter['filter_type'], $filter['time'] );
72 $total_orders = $lp_statistic_db->get_total_order_created( $filter['filter_type'], $filter['time'] );
73 $total_instructors = $lp_statistic_db->get_total_instructor_created( $filter['filter_type'], $filter['time'] );
74 $total_students = $lp_statistic_db->get_total_student_created( $filter['filter_type'], $filter['time'] );
75 $chart_data = $this->process_chart_data( $filter, $net_sales );
76 $top_courses = $lp_statistic_db->get_top_sold_courses( $filter['filter_type'], $filter['time'] );
77 $top_categories = $lp_statistic_db->get_top_sold_categories( $filter['filter_type'], $filter['time'] );
78 $chart_data['line_label'] = __( 'Net sales', 'learnpress' );
79 $total_sales = html_entity_decode( learn_press_format_price( array_sum( $chart_data['data'] ) ) );
80
81 $data = array(
82 'total_sales' => $total_sales,
83 'total_orders' => $total_orders,
84 'total_instructors' => $total_instructors,
85 'total_courses' => $total_courses,
86 'total_students' => $total_students,
87 'chart_data' => $chart_data,
88 'top_courses' => $top_courses,
89 'top_categories' => $top_categories,
90 );
91 $response->data = $data;
92 $response->status = 'success';
93 } catch ( Throwable $e ) {
94 $response->message = $e->getMessage();
95 $response->status = 'error';
96 }
97
98 return $response;
99 }
100
101 /**
102 * @param WP_REST_Request $request
103 * @return LP_REST_Response
104 */
105 public function get_order_statistics( WP_REST_Request $request ): LP_REST_Response {
106 $response = new LP_REST_Response();
107
108 try {
109 $params = $request->get_params();
110 $params = LP_Helper::sanitize_params_submitted( $params );
111 $filter = $this->get_statistics_filter( $params );
112 $lp_statistic_db = LP_Statistics_DB::getInstance();
113 $statistics = $lp_statistic_db->get_order_statics( $filter['filter_type'], $filter['time'] );
114 $completed_orders = $lp_statistic_db->get_completed_order_data( $filter['filter_type'], $filter['time'] );
115 $chart_data = $this->process_chart_data( $filter, $completed_orders );
116 $chart_data['line_label'] = __( 'Completed orders', 'learnpress' );
117 $data = array(
118 'statistics' => $statistics,
119 'chart_data' => $chart_data,
120 );
121 $response->data = $data;
122 $response->status = 'success';
123 } catch ( Throwable $e ) {
124 $response->message = $e->getMessage();
125 $response->status = 'error';
126 }
127
128 return $response;
129 }
130 public function get_courses_statistics( $request ) {
131 $response = new LP_REST_Response();
132 try {
133 $params = $request->get_params();
134 $params = LP_Helper::sanitize_params_submitted( $params );
135 $filter = $this->get_statistics_filter( $params );
136 $lp_statistic_db = LP_Statistics_DB::getInstance();
137 $published_course = $lp_statistic_db->get_published_course_data( $filter['filter_type'], $filter['time'] );
138 $courses = $lp_statistic_db->get_course_count_by_statuses( $filter['filter_type'], $filter['time'] );
139 $items = $lp_statistic_db->get_course_items_count( $filter['filter_type'], $filter['time'] );
140 $chart_data = $this->process_chart_data( $filter, $published_course );
141 $chart_data['line_label'] = __( 'Published Courses', 'learnpress' );
142 $data = array(
143 'courses' => $courses,
144 'items' => $items,
145 'chart_data' => $chart_data,
146 );
147 $response->data = $data;
148 $response->status = 'success';
149 } catch ( Throwable $e ) {
150 $response->message = $e->getMessage();
151 $response->status = 'error';
152 }
153
154 return $response;
155 }
156
157 /**
158 * @param $request
159 * @return LP_REST_Response
160 */
161 public function get_users_statistics( $request ): LP_REST_Response {
162 $response = new LP_REST_Response();
163 try {
164 $params = $request->get_params();
165 $params = LP_Helper::sanitize_params_submitted( $params );
166 $filter = $this->get_statistics_filter( $params );
167 $lp_statistic_db = LP_Statistics_DB::getInstance();
168 $user_registers = $lp_statistic_db->get_user_registered_data( $filter['filter_type'], $filter['time'] );
169 $user_course_statused = $lp_statistic_db->get_users_by_user_item_graduation_statuses( $filter['filter_type'], $filter['time'] );
170 $user_not_start_course = $lp_statistic_db->get_users_not_started_any_course( $filter['filter_type'], $filter['time'] );
171 $top_enrolled_courses = $lp_statistic_db->get_top_enrolled_courses( $filter['filter_type'], $filter['time'] );
172 $total_instructors = $lp_statistic_db->get_total_instructor_created( $filter['filter_type'], $filter['time'] );
173 $total_students = $lp_statistic_db->get_total_student_created( $filter['filter_type'], $filter['time'] );
174 $chart_data = $this->process_chart_data( $filter, $user_registers );
175 $top_enrolled_instructor = array();
176 if ( ! empty( $top_enrolled_courses ) ) {
177 foreach ( $top_enrolled_courses as $key => $course ) {
178 if ( ! array_key_exists( $course->instructor_id, $top_enrolled_instructor ) ) {
179 $top_enrolled_instructor[ $course->instructor_id ] = array(
180 'name' => $course->instructor_name,
181 'students' => (int) $course->enrolled_user,
182 );
183 } else {
184 $top_enrolled_instructor[ $course->instructor_id ]['students'] += (int) $course->enrolled_user;
185 }
186 }
187 }
188 $chart_data['line_label'] = __( 'User registed', 'learnpress' );
189 $data = array(
190 'chart_data' => $chart_data,
191 'user_course_statused' => $user_course_statused,
192 'user_not_start_course' => $user_not_start_course,
193 'top_enrolled_courses' => $top_enrolled_courses,
194 'top_enrolled_instructor' => $top_enrolled_instructor,
195 'total_instructors' => $total_instructors,
196 'total_students' => $total_students,
197 );
198 $response->data = $data;
199 $response->status = 'success';
200 } catch ( Throwable $e ) {
201 $response->message = $e->getMessage();
202 $response->status = 'error';
203 }
204
205 return $response;
206 }
207 /**
208 * Process data use for chart js
209 *
210 * @param array $filter The filter in get_statistics_filter
211 * @param array $input_data The input data ( data from DB )
212 *
213 * @return array $chart_data Data use for chart js
214 */
215 public function process_chart_data( array $filter, array $input_data ) {
216 $chart_data = array();
217 $data = array();
218 if ( $filter['filter_type'] == 'date' ) {
219 $data = $this->process_date_data( $input_data );
220 $chart_data['x_label'] = __( 'Hour', 'learnpress' );
221 } elseif ( $filter['filter_type'] == 'previous_days' ) {
222 $data = $this->process_previous_days_data( $filter['time'], $input_data );
223 $chart_data['x_label'] = __( 'Dates', 'learnpress' );
224 } elseif ( $filter['filter_type'] == 'month' ) {
225 $data = $this->process_month_data( $filter, $input_data );
226 $chart_data['x_label'] = __( 'Dates', 'learnpress' );
227 } elseif ( $filter['filter_type'] == 'previous_months' ) {
228 $data = $this->process_previous_months_data( $filter['time'], $input_data );
229 $chart_data['x_label'] = __( 'Months', 'learnpress' );
230 } elseif ( $filter['filter_type'] == 'year' ) {
231 $data = $this->process_year_data( $input_data );
232 $chart_data['x_label'] = __( 'Months', 'learnpress' );
233 } elseif ( $filter['filter_type'] == 'custom' ) {
234 $dates = $filter['time'];
235 $dates = explode( '+', $dates );
236 sort( $dates );
237 $diff = date_diff( date_create( $dates[0] ), date_create( $dates[1] ), true );
238 $y = $diff->y;
239 $m = $diff->m;
240 $d = $diff->d;
241 if ( $y < 1 ) {
242 if ( $m <= 1 ) {
243 if ( $d < 1 ) {
244 $data = $this->process_date_data( $input_data );
245 $chart_data['x_label'] = __( 'Hour', 'learnpress' );
246 } else {
247 $data = $this->process_previous_days_data( $d, $input_data, $dates[1] );
248 $chart_data['x_label'] = __( 'Dates', 'learnpress' );
249 }
250 } else {
251 $data = $this->process_previous_months_data( $m, $input_data, $dates[1] );
252 $chart_data['x_label'] = __( 'Months', 'learnpress' );
253 // $filter = $this->chart_filter_previous_months_group_by( $filter );
254 }
255 } elseif ( $y < 2 ) {
256 $months = $y * 12 + $m;
257 $data = $this->process_previous_months_data( $months, $input_data, $dates[1] );
258 $chart_data['x_label'] = __( 'Months', 'learnpress' );
259 } elseif ( $y < 5 ) {
260 // TODO
261 $data = $this->process_quarters_data( $dates, $input_data );
262 $chart_data['x_label'] = __( 'Quarters', 'learnpress' );
263 } else {
264 $data = $this->process_years_data( $y, $input_data, $dates[1] );
265 $chart_data['x_label'] = __( 'Years', 'learnpress' );
266 }
267 }
268 foreach ( $data as $row ) {
269 $chart_data['labels'][] = $row->x_data_label;
270 $chart_data['data'][] = (float) $row->x_data;
271 }
272 // $chart_data['line_label'] = __( 'Completed orders', 'learnpress' );
273
274 return $chart_data;
275 }
276 /**
277 * Gets the statistics filter.
278 *
279 * @param http request $params The parameters
280 *
281 * @return array The statistics filter. use for process data
282 */
283 public function get_statistics_filter( $params ) {
284 $filter = [];
285 $filtertype = $params['filtertype'] ?? 'today';
286 switch ( $filtertype ) {
287 case 'today':
288 $filter['filter_type'] = 'date';
289 $filter['time'] = current_time( 'Y-m-d' );
290 break;
291 case 'yesterday':
292 $filter['filter_type'] = 'date';
293 $filter['time'] = date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . '-1 days' ) );
294 break;
295 case 'last7days':
296 $filter['filter_type'] = 'previous_days';
297 $filter['time'] = 6;
298 break;
299 case 'last30days':
300 $filter['filter_type'] = 'previous_days';
301 $filter['time'] = 30;
302 break;
303 case 'thismonth':
304 $filter['filter_type'] = 'month';
305 $filter['time'] = current_time( 'Y-m-d' );
306 break;
307 case 'last12months':
308 $filter['filter_type'] = 'previous_months';
309 $filter['time'] = 11;
310 break;
311 case 'thisyear':
312 $filter['filter_type'] = 'year';
313 $filter['time'] = current_time( 'Y-m-d' );
314 break;
315 case 'custom':
316 $filter['filter_type'] = 'custom';
317 $filter['time'] = $params['date'];
318 break;
319 default:
320 break;
321 }
322 return $filter;
323 }
324 /**
325 * process data of a date ( in 24h )
326 *
327 * @param array $input_data The input data
328 *
329 * @return array ( description_of_the_return_value )
330 */
331 public function process_date_data( array $input_data ) {
332 $data = array();
333 for ( $i = 0; $i < 24;$i++ ) {
334 $row = new stdClass();
335 $row->x_data_label = $i;
336 $row->x_data = 0;
337 $data[ $i ] = $row;
338 }
339 if ( ! empty( $input_data ) ) {
340 foreach ( $input_data as $row ) {
341 $data[ $row->x_data_label ] = $row;
342 }
343 }
344 return $data;
345 }
346 /**
347 * process data of days since the last date, if dont have last date, last date is current date
348 *
349 * @param int $days The days
350 * @param array $input_data The input data
351 * @param bool $last_date The last date
352 *
353 * @return array ( description_of_the_return_value )
354 */
355 public function process_previous_days_data( int $days, array $input_data, $last_date = false ) {
356 $data = array();
357 for ( $i = $days; $i >= 0; $i-- ) {
358 $date = date( 'Y-m-d', strtotime( ( $last_date ? $last_date : '' ) . -$i . 'days' ) );
359 $row = new stdClass();
360 $row->x_data_label = $date;
361 $row->x_data = 0;
362 $data[ $date ] = $row;
363 }
364 if ( ! empty( $input_data ) ) {
365 foreach ( $input_data as $row ) {
366 $data[ $row->x_data_label ] = $row;
367 }
368 }
369 return $data;
370 }
371 /**
372 * process data of a month
373 *
374 * @param array $filter The filter
375 * @param array $input_data The input data
376 *
377 * @return array ( description_of_the_return_value )
378 */
379 public function process_month_data( array $filter, array $input_data ) {
380 $data = array();
381 $max_day = cal_days_in_month( 0, date( 'm', strtotime( $filter['time'] ) ), date( 'Y', strtotime( $filter['time'] ) ) );
382 for ( $i = 1; $i <= $max_day; $i++ ) {
383 $row = new stdClass();
384 $row->x_data_label = $i;
385 $row->x_data = 0;
386 $data[ $i ] = $row;
387 }
388 if ( ! empty( $input_data ) ) {
389 foreach ( $input_data as $row ) {
390 $data[ $row->x_data_label ] = $row;
391 }
392 }
393 return $data;
394 }
395 /**
396 * process data of months since the last date, if dont have last date, last date is current date
397 *
398 * @param int $months The months
399 * @param array $input_data The input data
400 * @param bool $last_date The last date
401 *
402 * @return array ( description_of_the_return_value )
403 */
404 public function process_previous_months_data( int $months, array $input_data, $last_date = false ) {
405 $data = array();
406 for ( $i = $months; $i >= 0; $i-- ) {
407 $date = date( 'm-Y', strtotime( ( $last_date ? $last_date : '' ) . -$i . 'months' ) );
408 $row = new stdClass();
409 $row->x_data_label = $date;
410 $row->x_data = 0;
411 $data[ $date ] = $row;
412 }
413 if ( ! empty( $input_data ) ) {
414 foreach ( $input_data as $row ) {
415 $data[ $row->x_data_label ] = $row;
416 }
417 }
418 return $data;
419 }
420 /**
421 *
422 * @param array $dates The dates
423 * @param array $input_data The input data
424 *
425 * @return array process data for date range 2-5 years
426 */
427 public function process_quarters_data( array $dates, array $input_data ) {
428 $data = array();
429 $start_time = strtotime( $dates[0] );
430 $end_time = strtotime( $dates[1] );
431 for ( $i = date( 'Y', $start_time ); $i <= date( 'Y', $end_time ); $i++ ) {
432 if ( $i == date( 'Y', $start_time ) ) {
433 $quarter = ceil( date( 'm', $start_time ) / 3 );
434 for ( $j = $quarter;$j <= 4;$j++ ) {
435 $row = new stdClass();
436 $row->x_data_label = 'q' . $j . '-' . $i;
437 $row->x_data = 0;
438 $data[] = $row;
439 }
440 } elseif ( $i == date( 'Y', $start_time ) ) {
441 $quarter = ceil( date( 'm', $end_time ) / 3 );
442 for ( $j = 1;$j <= $quarter;$j++ ) {
443 $row = new stdClass();
444 $row->x_data_label = 'q' . $j . '-' . $i;
445 $row->x_data = 0;
446 $data[] = $row;
447 }
448 } else {
449 for ( $j = 1; $j <= 4;$j++ ) {
450 $row = new stdClass();
451 $row->x_data_label = 'q' . $j . '-' . $i;
452 $row->x_data = 0;
453 $data[] = $row;
454 }
455 }
456 }
457 if ( ! empty( $input_data ) ) {
458 foreach ( $input_data as $row ) {
459 $data[ $row->x_data_label ] = $row;
460 }
461 }
462 return $data;
463 }
464 /**
465 * process data of a year
466 *
467 * @param array $input_data data from DB
468 *
469 * @return array chart data
470 */
471 public function process_year_data( array $input_data ) {
472 $data = array();
473 for ( $i = 1; $i <= 12; $i++ ) {
474 $row = new stdClass();
475 $row->x_data_label = $i;
476 $row->x_data = 0;
477 $data[ $i ] = $row;
478 }
479 if ( ! empty( $input_data ) ) {
480 foreach ( $input_data as $row ) {
481 $data[ $row->x_data_label ] = $row;
482 }
483 }
484 return $data;
485 }
486
487 /**
488 * process data of years( when date range > 5 years )
489 *
490 * @param int $years The years
491 * @param array $input_data The input data
492 * @param bool $last_date The last date
493 *
494 * @return array ( description_of_the_return_value )
495 */
496 public function process_years_data( int $years, array $input_data, $last_date = false ) {
497 $data = array();
498 for ( $i = $years; $i >= 0; $i-- ) {
499 $year = date( 'Y', strtotime( ( $last_date ? $last_date : '' ) . -$i . 'years' ) );
500 $row = new stdClass();
501 $row->x_data_label = $year;
502 $row->x_data = 0;
503 $data[ $year ] = $row;
504 }
505 if ( ! empty( $input_data ) ) {
506 foreach ( $input_data as $row ) {
507 $data[ $row->x_data_label ] = $row;
508 }
509 }
510 return $data;
511 }
512
513 public function permission_check( $request ) {
514 return apply_filters( 'learnpress/admin-statistics/permission', current_user_can( 'administrator' ) );
515 }
516 }
517