class-ld-rest-questions-controller.php 16.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
<?php
/**
 * LearnDash REST API V1 Questions Post Controller.
 *
 * @since 2.5.8
 * @package LearnDash\REST\V1
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ( ! class_exists( 'LD_REST_Questions_Controller_V1' ) ) && ( class_exists( 'WP_REST_Controller' ) ) ) {

	/**
	 * Class LearnDash REST API V1 Questions Post Controller.
	 *
	 * @since 2.5.8
	 */
	class LD_REST_Questions_Controller_V1 extends WP_REST_Controller /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {

		/**
		 * Registers the routes for the objects of the controller.
		 *
		 * @since 2.5.8
		 *
		 * @see register_rest_route() in WordPress core.
		 */
		public function register_routes() {
			$version   = '1';
			$namespace = LEARNDASH_REST_API_NAMESPACE . '/v' . $version;
			$base      = 'sfwd-questions';

			register_rest_route(
				$namespace,
				'/' . $base,
				array(
					array(
						'methods'             => WP_REST_Server::READABLE,
						'callback'            => array( $this, 'get_items' ),
						'permission_callback' => array( $this, 'permissions_check' ),
						'args'                => array(),
					),
				)
			);

			register_rest_route(
				$namespace,
				'/' . $base . '/(?P<id>[\d]+)',
				array(
					array(
						'methods'             => WP_REST_Server::READABLE,
						'callback'            => array( $this, 'get_item' ),
						'permission_callback' => array( $this, 'permissions_check' ),
						'args'                => array(
							'id' => array(
								'description'       => sprintf(
									// translators: question.
									esc_html_x( 'The %s ID', 'placeholder: question', 'learndash' ),
									learndash_get_custom_label_lower( 'question' )
								),
								'required'          => true,
								'validate_callback' => function( $param, $request, $key ) {
									return is_numeric( $param );
								},
								'sanitize_callback' => 'absint',
							),
						),
					),
					array(
						'methods'             => WP_REST_Server::EDITABLE,
						'callback'            => array( $this, 'update_item' ),
						'permission_callback' => array( $this, 'permissions_check' ),
						'args'                => array(
							'id' => array(
								'description'       => sprintf(
									// translators: question.
									esc_html_x( 'The %s ID', 'placeholder: question', 'learndash' ),
									learndash_get_custom_label_lower( 'question' )
								),
								'required'          => true,
								'validate_callback' => function( $param, $request, $key ) {
									return is_numeric( $param );
								},
								'sanitize_callback' => 'absint',
							),
						),
					),
					array(
						'methods'             => WP_REST_Server::DELETABLE,
						'callback'            => array( $this, 'delete_item' ),
						'permission_callback' => array( $this, 'permissions_check' ),
						'args'                => array(
							'id' => array(
								'description'       => sprintf(
									// translators: question.
									esc_html_x( 'The %s ID', 'placeholder: question', 'learndash' ),
									learndash_get_custom_label_lower( 'question' )
								),
								'required'          => true,
								'validate_callback' => function( $param, $request, $key ) {
									return is_numeric( $param );
								},
								'sanitize_callback' => 'absint',
							),
						),
					),
					'schema' => array( $this, 'get_schema' ),
				)
			);
		}

		/**
		 * Check if a given request has access manage the item.
		 *
		 * @since 2.5.8
		 *
		 * @param WP_REST_Request $request Full data about the request.
		 *
		 * @return WP_Error|bool
		 */
		public function permissions_check( $request ) {
			$params = $request->get_params();
			if ( ( isset( $params['id'] ) ) && ( ! empty( $params['id'] ) ) ) {
				$question_id = $params['id'];

				return current_user_can( 'edit_post', $question_id );
			}
			return false;
		}

		/**
		 * Get a question items
		 *
		 * @since 2.5.8
		 *
		 * @param WP_REST_Request $request Full data about the request.
		 *
		 * @return WP_Error|WP_REST_Response
		 */
		public function get_items( $request ) {
			$data = array();
			return new WP_REST_Response( $data, 200 );
		}

		/**
		 * Get a question item
		 *
		 * @since 2.5.8
		 *
		 * @param WP_REST_Request $request Full data about the request.
		 *
		 * @return WP_Error|WP_REST_Response
		 */
		public function get_item( $request ) {
			$params      = $request->get_params();
			$question_id = $params['id'];
			$data        = $this->get_question_data( $question_id );

			return new WP_REST_Response( $data, 200 );
		}

		/**
		 * Delete one item from the collection
		 *
		 * @since 2.5.8
		 *
		 * @param WP_REST_Request $request Full data about the request.
		 *
		 * @return WP_Error|WP_REST_Request
		 */
		public function delete_item( $request ) {
			$params          = $request->get_params();
			$question_id     = $params['id'];
			$question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true );
			$question_mapper = new \WpProQuiz_Model_QuestionMapper();

			if ( false !== $question_mapper->delete( $question_pro_id ) &&
				false !== wp_delete_post( $params['id'], false ) ) {
				return new WP_REST_Response( true, 200 );
			}

			return new WP_Error(
				'cant-delete',
				sprintf(
				// translators: placeholder: Question label.
					esc_html_x( 'Could not delete the %s.', 'placeholder: Question label', 'learndash' ),
					\LearnDash_Custom_Label::get_label( 'question' )
				),
				array( 'status' => 500 )
			);
		}

		/**
		 * Update one item from the collection
		 *
		 * @since 2.5.8
		 *
		 * @param WP_REST_Request $request Full data about the request.
		 *
		 * @return WP_Error|WP_REST_Request
		 */
		public function update_item( $request ) {
			global $learndash_question_types;

			$params          = $request->get_params();
			$question_id     = $params['id'];
			$question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true );
			$question_mapper = new \WpProQuiz_Model_QuestionMapper();

			$question_model = $question_mapper->fetch( $question_pro_id );

			// Update answer data if available.
			if ( isset( $params['_answerData'] ) && is_string( $params['_answerData'] ) ) {
				$params['_answerData'] = json_decode( $params['_answerData'], true );
			}

			// Also save points at question's post meta data.
			if ( isset( $params['_points'] ) ) {
				update_post_meta( $question_id, 'question_points', $params['_points'] );
			}

			if ( ( isset( $params['_answerType'] ) ) && ( ! empty( $params['_answerType'] ) ) ) {
				if ( isset( $learndash_question_types[ $params['_answerType'] ] ) ) {
					update_post_meta( $question_id, 'question_type', $params['_answerType'] );
				}
			}

			// Update question's post content.
			if ( isset( $params['_question'] ) ) {
				wp_update_post(
					array(
						'ID'           => $question_id,
						'post_content' => wp_slash( $params['_question'] ),
					)
				);
			}

			// Update the question object with new data.
			$question_model->set_array_to_object( $params );

			// Save the new data to database.
			$question_mapper->save( $question_model );

			return new WP_REST_Response( $this->get_question_data( $question_id ), 200 );
		}

		/**
		 * Get question data.
		 *
		 * @since 2.5.8
		 *
		 * @param int $question_id The question ID.
		 *
		 * @return object
		 */
		public function get_question_data( $question_id ) {
			// Get Answers from Question.
			$question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true );
			$question_mapper = new \WpProQuiz_Model_QuestionMapper();

			if ( ! empty( $question_pro_id ) ) {
				$question_model = $question_mapper->fetch( $question_pro_id );
			} else {
				$question_model = $question_mapper->fetch( null );
			}

			// Get data as array.
			$question_data = $question_model->get_object_as_array();

			$answer_data = array();

			// Get answer data.
			foreach ( $question_data['_answerData'] as $answer ) {
				$answer_data[] = $answer->get_object_as_array();
			}

			unset( $question_data['_answerData'] );

			$question_data['_answerData'] = $answer_data;

			// Generate output object.
			$data = array_merge(
				$question_data,
				array(
					'question_id'         => $question_id,
					'question_post_title' => get_the_title( $question_id ),
				)
			);

			return $data;
		}

		/**
		 * Gets the sfwd-question schema.
		 *
		 * @since 2.5.8
		 *
		 * @return array
		 */
		public function get_schema() {
			$schema = array(
				'$schema'    => 'http://json-schema.org/draft-04/schema#',
				'title'      => 'question',
				'type'       => 'object',
				'properties' => array(
					'_id'                             => array(
						'description' => __( 'Unique identifier for the object.', 'learndash' ),
						'type'        => 'integer',
						'context'     => array( 'view', 'edit', 'embed' ),
						'readonly'    => true,
					),
					'_quizId'                         => array(
						// translators: quiz, question.
						'description' => sprintf( esc_html_x( 'The ID of the %1$s associated with the %2$s.', 'placeholder: quiz, question', 'learndash' ), learndash_get_custom_label_lower( 'quiz' ), learndash_get_custom_label_lower( 'question' ) ),
						'type'        => 'integer',
						'context'     => array( 'view', 'edit', 'embed' ),
						'readonly'    => true,
					),
					'_sort'                           => array(
						// translators: question, quiz.
						'description' => sprintf( esc_html_x( 'The order of the %1$s in the %2$s', 'placeholder: question, quiz', 'learndash' ), learndash_get_custom_label_lower( 'question' ), learndash_get_custom_label_lower( 'quiz' ) ),
						'type'        => 'integer',
						'context'     => array( 'view', 'edit' ),
					),
					'_title'                          => array(
						'description' => __( 'The title for the object.', 'learndash' ),
						'type'        => 'string',
						'context'     => array( 'view', 'edit' ),
					),
					'_question'                       => array(
						// translators: question.
						'description' => sprintf( esc_html_x( 'The %s content', 'placeholder: question', 'learndash' ), learndash_get_custom_label_lower( 'question' ) ),
						'type'        => 'string',
						'context'     => array( 'view', 'edit' ),
					),
					'_correctMsg'                     => array(
						'description' => __( 'The message to show when the answer is correct.', 'learndash' ),
						'type'        => 'string',
						'context'     => array( 'view', 'edit' ),
					),
					'_incorrectMsg'                   => array(
						'description' => __( 'The message to show when the answer is incorrect.', 'learndash' ),
						'type'        => 'string',
						'context'     => array( 'view', 'edit' ),
					),
					'_correctSameText'                => array(
						'description' => __( 'Whether the incorrect and correct message are same.', 'learndash' ),
						'type'        => 'boolean',
						'context'     => array( 'view', 'edit' ),
					),
					'_tipEnabled'                     => array(
						'description' => __( 'The message to show when the answer is incorrect.', 'learndash' ),
						'type'        => 'boolean',
						'context'     => array( 'view', 'edit' ),
					),
					'_tipMsg'                         => array(
						'description' => __( 'The solution hint for the question.', 'learndash' ),
						'type'        => 'string',
						'context'     => array( 'view', 'edit' ),
					),
					'_points'                         => array(
						// translators: placeholder: question.
						'description' => sprintf( esc_html_x( 'The total number of points that can be obtained from the %s', 'placeholder: question', 'learndash' ), learndash_get_custom_label_lower( 'question' ) ),
						'type'        => 'boolean',
						'context'     => array( 'view', 'edit' ),
					),
					'_showPointsInBox'                => array(
						'description' => __( 'Whether to show points in box.', 'learndash' ),
						'type'        => 'boolean',
						'context'     => array( 'view', 'edit' ),
					),
					'_answerPointsActivated'          => array(
						'description' => __( 'Whether the individual points for the answers are activated.', 'learndash' ),
						'type'        => 'boolean',
						'context'     => array( 'view', 'edit' ),
					),
					'_answerType'                     => array(
						'description' => __( 'The type of the answer.', 'learndash' ),
						'type'        => 'string',
						'enum'        => array(
							'single',
							'multiple',
							'free_answer',
							'sort_answer',
							'matrix_sort_answer',
							'cloze_answer',
							'essay',
							'assessment_answer',
						),
						'context'     => array( 'view', 'edit' ),
					),
					'_answerPointsDiffModusActivated' => array(
						'description' => __( 'Whether the different points modus is activated.', 'learndash' ),
						'type'        => array( 'boolean', 'null' ),
						'context'     => array( 'view', 'edit' ),
					),
					'_disableCorrect'                 => array(
						'description' => __( 'Whether to distinguish between correct and incorrect when the different point modus is activated.', 'learndash' ),
						'type'        => 'boolean',
						'context'     => array( 'view', 'edit' ),
					),
					'_matrixSortAnswerCriteriaWidth'  => array(
						'description' => __( 'The percentage width of the criteria table column for matrix sort answer.', 'learndash' ),
						'type'        => 'integer',
						'context'     => array( 'view', 'edit' ),
					),
					'_answerData'                     => array(
						'description' => __( 'An array of answer data objects', 'learndash' ),
						'type'        => 'array',
						'context'     => array( 'view', 'edit' ),
						'properties'  => array(
							'_answer'             => array(
								'description' => __( 'The answer text.', 'learndash' ),
								'type'        => 'string',
								'context'     => array( 'view', 'edit' ),
							),
							'_html'               => array(
								'description' => __( 'Whether the HTML is allowed in the answer or not', 'learndash' ),
								'type'        => 'boolean',
								'context'     => array( 'view', 'edit' ),
							),
							'_points'             => array(
								'description' => __( 'The number of points that can be obtained from the answer.', 'learndash' ),
								'type'        => 'integer',
								'context'     => array( 'view', 'edit' ),
							),
							'_correct'            => array(
								'description' => __( 'Whether the answer is correct.', 'learndash' ),
								'type'        => 'boolean',
								'context'     => array( 'view', 'edit' ),
							),
							'_sortString'         => array(
								'description' => __( 'Sort string.', 'learndash' ),
								'type'        => 'string',
								'context'     => array( 'view', 'edit' ),
							),
							'_sortStringHtml'     => array(
								'description' => __( 'Whether to allow HTML in sort string.', 'learndash' ),
								'type'        => 'boolean',
								'context'     => array( 'view', 'edit' ),
							),
							'_graded'             => array(
								'description' => __( 'Whether the answer can be graded or not.', 'learndash' ),
								'type'        => 'boolean',
								'context'     => array( 'view', 'edit' ),
							),
							'_gradingProgression' => array(
								// translators: question, quiz.
								'description' => sprintf( esc_html_x( 'Determines how should the answer to this %1$s be marked and graded upon %2$s submission.', 'placeholder: question, quiz', 'learndash' ), learndash_get_custom_label_lower( 'question' ), learndash_get_custom_label_lower( 'quiz' ) ),
								'type'        => 'text',
								'context'     => array( 'view', 'edit' ),
								'enum'        => array( 'not-graded-none', 'not-graded-full', 'graded-full' ),
							),
							'_gradedType'         => array(
								'description' => __( 'Determines how a user can submit answer.', 'learndash' ),
								'type'        => 'boolean',
								'context'     => array( 'view', 'edit' ),
								'enum'        => array( 'text', 'upload' ),
							),
						),
					),
					'question_id'                     => array(
						// translators: question.
						'description' => sprintf( esc_html_x( 'The %s post ID.', 'placeholder: question', 'learndash' ), learndash_get_custom_label_lower( 'question' ) ),
						'type'        => 'integer',
						'context'     => array( 'view', 'edit', 'embed' ),
						'readonly'    => true,
					),
					'question_post_title'             => array(
						// translators: question.
						'description' => sprintf( esc_html_x( 'The %s post title.', 'placeholder: question', 'learndash' ), learndash_get_custom_label_lower( 'question' ) ),
						'type'        => 'string',
						'context'     => array( 'view', 'edit' ),
					),
				),
			);

			return $schema;
		}
	}
}