| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Course. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 4.0.1 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit(); |
| 11 |
|
| 12 |
if ( ! class_exists( 'LP_Course' ) ) { |
| 13 |
|
| 14 |
/** |
| 15 |
* Class LP_Course |
| 16 |
*/ |
| 17 |
class LP_Course extends LP_Abstract_Course { |
| 18 |
protected $key_info_extra_fast_query = '_lp_info_extra_fast_query'; |
| 19 |
|
| 20 |
/** |
| 21 |
* LP_Course constructor. |
| 22 |
* |
| 23 |
* @param mixed $course |
| 24 |
*/ |
| 25 |
public function __construct( $course ) { |
| 26 |
parent::__construct( $course ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Debug log. |
| 31 |
* |
| 32 |
* @param $data |
| 33 |
* |
| 34 |
* @return array |
| 35 |
* @editor tungnx |
| 36 |
* @modify 4.1.5 - comment - not use |
| 37 |
*/ |
| 38 |
/*public static function log( $data ) { |
| 39 |
return $data; |
| 40 |
}*/ |
| 41 |
|
| 42 |
/** |
| 43 |
* Get default course meta. |
| 44 |
* |
| 45 |
* @return mixed |
| 46 |
* @since 3.0.0 |
| 47 |
*/ |
| 48 |
public static function get_default_meta() { |
| 49 |
$meta = array( |
| 50 |
'duration' => '10 weeks', |
| 51 |
'max_students' => 1000, |
| 52 |
'students' => 0, |
| 53 |
'retake_count' => 0, |
| 54 |
'featured' => 'no', |
| 55 |
//'block_lesson_content' => 'no', |
| 56 |
'external_link_buy_course' => '', |
| 57 |
'course_result' => 'evaluate_lesson', |
| 58 |
'passing_condition' => 80, |
| 59 |
'price' => '', |
| 60 |
'sale_price' => '', |
| 61 |
'sale_start' => '', |
| 62 |
'sale_end' => '', |
| 63 |
'required_enroll' => 'yes', |
| 64 |
'course_author' => learn_press_get_current_user_id(), |
| 65 |
); |
| 66 |
|
| 67 |
return apply_filters( 'learn-press/course/default-meta', $meta ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get LP Course. |
| 72 |
* |
| 73 |
* @param int $course_id |
| 74 |
* |
| 75 |
* @return mixed|bool|LP_Course |
| 76 |
* @Todo - Tungnx review to rewrite this method. |
| 77 |
*/ |
| 78 |
public static function get_course( int $course_id = 0 ) { |
| 79 |
if ( isset( LP_Global::$courses[ $course_id ] ) ) { |
| 80 |
return LP_Global::$courses[ $course_id ]; |
| 81 |
} |
| 82 |
|
| 83 |
$the_course = self::get_course_object( $course_id ); |
| 84 |
|
| 85 |
if ( ! $the_course ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
$key_args = wp_parse_args( |
| 90 |
array( |
| 91 |
'id' => $the_course->ID, |
| 92 |
'type' => $the_course->post_type, |
| 93 |
) |
| 94 |
); |
| 95 |
|
| 96 |
$key = LP_Helper::array_to_md5( $key_args ); |
| 97 |
|
| 98 |
if ( empty( LP_Global::$courses[ $key ] ) ) { |
| 99 |
$class_name = self::get_course_class( $the_course ); |
| 100 |
if ( is_string( $class_name ) && class_exists( $class_name ) ) { |
| 101 |
$course = new $class_name( $the_course->ID ); |
| 102 |
} elseif ( $class_name instanceof LP_Abstract_Course ) { |
| 103 |
$course = $class_name; |
| 104 |
} else { |
| 105 |
$course = new self( $the_course->ID ); |
| 106 |
} |
| 107 |
LP_Global::$courses[ $key ] = $course; |
| 108 |
} else { |
| 109 |
$course = LP_Global::$courses[ $key ]; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Force to reload course data into cache if it is not |
| 114 |
* loaded or has been deleted for some reasons. |
| 115 |
*/ |
| 116 |
$course->load(); |
| 117 |
|
| 118 |
return $course; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param string $course_type |
| 123 |
* |
| 124 |
* @return string|false |
| 125 |
*/ |
| 126 |
private static function get_class_name_from_course_type( $course_type ) { |
| 127 |
return LP_COURSE_CPT === $course_type ? __CLASS__ : 'LP_Course_' . implode( |
| 128 |
'_', |
| 129 |
array_map( 'ucfirst', explode( '-', $course_type ) ) |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get the course class name |
| 135 |
* |
| 136 |
* @param WP_Post $the_course |
| 137 |
* @param array $args (default: array()) |
| 138 |
* |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
private static function get_course_class( $the_course, $args = array() ) { |
| 142 |
$course_id = absint( $the_course->ID ); |
| 143 |
$type = $the_course->post_type; |
| 144 |
|
| 145 |
$class_name = self::get_class_name_from_course_type( $type ); |
| 146 |
|
| 147 |
// Filter class name so that the class can be overridden if extended. |
| 148 |
return apply_filters( 'learn-press/course/object-class', $class_name, $type, $course_id ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the course object |
| 153 |
* |
| 154 |
* @param mixed $the_course |
| 155 |
* |
| 156 |
* @return WP_Post|bool false on failure |
| 157 |
* @uses WP_Post |
| 158 |
*/ |
| 159 |
private static function get_course_object( $the_course ) { |
| 160 |
$the_course_passed = $the_course; |
| 161 |
if ( false === $the_course ) { |
| 162 |
$the_course = get_post_type() === LP_COURSE_CPT ? $GLOBALS['post'] : false; |
| 163 |
} elseif ( is_numeric( $the_course ) ) { |
| 164 |
$the_course = get_post( $the_course ); |
| 165 |
} elseif ( $the_course instanceof LP_Abstract_Course ) { |
| 166 |
$the_course = get_post( $the_course->get_id() ); |
| 167 |
} elseif ( ! ( $the_course instanceof WP_Post ) ) { |
| 168 |
$the_course = false; |
| 169 |
} |
| 170 |
|
| 171 |
if ( $the_course && $the_course->post_type !== LP_COURSE_CPT ) { |
| 172 |
$the_course = false; |
| 173 |
} |
| 174 |
|
| 175 |
return apply_filters( 'learn-press/course/post-object', $the_course, $the_course_passed ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Check time remaining course when enable duration expire |
| 180 |
* Value: -1 is no limit (default) |
| 181 |
* Value: 0 is block |
| 182 |
* Administrator || (is instructor && is author course) will be not block. |
| 183 |
* |
| 184 |
* @return int second |
| 185 |
* @since 4.0.0 |
| 186 |
* @author tungnx |
| 187 |
* @version 1.0.1 |
| 188 |
*/ |
| 189 |
public function timestamp_remaining_duration(): int { |
| 190 |
$timestamp_remaining = - 1; |
| 191 |
$user = learn_press_get_user( get_current_user_id() ); |
| 192 |
|
| 193 |
if ( current_user_can( 'administrator' ) || |
| 194 |
( current_user_can( LP_TEACHER_ROLE ) && $this->get_author()->get_id() === $user->get_id() ) ) { |
| 195 |
return $timestamp_remaining; |
| 196 |
} |
| 197 |
|
| 198 |
if ( 0 === absint( $this->get_data( 'duration' ) ) ) { |
| 199 |
return $timestamp_remaining; |
| 200 |
} |
| 201 |
|
| 202 |
if ( 'yes' !== $this->get_data( 'block_course_duration_expire' ) ) { |
| 203 |
return $timestamp_remaining; |
| 204 |
} |
| 205 |
|
| 206 |
if ( $user instanceof LP_User_Guest ) { |
| 207 |
return $timestamp_remaining; |
| 208 |
} |
| 209 |
|
| 210 |
$course_item_data = $user->get_course_data( $this->get_id() ); |
| 211 |
|
| 212 |
if ( ! $course_item_data ) { |
| 213 |
return $timestamp_remaining; |
| 214 |
} |
| 215 |
|
| 216 |
$course_start_time = $course_item_data->get_start_time()->get_raw_date(); |
| 217 |
$duration = $this->get_data( 'duration' ); |
| 218 |
$timestamp_expire = strtotime( $course_start_time . ' +' . $duration ); |
| 219 |
$timestamp_current = time(); |
| 220 |
$timestamp_remaining = $timestamp_expire - $timestamp_current; |
| 221 |
|
| 222 |
if ( $timestamp_remaining < 0 ) { |
| 223 |
$timestamp_remaining = 0; |
| 224 |
} |
| 225 |
|
| 226 |
return apply_filters( 'learnpress/course/block_duration_expire/timestamp_remaining', $timestamp_remaining ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Get option enable block course when finish course |
| 231 |
* |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
public function enable_block_item_when_finish(): bool { |
| 235 |
return 'yes' === $this->get_data( 'block_course_finished' ); |
| 236 |
} |
| 237 |
|
| 238 |
public function allow_repurchase() : bool { |
| 239 |
return 'yes' === $this->get_data( 'allow_repurchase' ); |
| 240 |
} |
| 241 |
|
| 242 |
public function allow_repurchase_course_option() : string { |
| 243 |
return $this->get_data( 'allow_repurchase_course_option', 'reset' ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Get first item of course |
| 248 |
* |
| 249 |
* @author tungnx |
| 250 |
* @since 4.0.0 |
| 251 |
* @modify 4.1.3 |
| 252 |
* @version 1.0.1 |
| 253 |
* @return int |
| 254 |
*/ |
| 255 |
public function get_first_item_id(): int { |
| 256 |
$course_id = $this->get_id(); |
| 257 |
|
| 258 |
try { |
| 259 |
// Get cache |
| 260 |
$lp_course_cache = LP_Course_Cache::instance(); |
| 261 |
$key_cache = "$course_id/first_item_id"; |
| 262 |
$first_item_id = $lp_course_cache->get_cache( $key_cache ); |
| 263 |
|
| 264 |
if ( ! $first_item_id ) { |
| 265 |
$extra_info = $this->get_info_extra_for_fast_query(); |
| 266 |
|
| 267 |
if ( ! $extra_info->first_item_id ) { |
| 268 |
$first_item_id = LP_Course_DB::getInstance()->get_first_item_id( $course_id ); |
| 269 |
$extra_info->first_item_id = $first_item_id; |
| 270 |
|
| 271 |
// Save post meta |
| 272 |
$this->set_info_extra_for_fast_query( $extra_info ); |
| 273 |
} else { |
| 274 |
$first_item_id = $extra_info->first_item_id; |
| 275 |
} |
| 276 |
|
| 277 |
$lp_course_cache->set_cache( $key_cache, $first_item_id ); |
| 278 |
} |
| 279 |
} catch ( Throwable $e ) { |
| 280 |
$first_item_id = 0; |
| 281 |
} |
| 282 |
|
| 283 |
return $first_item_id; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Get redirect url after enroll course |
| 288 |
* |
| 289 |
* @author tungnx |
| 290 |
* @version 1.0.0 |
| 291 |
* @since 4.0.0 |
| 292 |
* @return false|string|WP_Error |
| 293 |
*/ |
| 294 |
public function get_redirect_url_after_enroll() { |
| 295 |
$first_item_id = $this->get_first_item_id(); |
| 296 |
$redirect = $first_item_id ? $this->get_item_link( $first_item_id ) : get_the_permalink( $this->get_id() ); |
| 297 |
|
| 298 |
return apply_filters( 'learnpress/rest-api/enroll-course/redirect', $redirect ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Get info extra on post meta to query fast |
| 303 |
* |
| 304 |
* @since 4.1.3 |
| 305 |
* @author tungnx |
| 306 |
* @version 1.0.0 |
| 307 |
* @return LP_Course_Extra_Info_Fast_Query_Model |
| 308 |
*/ |
| 309 |
public function get_info_extra_for_fast_query(): LP_Course_Extra_Info_Fast_Query_Model { |
| 310 |
$extra_info = new LP_Course_Extra_Info_Fast_Query_Model(); |
| 311 |
|
| 312 |
try { |
| 313 |
$extra_info_str = get_post_meta( $this->get_id(), $this->key_info_extra_fast_query, true ); |
| 314 |
|
| 315 |
if ( $extra_info_str ) { |
| 316 |
$extra_info_stdclass = json_decode( $extra_info_str ); |
| 317 |
|
| 318 |
if ( JSON_ERROR_NONE !== json_last_error() ) { |
| 319 |
throw new Exception( 'Error json decode on ' . __METHOD__ ); |
| 320 |
} |
| 321 |
|
| 322 |
$extra_info = $extra_info->map_stdclass( $extra_info_stdclass ); |
| 323 |
} |
| 324 |
} catch ( Throwable $e ) { |
| 325 |
error_log( $e->getMessage() ); |
| 326 |
} |
| 327 |
|
| 328 |
return $extra_info; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Set extra info for query fast on post meta |
| 333 |
* |
| 334 |
* @since 4.1.3 |
| 335 |
* @author tungnx |
| 336 |
* @version 1.0.0 |
| 337 |
* @param LP_Course_Extra_Info_Fast_Query_Model $data_object |
| 338 |
*/ |
| 339 |
public function set_info_extra_for_fast_query( LP_Course_Extra_Info_Fast_Query_Model $data_object ) { |
| 340 |
try { |
| 341 |
$extra_info_json = json_encode( $data_object, JSON_UNESCAPED_UNICODE ); |
| 342 |
|
| 343 |
if ( JSON_ERROR_NONE !== json_last_error() ) { |
| 344 |
throw new Exception( 'Error encode on ' . __METHOD__ ); |
| 345 |
} |
| 346 |
|
| 347 |
update_post_meta( $this->get_id(), $this->key_info_extra_fast_query, $extra_info_json ); |
| 348 |
} catch ( Throwable $e ) { |
| 349 |
error_log( $e->getMessage() ); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Get info total items of Course |
| 355 |
* |
| 356 |
* @param string $type |
| 357 |
* @param bool $include_preview |
| 358 |
* @author tungnx |
| 359 |
* @since 4.1.4.1 |
| 360 |
* @version 1.0.0 |
| 361 |
* @return int |
| 362 |
*/ |
| 363 |
public function count_items( string $type = '', bool $include_preview = true ): int { |
| 364 |
$course_id = $this->get_id(); |
| 365 |
|
| 366 |
// Get cache |
| 367 |
$lp_course_cache = LP_Course_Cache::instance(); |
| 368 |
$key_cache = "$course_id/total_items"; |
| 369 |
$total_items = $lp_course_cache->get_cache( $key_cache ); |
| 370 |
$count_items = 0; |
| 371 |
|
| 372 |
if ( ! $total_items ) { |
| 373 |
$extra_info = $this->get_info_extra_for_fast_query(); |
| 374 |
|
| 375 |
if ( ! $extra_info->total_items ) { |
| 376 |
$total_items = LP_Course_DB::getInstance()->get_total_items( $course_id ); |
| 377 |
$extra_info->total_items = $total_items; |
| 378 |
|
| 379 |
// Save post meta |
| 380 |
$this->set_info_extra_for_fast_query( $extra_info ); |
| 381 |
} else { |
| 382 |
$total_items = $extra_info->total_items; |
| 383 |
} |
| 384 |
|
| 385 |
$lp_course_cache->set_cache( $key_cache, $total_items ); |
| 386 |
} |
| 387 |
|
| 388 |
if ( ! empty( $total_items ) ) { |
| 389 |
if ( ! empty( $type ) ) { |
| 390 |
if ( isset( $total_items->{$type} ) ) { |
| 391 |
$count_items = $total_items->{$type}; |
| 392 |
} |
| 393 |
} else { |
| 394 |
$count_items = $total_items->count_items; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
return apply_filters( 'learn-press/course/count-items', intval( $count_items ), $course_id ); |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Delete relate data when delete course |
| 403 |
* |
| 404 |
* @since 4.1.4.1 |
| 405 |
* @author tungnx |
| 406 |
* @version 1.0.0 |
| 407 |
*/ |
| 408 |
public function delete_relate_data_when_delete_course() { |
| 409 |
$lp_section_db = LP_Section_DB::getInstance(); |
| 410 |
$lp_user_items_db = LP_User_Items_DB::getInstance(); |
| 411 |
|
| 412 |
try { |
| 413 |
// Check valid user |
| 414 |
if ( ! current_user_can( 'administrator' ) && $this->get_author( 'id' ) !== get_current_user_id() ) { |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
$section_ids = $lp_section_db->get_section_ids_by_course( $this->get_id() ); |
| 419 |
|
| 420 |
$filter = new LP_Section_Filter(); |
| 421 |
$filter->section_ids = $section_ids; |
| 422 |
$filter->author_id_course = $this->get_author( 'id' ); |
| 423 |
|
| 424 |
// Delete section |
| 425 |
$lp_section_db->delete_section( $filter ); |
| 426 |
// Delete section items |
| 427 |
$lp_section_db->delete_section_items( $filter ); |
| 428 |
|
| 429 |
$filter_user_items = new LP_User_Items_Filter(); |
| 430 |
$filter_user_items->item_id = $this->get_id(); |
| 431 |
$user_course_ids = $lp_user_items_db->get_user_items_by_course( $filter_user_items ); |
| 432 |
|
| 433 |
$this->delete_user_item_and_result( $user_course_ids ); |
| 434 |
} catch ( Throwable $e ) { |
| 435 |
error_log( __FUNCTION__ . ':' . $e->getMessage() ); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Delete user_items, user_itemmeta, user_item_results |
| 441 |
* WHERE IN user_item_ids |
| 442 |
* |
| 443 |
* @param array $user_course_ids |
| 444 |
*/ |
| 445 |
public function delete_user_item_and_result( array $user_course_ids ) { |
| 446 |
$lp_user_items_db = LP_User_Items_DB::getInstance(); |
| 447 |
$lp_user_item_results = LP_User_Items_Result_DB::instance(); |
| 448 |
|
| 449 |
try { |
| 450 |
if ( empty( $user_course_ids ) ) { |
| 451 |
return; |
| 452 |
} |
| 453 |
|
| 454 |
// Get user_item_ids has parent in $user_course_ids |
| 455 |
$filter = new LP_User_Items_Filter(); |
| 456 |
$filter->user_item_ids = $user_course_ids; |
| 457 |
$user_item_ids = $lp_user_items_db->get_item_ids_of_user_course( $filter ); |
| 458 |
|
| 459 |
$user_item_ids_concat = array_merge( $user_course_ids, $user_item_ids ); |
| 460 |
|
| 461 |
// Delete on tb lp_user_items |
| 462 |
$filter_delete = new LP_User_Items_Filter(); |
| 463 |
$filter_delete->user_item_ids = $user_item_ids_concat; |
| 464 |
$lp_user_items_db->remove_user_item_ids( $filter_delete ); |
| 465 |
|
| 466 |
// Delete user_itemmeta |
| 467 |
$lp_user_items_db->remove_user_itemmeta( $filter_delete ); |
| 468 |
|
| 469 |
// Delete user_item_results |
| 470 |
$lp_user_item_results->remove_user_item_results( $filter_delete ); |
| 471 |
} catch ( Throwable $e ) { |
| 472 |
error_log( __FUNCTION__ . ':' . $e->getMessage() ); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Get list course |
| 478 |
* Order By: price, title, rating, date ... |
| 479 |
* Order: ASC, DES |
| 480 |
* |
| 481 |
* @param LP_Course_Filter $filter |
| 482 |
* @param int $total_rows |
| 483 |
* |
| 484 |
* @return array|null|string|int |
| 485 |
* @author tungnx |
| 486 |
* @version 1.0.0 |
| 487 |
* @sicne 4.1.5 |
| 488 |
*/ |
| 489 |
public static function get_courses( LP_Course_Filter $filter, int &$total_rows = 0 ) { |
| 490 |
$lp_course_db = LP_Course_DB::getInstance(); |
| 491 |
|
| 492 |
try { |
| 493 |
$key_cache = md5( json_encode( $filter ) ); |
| 494 |
$key_cache_total_rows = md5( json_encode( $filter ) . 'total_rows' ); |
| 495 |
$courses_cache = LP_Courses_Cache::instance()->get_cache( $key_cache ); |
| 496 |
|
| 497 |
if ( false !== $courses_cache ) { |
| 498 |
$total_rows = LP_Courses_Cache::instance()->get_cache( $key_cache_total_rows ); |
| 499 |
return $courses_cache; |
| 500 |
} |
| 501 |
|
| 502 |
// Sort by |
| 503 |
$filter->sort_by = (array) $filter->sort_by; |
| 504 |
foreach ( $filter->sort_by as $sort_by ) { |
| 505 |
$filter_tmp = clone $filter; |
| 506 |
$filter_tmp->only_fields = array( 'ID' ); |
| 507 |
$filter_tmp->return_string_query = true; |
| 508 |
|
| 509 |
switch ( $sort_by ) { |
| 510 |
case 'on_sale': |
| 511 |
$filter_tmp = $lp_course_db->get_courses_sort_by_sale( $filter_tmp ); |
| 512 |
break; |
| 513 |
case 'on_feature': |
| 514 |
$filter_tmp = $lp_course_db->get_courses_sort_by_feature( $filter_tmp ); |
| 515 |
break; |
| 516 |
default: |
| 517 |
$filter_tmp = apply_filters( 'lp/courses/filter/sort_by/' . $sort_by, $filter_tmp ); |
| 518 |
break; |
| 519 |
} |
| 520 |
|
| 521 |
$query_courses_str = $lp_course_db->get_courses( $filter_tmp ); |
| 522 |
|
| 523 |
$filter->where[] = "AND ID IN ({$query_courses_str})"; |
| 524 |
} |
| 525 |
|
| 526 |
// Order by |
| 527 |
switch ( $filter->order_by ) { |
| 528 |
case 'price': |
| 529 |
case 'price_low': |
| 530 |
if ( 'price_low' === $filter->order_by ) { |
| 531 |
$filter->order = 'ASC'; |
| 532 |
} else { |
| 533 |
$filter->order = 'DESC'; |
| 534 |
} |
| 535 |
|
| 536 |
$filter = $lp_course_db->get_courses_order_by_price( $filter ); |
| 537 |
break; |
| 538 |
case 'popular': |
| 539 |
$filter = $lp_course_db->get_courses_order_by_popular( $filter ); |
| 540 |
break; |
| 541 |
default: |
| 542 |
$filter = apply_filters( 'lp/courses/filter/order_by/' . $filter->order_by, $filter ); |
| 543 |
break; |
| 544 |
} |
| 545 |
|
| 546 |
// Query get results |
| 547 |
$filter = apply_filters( 'lp/courses/filter', $filter ); |
| 548 |
$courses = LP_Course_DB::getInstance()->get_courses( $filter, $total_rows ); |
| 549 |
|
| 550 |
LP_Courses_Cache::instance()->set_cache( $key_cache, $courses ); |
| 551 |
LP_Courses_Cache::instance()->set_cache( $key_cache_total_rows, $total_rows ); |
| 552 |
|
| 553 |
/** |
| 554 |
* Save key cache to array to clear |
| 555 |
* @see LP_Background_Single_Course::save_post() - clear cache when save post |
| 556 |
*/ |
| 557 |
LP_Courses_Cache::instance()->save_cache_keys( $key_cache ); |
| 558 |
LP_Courses_Cache::instance()->save_cache_keys( $key_cache_total_rows ); |
| 559 |
} catch ( Throwable $e ) { |
| 560 |
$courses = []; |
| 561 |
error_log( __FUNCTION__ . ': ' . $e->getMessage() ); |
| 562 |
} |
| 563 |
|
| 564 |
return $courses; |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Get full sections, items of course via Cache, extra info (if it has) |
| 569 |
* |
| 570 |
* @return array |
| 571 |
* @since 4.1.6.9 |
| 572 |
* @version 1.0.0 |
| 573 |
* @author tungnx |
| 574 |
*/ |
| 575 |
public function get_full_sections_and_items_course(): array { |
| 576 |
$sections_items = []; |
| 577 |
$course_id = $this->get_id(); |
| 578 |
|
| 579 |
try { |
| 580 |
// Get cache |
| 581 |
$lp_course_cache = LP_Course_Cache::instance(); |
| 582 |
$key_cache = "$course_id/sections_items"; |
| 583 |
$sections_items = $lp_course_cache->get_cache( $key_cache ); |
| 584 |
|
| 585 |
if ( ! $sections_items ) { |
| 586 |
$extra_info = $this->get_info_extra_for_fast_query(); |
| 587 |
|
| 588 |
if ( empty( $extra_info->sections_items ) ) { |
| 589 |
$sections_items = $this->get_sections_and_items_course_from_db_and_sort(); |
| 590 |
$extra_info->sections_items = $sections_items; |
| 591 |
|
| 592 |
// Save post meta |
| 593 |
$this->set_info_extra_for_fast_query( $extra_info ); |
| 594 |
} else { |
| 595 |
$sections_items = $extra_info->sections_items; |
| 596 |
} |
| 597 |
|
| 598 |
$lp_course_cache->set_cache( $key_cache, $sections_items ); |
| 599 |
} |
| 600 |
} catch ( Throwable $e ) { |
| 601 |
if ( LP_Debug::is_debug() ) { |
| 602 |
error_log( $e->getMessage() ); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
return $sections_items; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Get all sections and items from database, then handle sort |
| 611 |
* Only call when data change or not set |
| 612 |
* |
| 613 |
* @return array |
| 614 |
* @since 4.1.6.9 |
| 615 |
* @version 1.0.0 |
| 616 |
* @author tungnx |
| 617 |
*/ |
| 618 |
public function get_sections_and_items_course_from_db_and_sort(): array { |
| 619 |
$sections_items = []; |
| 620 |
$course_id = $this->get_id(); |
| 621 |
$lp_course_db = LP_Course_DB::getInstance(); |
| 622 |
$lp_course_cache = LP_Course_Cache::instance(); |
| 623 |
$key_cache = "$course_id/sections_items"; |
| 624 |
|
| 625 |
try { |
| 626 |
$sections_results = $lp_course_db->get_sections( $course_id ); |
| 627 |
$sections_items_results = $lp_course_db->get_full_sections_and_items_course( $course_id ); |
| 628 |
$count_items = count( $sections_items_results ); |
| 629 |
$index_items_last = $count_items - 1; |
| 630 |
$section_current = 0; |
| 631 |
|
| 632 |
foreach ( $sections_items_results as $index => $sections_item ) { |
| 633 |
$section_new = $sections_item->section_id; |
| 634 |
$section_order = $sections_item->section_order; |
| 635 |
$item = new stdClass(); |
| 636 |
$item->id = $sections_item->item_id; |
| 637 |
$item->order = $sections_item->item_order; |
| 638 |
$item->type = $sections_item->item_type; |
| 639 |
|
| 640 |
if ( $section_new != $section_current ) { |
| 641 |
$sections_items[ $section_new ] = new stdClass(); |
| 642 |
$sections_items[ $section_new ]->id = $section_new; |
| 643 |
$sections_items[ $section_new ]->order = $section_order; |
| 644 |
$sections_items[ $section_new ]->title = html_entity_decode( $sections_item->section_name ); |
| 645 |
$sections_items[ $section_new ]->description = html_entity_decode( $sections_item->section_description ); |
| 646 |
$sections_items[ $section_new ]->items = []; |
| 647 |
|
| 648 |
// Sort item by item_order |
| 649 |
if ( $section_current != 0 ) { |
| 650 |
usort( |
| 651 |
$sections_items[ $section_current ]->items, |
| 652 |
function ( $item1, $item2 ) { |
| 653 |
return $item1->order - $item2->order; |
| 654 |
} |
| 655 |
); |
| 656 |
} |
| 657 |
|
| 658 |
$section_current = $section_new; |
| 659 |
} |
| 660 |
|
| 661 |
$sections_items[ $section_new ]->items[ $item->id ] = $item; |
| 662 |
|
| 663 |
if ( $index_items_last === $index ) { |
| 664 |
usort( |
| 665 |
$sections_items[ $section_current ]->items, |
| 666 |
function ( $item1, $item2 ) { |
| 667 |
return $item1->order - $item2->order; |
| 668 |
} |
| 669 |
); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
// Check case if section empty items |
| 674 |
foreach ( $sections_results as $section ) { |
| 675 |
$section_id = $section->section_id; |
| 676 |
if ( isset( $sections_items[ $section_id ] ) ) { |
| 677 |
continue; |
| 678 |
} |
| 679 |
|
| 680 |
$section_obj = new stdClass(); |
| 681 |
$section_obj->id = $section_id; |
| 682 |
$section_obj->order = $section->section_order; |
| 683 |
$section_obj->title = html_entity_decode( $section->section_name ); |
| 684 |
$section_obj->description = html_entity_decode( $section->section_description ); |
| 685 |
$section_obj->items = []; |
| 686 |
$sections_items[ $section_id ] = $section_obj; |
| 687 |
} |
| 688 |
|
| 689 |
// Sort section by section_order |
| 690 |
usort( |
| 691 |
$sections_items, |
| 692 |
function ( $section1, $section2 ) { |
| 693 |
return $section1->order - $section2->order; |
| 694 |
} |
| 695 |
); |
| 696 |
|
| 697 |
$lp_course_cache->set_cache( $key_cache, $sections_items ); |
| 698 |
} catch ( Throwable $e ) { |
| 699 |
if ( LP_Debug::is_debug() ) { |
| 700 |
error_log( $e->getMessage() ); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
return $sections_items; |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* Get sections of course. |
| 709 |
* |
| 710 |
* @param string $return - Optional. |
| 711 |
* @param int $section_id - Optional. |
| 712 |
* |
| 713 |
* @return array|bool|LP_Course_Section[]|LP_Course_Section |
| 714 |
* @version 4.0.0 |
| 715 |
*/ |
| 716 |
public function get_sections( $return = 'object', $section_id = 0 ) { |
| 717 |
// $this->load_curriculum(); |
| 718 |
// $sections = LP_Course_Utils::get_cached_db_sections( $this->get_id() ); |
| 719 |
|
| 720 |
$sections_items = $this->get_full_sections_and_items_course(); |
| 721 |
|
| 722 |
/*if ( false === $sections ) { |
| 723 |
return false; |
| 724 |
}*/ |
| 725 |
|
| 726 |
//$position = 0; |
| 727 |
$sections = array(); |
| 728 |
foreach ( $sections_items as $k => $section_items ) { |
| 729 |
$position = $k + 1; |
| 730 |
$section_items_tmp = [ |
| 731 |
'section_id' => $section_items->id, |
| 732 |
'section_name' => $section_items->title, |
| 733 |
'section_course_id' => $this->get_id(), |
| 734 |
'section_order' => $section_items->order, |
| 735 |
'section_description' => $section_items->description, |
| 736 |
'items' => $section_items->items, |
| 737 |
]; |
| 738 |
$sid = $section_items->id; |
| 739 |
$section = new LP_Course_Section( $section_items_tmp ); |
| 740 |
$section->set_position( $position ); |
| 741 |
$sections[ $sid ] = $section; |
| 742 |
} |
| 743 |
|
| 744 |
if ( $section_id ) { |
| 745 |
$sections = $sections[ $section_id ] ?? []; |
| 746 |
} |
| 747 |
|
| 748 |
return apply_filters( 'learn-press/course-sections', $sections, $this->get_id(), $return, $section_id ); |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Get sections arr |
| 753 |
* |
| 754 |
* @param int $section_id |
| 755 |
* |
| 756 |
* @return array |
| 757 |
* @version 1.0.0 |
| 758 |
* @sicne 4.1.7 |
| 759 |
*/ |
| 760 |
public function get_sections_data_arr( int $section_id = 0 ): array { |
| 761 |
$sections_items = $this->get_full_sections_and_items_course(); |
| 762 |
$sections = array(); |
| 763 |
|
| 764 |
foreach ( $sections_items as $section_items ) { |
| 765 |
$section = [ |
| 766 |
'section_id' => $section_items->id, |
| 767 |
'section_name' => $section_items->title, |
| 768 |
'section_course_id' => $this->get_id(), |
| 769 |
'section_order' => $section_items->order, |
| 770 |
'section_description' => $section_items->description, |
| 771 |
'items' => $section_items->items, |
| 772 |
]; |
| 773 |
|
| 774 |
if ( $section_id && $section_id == $section['section_id'] ) { |
| 775 |
$sections = $section; |
| 776 |
break; |
| 777 |
} |
| 778 |
|
| 779 |
$sections[] = $section; |
| 780 |
} |
| 781 |
|
| 782 |
return $sections; |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* Get raw data curriculum. |
| 787 |
* |
| 788 |
* @return array |
| 789 |
* @since 3.0.0 |
| 790 |
* @editor tungnx |
| 791 |
* @version 1.0.1 |
| 792 |
*/ |
| 793 |
public function get_curriculum_raw(): array { |
| 794 |
$sections_data = array(); |
| 795 |
|
| 796 |
$sections_items = $this->get_full_sections_and_items_course(); |
| 797 |
foreach ( $sections_items as $section_items ) { |
| 798 |
$section_items_arr = (array) $section_items; |
| 799 |
$section_items_arr['course_id'] = $this->get_id(); |
| 800 |
$section_items_arr['items'] = []; |
| 801 |
|
| 802 |
foreach ( $section_items->items as $item ) { |
| 803 |
$itemObject = $this->get_item( $item->id ); |
| 804 |
|
| 805 |
if ( ! $itemObject instanceof LP_Course_Item ) { |
| 806 |
continue; |
| 807 |
} |
| 808 |
|
| 809 |
$item_arr = (array) $item; |
| 810 |
$item_arr['title'] = html_entity_decode( $itemObject->get_title() ); |
| 811 |
$item_arr['preview'] = $itemObject->is_preview(); |
| 812 |
$section_items_arr['items'][] = apply_filters( 'learn-press/item/to_array', $item_arr ); |
| 813 |
} |
| 814 |
|
| 815 |
$sections_data[] = $section_items_arr; |
| 816 |
} |
| 817 |
|
| 818 |
return $sections_data; |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Get all curriculum of this course. |
| 823 |
* |
| 824 |
* @param int $section_id |
| 825 |
* @param bool $force |
| 826 |
* |
| 827 |
* @return bool|LP_Course_Section[] |
| 828 |
*/ |
| 829 |
public function get_curriculum( $section_id = 0, $force = false ) { |
| 830 |
return $this->get_sections( 'object', $section_id ); |
| 831 |
} |
| 832 |
|
| 833 |
/** |
| 834 |
* Return list of item's ids in course's curriculum. |
| 835 |
* |
| 836 |
* @param string|array $type |
| 837 |
* |
| 838 |
* @return array |
| 839 |
* @since 3.0.0 |
| 840 |
* @version 3.0.2 |
| 841 |
*/ |
| 842 |
public function get_items( $type = '' ) { |
| 843 |
//$this->load(); |
| 844 |
|
| 845 |
$sections_items = $this->get_full_sections_and_items_course(); |
| 846 |
|
| 847 |
$items = array(); |
| 848 |
|
| 849 |
foreach ( $sections_items as $section_items ) { |
| 850 |
foreach ( $section_items->items as $item ) { |
| 851 |
if ( ! empty( $type ) ) { |
| 852 |
if ( $type === $item->type ) { |
| 853 |
$items[] = $item->id; |
| 854 |
} |
| 855 |
} else { |
| 856 |
$items[] = $item->id; |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
return $items; |
| 862 |
} |
| 863 |
|
| 864 |
/** |
| 865 |
* Get all items in a course. |
| 866 |
* |
| 867 |
* @param string $type . Type of items, eg: lp_lesson, lp_quiz... |
| 868 |
* |
| 869 |
* @return array |
| 870 |
* @deprecated |
| 871 |
*/ |
| 872 |
public function get_curriculum_items( $type = '' ) { |
| 873 |
return $this->get_items( $type ); |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
|