PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
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 / Models / CourseSectionItemModel.php

CourseSectionItemModel.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.4, at inc/Models/CourseSectionItemModel.php

318 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Models;
4
5 use Exception;
6 use LearnPress\Databases\Course\CourseSectionItemsDB;
7 use LearnPress\Databases\CourseSectionDB;
8 use LearnPress\Filters\Course\CourseSectionItemsFilter;
9 use LearnPress\Filters\CourseSectionFilter;
10 use LearnPress\Models\CourseSectionModel;
11 use LP_Background_Single_Course;
12 use LP_Cache;
13 use LP_Debug;
14 use LP_Section_DB;
15 use LP_Section_Filter;
16 use LP_Section_Items_DB;
17 use LP_Section_Items_Filter;
18 use stdClass;
19 use Throwable;
20
21 /**
22 * Class CourseSectionModel
23 *
24 * Handle all method about section course
25 *
26 * @package LearnPress/Classes
27 * @version 1.0.0
28 * @since 4.2.8.6
29 */
30 class CourseSectionItemModel {
31 /**
32 * Auto increment, Primary key
33 *
34 * @var int
35 */
36 private $section_item_id = 0;
37 /**
38 * Foreign key, section_id
39 *
40 * @var int
41 */
42 public $section_id = '';
43 /**
44 * Foreign key, item ID (lesson, quiz, etc.)
45 *
46 * @var int
47 */
48 public $item_id = 0;
49 /**
50 * Order of the item
51 *
52 * @var int
53 */
54 public $item_order = 0;
55 /**
56 * Type of the item
57 *
58 * @var string
59 */
60 public $item_type = '';
61 /**
62 * @var int course id
63 */
64 public $section_course_id = 0;
65
66 /**
67 * If data get from database, map to object.
68 * Else create new object to save data to database.
69 *
70 * @param array|object|mixed $data
71 */
72 public function __construct( $data = null ) {
73 if ( $data ) {
74 $this->map_to_object( $data );
75 }
76 }
77
78 /**
79 * Map array, object data to CourseSectionModel.
80 * Use for data get from database.
81 *
82 * @param array|object|mixed $data
83 *
84 * @return CourseSectionItemModel
85 */
86 public function map_to_object( $data ): CourseSectionItemModel {
87 foreach ( $data as $key => $value ) {
88 if ( property_exists( $this, $key ) ) {
89 $this->{$key} = $value;
90 }
91 }
92
93 return $this;
94 }
95
96 /**
97 * Get course model
98 *
99 * @return CourseModel|false
100 */
101 public function get_course_model(): ?CourseModel {
102 return CourseModel::find( $this->section_course_id, true );
103 }
104
105 /**
106 * Get course post model
107 *
108 * @return false|CoursePostModel
109 */
110 public function get_course_post_model(): ?CoursePostModel {
111 $courseModel = $this->get_course_model();
112 if ( $courseModel ) {
113 return new CoursePostModel( $courseModel );
114 }
115
116 return false;
117 }
118
119 /**
120 * Get section item id
121 *
122 * @return int
123 */
124 public function get_section_item_id(): int {
125 return $this->section_item_id;
126 }
127
128 /**
129 * Get section item by section id and item id.
130 *
131 * @return false|CourseSectionItemModel
132 */
133 public static function find( int $section_id, $item_id, $check_cache = true ) {
134 $filter = new CourseSectionItemsFilter();
135 $filter->section_id = $section_id;
136 $filter->item_id = $item_id;
137 $key_cache = "courseSectionItem/find/{$section_id}/{$item_id}";
138 $lpSectionCache = new LP_Cache();
139
140 // Check cache
141 if ( $check_cache ) {
142 $courseSectionModel = $lpSectionCache->get_cache( $key_cache );
143 if ( $courseSectionModel instanceof CourseSectionItemModel ) {
144 return $courseSectionModel;
145 }
146 }
147
148 $courseSectionModel = static::get_item_model_from_db( $filter );
149
150 // Set cache
151 if ( $courseSectionModel instanceof CourseSectionItemModel ) {
152 $lpSectionCache->set_cache( $key_cache, $courseSectionModel );
153 }
154
155 return $courseSectionModel;
156 }
157
158 /**
159 * Get data from database.
160 * If not exists, return false.
161 * If exists, return CourseSectionItemModel.
162 *
163 * @param CourseSectionItemsFilter|LP_Section_Items_Filter $filter
164 *
165 * @return CourseSectionItemModel|false|static
166 * @version 1.0.0
167 */
168 public static function get_item_model_from_db( $filter ) {
169 $lp_section_db = CourseSectionItemsDB::getInstance();
170 $sectionModel = false;
171
172 try {
173 $lp_section_db->get_query_single_row( $filter );
174 $query_single_row = $lp_section_db->get_section_items( $filter );
175 $section_rs = $lp_section_db->wpdb->get_row( $query_single_row );
176
177 if ( $section_rs instanceof stdClass ) {
178 $sectionModel = new static( $section_rs );
179 }
180 } catch ( Throwable $e ) {
181 error_log( __METHOD__ . ': ' . $e->getMessage() );
182 }
183
184 return $sectionModel;
185 }
186
187 /**
188 * Get course ids from item id.
189 *
190 * @param int $item_id
191 * @param string $item_type
192 *
193 * @return array [ stdClass(section_course_id) ]
194 * @version 1.0.0
195 * @since 4.3.3
196 */
197 public static function get_courses_from_item_id( int $item_id, string $item_type ): array {
198 $lp_section_db = CourseSectionItemsDB::getInstance();
199 $course_ids = [];
200
201 try {
202 $filter = new CourseSectionItemsFilter();
203 $filter->item_id = $item_id;
204 $filter->item_type = $item_type;
205 $filter->run_query_count = false;
206 $filter->only_fields = [ CourseSectionItemsFilter::COL_SECTION_ID ];
207 $filter->return_string_query = true;
208
209 $query_get_list_section = $lp_section_db->get_section_items( $filter );
210
211 // Find course ids from section ids
212 $courseSectionDB = CourseSectionDB::getInstance();
213 $filterSection = new CourseSectionFilter();
214 $filterSection->run_query_count = false;
215 $filterSection->where[] = sprintf( 'AND section_id IN (%s)', $query_get_list_section );
216 $filterSection->only_fields = [ CourseSectionFilter::COL_SECTION_COURSE_ID ];
217 $course_ids = $courseSectionDB->get_sections( $filterSection );
218
219 if ( ! is_array( $course_ids ) ) {
220 $course_ids = [];
221 }
222 } catch ( Throwable $e ) {
223 LP_Debug::error_log( $e );
224 }
225
226 return $course_ids;
227 }
228
229 /**
230 * Save course data to table learnpress_section_items.
231 *
232 * @throws Exception
233 * @since 4.2.8.6
234 * @version 1.0.1
235 */
236 public function save(): CourseSectionItemModel {
237 // Check permission
238 $this->check_permission();
239
240 $lp_section_items_db = LP_Section_items_DB::getInstance();
241
242 $data = get_object_vars( $this );
243
244 if ( $data['section_item_id'] === 0 ) { // Insert data.
245 $section_item_id = $lp_section_items_db->insert_data( $data );
246 $this->section_item_id = $section_item_id;
247 } else { // Update data.
248 $lp_section_items_db->update_data( $data );
249 }
250
251 // Clear cache
252 $this->clean_caches();
253
254 return $this;
255 }
256
257 /**
258 * Delete row
259 *
260 * @throws Exception
261 * @since 4.2.8.6
262 * @version 1.0.1
263 */
264 public function delete() {
265 // Check permission
266 $this->check_permission();
267
268 $lp_section_items_db = LP_Section_Items_DB::getInstance();
269 $filter = new LP_Section_Items_Filter();
270 $filter->where[] = $lp_section_items_db->wpdb->prepare( 'AND section_item_id = %d', $this->section_item_id );
271 $filter->collection = $lp_section_items_db->tb_lp_section_items;
272 $lp_section_items_db->delete_execute( $filter );
273
274 // Clear cache
275 $this->clean_caches();
276 }
277
278 /**
279 * Check permission to handle
280 *
281 * @throws Exception
282 */
283 public function check_permission() {
284 $coursePostModel = $this->get_course_post_model();
285 if ( ! $coursePostModel || ! $coursePostModel->check_capabilities_update() ) {
286 throw new Exception( esc_html__( 'You do not have permission to delete section item.', 'learnpress' ) );
287 }
288 }
289
290 /**
291 * Clean caches
292 *
293 * @return void
294 * @throws Exception
295 * @since 4.2.8.6
296 * @version 1.0.0
297 */
298 public function clean_caches() {
299 // Call background multiple times will not cause any problem.
300 /*$bg = LP_Background_Single_Course::instance();
301 $bg->data(
302 array(
303 'handle_name' => 'save_post',
304 'course_id' => $this->section_course_id,
305 'data' => [],
306 )
307 )->dispatch();*/
308 $courseModel = CourseModel::find( $this->section_course_id, true );
309 $courseModel->sections_items = null;
310 $courseModel->total_items = null;
311 $courseModel->save();
312
313 $key_cache = "courseSectionItem/find/{$this->section_id}/{$this->item_id}";
314 $lp_course_cache = new LP_Cache();
315 $lp_course_cache->clear( $key_cache );
316 }
317 }
318