Quiz::dozent_quiz_add_question()

Quiz Add Question


Description

See also


Source

File: classes/Quiz.php

	public function dozent_quiz_add_question() {
		global $wpdb;

		$question_type  = dozent_input_text( 'question_type' );
		$question_title = dozent_input_text( 'question_title' );
		$quiz_id        = dozent_input_text( 'quiz_id' );
		$score          = dozent_input_text( 'score' );
		$image_id       = dozent_input_text( 'image_id' );

		//For the question type true false
		$is_true = dozent_input_text( 'question_type_is_true' );

		if ( empty( $question_title ) ) {
			$error_text    = __( 'The question title field is required.', 'dozent' );
			$error_message = "<p class='dozent-text-danger'> <i class='dicon-notification'></i> {$error_text} </p>";

			wp_send_json_error( [ 'message' => $error_message ] );
		}

		$user_id = get_current_user_id();
		$question_order
		         = $wpdb->get_var( "SELECT MAX(sort_order) FROM {$wpdb->dozent_quiz_questions} WHERE quiz_id = {$quiz_id} " );
		$question_order ++;

		$data = apply_filters( 'dozent/quiz/add_question/data', [
			'user_id'       => $user_id,
			'quiz_id'       => $quiz_id,
			'title'         => $question_title,
			'image_id'      => $image_id,
			'question_type' => $question_type,
			'score'         => $score,
			'sort_order'    => $question_order,
		] );

		if ( $question_type === 'true_false' ) {
			$data['mixed_value'] = $is_true;
		}

		do_action( 'dozent/quiz/add_question/before', $data );

		$wpdb->insert( $wpdb->dozent_quiz_questions, $data );
		$question_id = (int) $wpdb->insert_id;

		$question_options = (array) dozent_input_array_field( 'options' );
		$question_options = dozent_array_except( $question_options, '{index}' );

		if ( dozent_count( $question_options ) ) {
			$sort = 0;
			foreach ( $question_options as $option ) {
				$sort ++;

				$optionData = [
					'question_id'        => $question_id,
					'title'              => dozent_array_get( 'title', $option ),
					'image_id'           => dozent_array_get( 'image_id', $option ),
					'display_preference' => dozent_array_get( 'd_pref', $option ),
					'is_correct'         => (int) dozent_array_get( 'is_correct', $option ),
					'sort_order'         => $sort,
				];

				$wpdb->insert( $wpdb->dozent_quiz_question_options, $optionData );
			}
		}

		do_action( 'dozent/quiz/add_question/after', $data, $question_id );

		ob_start();
		include DOZENT_ABSPATH . 'views/course_builder/quiz_questions.php';
		$questions_html = ob_get_clean();

		wp_send_json_success( [ 'questions_html' => $questions_html ] );
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.