PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.2
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 / user-item / class-lp-user-item.php

class-lp-user-item.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3.2, at inc/user-item/class-lp-user-item.php

1,014 lines 22.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_User_Item
5 *
6 * @since 3.0.0
7 */
8 class LP_User_Item extends LP_Abstract_Object_Data implements ArrayAccess {
9 /**
10 * Auto increment
11 *
12 * @var int
13 */
14 public $_user_item_id = 0;
15 /**
16 * User id
17 *
18 * @var string
19 */
20 public $_user_id = 0;
21 /**
22 * Item id (course, lesson, quiz ...)
23 *
24 * @var string
25 */
26 public $_item_id = 0;
27 /**
28 * @var string
29 */
30 public $_start_time = '';
31 /**
32 * @var string
33 */
34 public $_end_time = '';
35 /**
36 * Item type (course, lesson, quiz ...)
37 *
38 * @var string
39 */
40 public $_item_type = '';
41 /**
42 * Status
43 *
44 * @var string
45 */
46 public $_status = '';
47 /**
48 * Graduation
49 *
50 * @var string
51 */
52 public $_graduation = '';
53 /**
54 * Ref id (Order, course ...)
55 *
56 * @var string
57 */
58 public $_ref_id = '';
59 /**
60 * Ref type (Order, course ...)
61 *
62 * @var string
63 */
64 public $_ref_type = '';
65 /**
66 * Parent id
67 *
68 * @var string
69 */
70 public $_parent_id = '';
71
72 /**
73 * @var string
74 * @deprecated 4.1.7.3
75 */
76 protected $_data_key = '';
77
78 /**
79 * LP_User_Item constructor.
80 *
81 * @param array $item . A record fetched from table _learnpress_user_items
82 */
83 public function __construct( $item ) {
84 if ( is_numeric( $item ) ) {
85 $item = array( 'item_id' => $item );
86 } else {
87 $item = (array) $item;
88 }
89
90 // $this->_data = self::get_empty_item();
91
92 parent::__construct( $item );
93 $this->set_default_data( $item );
94
95 }
96
97 /**
98 * Set data from passed args
99 *
100 * @param array $item
101 */
102 protected function set_default_data( $item ) {
103 $this->_changes = array();
104 $item_id = 0;
105
106 if ( ! empty( $item['user_item_id'] ) ) {
107 $this->set_data( 'user_item_id', absint( $item['user_item_id'] ) );
108 }
109
110 if ( ! empty( $item['item_id'] ) ) {
111 $item['item_id'] = absint( $item['item_id'] );
112
113 $this->set_id( $item['item_id'] );
114 $this->set_data( 'item_id', $item['item_id'] );
115 $this->set_data( 'item_type', learn_press_get_post_type( $item['item_id'] ) );
116 $item_id = $item['item_id'];
117 }
118
119 $this->set_start_time( $item['start_time'] ?? time() );
120 $this->set_end_time( $item['end_time'] ?? '' );
121 $this->set_user_id( absint( $item['user_id'] ?? get_current_user_id() ) );
122 $this->set_status( $item['status'] ?? '' );
123
124 if ( ! empty( $item['ref_id'] ) ) {
125 $item['ref_id'] = absint( $item['ref_id'] );
126
127 $this->set_ref_id( $item['ref_id'] );
128 if ( empty( $item['ref_type'] ) ) {
129 $this->set_data( 'ref_type', learn_press_get_post_type( $item['ref_id'] ) );
130 }
131 }
132
133 if ( ! empty( $item['ref_type'] ) ) {
134 $this->set_data( 'ref_type', $item['ref_type'] );
135 }
136
137 if ( ! empty( $item['parent_id'] ) ) {
138 $item['parent_id'] = absint( $item['parent_id'] );
139
140 $this->set_parent_id( $item['parent_id'] );
141 }
142
143 if ( ! empty( $item['access_level'] ) ) {
144 $this->set_data( 'access_level', $item['access_level'] );
145 }
146
147 $this->set_data( 'graduation', $item['graduation'] ?? LP_COURSE_GRADUATION_IN_PROGRESS );
148
149 // $new_data = $this->get_mysql_data();
150 // ksort( $new_data );
151 // $this->_data_key = md5( serialize( $new_data ) );
152 }
153
154 public function set_user_id( $user_id ) {
155 $this->set_data( 'user_id', $user_id );
156 }
157
158 public function get_user_id() {
159 return $this->get_data( 'user_id' );
160 }
161
162 public function set_ref_id( $ref_id ) {
163 $this->set_data( 'ref_id', $ref_id );
164 $this->set_data( 'ref_type', learn_press_get_post_type( $ref_id ) );
165 }
166
167 public function get_parent_id() {
168 return absint( $this->get_data( 'parent_id' ) );
169 }
170
171 public function set_parent_id( $parent_id ) {
172 $this->set_data( 'parent_id', $parent_id );
173 }
174
175 /**
176 * Get access level to a course.
177 *
178 * @return int
179 * @since 3.x.x
180 */
181 public function get_access_level() {
182 return absint( $this->get_data( 'access_level' ) );
183 }
184
185 /**
186 * Get type of item. Consider is post-type.
187 *
188 * @return array|mixed
189 */
190 public function get_type() {
191 return $this->get_data( 'item_type' );
192 }
193
194 /**
195 * Set start-time.
196 *
197 * @param mixed $time .
198 *
199 * @return $this
200 */
201 public function set_start_time( $time ): LP_User_Item {
202 $lp_time = new LP_Datetime( $time );
203 $this->set_data( 'start_time', $lp_time );
204
205 return $this;
206 }
207
208 public function get_time( $field, $format = '', $human_diff_time = false ) {
209 if ( ! $format ) {
210 $format = get_option( 'date_format' );
211 }
212
213 $m_time = call_user_func( array( $this, 'get_' . $field ) );
214 $time = mysql2date( 'G', call_user_func( array( $this, 'get_' . $field . '_gmt' ) ) );
215 $time_diff = time() - $time;
216
217 if ( $human_diff_time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
218 $h_time = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time ) );
219 } else {
220 $h_time = mysql2date( $format, $m_time );
221 }
222
223 return $h_time;
224 }
225
226 /**
227 * Get start-time.
228 *
229 * @param string $format
230 * @param bool $local
231 *
232 * @return string|LP_Datetime
233 */
234 public function get_start_time( $format = '', $local = false ) {
235 $date = $this->get_data( 'start_time' );
236
237 return $this->format_time( $date, $format, $local );
238 }
239
240 /**
241 * @param string $format
242 *
243 * @return array|bool|LP_Datetime|mixed|string
244 * @deprecated
245 * Addon certificate v4.0.3 is using.
246 */
247 public function get_start_time_gmt( $format = '' ) {
248 _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
249
250 // $date = $this->get_data_date( 'start_time_gmt' );
251 // if ( $format ) {
252 // return $date->is_null() ? false : ( $format = 'i18n' ? learn_press_date_i18n( $date->getTimestamp() ) : $date->format( $format ) );
253 // }
254 //
255 // return $date;
256 }
257
258 /**
259 * Set end-time for item.
260 *
261 * @param mixed $time
262 *
263 * @return $this
264 */
265 public function set_end_time( $time = '' ): LP_User_Item {
266 $this->set_data_date( 'end_time', $time );
267
268 return $this;
269 }
270
271 /**
272 * Get end-time.
273 *
274 * @param string $format
275 *
276 * @return string|LP_Datetime
277 */
278 public function get_end_time( $format = '' ) {
279 $date = $this->get_data( 'end_time' );
280
281 return $this->format_time( $date, $format );
282 }
283
284 public function get_start_time_local() {
285
286 }
287
288 /**
289 * @param string|int|LP_Datetime $date
290 * @param string $format
291 * @param bool $local
292 *
293 * @return bool|float|int|LP_Datetime|string
294 */
295 public function format_time( $date, $format = '', $local = false ) {
296 if ( ! $date ) {
297 return false;
298 }
299
300 if ( ! $date instanceof LP_Datetime ) {
301 $date = new LP_Datetime( $date );
302 }
303
304 return $format ? $date->format( $format, $local ) : $date;
305 }
306
307 /**
308 * Get end-time.
309 *
310 * @param mixed $time
311 *
312 * @deprecated 4.0.0
313 */
314 public function set_end_time_gmt( $time ) {
315 _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
316 }
317
318 /**
319 * Get end-time.
320 *
321 * @param string $format
322 *
323 * @return string|LP_Datetime
324 * @deprecated
325 */
326 public function get_end_time_gmt( $format = '' ) {
327 _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
328 }
329
330 /**
331 * Get expiration time.
332 *
333 * @param string $format
334 *
335 * @return string|LP_Datetime $time
336 * @since 3.3.0
337 * @version 3.3.1
338 */
339 public function get_expiration_time( string $format = '' ) {
340 $duration = get_post_meta( $this->get_item_id(), '_lp_duration', true );
341 $start_time = $this->get_start_time( '', false );
342
343 if ( ! absint( $duration ) || ! $start_time ) {
344 $expire = null;
345 } else {
346 $date = new LP_Datetime( $start_time->getPeriod( $duration, true ) );
347 $expire = $this->format_time( $date, $format );
348 }
349
350 return apply_filters( 'learn-press/user-item/expiration-time', $expire, $format, $this );
351 }
352
353 /**
354 * Set item-status.
355 *
356 * @param string $status .
357 *
358 * @return $this
359 */
360 public function set_status( string $status ): LP_User_Item {
361 $this->_set_data( 'status', $status );
362
363 return $this;
364 }
365
366 /**
367 * Get item status.
368 *
369 * @param string $field
370 * @param bool $force_cache Reset first cache
371 *
372 * @version 1.0.1
373 * @return string
374 * @editor tungnx
375 * @modify 4.1.3
376 */
377 public function get_status( string $field = 'status', bool $force_cache = false ): string {
378 $got_status = '';
379
380 try {
381 // User is Guest
382 if ( (int) $this->get_user_id() === 0 ) {
383 return $got_status;
384 }
385
386 $lp_user_item = LP_User_Items_DB::getInstance();
387 $filter = new LP_User_Items_Filter();
388
389 $filter->user_id = $this->get_user_id();
390 $filter->item_id = $this->get_item_id();
391 $filter->ref_id = $this->get_data( 'ref_id' );
392 $filter->parent_id = $this->get_parent_id();
393
394 $user_item = $lp_user_item->get_user_course_item( $filter, $force_cache );
395
396 if ( ! empty( $user_item ) && isset( $user_item->$field ) ) {
397 $got_status = $user_item->$field;
398 }
399
400 $this->set_data( $field, $got_status );
401 } catch ( Throwable $e ) {
402 LP_Debug::error_log( __FUNCTION__ . ':' . $e->getMessage() );
403 }
404
405 return $got_status;
406 }
407
408 public function is_exists() {
409 return ! ! $this->get_user_item_id();
410 }
411
412 /**
413 * @param string $return
414 *
415 * @return LP_User|int
416 */
417 public function get_user( $return = '' ) {
418 return $this->get_data( 'user_id', 0 );
419 }
420
421 /**
422 * Get course.
423 *
424 * @param string $return
425 *
426 * @return bool|LP_Course|int|mixed
427 */
428 public function get_course( string $return = '' ) {
429 $course_id = $this->get_data( 'ref_id', 0 );
430 if ( $return == '' ) {
431 return $course_id ? learn_press_get_course( $course_id ) : false;
432 }
433
434 return $course_id;
435 }
436
437 /**
438 * @return int
439 */
440 public function get_user_item_id() {
441 return intval( $this->get_data( 'user_item_id' ) );
442 }
443
444 /**
445 * Change the primary key user_item_id of user-items.
446 * Only use zero value to force creating new item.
447 *
448 * @param int $user_item_id
449 */
450 public function set_user_item_id( $user_item_id ) {
451 $this->_set_data( 'user_item_id', absint( $user_item_id ) );
452 }
453
454 public function get_item_id() {
455 return $this->get_data( 'item_id' );
456 }
457
458 public function get_parent() {
459 $user = learn_press_get_user( $this->get_user_id() );
460 $ref_id = $this->get_data( 'ref_id' );
461
462 if ( get_post_type( $ref_id ) === LP_COURSE_CPT ) {
463 return $user->get_course_data( $ref_id );
464 }
465
466 return false;
467 }
468
469 public function get_result( $prop = 'result' ) {
470 $result = array(
471 'result' => $this->is_completed() ? 100 : 0,
472 'grade' => $this->is_completed() ? 'passed' : 'failed',
473 );
474
475 return $prop && array_key_exists( $prop, $result ) ? $result[ $prop ] : $result;
476 }
477
478 public function read_meta() {
479 global $wpdb;
480 $query = $wpdb->prepare(
481 "
482 SELECT *
483 FROM {$wpdb->learnpress_user_itemmeta}
484 WHERE learnpress_user_item_id = %d
485 ",
486 $this->get_user_item_id()
487 );
488
489 $results = $wpdb->get_results( $query );
490 if ( $results ) {
491 foreach ( $results as $result ) {
492 $result->meta_value = LP_Helper::maybe_unserialize( $result->meta_value );
493 $this->_meta_data[] = $result;
494 }
495 }
496
497 }
498
499 /**
500 * Get structure of an user item.
501 *
502 * @return array
503 * @since 3.1.0
504 */
505 public static function get_empty_item() {
506 return array(
507 'user_item_id' => 0,
508 'user_id' => 0,
509 'item_id' => 0,
510 'start_time' => '',
511 'end_time' => '',
512 'item_type' => '',
513 'status' => '',
514 'graduation' => '',
515 'access_level' => 50,
516 'ref_id' => '',
517 'ref_type' => '',
518 'parent_id' => 0,
519 );
520 }
521
522 /**
523 * Get user-item meta data.
524 * Check if meta data does not exist then return FALSE.
525 *
526 * @updated 3.1.0
527 *
528 * @param string $key
529 * @param bool $single
530 *
531 * @return bool|mixed
532 */
533 public function get_meta( $key, $single = true ) {
534 if ( ! metadata_exists( 'learnpress_user_item', $this->get_user_item_id(), $key ) ) {
535 return false;
536 }
537
538 return learn_press_get_user_item_meta( $this->get_user_item_id(), $key, $single );
539 }
540
541 /**
542 * Update meta data
543 *
544 * @updated 3.1.0
545 *
546 * @param string $key
547 * @param string $value
548 * @param string $prev_value
549 *
550 * @editor tungnx add bk method
551 */
552 public function update_meta_bk( $key = '', $value = '', $prev_value = '' ) {
553 if ( func_num_args() === 0 ) {
554 if ( $this->_meta_data ) {
555 foreach ( $this->_meta_data as $meta_data ) {
556 if ( $meta_data->meta_value ) {
557 learn_press_update_user_item_meta(
558 $this->get_user_item_id(),
559 $meta_data->meta_key,
560 $meta_data->meta_value
561 );
562 } else {
563 learn_press_delete_user_item_meta( $this->get_user_item_id(), $meta_data->meta_key );
564 }
565 }
566 }
567 } else {
568 if ( is_array( $key ) ) {
569 foreach ( $key as $k => $v ) {
570 if ( $v === false ) {
571 learn_press_delete_user_item_meta( $this->get_user_item_id(), $k );
572 } else {
573 learn_press_update_user_item_meta( $this->get_user_item_id(), $k, $v );
574 }
575 }
576 } else {
577 if ( $value === false ) {
578 learn_press_delete_user_item_meta( $this->get_user_item_id(), $key );
579 } else {
580 learn_press_update_user_item_meta( $this->get_user_item_id(), $key, $value, $prev_value );
581 }
582 }
583 }
584 }
585
586 /**
587 * Update meta data
588 *
589 * @param string $key .
590 * @param string $value .
591 * @param string $prev_value .
592 *
593 * @return int|bool
594 * @editor tungnx
595 */
596 public function update_meta( $key = '', $value = '', $prev_value = '' ) {
597 $result = false;
598
599 if ( is_array( $key ) ) {
600 $result = $this->update_meta_multiple_keys( $key );
601 } else {
602 if ( false === $value ) {
603 $result = learn_press_delete_user_item_meta( $this->get_user_item_id(), $key );
604 } else {
605 $result = learn_press_update_user_item_meta( $this->get_user_item_id(), $key, $value, $prev_value );
606 }
607 }
608
609 return $result;
610 }
611
612 /**
613 * Update metadata with array keys values
614 *
615 * @param array $meta_datas .
616 *
617 * @return bool|int
618 */
619 public function update_meta_multiple_keys( $meta_datas = array() ) {
620 $result = false;
621
622 foreach ( $meta_datas as $k => $v ) {
623 if ( false === $v ) {
624 $result = learn_press_delete_user_item_meta( $this->get_user_item_id(), $k );
625 } else {
626 $result = learn_press_update_user_item_meta( $this->get_user_item_id(), $k, $v );
627 }
628 }
629
630 return $result;
631 }
632
633 public function get_mysql_data() {
634 /**
635 * @var LP_Datetime $v
636 */
637 $columns = array();
638
639 foreach ( $this->get_data() as $k => $v ) {
640 switch ( $k ) {
641 case 'start_time':
642 case 'end_time':
643 //$v = is_a( $v, 'LP_Datetime' ) ? $v->toSql( false ) : $v;
644 break;
645 }
646 $columns[ $k ] = $v;
647 }
648
649 return $columns;
650 }
651
652 /**
653 * @param $data
654 *
655 * @return LP_User_Item|bool
656 */
657 public static function get_item_object( $data ) {
658 $item_id = 0;
659
660 if ( is_array( $data ) && isset( $data['item_id'] ) ) {
661 $item_id = $data['item_id'];
662 } elseif ( is_object( $data ) && isset( $data->item_id ) ) {
663 $item_id = $data->item_id;
664 } elseif ( is_numeric( $data ) ) {
665 $item_id = absint( $data );
666 } elseif ( $data instanceof LP_User_Item ) {
667 $item_id = $data->get_id();
668 }
669
670 $item = false;
671 $item_type = learn_press_get_post_type( $item_id );
672
673 switch ( $item_type ) {
674 case LP_LESSON_CPT:
675 $item = new LP_User_Item( $data );
676 break;
677 case LP_QUIZ_CPT:
678 $item = new LP_User_Item_Quiz( $data );
679 break;
680 default:
681 break;
682 }
683
684 return apply_filters( 'learn-press/user-item-object', $item, $data, $item_type );
685 }
686
687 /**
688 * Set graduation
689 *
690 * @param string $graduation .
691 *
692 * @return $this
693 */
694 public function set_graduation( string $graduation ): LP_User_Item {
695 $this->_set_data( 'graduation', $graduation );
696
697 return $this;
698 }
699
700 /**
701 * Get graduation
702 *
703 * @param string $context
704 *
705 * @return string
706 */
707 public function get_graduation( string $context = '' ): string {
708 $grade = $this->get_data( 'graduation', '' );
709 if ( ! $grade ) {
710 return '';
711 }
712 return $context == 'display' ? learn_press_course_grade_html( $grade, false ) : $grade;
713 }
714
715 /**
716 * Update data from memory to database.
717 *
718 * @updated 3.1.0
719 *
720 * @return bool|mixed
721 */
722 public function update( $force = false, $wp_error = false ) {
723 $data = $this->get_mysql_data();
724 $data = apply_filters( 'learn-press/update-user-item-data', $data, $this->get_user_item_id() );
725 $where = array();
726
727 if ( $this->get_user_item_id() ) {
728 $where = array( 'user_item_id' => $this->get_user_item_id() );
729 }
730
731 $rs = learn_press_update_user_item_field( $data, $where );
732
733 // Clear cache first status
734 $this->get_status( 'status', true );
735
736 if ( $rs ) {
737 foreach ( (array) $rs as $k => $v ) {
738 $this->_set_data( $k, $v );
739 }
740 $this->_changes = array();
741 }
742
743 return $rs;
744 }
745
746 /**
747 * @editor tungnx
748 * @reason commnet - not use
749 * @modify 4.1.2
750 */
751 /*
752 public function is_course_item() {
753 return learn_press_is_support_course_item_type( $this->get_data( 'item_type' ) );
754 }*/
755
756 public function get_status_label( $status = '' ) {
757 $statuses = array(
758 LP_COURSE_ENROLLED => esc_html__( 'Enrolled', 'learnpress' ),
759 LP_COURSE_PURCHASED => esc_html__( 'Purchased', 'learnpress' ),
760 LP_ITEM_COMPLETED => esc_html__( 'Completed', 'learnpress' ),
761 LP_ITEM_STARTED => esc_html__( 'Started', 'learnpress' ),
762 LP_COURSE_FINISHED => esc_html__( 'Finished', 'learnpress' ),
763 );
764
765 if ( ! $status ) {
766 $status = $this->get_status();
767 }
768
769 return ! empty( $statuses[ $status ] ) ? $statuses[ $status ] : esc_html__( 'Not Enrolled', 'learnpress' );
770 }
771
772 /**
773 * Get time from user started to ended.
774 *
775 * @param string $context
776 *
777 * @return bool|float|int
778 */
779 public function get_time_interval( $context = '' ) {
780 $start = $this->get_start_time();
781 $end = $this->get_end_time();
782
783 if ( ! $start instanceof LP_Datetime || ! $end instanceof LP_Datetime ) {
784 return false;
785 }
786
787 if ( $start->is_null() || $end->is_null() ) {
788 return false;
789 }
790 $interval = $end->getTimestamp() - $start->getTimestamp();
791
792 return $interval;
793 }
794
795 /**
796 * Return number of seconds has exceeded from the expiration time to now.
797 * If less than or equals to 0 that means the time is exceeded.
798 * Otherwise, the time is not exceeded.
799 *
800 * @return float|int
801 * @since 3.3.0
802 */
803 public function get_exceeded() {
804 $time = new LP_Datetime();
805 $current = $time->getTimestamp( false );
806 $exceeded = $this->get_expiration_time();
807
808 return false !== $exceeded ? $exceeded->getTimestamp() - $current : false;
809 }
810
811 /**
812 * Check if user was finished before the expiration time is exceeded.
813 * If the expiration-time is NULL that mean the course is not set duration.
814 *
815 * @return bool|float|int
816 * @since 3.3.0
817 * Todo: check remove function
818 */
819 public function is_exceeded() {
820 $expiration = $this->get_expiration_time();
821 $end = $this->get_end_time();
822
823 if ( ! $expiration ) {
824 return false;
825 }
826
827 // If course is not finished then consider end time is current time
828 if ( ! $end || 0 >= $end->getTimestamp() ) {
829 $end = new LP_Datetime();
830 $end = $end->getTimestamp( false );
831 } else {
832 $end = $end->getTimestamp();
833 }
834
835 return $expiration->getTimestamp() - $end;
836 }
837
838 /**
839 * Get time remaining for user item.
840 *
841 * @param string $return - Optional. What kind of data to return.
842 *
843 * @return LP_Duration
844 * @since 3.3.0
845 */
846 public function get_time_remaining( $return = 'object' ) {
847 $is_exceeded = $this->is_exceeded();
848 $time = false;
849
850 if ( false !== $is_exceeded ) {
851 $time = 0 < $is_exceeded ? absint( $is_exceeded ) : 0;
852 }
853
854 // return apply_filters( 'learn-press/quiz/time-remaining', $remaining, $this->get_item_id(), $this->get_course_id() );
855 return apply_filters(
856 'learn-press/user-item-time-remaining',
857 $return === 'object' ? new LP_Duration( $time ) : $time,
858 $this->get_item_id(),
859 $this->get_parent_id(),
860 $this->get_user_id()
861 );
862 }
863
864 /**
865 * Return true of item is completed/finished
866 *
867 * @param string $status
868 *
869 * @return bool
870 */
871 public function is_completed( $status = 'completed' ) {
872 return $this->get_status() === $status;
873 }
874
875 /**
876 * Complete item
877 *
878 * @editor tungnx
879 * @modify 4.1.4.1 - should review to improve
880 * @version 4.0.1
881 */
882 public function complete( $status = 'completed' ) {
883 try {
884 $this->set_end_time( time() );
885 $this->set_status( $status );
886 $this->update();
887 } catch ( Throwable $e ) {
888 error_log( __FUNCTION__ . ':' . $e->getMessage() );
889 return false;
890 }
891
892 return true;
893 }
894
895 /**
896 * Get post type of item.
897 *
898 * @return string
899 */
900 public function get_post_type() {
901 return learn_press_get_post_type( $this->get_item_id() );
902 }
903
904 /**
905 * @return bool
906 */
907 public function is_passed() {
908 return 'passed' === $this->get_graduation();
909 }
910
911 /**
912 * @param int $decimal
913 *
914 * @return mixed|null
915 */
916 public function get_percent_result( $decimal = 1 ) {
917 return apply_filters(
918 'learn-press/user/item-percent-result',
919 sprintf( '%s%', round( $this->get_result( 'result' ), $decimal ) ),
920 $this->get_user_id(),
921 $this->get_item_id()
922 );
923 }
924
925 /**
926 * Calculate expiration time from the start time and duration.
927 *
928 * @param int|string|LP_Datetime $duration
929 *
930 * @return LP_Datetime
931 * @since 3.3.0
932 * @deprecated 4.1.7
933 */
934 /*public function set_duration( $duration ) {
935 if ( $duration instanceof LP_Datetime ) {
936 $period = $duration->toSql();
937 } else {
938 $period = $duration;
939 }
940
941 return $this->get_expiration_time();
942 }*/
943
944 /**
945 * @deprecated 4.1.7
946 */
947 /*public function is_change() {
948
949 $new_data = $this->get_mysql_data();
950 ksort( $new_data );
951
952 return $this->_data_key !== md5( serialize( $new_data ) );
953 }*/
954
955 /**
956 * @deprecated 4.1.7.3
957 */
958 protected function _set_data_date( $key, $value, $extra = false ) {
959 _deprecated_function( __FUNCTION__, '4.1.7.3', 'set_data_date' );
960 if ( $value instanceof LP_Datetime ) {
961 $value = $value->getTimestamp();
962 } else {
963 $value = is_numeric( $value ) ? $value : strtotime( $value );
964 }
965 $offset = (int) ( get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS );
966
967 parent::_set_data_date( $key, $value + $offset, $extra );
968 }
969
970 /**
971 * @param $name
972 * @param $arguments
973 *
974 * @return mixed
975 * @deprecated 4.1.7
976 */
977 /*public function __call( $name, $arguments ) {
978 if ( ! method_exists( $this, $name ) ) {
979 $course = $this->get_course();
980
981 if ( $course ) {
982 $item = $course->get_item( $this->get_item_id() );
983
984 if ( is_callable( array( $item, $name ) ) ) {
985 return call_user_func_array( array( $item, $name ), $arguments );
986 }
987 }
988 }
989
990 return false;
991 }*/
992
993 public function offsetSet( $offset, $value ) {
994 // TODO: Implement offsetSet() method.
995 }
996
997 public function offsetGet( $offset ) {
998 if ( is_callable( array( $this, 'get_' . $offset ) ) ) {
999 return call_user_func( array( $this, 'get_' . $offset ) );
1000 }
1001
1002 return false;
1003 }
1004
1005 public function offsetUnset( $offset ) {
1006 // TODO: Implement offsetUnset() method.
1007 }
1008
1009 public function offsetExists( $offset ) {
1010 // TODO: Implement offsetExists() method.
1011 }
1012 }
1013
1014