| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Course Post Model |
| 5 |
* To replace class LP_Course old |
| 6 |
* |
| 7 |
* @package LearnPress/Classes |
| 8 |
* @version 1.0.0 |
| 9 |
* @since 4.2.6.9 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace LearnPress\Models; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use LearnPress\Databases\Course\CourseSectionItemsDB; |
| 16 |
use LearnPress\Databases\CourseSectionDB; |
| 17 |
use LearnPress\Filters\Course\CourseSectionItemsFilter; |
| 18 |
use LP_Course_Filter; |
| 19 |
|
| 20 |
use LP_Helper; |
| 21 |
|
| 22 |
class CoursePostModel extends PostModel { |
| 23 |
/** |
| 24 |
* @var string Post Type |
| 25 |
*/ |
| 26 |
public $post_type = LP_COURSE_CPT; |
| 27 |
|
| 28 |
// Taxonomy |
| 29 |
const TAXONOMY_CATEGORY = 'course_category'; |
| 30 |
const TAXONOMY_TAG = 'course_tag'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Const meta key |
| 34 |
*/ |
| 35 |
const META_KEY_PRICE = '_lp_price'; |
| 36 |
const META_KEY_REGULAR_PRICE = '_lp_regular_price'; |
| 37 |
const META_KEY_SALE_PRICE = '_lp_sale_price'; |
| 38 |
const META_KEY_SALE_START = '_lp_sale_start'; |
| 39 |
const META_KEY_SALE_END = '_lp_sale_end'; |
| 40 |
const META_KEY_EVALUATION_TYPE = '_lp_course_result'; |
| 41 |
const META_KEY_PASSING_CONDITION = '_lp_passing_condition'; |
| 42 |
const META_KEY_DURATION = '_lp_duration'; |
| 43 |
const META_KEY_BLOCK_EXPIRE_DURATION = '_lp_block_expire_duration'; |
| 44 |
const META_KEY_BLOCK_FINISH = '_lp_block_finished'; |
| 45 |
const META_KEY_ALLOW_COURSE_REPURCHASE = '_lp_allow_course_repurchase'; |
| 46 |
const META_KEY_COURSE_REPURCHASE_OPTION = '_lp_course_repurchase_option'; |
| 47 |
const META_KEY_LEVEL = '_lp_level'; |
| 48 |
const META_KEY_STUDENTS = '_lp_students'; // Fake students key |
| 49 |
const META_KEY_MAX_STUDENTS = '_lp_max_students'; |
| 50 |
const META_KEY_RETAKE_COUNT = '_lp_retake_count'; |
| 51 |
const META_KEY_HAS_FINISH = '_lp_has_finish'; |
| 52 |
const META_KEY_FEATURED = '_lp_featured'; |
| 53 |
const META_KEY_FEATURED_REVIEW = '_lp_featured_review'; |
| 54 |
const META_KEY_EXTERNAL_LINK_BY_COURSE = '_lp_external_link_buy_course'; |
| 55 |
const META_KEY_IS_SALE = '_lp_course_is_sale'; |
| 56 |
const META_KEY_NO_REQUIRED_ENROLL = '_lp_no_required_enroll'; |
| 57 |
const META_KEY_OFFLINE_COURSE = '_lp_offline_course'; |
| 58 |
const META_KEY_ADDRESS = '_lp_address'; |
| 59 |
const META_KEY_DELIVER = '_lp_deliver_type'; |
| 60 |
const META_KEY_OFFLINE_LESSON_COUNT = '_lp_offline_lesson_count'; |
| 61 |
const META_KEY_REQUIREMENTS = '_lp_requirements'; |
| 62 |
const META_KEY_TARGET = '_lp_target_audiences'; |
| 63 |
const META_KEY_FEATURES = '_lp_key_features'; |
| 64 |
const META_KEY_FAQS = '_lp_faqs'; |
| 65 |
const META_KEY_PRICE_PREFIX = '_lp_price_prefix'; |
| 66 |
const META_KEY_PRICE_SUFFIX = '_lp_price_suffix'; |
| 67 |
const META_KEY_FINAL_QUIZ = '_lp_final_quiz'; |
| 68 |
const META_KEY_SAMPLE_DATA = '_lp_sample_data'; |
| 69 |
|
| 70 |
/** |
| 71 |
* Get the regular price of course. |
| 72 |
* |
| 73 |
* @return float |
| 74 |
*/ |
| 75 |
public function get_regular_price(): float { |
| 76 |
// Regular price |
| 77 |
$regular_price = $this->get_meta_value_by_key( self::META_KEY_PRICE, '' ); // For LP version < 1.4.1.2 |
| 78 |
if ( metadata_exists( 'post', $this->ID, self::META_KEY_REGULAR_PRICE ) ) { |
| 79 |
$regular_price = $this->get_meta_value_by_key( self::META_KEY_REGULAR_PRICE, '' ); |
| 80 |
} |
| 81 |
|
| 82 |
$regular_price = floatval( $regular_price ); |
| 83 |
|
| 84 |
return apply_filters( 'learnPress/course/regular-price', $regular_price, $this ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get the sale price of course. Check if sale price is set |
| 89 |
* and the dates are valid. |
| 90 |
* |
| 91 |
* @return string|float |
| 92 |
*/ |
| 93 |
public function get_sale_price() { |
| 94 |
$sale_price_value = $this->get_meta_value_by_key( self::META_KEY_SALE_PRICE, '' ); |
| 95 |
|
| 96 |
if ( '' !== $sale_price_value ) { |
| 97 |
return floatval( $sale_price_value ); |
| 98 |
} |
| 99 |
|
| 100 |
return $sale_price_value; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get post course by ID |
| 105 |
* |
| 106 |
* @param int $post_id |
| 107 |
* @param bool $check_cache |
| 108 |
* |
| 109 |
* @return false|static |
| 110 |
*/ |
| 111 |
public static function find( int $post_id, bool $check_cache = false ) { |
| 112 |
$filter_post = new LP_Course_Filter(); |
| 113 |
$filter_post->ID = $post_id; |
| 114 |
|
| 115 |
return self::get_item_model_from_db( $filter_post ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get course model |
| 120 |
* |
| 121 |
* @return CourseModel|null |
| 122 |
*/ |
| 123 |
public function get_course_model(): ?CourseModel { |
| 124 |
return CourseModel::find( $this->get_id(), true ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Add section to course |
| 129 |
* |
| 130 |
* @param array $data [ 'section_name' => '', 'section_description' => '' ] |
| 131 |
* |
| 132 |
* @throws Exception |
| 133 |
* @since 4.3.0 |
| 134 |
* @version 1.0.0 |
| 135 |
*/ |
| 136 |
public function add_section( array $data ): CourseSectionModel { |
| 137 |
$course_id = $this->get_id(); |
| 138 |
$section_name = trim( $data['section_name'] ?? '' ); |
| 139 |
if ( empty( $section_name ) ) { |
| 140 |
throw new Exception( __( 'Section title is required', 'learnpress' ) ); |
| 141 |
} |
| 142 |
|
| 143 |
$section_description = LP_Helper::sanitize_params_submitted( $data['section_description'] ?? '', 'html' ); |
| 144 |
|
| 145 |
// Get max section order |
| 146 |
$max_order = CourseSectionDB::getInstance()->get_last_number_order( $course_id ); |
| 147 |
|
| 148 |
$sectionNew = new CourseSectionModel(); |
| 149 |
$sectionNew->section_name = $section_name; |
| 150 |
$sectionNew->section_description = $section_description; |
| 151 |
$sectionNew->section_course_id = $course_id; |
| 152 |
$sectionNew->section_order = $max_order + 1; |
| 153 |
$sectionNew->save(); |
| 154 |
|
| 155 |
return $sectionNew; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Update section |
| 160 |
* |
| 161 |
* @throws Exception |
| 162 |
* @since 4.3.0 |
| 163 |
* @version 1.0.1 |
| 164 |
*/ |
| 165 |
public function update_section( CourseSectionModel $courseSectionModel, array $data ) { |
| 166 |
foreach ( $data as $key => $value ) { |
| 167 |
if ( $key !== 'section_id' && property_exists( $courseSectionModel, $key ) ) { |
| 168 |
$courseSectionModel->{$key} = $value; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$courseSectionModel->save(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Update sections position |
| 177 |
* new_position => list of section id by order |
| 178 |
* |
| 179 |
* @throws Exception |
| 180 |
* @version 1.0.0 |
| 181 |
* @since 4.3.0 |
| 182 |
*/ |
| 183 |
public function update_sections_position( array $data ) { |
| 184 |
// Check permission |
| 185 |
if ( ! $this->check_capabilities_update() ) { |
| 186 |
throw new Exception( __( 'You do not have permission to update course sections', 'learnpress' ) ); |
| 187 |
} |
| 188 |
|
| 189 |
$new_position = $data['new_position'] ?? []; |
| 190 |
if ( ! is_array( $new_position ) ) { |
| 191 |
throw new Exception( __( 'Invalid section position', 'learnpress' ) ); |
| 192 |
} |
| 193 |
|
| 194 |
$course_id = $this->get_id(); |
| 195 |
$courseModel = $this->get_course_model(); |
| 196 |
if ( ! $courseModel ) { |
| 197 |
throw new Exception( __( 'Course not found', 'learnpress' ) ); |
| 198 |
} |
| 199 |
|
| 200 |
CourseSectionDB::getInstance()->update_sections_position( $new_position, $course_id ); |
| 201 |
|
| 202 |
$courseModel->sections_items = null; |
| 203 |
$courseModel->save(); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Update items position in curriculum, can change section of item. |
| 208 |
* |
| 209 |
* @param array $data [ items_position, item_id_change, section_id_new_of_item, section_id_old_of_item ] |
| 210 |
* |
| 211 |
* @return void |
| 212 |
* @throws Exception |
| 213 |
* @since 4.3.2 |
| 214 |
* @version 1.0.0 |
| 215 |
*/ |
| 216 |
public function update_items_position( array $data ) { |
| 217 |
// Check permission |
| 218 |
if ( ! $this->check_capabilities_update() ) { |
| 219 |
throw new Exception( __( 'You do not have permission to update course items position', 'learnpress' ) ); |
| 220 |
} |
| 221 |
|
| 222 |
$courseModel = $this->get_course_model(); |
| 223 |
|
| 224 |
$items_position = $data['items_position'] ?? []; |
| 225 |
$item_id_change = $data['item_id_change'] ?? 0; |
| 226 |
$section_id_new_of_item = $data['section_id_new_of_item'] ?? 0; |
| 227 |
$section_id_old_of_item = $data['section_id_old_of_item'] ?? 0; |
| 228 |
|
| 229 |
if ( ! is_array( $items_position ) ) { |
| 230 |
throw new Exception( __( 'Invalid item position', 'learnpress' ) ); |
| 231 |
} |
| 232 |
|
| 233 |
// Find item of section id old |
| 234 |
$filter = new CourseSectionItemsFilter(); |
| 235 |
$filter->section_id = $section_id_old_of_item; |
| 236 |
$filter->item_id = $item_id_change; |
| 237 |
$filter->run_query_count = false; |
| 238 |
|
| 239 |
$courseSectionItemModel = CourseSectionItemModel::get_item_model_from_db( $filter ); |
| 240 |
if ( ! $courseSectionItemModel ) { |
| 241 |
throw new Exception( __( 'Item not found in section', 'learnpress' ) ); |
| 242 |
} |
| 243 |
|
| 244 |
// Update section id of item |
| 245 |
$courseSectionItemModel->section_id = $section_id_new_of_item; |
| 246 |
$courseSectionItemModel->section_course_id = $this->get_id(); |
| 247 |
$courseSectionItemModel->save(); |
| 248 |
|
| 249 |
// For each section to find item then update section id of item and position of item in the new section |
| 250 |
$sections_items = $courseModel->get_section_items(); |
| 251 |
foreach ( $sections_items as $section_items ) { |
| 252 |
$section_id = $section_items->section_id ?? 0; |
| 253 |
|
| 254 |
if ( $section_id != $section_id_new_of_item ) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
// Update position of item in section |
| 259 |
CourseSectionItemsDB::getInstance()->update_items_position( $items_position, $section_id_new_of_item ); |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
// Clear cache |
| 264 |
$courseModel->sections_items = null; |
| 265 |
} |
| 266 |
|
| 267 |
public function check_capabilities_create(): bool { |
| 268 |
return current_user_can( 'edit_' . $this->post_type . 's' ); |
| 269 |
} |
| 270 |
|
| 271 |
public function check_capabilities_update(): bool { |
| 272 |
return current_user_can( 'edit_' . $this->post_type, $this->ID ); |
| 273 |
} |
| 274 |
} |
| 275 |
|