$value ) { $data[ $key ] = ( 'answer_title' === $key && is_scalar( $value ) ) ? self::kses_title( $value ) : self::kses_answer_titles( $value ); } } elseif ( is_object( $data ) ) { foreach ( get_object_vars( $data ) as $key => $value ) { $data->{$key} = ( 'answer_title' === $key && is_scalar( $value ) ) ? self::kses_title( $value ) : self::kses_answer_titles( $value ); } } return $data; } private static function build_given_answer( $question_type, $attempt_item ) { if ( 'imageAnswer' === $question_type ) { $response = array(); $answers = json_decode( $attempt_item->given_answer ?? '', true ); if ( ! is_array( $answers ) ) { return $response; } foreach ( $answers as $id => $answer ) { $quiz_answer = Classes\Query::get_quiz_answer( $id ); $image = wp_get_attachment_image_src( $quiz_answer->image_id ); $response[] = array( 'id' => $id, 'image_url' => $image[0], 'answer_title' => $answer, ); } return $response; } elseif ( 'fillInTheBlanks' === $question_type ) { $replacement = explode( ',', $attempt_item->given_answer ); return array( array( 'answer_title' => preg_replace_callback('/\{dash\}/', function ( $match ) use ( $replacement ) { static $index = 0; $value = '{' . trim( $replacement[ $index ] ) . '}'; $index++; return $value; }, $attempt_item->correct_answer), ) ); } elseif ( 'shortAnswer' === $question_type ) { return array( array( 'answer_title' => $attempt_item->given_answer, ) ); }//end if $answers = \AcademyQuizzes\Classes\Query::get_quiz_all_answer_title_by_ids( explode( ',', $attempt_item->given_answer ?? '' ) ); // convert image id to image url if ( empty( $answers ) ) { return []; } foreach ( $answers as $answer ) { if ( $answer->image_id ) { $image = wp_get_attachment_image_src( $answer->image_id ); $answer->image_url = $image[0]; } unset( $answer->image_id ); } return $answers; } public static function prepare_correct_answer( $question_type, $attempt_item ) { if ( 'imageAnswer' === $question_type ) { $image_answers = \AcademyQuizzes\Classes\Query::get_quiz_answers_by_question_id( $attempt_item->question_id, 'imageAnswer' ); $response = []; foreach ( $image_answers as $image_answer ) { $image = wp_get_attachment_image_src( $image_answer->image_id ); $response[] = array( 'id' => $image_answer->answer_id, 'image_url' => $image[0], 'answer_title' => $image_answer->answer_title, ); } return $response; } elseif ( 'fillInTheBlanks' === $question_type ) { $replacement = explode( '|', $attempt_item->answer_content ); return array( 'answer_title' => preg_replace_callback('/\{dash\}/', function ( $match ) use ( $replacement ) { static $index = 0; $value = '{' . trim( $replacement[ $index ] ) . '}'; $index++; return $value; }, $attempt_item->correct_answer), ); } elseif ( 'shortAnswer' === $question_type ) { return array( 'answer_title' => __( 'Manually Reviewed Required.', 'academy' ), ); }//end if $answers = \AcademyQuizzes\Classes\Query::get_quiz_correct_answers( $attempt_item->question_id, $question_type ); // convert image id to image url foreach ( $answers as $answer ) { if ( $answer->image_id ) { $image = wp_get_attachment_image_src( $answer->image_id ); $answer->image_url = $image[0]; } unset( $answer->image_id ); } return $answers; } public static function render_quiz_by_course_and_quiz_id( $course_id, $quiz_id ) { $has_permission = \Academy\Helper::has_permission_to_access_curriculum( $course_id ); if ( $has_permission ) { $question_order = get_post_meta( $quiz_id, 'academy_quiz_questions_order', true ); // A quiz whose Settings step was never saved has no order; the form // defaults to random, so do the same instead of failing to load. $question_order = $question_order ? $question_order : 'rand'; $questions = \AcademyQuizzes\Classes\Query::get_questions_by_quid_id( $quiz_id, $question_order ); $layout = get_post_meta( $quiz_id, 'academy_quiz_questions_layout', true ); $order = $question_order; if ( count( $questions ) && $order ) { do_action( 'academy_quizzes/frontend/before_render_quiz', $course_id, $quiz_id ); if ( 'all' === $layout ) { foreach ( $questions as &$question ) { $question->answers = \AcademyQuizzes\Classes\Query::get_quiz_answers_by_question_id( $question->question_id, $question->question_type ); } } $settings = \AcademyQuizzes\Classes\Query::get_question_settings_by_quiz_id( $quiz_id, $order ); return( [ 'questions' => $questions, 'settings' => $settings, 'title' => get_the_title( $quiz_id ), 'content' => get_post_field( 'post_content', $quiz_id ), ] ); } return( esc_html__( 'Sorry, something went wrong!', 'academy' ) ); }//end if return( esc_html__( 'Access Denied', 'academy' ) ); } public static function get_quiz_attempt_answer_details_by_attempt_id( $attempt_id ) { $user_id = get_current_user_id(); $prepare_attempt_details = []; $attempt_details = \AcademyQuizzes\Classes\Query::get_quiz_attempt_details( $attempt_id, $user_id ); $quiz_id = \AcademyQuizzes\Classes\Query::get_quiz_attempt( $attempt_id )->quiz_id; $is_enable_skip_question = get_post_meta( $quiz_id, 'academy_quiz_skip_question_showing', true ); $is_hide_see_more_button = (bool) get_post_meta( $quiz_id, 'academy_quiz_show_full_answer_content', true ); $is_explanation_enabled = (bool) get_post_meta( $quiz_id, 'academy_quiz_explanation_enabled', true ); if ( $is_enable_skip_question ) { $skip_questions = \AcademyQuizzes\Classes\Query::get_quiz_attempt_skip_questions( $attempt_id, $user_id, $quiz_id ); $attempt_details = array_merge( $attempt_details, $skip_questions ); } foreach ( $attempt_details as $attempt_item ) { $attempt_item->given_answer = self::prepare_given_answer( $attempt_item->question_type, $attempt_item ); $attempt_item->is_correct = (bool) $attempt_item->is_correct; $attempt_item->correct_answer = self::prepare_correct_answer( $attempt_item->question_type, $attempt_item ); $attempt_answer_id = $attempt_item->attempt_answer_id ? $attempt_item->attempt_answer_id : $attempt_item->question_id; $attempt_item->is_skipped_question = (bool) $attempt_item->attempt_answer_id ? false : true; $attempt_item->academy_quiz_show_full_answer_content = $is_hide_see_more_button; $attempt_item->academy_quiz_explanation_enabled = $is_explanation_enabled; $prepare_attempt_details[ $attempt_answer_id ] = $attempt_item; } return $prepare_attempt_details; } public static function process_the_fill_in_the_blanks_question_title( $question, $question_id, $attempt_id ) { // Filter the stored title before the inputs are substituted in. $question = wp_kses_post( (string) $question ); $total_dash = substr_count( $question, '{dash}' ); while ( $total_dash-- ) { $name = sprintf( 'attempt[%d][quiz_question][%d][fillInTheBlanks][%d]', (int) $attempt_id, (int) $question_id, (int) $total_dash ); $replace = ''; $question = preg_replace( '/\{dash\}/', $replace, $question, 1 ); } return $question; } /** * Sanitize client-supplied attempt_info: keys through sanitize_key(), * strings through sanitize_text_field(), numbers and booleans kept. * * @param mixed $info Attempt info. * @param int $depth Current depth. * @return array */ public static function sanitize_attempt_info( $info, $depth = 0 ) { if ( is_object( $info ) ) { $info = (array) $info; } if ( ! is_array( $info ) || $depth > 5 ) { return array(); } $clean = array(); foreach ( $info as $key => $value ) { $key = is_int( $key ) ? $key : sanitize_key( (string) $key ); if ( '' === $key ) { continue; } if ( is_array( $value ) || is_object( $value ) ) { $clean[ $key ] = self::sanitize_attempt_info( $value, $depth + 1 ); } elseif ( is_bool( $value ) || is_int( $value ) || is_float( $value ) || null === $value ) { $clean[ $key ] = $value; } else { $clean[ $key ] = sanitize_text_field( (string) $value ); } } return $clean; } /** * Clean a quiz question/answer title for storage or output. * * Entities are decoded FIRST and the result is then filtered with * wp_kses_post(), so an entity-encoded tag (e.g. "<img onerror>") * can never turn back into live markup after filtering. * * A rich-text question title is already HTML, so it is only filtered, * never decoded. * * @param string $title Stored or submitted title. * @param string $type 'plain' (default) or 'rich'. * @return string */ public static function kses_title( $title, $type = 'plain' ) { $title = (string) $title; if ( 'rich' !== $type ) { $title = html_entity_decode( $title, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } return wp_kses_post( $title ); } /** * Filter the titles of question rows read from the database before they * are sent to the browser, so rows stored by older versions are safe too. * * @param object[] $questions Question rows. * @return object[] */ public static function kses_question_rows( $questions ) { foreach ( (array) $questions as $question ) { if ( is_object( $question ) && isset( $question->question_title ) ) { $question->question_title = self::kses_title( $question->question_title, $question->question_title_type ?? 'plain' ); } } return $questions; } /** * Allowed HTML for a processed fill-in-the-blanks title: post HTML plus the * generated blank inputs. */ public static function fill_in_the_blanks_allowed_html() { $allowed = wp_kses_allowed_html( 'post' ); $allowed['input'] = array( 'type' => true, 'class' => true, 'name' => true, 'value' => true, ); return $allowed; } public static function get_successful_attempts_from_attempts( $attempts, $quiz_id ): array { $successful_attempts = []; foreach ( $attempts as $attempt ) { if ( 'pending' === $attempt->attempt_status && \AcademyQuizzes\Classes\Query::is_required_manually_reviewed( $quiz_id ) ) { // if short answer $successful_attempts[] = $attempt; } elseif ( 'pending' !== $attempt->attempt_status ) { // if correct or wrong $successful_attempts[] = $attempt; } } return $successful_attempts; } public static function get_questions_with_options_from_quiz_array( $quiz ) { $questions_with_options = []; foreach ( $quiz['questions'] as $question ) { $question_with_options = array( 'question' => $question, 'options' => \AcademyQuizzes\Classes\Query::get_quiz_answers_by_question_id( $question->question_id, $question->question_type ), ); $questions_with_options[] = $question_with_options; } return $questions_with_options; } public static function check_if_quiz_time_is_expired_by_quiz_id_and_attempt_id( $course_id, $quiz_id, $attempt_id ) { $quiz = self::render_quiz_by_course_and_quiz_id( $course_id, $quiz_id ); $attempt = \AcademyQuizzes\Classes\Query::get_quiz_attempt( $attempt_id ); $quiz_time = (int) ( $quiz['settings']['quiz_time'] ?? 0 ); // "No time limit" quizzes have quiz_time 0 and/or no unit set. Building a // DateInterval spec from that (e.g. "P0") throws, so bail out early instead. if ( ! $quiz_time ) { return false; } $unit = strtoupper( substr( (string) ( $quiz['settings']['quiz_time_unit'] ?? '' ), 0, 1 ) ); if ( '' === $unit ) { return false; } $current_time = new \DateTime(); $start_time = new \DateTime( $attempt->attempt_started_at ); $interval = ( 'H' === $unit || 'M' === $unit || 'S' === $unit ) ? "PT{$quiz_time}{$unit}" : "P{$quiz_time}{$unit}"; $end_time = $start_time->add( new \DateInterval( $interval ) ); return $end_time < $current_time; } public static function has_attempt_quiz( $course_id, $quiz_id, $user_id = '' ) { if ( empty( $user_id ) ) { $user_id = get_current_user_id(); } return \AcademyQuizzes\Classes\Query::get_quiz_attempt_details_by_quiz_id( array( 'course_id' => $course_id, 'quiz_id' => $quiz_id, 'user_id' => $user_id ) ); } }