| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Models; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Databases\CourseSectionDB; |
| 7 |
use LearnPress\Filters\Course\CourseSectionItemsFilter; |
| 8 |
use LearnPress\Filters\CourseSectionFilter; |
| 9 |
use LP_Background_Single_Course; |
| 10 |
use LP_Cache; |
| 11 |
use LP_Database; |
| 12 |
use LP_Helper; |
| 13 |
use LP_Section_DB; |
| 14 |
use LP_Section_Filter; |
| 15 |
use LP_Section_Items_DB; |
| 16 |
use LP_Section_Items_Filter; |
| 17 |
use stdClass; |
| 18 |
use Throwable; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class CourseSectionModel |
| 22 |
* |
| 23 |
* Handle all method about section course |
| 24 |
* |
| 25 |
* @package LearnPress/Classes |
| 26 |
* @version 1.0.0 |
| 27 |
* @since 4.2.8.6 |
| 28 |
*/ |
| 29 |
class CourseSectionModel { |
| 30 |
/** |
| 31 |
* Auto increment, Primary key |
| 32 |
* |
| 33 |
* @var int |
| 34 |
*/ |
| 35 |
public $section_id = 0; |
| 36 |
/** |
| 37 |
* Title of the section |
| 38 |
* |
| 39 |
* @var int |
| 40 |
*/ |
| 41 |
public $section_name = ''; |
| 42 |
/** |
| 43 |
* Foreign key, Course ID |
| 44 |
* |
| 45 |
* @var int |
| 46 |
*/ |
| 47 |
public $section_course_id = 0; |
| 48 |
/** |
| 49 |
* Order of the section |
| 50 |
* |
| 51 |
* @var int |
| 52 |
*/ |
| 53 |
public $section_order = 0; |
| 54 |
/** |
| 55 |
* Description of the section |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
public $section_description = ''; |
| 60 |
|
| 61 |
/** |
| 62 |
* If data get from database, map to object. |
| 63 |
* Else create new object to save data to database. |
| 64 |
* |
| 65 |
* @param array|object|mixed $data |
| 66 |
*/ |
| 67 |
public function __construct( $data = null ) { |
| 68 |
if ( $data ) { |
| 69 |
$this->map_to_object( $data ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Map array, object data to CourseSectionModel. |
| 75 |
* Use for data get from database. |
| 76 |
* |
| 77 |
* @param array|object|mixed $data |
| 78 |
* |
| 79 |
* @return CourseSectionModel |
| 80 |
*/ |
| 81 |
public function map_to_object( $data ): CourseSectionModel { |
| 82 |
foreach ( $data as $key => $value ) { |
| 83 |
if ( property_exists( $this, $key ) ) { |
| 84 |
$this->{$key} = $value; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get section id |
| 93 |
* |
| 94 |
* @return int |
| 95 |
*/ |
| 96 |
public function get_section_id(): int { |
| 97 |
return $this->section_id; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get course model |
| 102 |
* |
| 103 |
* @return false|CourseModel |
| 104 |
*/ |
| 105 |
public function get_course_model() { |
| 106 |
return CourseModel::find( $this->section_course_id, true ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get course post model |
| 111 |
* |
| 112 |
* @return false|CoursePostModel |
| 113 |
* @version 1.0.0 |
| 114 |
* @since 4.3.2 |
| 115 |
*/ |
| 116 |
public function get_course_post_model() { |
| 117 |
$courseModel = $this->get_course_model(); |
| 118 |
if ( $courseModel instanceof CourseModel ) { |
| 119 |
return new CoursePostModel( $courseModel ); |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get section by course id |
| 127 |
* |
| 128 |
* @return false|CourseSectionModel |
| 129 |
*/ |
| 130 |
public static function find( int $section_id, int $course_id, $check_cache = true ) { |
| 131 |
$filter = new LP_Section_Filter(); |
| 132 |
$filter->section_id = $section_id; |
| 133 |
$filter->section_course_id = $course_id; |
| 134 |
$key_cache = "courseSection/find/{$section_id}/{$course_id}"; |
| 135 |
$lpSectionCache = new LP_Cache(); |
| 136 |
|
| 137 |
// Check cache |
| 138 |
if ( $check_cache ) { |
| 139 |
$courseSectionModel = $lpSectionCache->get_cache( $key_cache ); |
| 140 |
if ( $courseSectionModel instanceof CourseSectionModel ) { |
| 141 |
return $courseSectionModel; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$courseSectionModel = static::get_item_model_from_db( $filter ); |
| 146 |
|
| 147 |
// Set cache |
| 148 |
if ( $courseSectionModel instanceof CourseSectionModel ) { |
| 149 |
$lpSectionCache->set_cache( $key_cache, $courseSectionModel ); |
| 150 |
} |
| 151 |
|
| 152 |
return $courseSectionModel; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get section by course id |
| 157 |
* |
| 158 |
* @return false|CourseSectionModel |
| 159 |
*/ |
| 160 |
public static function find_by_course( int $course_id, $check_cache = true ) { |
| 161 |
$filter = new LP_Section_Filter(); |
| 162 |
$filter->section_course_id = $course_id; |
| 163 |
$key_cache = "courseSection/find/course/{$course_id}"; |
| 164 |
$lpSectionCache = new LP_Cache(); |
| 165 |
|
| 166 |
// Check cache |
| 167 |
if ( $check_cache ) { |
| 168 |
$courseSectionModel = $lpSectionCache->get_cache( $key_cache ); |
| 169 |
if ( $courseSectionModel instanceof CourseSectionModel ) { |
| 170 |
return $courseSectionModel; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$courseSectionModel = static::get_item_model_from_db( $filter ); |
| 175 |
|
| 176 |
// Set cache |
| 177 |
if ( $courseSectionModel instanceof CourseSectionModel ) { |
| 178 |
$lpSectionCache->set_cache( $key_cache, $courseSectionModel ); |
| 179 |
} |
| 180 |
|
| 181 |
return $courseSectionModel; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get post from database. |
| 186 |
* If not exists, return false. |
| 187 |
* If exists, return CourseSectionModel. |
| 188 |
* |
| 189 |
* @param LP_Section_Filter $filter |
| 190 |
* |
| 191 |
* @return CourseSectionModel|false|static |
| 192 |
* @version 1.0.0 |
| 193 |
*/ |
| 194 |
public static function get_item_model_from_db( LP_Section_Filter $filter ) { |
| 195 |
$lp_section_db = LP_Section_DB::getInstance(); |
| 196 |
$sectionModel = false; |
| 197 |
|
| 198 |
try { |
| 199 |
$lp_section_db->get_query_single_row( $filter ); |
| 200 |
$query_single_row = $lp_section_db->get_sections( $filter ); |
| 201 |
$section_rs = $lp_section_db->wpdb->get_row( $query_single_row ); |
| 202 |
|
| 203 |
if ( $section_rs instanceof stdClass ) { |
| 204 |
$sectionModel = new static( $section_rs ); |
| 205 |
} |
| 206 |
} catch ( Throwable $e ) { |
| 207 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 208 |
} |
| 209 |
|
| 210 |
return $sectionModel; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Create item and add to section. |
| 215 |
* |
| 216 |
* @param array $data [ 'item_type' => '', 'item_title' => '', 'item_content' => '' ] |
| 217 |
* |
| 218 |
* @return CourseSectionItemModel |
| 219 |
* @throws Exception |
| 220 |
* @since 4.2.8.6 |
| 221 |
* @version 1.0.1 |
| 222 |
*/ |
| 223 |
public function create_item_and_add( array $data ): CourseSectionItemModel { |
| 224 |
$item_type = trim( $data['item_type'] ?? '' ); |
| 225 |
$item_title = $data['item_title'] ?? ''; |
| 226 |
$item_content = $data['item_content'] ?? ''; |
| 227 |
$courseModel = $this->get_course_model(); |
| 228 |
$section_id = $this->get_section_id(); |
| 229 |
|
| 230 |
if ( ! $courseModel instanceof CourseModel ) { |
| 231 |
throw new Exception( __( 'Course not found', 'learnpress' ) ); |
| 232 |
} |
| 233 |
|
| 234 |
$item_types = CourseModel::item_types_support(); |
| 235 |
if ( ! in_array( $item_type, $item_types ) ) { |
| 236 |
throw new Exception( __( 'Item type invalid', 'learnpress' ) ); |
| 237 |
} |
| 238 |
|
| 239 |
// Create new item |
| 240 |
if ( empty( $item_title ) ) { |
| 241 |
throw new Exception( __( 'Item title is required', 'learnpress' ) ); |
| 242 |
} |
| 243 |
|
| 244 |
// Create item |
| 245 |
$itemModelNew = new PostModel(); |
| 246 |
$itemModelNew->post_type = $item_type; |
| 247 |
$itemModelNew->post_title = $item_title; |
| 248 |
$itemModelNew->post_content = $item_content; |
| 249 |
$itemModelNew->post_status = 'publish'; |
| 250 |
$itemModelNew->post_author = get_current_user_id(); |
| 251 |
$itemModelNew->save(); |
| 252 |
$item_id = $itemModelNew->get_id(); |
| 253 |
|
| 254 |
/*$post_args = [ |
| 255 |
'post_author' => get_current_user_id(), |
| 256 |
'post_title' => $item_title, |
| 257 |
'post_type' => $item_type, |
| 258 |
'post_status' => 'publish', |
| 259 |
]; |
| 260 |
$item_id = wp_insert_post( $post_args ); |
| 261 |
if ( is_wp_error( $item_id ) ) { |
| 262 |
throw new Exception( $item_id->get_error_message() ); |
| 263 |
}*/ |
| 264 |
|
| 265 |
// Get max item order |
| 266 |
$max_order = LP_Section_Items_DB::getInstance()->get_last_number_order( $section_id ); |
| 267 |
|
| 268 |
// Add item to section |
| 269 |
$courseSectionItemModel = new CourseSectionItemModel(); |
| 270 |
$courseSectionItemModel->item_id = $item_id; |
| 271 |
$courseSectionItemModel->item_type = $item_type; |
| 272 |
$courseSectionItemModel->section_id = $section_id; |
| 273 |
$courseSectionItemModel->item_order = $max_order + 1; |
| 274 |
$courseSectionItemModel->section_course_id = $this->section_course_id; |
| 275 |
$courseSectionItemModel->save(); |
| 276 |
|
| 277 |
return $courseSectionItemModel; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Add items created to section. |
| 282 |
* |
| 283 |
* @param array $data |
| 284 |
* |
| 285 |
* @return array |
| 286 |
* @throws Exception |
| 287 |
* @since 4.2.8.6 |
| 288 |
* @version 1.0.2 |
| 289 |
*/ |
| 290 |
public function add_items( array $data ): array { |
| 291 |
$courseSectionItems = []; |
| 292 |
$courseModel = $this->get_course_model(); |
| 293 |
$section_id = $this->get_section_id(); |
| 294 |
|
| 295 |
if ( ! $courseModel instanceof CourseModel ) { |
| 296 |
throw new Exception( __( 'Course not found', 'learnpress' ) ); |
| 297 |
} |
| 298 |
|
| 299 |
$items = $data['items'] ?? []; |
| 300 |
foreach ( $items as $item ) { |
| 301 |
if ( ! key_exists( 'id', $item ) || ! key_exists( 'type', $item ) ) { |
| 302 |
throw new Exception( __( 'Keys data invalid!', 'learnpress' ) ); |
| 303 |
} |
| 304 |
|
| 305 |
$item_id = intval( $item['id'] ?? 0 ); |
| 306 |
$item_type = $item['type'] ?? ''; |
| 307 |
if ( ! $item_id ) { |
| 308 |
continue; |
| 309 |
} |
| 310 |
|
| 311 |
// Check if item already exists in course. |
| 312 |
$lp_db = LP_Database::getInstance(); |
| 313 |
$filter = new CourseSectionItemsFilter(); |
| 314 |
$filter->item_id = $item_id; |
| 315 |
$filter->item_type = $item_type; |
| 316 |
$filter->join[] = 'LEFT JOIN ' . $lp_db->tb_lp_sections . ' AS cs ON si.section_id = cs.section_id'; |
| 317 |
$filter->where[] = $lp_db->wpdb->prepare( 'AND cs.section_course_id = %d', $this->section_course_id ); |
| 318 |
$courseSectionItemModel = CourseSectionItemModel::get_item_model_from_db( $filter ); |
| 319 |
if ( $courseSectionItemModel instanceof CourseSectionItemModel ) { |
| 320 |
throw new Exception( __( 'Item already exists in this course', 'learnpress' ) ); |
| 321 |
} |
| 322 |
|
| 323 |
// Get max item order |
| 324 |
$max_order = LP_Section_Items_DB::getInstance()->get_last_number_order( $section_id ); |
| 325 |
|
| 326 |
// Add item to section |
| 327 |
$courseSectionItemModel = new CourseSectionItemModel(); |
| 328 |
$courseSectionItemModel->item_id = $item_id; |
| 329 |
$courseSectionItemModel->item_type = $item_type; |
| 330 |
$courseSectionItemModel->item_order = ++ $max_order; |
| 331 |
$courseSectionItemModel->section_id = $section_id; |
| 332 |
$courseSectionItemModel->section_course_id = $this->section_course_id; |
| 333 |
$courseSectionItemModel->save(); |
| 334 |
|
| 335 |
$courseSectionItems[] = $courseSectionItemModel; |
| 336 |
} |
| 337 |
|
| 338 |
return $courseSectionItems; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Save course data to table learnpress_sections. |
| 343 |
* |
| 344 |
* @throws Exception |
| 345 |
* @since 4.2.8.6 |
| 346 |
* @version 1.0.2 |
| 347 |
*/ |
| 348 |
public function save(): CourseSectionModel { |
| 349 |
// Check permission |
| 350 |
$this->check_permission(); |
| 351 |
|
| 352 |
$courseSectionDB = CourseSectionDB::getInstance(); |
| 353 |
|
| 354 |
$data = get_object_vars( $this ); |
| 355 |
|
| 356 |
$args = [ |
| 357 |
'data' => $data, |
| 358 |
'filter' => new CourseSectionFilter(), |
| 359 |
'table_name' => $courseSectionDB->tb_lp_sections, |
| 360 |
'key_auto_increment' => 'section_id', |
| 361 |
]; |
| 362 |
|
| 363 |
if ( $data['section_id'] === 0 ) { // Insert data. |
| 364 |
$section_id = $courseSectionDB->insert_data( $args ); |
| 365 |
$this->section_id = $section_id; |
| 366 |
} else { // Update data. |
| 367 |
$args['where_key'] = 'section_id'; |
| 368 |
$courseSectionDB->update_data( $args ); |
| 369 |
} |
| 370 |
|
| 371 |
// Clear cache |
| 372 |
$this->clean_caches(); |
| 373 |
|
| 374 |
return $this; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Delete row |
| 379 |
* |
| 380 |
* @throws Exception |
| 381 |
* @since 4.2.8.6 |
| 382 |
* @version 1.0.2 |
| 383 |
*/ |
| 384 |
public function delete() { |
| 385 |
// Check permission |
| 386 |
$this->check_permission(); |
| 387 |
|
| 388 |
// Unassign items of section |
| 389 |
$lp_section_items_db = LP_Section_Items_DB::getInstance(); |
| 390 |
$filter = new LP_Section_Items_Filter(); |
| 391 |
$filter->where[] = $lp_section_items_db->wpdb->prepare( 'AND section_id = %d', $this->section_id ); |
| 392 |
$filter->collection = $lp_section_items_db->tb_lp_section_items; |
| 393 |
$lp_section_items_db->delete_execute( $filter ); |
| 394 |
|
| 395 |
// Delete section |
| 396 |
$lp_section_db = LP_Section_DB::getInstance(); |
| 397 |
$filter = new LP_Section_Filter(); |
| 398 |
$filter->where[] = $lp_section_db->wpdb->prepare( 'AND section_id = %d', $this->section_id ); |
| 399 |
$filter->collection = $lp_section_db->tb_lp_sections; |
| 400 |
$lp_section_db->delete_execute( $filter ); |
| 401 |
|
| 402 |
// Clear cache |
| 403 |
$this->clean_caches(); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Check permission to handle |
| 408 |
* |
| 409 |
* @throws Exception |
| 410 |
* @since 4.3.2 |
| 411 |
* @version 1.0.0 |
| 412 |
*/ |
| 413 |
public function check_permission() { |
| 414 |
$coursePostModel = $this->get_course_post_model(); |
| 415 |
if ( ! $coursePostModel || ! $coursePostModel->check_capabilities_update() ) { |
| 416 |
throw new Exception( __( 'You do not have permission to delete section', 'learnpress' ) ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Clean caches |
| 422 |
* |
| 423 |
* @return void |
| 424 |
* @throws Exception |
| 425 |
* @since 4.2.8.6 |
| 426 |
* @version 1.0.0 |
| 427 |
*/ |
| 428 |
public function clean_caches() { |
| 429 |
// Call background multiple times will not cause any problem. |
| 430 |
/*$bg = LP_Background_Single_Course::instance(); |
| 431 |
$bg->data( |
| 432 |
array( |
| 433 |
'handle_name' => 'save_post', |
| 434 |
'course_id' => $this->section_course_id, |
| 435 |
'data' => [], |
| 436 |
) |
| 437 |
)->dispatch();*/ |
| 438 |
$courseModel = CourseModel::find( $this->section_course_id, true ); |
| 439 |
$courseModel->sections_items = null; |
| 440 |
$courseModel->save(); |
| 441 |
|
| 442 |
$key_cache = "courseSection/find/{$this->get_section_id()}/{$this->section_course_id}"; |
| 443 |
$key_cache_course = "courseSection/find/course/{$this->section_course_id}"; |
| 444 |
$lp_course_cache = new LP_Cache(); |
| 445 |
$lp_course_cache->clear( $key_cache ); |
| 446 |
$lp_course_cache->clear( $key_cache_course ); |
| 447 |
} |
| 448 |
} |
| 449 |
|