Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/rest-api/Controllers/QAndAController.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Q&A Controller
4 + *
5 + * Manage API for Q&A
6 + *
7 + * @package TutorPro\RestAPI
8 + * @author Themeum <support@themeum.com>
9 + * @link https://themeum.com
10 + * @since 2.6.2
11 + */
12 +
13 + namespace TutorPro\RestAPI\Controllers;
14 +
15 + use Tutor\Helpers\ValidationHelper;
16 + use TUTOR\Input;
17 + use TUTOR\Q_And_A;
18 + use WP_REST_Request;
19 +
20 + if ( ! defined( 'ABSPATH' ) ) {
21 + exit;
22 + }
23 +
24 + /**
25 + * Q&A Controller
26 + */
27 + class QAndAController extends BaseController {
28 +
29 + /**
30 + * Operation codes
31 + *
32 + * @since 2.6.2
33 + *
34 + * @var string
35 + */
36 + public $operation = 'q_and_a';
37 +
38 + /**
39 + * Fillable fields
40 + *
41 + * @since 2.6.2
42 + *
43 + * @var array
44 + */
45 + private $fillable_fields = array(
46 + 'user_id',
47 + 'offset',
48 + 'limit',
49 + 'course_id',
50 + 'question_id',
51 + 'qna_text',
52 + );
53 +
54 + /**
55 + * Required fields
56 + *
57 + * @since 2.6.2
58 + *
59 + * @var array
60 + */
61 + private $required_fields = array(
62 + 'user_id',
63 + 'course_id',
64 + 'qna_text',
65 + );
66 +
67 + /**
68 + * Handle Q&A get API request
69 + *
70 + * @since 2.7.0
71 + *
72 + * @param WP_REST_Request $request request obj.
73 + *
74 + * @return WP_REST_Response|WP_Error
75 + */
76 + public function list( WP_REST_Request $request ) {
77 + // Get params and sanitize it.
78 + $params = Input::sanitize_array( $request->get_params() );
79 +
80 + // Extract fillable fields.
81 + $params = array_intersect_key( $params, array_flip( $this->fillable_fields ) );
82 +
83 + // Validate request.
84 + $validation = $this->validate( $params );
85 + if ( ! $validation->success ) {
86 + $errors = $validation->errors;
87 + }
88 +
89 + if ( ! empty( $errors ) ) {
90 + return $this->response(
91 + $this->code_read,
92 + __( 'Q&A retrieved failed', 'tutor-pro' ),
93 + $errors,
94 + $this->client_error_code
95 + );
96 + }
97 +
98 + $user_id = (int) $request->get_param( 'user_id' );
99 + $offset = (int) $request->get_param( 'offset' );
100 + $limit = ! empty( (int) $request->get_param( 'limit' ) ) ? (int) $request->get_param( 'limit' ) : 10;
101 +
102 + $offset = max( $offset, 0 );
103 + $limit = max( $limit, 10 );
104 +
105 + try {
106 + $args = array();
107 + if ( $request->get_param( 'course_id' ) ) {
108 + $args['course_id'] = $request->get_param( 'course_id' );
109 + }
110 +
111 + $qna_list = tutor_utils()->get_qa_questions( $offset, $limit, '', null, null, $user_id, null, false, $args );
112 +
113 + return $this->response(
114 + $this->code_read,
115 + __( 'Q&A retrieved successfully', 'tutor-pro' ),
116 + $qna_list
117 + );
118 + } catch ( \Throwable $th ) {
119 + return $this->response(
120 + $this->code_read,
121 + __( 'Q&A retrieved failed', 'tutor-pro' ),
122 + $th->getMessage(),
123 + $this->server_error_code
124 + );
125 + }
126 + }
127 +
128 + /**
129 + * Handle Q&A create API request
130 + *
131 + * @since 2.7.0
132 + *
133 + * @param WP_REST_Request $request request obj.
134 + *
135 + * @return WP_REST_Response|WP_Error
136 + */
137 + public function create( WP_REST_Request $request ) {
138 + // Get params and sanitize it.
139 + $params = Input::sanitize_array( $request->get_params() );
140 +
141 + // Extract fillable fields.
142 + $params = array_intersect_key( $params, array_flip( $this->fillable_fields ) );
143 +
144 + // Set empty value if required fields not set.
145 + $this->setup_required_fields( $params, $this->required_fields );
146 +
147 + // Validate request.
148 + $validation = $this->validate( $params );
149 + if ( ! $validation->success ) {
150 + return $this->validation_error_response( $validation->errors, $this->code_create );
151 + }
152 +
153 + $user_id = (int) $params['user_id'];
154 + $course_id = (int) $params['course_id'];
155 + $question_id = ! empty( $params['question_id'] ) ? (int) $params['question_id'] : 0;
156 + $qna_text = $params['qna_text'];
157 + $date = gmdate( 'Y-m-d H:i:s', tutor_time() );
158 + $user = get_userdata( $user_id );
159 +
160 + $qna_data = new \stdClass();
161 + $qna_data->user_id = $user_id;
162 + $qna_data->course_id = $course_id;
163 + $qna_data->question_id = $question_id;
164 + $qna_data->qna_text = $qna_text;
165 + $qna_data->user = $user;
166 + $qna_data->date = $date;
167 +
168 + try {
169 + $qna = new Q_And_A( false );
170 +
171 + if ( ! $qna->has_qna_access( $user_id, $course_id ) ) {
172 + return $this->response(
173 + $this->code_create,
174 + __( 'Q&A add failed', 'tutor-pro' ),
175 + __( 'You are not authorized to perform this action', 'tutor-pro' ),
176 + $this->server_error_code
177 + );
178 + }
179 +
180 + $question_id = $qna->inset_qna( $qna_data );
181 +
182 + if ( $question_id ) {
183 + return $this->response(
184 + $this->code_create,
185 + __( 'Q&A added successfully', 'tutor-pro' ),
186 + );
187 + } else {
188 + return $this->response(
189 + $this->code_create,
190 + __( 'Q&A add failed', 'tutor-pro' ),
191 + );
192 + }
193 + } catch ( \Throwable $th ) {
194 + return $this->response(
195 + $this->code_create,
196 + __( 'Q&A add failed', 'tutor-pro' ),
197 + $th->getMessage(),
198 + $this->server_error_code
199 + );
200 + }
201 + }
202 +
203 + /**
204 + * Delete Q&A
205 + *
206 + * @since 2.7.0
207 + *
208 + * @param WP_REST_Request $request params.
209 + *
210 + * @return WP_REST_Response|WP_Error
211 + */
212 + public function delete( WP_REST_Request $request ) {
213 + $question_id = (int) $request->get_param( 'id' );
214 + $user_id = (int) $request->get_param( 'user_id' );
215 +
216 + $validation = ValidationHelper::validate(
217 + array( 'id' => 'has_record:comments,comment_ID' ),
218 + array( 'id' => $question_id )
219 + );
220 +
221 + if ( ! $validation->success ) {
222 + return $this->validation_error_response( $validation->errors, $this->code_delete );
223 + }
224 +
225 + $is_user = get_userdata( tutor_utils()->get_user_id( $user_id ) );
226 +
227 + if ( ! $is_user ) {
228 + return $this->response(
229 + $this->code_delete,
230 + __( 'User is not valid', 'tutor-pro' ),
231 + );
232 + }
233 +
234 + $can_delete = tutor_utils()->can_delete_qa( $user_id, $question_id );
235 +
236 + if ( ! $can_delete ) {
237 + return $this->response(
238 + $this->code_delete,
239 + __( 'Q&A delete failed', 'tutor-pro' ),
240 + __( 'You are not authorized to perform this action', 'tutor-pro' ),
241 + $this->client_error_code
242 + );
243 + }
244 +
245 + try {
246 + $qna = new Q_And_A( false );
247 + $qna->delete_qna_permanently( array( $question_id ) );
248 + } catch ( \Throwable $th ) {
249 + return $this->response(
250 + $this->code_delete,
251 + __( 'Q&A delete failed', 'tutor-pro' ),
252 + $th->getMessage(),
253 + $this->client_error_code
254 + );
255 + }
256 +
257 + return $this->response(
258 + $this->code_delete,
259 + __( 'Q&A deleted successfully', 'tutor-pro' ),
260 + );
261 + }
262 +
263 + /**
264 + * Mark read/unread Q&A
265 + *
266 + * @since 2.6.2
267 + *
268 + * @param WP_REST_Request $request params.
269 + *
270 + * @return WP_REST_Response|WP_Error
271 + */
272 + public function mark_read_unread( WP_REST_Request $request ) {
273 + $question_id = (int) $request->get_param( 'id' );
274 + $user_id = (int) $request->get_param( 'user_id' );
275 +
276 + $is_user = get_userdata( tutor_utils()->get_user_id( $user_id ) );
277 +
278 + if ( ! $is_user ) {
279 + return $this->response(
280 + $this->code_update,
281 + __( 'User is not valid', 'tutor-pro' ),
282 + );
283 + }
284 +
285 + $can_update = tutor_utils()->can_delete_qa( $user_id, $question_id );
286 +
287 + if ( ! $can_update ) {
288 + return $this->response(
289 + $this->code_update,
290 + __( 'Q&A mark read/unread failed', 'tutor-pro' ),
291 + __( 'You are not authorized to perform this action', 'tutor-pro' ),
292 + $this->client_error_code
293 + );
294 + }
295 +
296 + try {
297 + $qna = new Q_And_A( false );
298 + $result = $qna->trigger_qna_action( $question_id, 'read', 'frontend-dashboard-qna-table-student', $user_id );
299 + $message = $result ? __( 'Q&A marked as read', 'tutor-pro' ) : __( 'Q&A marked as unread', 'tutor-pro' );
300 +
301 + return $this->response(
302 + $this->code_update,
303 + $message,
304 + );
305 + } catch ( \Throwable $th ) {
306 + return $this->response(
307 + $this->code_update,
308 + __( 'Q&A mark read/unread failed', 'tutor-pro' ),
309 + $th->getMessage(),
310 + $this->client_error_code
311 + );
312 + }
313 + }
314 +
315 + /**
316 + * Validate data
317 + *
318 + * @since 2.6.2
319 + *
320 + * @param array $data form data.
321 + *
322 + * @return object
323 + */
324 + protected function validate( array $data ): object {
325 + $validation_rules = array(
326 + 'user_id' => 'required|numeric|user_exists',
327 + 'offset' => 'numeric',
328 + 'limit' => 'numeric',
329 + 'question_id' => 'numeric',
330 + 'course_id' => 'required|numeric',
331 + 'qna_text' => 'required',
332 + );
333 +
334 + // Skip validation rules for not available fields in data.
335 + foreach ( $validation_rules as $key => $value ) {
336 + if ( ! array_key_exists( $key, $data ) ) {
337 + unset( $validation_rules[ $key ] );
338 + }
339 + }
340 +
341 + return ValidationHelper::validate( $validation_rules, $data );
342 + }
343 + }
344 +