PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 / MCP / Mappers / ResponseMapper.php

ResponseMapper.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/MCP/Mappers/ResponseMapper.php

359 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\MCP\Mappers;
4
5 use LearnPress\Models\CourseModel;
6 use LearnPress\Models\CoursePostModel;
7 use LearnPress\Models\CourseSectionModel;
8 use LearnPress\Models\LessonPostModel;
9 use LearnPress\Models\QuizPostModel;
10 use LearnPress\Models\Question\QuestionPostModel;
11 use LearnPress\Models\Question\QuestionAnswerModel;
12 use LearnPress\Models\UserItems\UserItemModel;
13 use LearnPress\Models\UserModel;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Response payload composition for Phase 2 MCP tools.
19 *
20 * Keeps executor logic free of inline array assembly and guarantees a
21 * consistent shape across create/update/read responses.
22 */
23 class ResponseMapper {
24
25 /**
26 * Minimal course write result.
27 *
28 * @param CourseModel $course Course model.
29 *
30 * @return array
31 */
32 public static function course_write_result( CourseModel $course ): array {
33 return array(
34 'course_id' => $course->get_id(),
35 'title' => (string) $course->get_title(),
36 'status' => (string) $course->get_status(),
37 'permalink' => (string) $course->get_permalink(),
38 'edit_url' => (string) $course->get_post_model()->get_edit_link(),
39 );
40 }
41
42 /**
43 * Full course object.
44 *
45 * @param CourseModel $course Course model.
46 *
47 * @return array
48 */
49 public static function course_object( CourseModel $course ): array {
50 $categories = array();
51 foreach ( $course->get_categories() as $term ) {
52 $categories[] = array(
53 'term_id' => (int) $term->term_id,
54 'name' => (string) $term->name,
55 'slug' => (string) $term->slug,
56 );
57 }
58
59 $tags = array();
60 foreach ( $course->get_tags() as $term ) {
61 $tags[] = array(
62 'term_id' => (int) $term->term_id,
63 'name' => (string) $term->name,
64 'slug' => (string) $term->slug,
65 );
66 }
67
68 $author = $course->get_author_model();
69
70 return array(
71 'course_id' => $course->get_id(),
72 'title' => (string) $course->get_title(),
73 'status' => (string) $course->get_status(),
74 'excerpt' => (string) ( $course->post_excerpt ?? '' ),
75 'description' => (string) $course->get_description(),
76 'price' => (float) $course->get_price(),
77 'regular_price' => (float) $course->get_regular_price(),
78 'sale_price' => (float) $course->get_sale_price(),
79 'duration' => (string) $course->get_duration(),
80 'level' => (string) $course->get_meta_value_by_key( CoursePostModel::META_KEY_LEVEL, '' ),
81 'requirements' => $course->get_meta_value_by_key( CoursePostModel::META_KEY_REQUIREMENTS, '' ),
82 'target_audiences' => $course->get_meta_value_by_key( CoursePostModel::META_KEY_TARGET, '' ),
83 'features' => $course->get_meta_value_by_key( CoursePostModel::META_KEY_FEATURES, '' ),
84 'faqs' => $course->get_meta_value_by_key( CoursePostModel::META_KEY_FAQS, '' ),
85 'permalink' => (string) $course->get_permalink(),
86 'edit_url' => (string) $course->get_post_model()->get_edit_link(),
87 'instructor' => array(
88 'user_id' => $author instanceof UserModel ? $author->get_id() : 0,
89 'display_name' => $author instanceof UserModel ? $author->get_display_name() : '',
90 ),
91 'categories' => $categories,
92 'tags' => $tags,
93 );
94 }
95
96 /**
97 * Section object.
98 *
99 * @param CourseSectionModel $section Section model.
100 *
101 * @return array
102 */
103 public static function section( CourseSectionModel $section ): array {
104 return array(
105 'section_id' => (int) $section->get_section_id(),
106 'course_id' => (int) $section->section_course_id,
107 'name' => (string) $section->section_name,
108 'description' => (string) $section->section_description,
109 'order' => (int) $section->section_order,
110 );
111 }
112
113 /**
114 * Lesson object.
115 *
116 * @param LessonPostModel $lesson Lesson model.
117 * @param array $location Optional curriculum location data.
118 *
119 * @return array
120 */
121 public static function lesson( LessonPostModel $lesson, array $location = array() ): array {
122 return array(
123 'lesson_id' => $lesson->get_id(),
124 'course_id' => absint( $location['course_id'] ?? 0 ),
125 'section_id' => absint( $location['section_id'] ?? 0 ),
126 'title' => (string) $lesson->get_the_title(),
127 'content' => (string) $lesson->get_the_content(),
128 'excerpt' => (string) $lesson->get_the_excerpt(),
129 'status' => (string) $lesson->post_status,
130 'duration' => (string) $lesson->get_duration(),
131 'preview' => (bool) $lesson->has_preview(),
132 'video_intro' => (string) $lesson->get_meta_value_by_key( '_lp_lesson_video_intro', '' ),
133 'permalink' => (string) $lesson->get_permalink(),
134 );
135 }
136
137 /**
138 * Lesson minimal write result.
139 *
140 * @param LessonPostModel $lesson Lesson model.
141 * @param array $location Curriculum location data.
142 *
143 * @return array
144 */
145 public static function lesson_write_result( LessonPostModel $lesson, array $location = array() ): array {
146 return array(
147 'lesson_id' => $lesson->get_id(),
148 'course_id' => absint( $location['course_id'] ?? 0 ),
149 'section_id' => absint( $location['section_id'] ?? 0 ),
150 'title' => (string) $lesson->get_the_title(),
151 'status' => (string) $lesson->post_status,
152 'permalink' => (string) $lesson->get_permalink(),
153 );
154 }
155
156 /**
157 * Quiz object.
158 *
159 * @param QuizPostModel $quiz Quiz model.
160 * @param array $location Optional curriculum location data.
161 *
162 * @return array
163 */
164 public static function quiz( QuizPostModel $quiz, array $location = array() ): array {
165 return array(
166 'quiz_id' => $quiz->get_id(),
167 'course_id' => absint( $location['course_id'] ?? 0 ),
168 'section_id' => absint( $location['section_id'] ?? 0 ),
169 'title' => (string) $quiz->get_the_title(),
170 'content' => (string) $quiz->get_the_content(),
171 'status' => (string) $quiz->post_status,
172 'duration' => (string) $quiz->get_duration(),
173 'passing_grade' => (float) $quiz->get_passing_grade(),
174 'retake_count' => (int) $quiz->get_retake_count(),
175 'instant_check' => (bool) $quiz->has_instant_check(),
176 'negative_marking' => (bool) $quiz->has_negative_marking(),
177 'show_correct_review' => (bool) $quiz->has_show_correct_review(),
178 'questions_count' => (int) $quiz->count_questions(),
179 'permalink' => (string) $quiz->get_permalink(),
180 );
181 }
182
183 /**
184 * Quiz minimal write result.
185 *
186 * @param QuizPostModel $quiz Quiz model.
187 * @param array $location Curriculum location data.
188 *
189 * @return array
190 */
191 public static function quiz_write_result( QuizPostModel $quiz, array $location = array() ): array {
192 return array(
193 'quiz_id' => $quiz->get_id(),
194 'course_id' => absint( $location['course_id'] ?? 0 ),
195 'section_id' => absint( $location['section_id'] ?? 0 ),
196 'title' => (string) $quiz->get_the_title(),
197 'status' => (string) $quiz->post_status,
198 'permalink' => (string) $quiz->get_permalink(),
199 );
200 }
201
202 /**
203 * Question object, with sensitive fields gated by privilege.
204 *
205 * @param QuestionPostModel $question Question model.
206 * @param bool $privileged Whether to expose sensitive data.
207 * @param int $order Question order in the quiz.
208 *
209 * @return array
210 */
211 public static function question( QuestionPostModel $question, bool $privileged, int $order = 0 ): array {
212 $answers = array();
213 foreach ( $question->get_answer_option() as $answer ) {
214 if ( $answer instanceof QuestionAnswerModel ) {
215 $answers[] = self::answer( $answer, $privileged );
216 }
217 }
218
219 $data = array(
220 'question_id' => $question->get_id(),
221 'title' => (string) $question->get_the_title(),
222 'content' => (string) $question->get_the_content(),
223 'type' => (string) $question->get_type(),
224 'mark' => (float) $question->get_mark(),
225 'order' => $order,
226 'status' => (string) $question->post_status,
227 'answers' => $answers,
228 );
229
230 if ( $privileged ) {
231 $data['hint'] = (string) $question->get_hint();
232 $data['explanation'] = (string) $question->get_explanation();
233 }
234
235 return $data;
236 }
237
238 /**
239 * Answer object, with correctness gated by privilege.
240 *
241 * @param QuestionAnswerModel $answer Answer model.
242 * @param bool $privileged Whether to expose correctness.
243 *
244 * @return array
245 */
246 public static function answer( QuestionAnswerModel $answer, bool $privileged ): array {
247 $data = array(
248 'answer_id' => (int) $answer->question_answer_id,
249 'title' => (string) $answer->title,
250 'value' => (string) $answer->value,
251 'order' => (int) $answer->order,
252 );
253
254 if ( $privileged ) {
255 $data['is_correct'] = ( 'yes' === $answer->is_true );
256 }
257
258 return $data;
259 }
260
261 /**
262 * Enrollment object.
263 *
264 * @param UserItemModel $enrollment Enrollment model.
265 *
266 * @return array
267 */
268 public static function enrollment( UserItemModel $enrollment ): array {
269 return array(
270 'enrollment_id' => (int) $enrollment->get_user_item_id(),
271 'user_id' => (int) $enrollment->user_id,
272 'course_id' => (int) $enrollment->item_id,
273 'status' => (string) $enrollment->get_status(),
274 'graduation' => (string) $enrollment->get_graduation(),
275 'start_time' => (string) $enrollment->get_start_time(),
276 'end_time' => (string) $enrollment->get_end_time(),
277 );
278 }
279
280 /**
281 * Course list summary (read tools).
282 *
283 * @param CourseModel $course Course model.
284 *
285 * @return array
286 */
287 public static function course_summary( CourseModel $course ): array {
288 $categories = array();
289 foreach ( $course->get_categories() as $term ) {
290 $categories[] = array(
291 'term_id' => (int) $term->term_id,
292 'name' => (string) $term->name,
293 'slug' => (string) $term->slug,
294 );
295 }
296 $author = $course->get_author_model();
297
298 return array(
299 'course_id' => $course->get_id(),
300 'title' => (string) $course->get_title(),
301 'status' => (string) $course->get_status(),
302 'price' => (float) $course->get_price(),
303 'duration' => (string) $course->get_duration(),
304 'permalink' => (string) $course->get_permalink(),
305 'instructor' => array(
306 'user_id' => $author instanceof UserModel ? $author->get_id() : 0,
307 'display_name' => $author instanceof UserModel ? $author->get_display_name() : '',
308 ),
309 'categories' => $categories,
310 );
311 }
312
313 /**
314 * Lesson list summary (read tools).
315 *
316 * @param LessonPostModel $lesson Lesson model.
317 * @param array $ref Curriculum reference (from Curriculum::collect_items).
318 *
319 * @return array
320 */
321 public static function lesson_summary( LessonPostModel $lesson, array $ref ): array {
322 return array(
323 'lesson_id' => $lesson->get_id(),
324 'course_id' => (int) $ref['course_id'],
325 'section_id' => (int) $ref['section_id'],
326 'section_name' => (string) $ref['section_name'],
327 'title' => (string) $lesson->get_the_title(),
328 'excerpt' => (string) $lesson->get_the_excerpt(),
329 'duration' => (string) $lesson->get_duration(),
330 'preview' => (bool) $ref['preview'],
331 'status' => (string) $lesson->post_status,
332 'permalink' => (string) $lesson->get_permalink(),
333 );
334 }
335
336 /**
337 * Quiz list summary (read tools).
338 *
339 * @param QuizPostModel $quiz Quiz model.
340 * @param array $ref Curriculum reference (from Curriculum::collect_items).
341 *
342 * @return array
343 */
344 public static function quiz_summary( QuizPostModel $quiz, array $ref ): array {
345 return array(
346 'quiz_id' => $quiz->get_id(),
347 'course_id' => (int) $ref['course_id'],
348 'section_id' => (int) $ref['section_id'],
349 'section_name' => (string) $ref['section_name'],
350 'title' => (string) $quiz->get_the_title(),
351 'duration' => (string) $quiz->get_duration(),
352 'passing_grade' => (float) $quiz->get_passing_grade(),
353 'questions_count' => (int) $quiz->count_questions(),
354 'status' => (string) $quiz->post_status,
355 'permalink' => (string) $quiz->get_permalink(),
356 );
357 }
358 }
359