<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       http://ays-pro.com/
 * @since      1.0.0
 *
 * @package    Quiz_Maker
 * @subpackage Quiz_Maker/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Quiz_Maker
 * @subpackage Quiz_Maker/public
 * @author     AYS Pro LLC <info@ays-pro.com>
 */
class Quiz_Maker_Public
{

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $plugin_name The ID of this plugin.
     */
    protected $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $version The current version of this plugin.
     */
    protected $version;

    protected $settings;
    
    protected $default_texts;
    protected $buttons_texts;
    protected $fields_placeholders;
    protected $is_training;
    protected $category_selective;
    protected $title;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string $plugin_name The name of the plugin.
     * @param      string $version The version of this plugin.
     */
    public function __construct($plugin_name, $version){
        $this->plugin_name = $plugin_name;
        $this->version = $version;
        $this->settings = new Quiz_Maker_Settings_Actions($this->plugin_name);
        
        add_shortcode('ays_quiz', array($this, 'ays_generate_quiz_method'));
    }

    /**
     * Register the stylesheets for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function enqueue_styles(){

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Quiz_Maker_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Quiz_Maker_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */
        wp_enqueue_style($this->plugin_name.'-font-awesome', plugin_dir_url(__FILE__) . 'css/quiz-maker-font-awesome.min.css', array(), $this->version, 'all');
        wp_enqueue_style($this->plugin_name.'-sweetalert-css', plugin_dir_url(__FILE__) . 'css/quiz-maker-sweetalert2.min.css', array(), $this->version, 'all' );
        wp_enqueue_style($this->plugin_name.'-animate', plugin_dir_url(__FILE__) . 'css/animate.css', array(), $this->version, 'all');
        wp_enqueue_style($this->plugin_name.'-animations', plugin_dir_url(__FILE__) . 'css/animations.css', array(), $this->version, 'all');
        wp_enqueue_style($this->plugin_name.'-rating', plugin_dir_url(__FILE__) . 'css/rating.min.css', array(), $this->version, 'all');
        wp_enqueue_style($this->plugin_name.'-select2', plugin_dir_url(__FILE__) . 'css/quiz-maker-select2.min.css', array(), $this->version, 'all');
        wp_enqueue_style($this->plugin_name.'-loaders', plugin_dir_url(__FILE__) . 'css/loaders.css', array(), $this->version, 'all');
        wp_enqueue_style($this->plugin_name . '-dataTable-min', plugin_dir_url(__FILE__) . 'css/quiz-maker-dataTables.min.css', array(), $this->version, 'all');

    }

    public function enqueue_styles_early(){

        $settings_options = $this->settings->ays_get_setting('options');
        if($settings_options){
            $settings_options = json_decode(stripcslashes($settings_options), true);
        }else{
            $settings_options = array();
        }

        // General CSS File
        $settings_options['quiz_exclude_general_css'] = isset($settings_options['quiz_exclude_general_css']) ? esc_attr( $settings_options['quiz_exclude_general_css'] ) : 'off';
        $quiz_exclude_general_css = (isset($settings_options['quiz_exclude_general_css']) && esc_attr( $settings_options['quiz_exclude_general_css'] ) == "on") ? true : false;

        if ( ! $quiz_exclude_general_css ) {
            wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/quiz-maker-public.css', array(), $this->version, 'all');
        }else {
            if ( ! is_front_page() ) {
                wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/quiz-maker-public.css', array(), $this->version, 'all');
            }

        }

        $is_elementor_exists = $this->ays_quiz_is_elementor();
        if ( $is_elementor_exists ) {
            wp_enqueue_style($this->plugin_name.'-font-awesome', plugin_dir_url(__FILE__) . 'css/quiz-maker-font-awesome.min.css', array(), $this->version, 'all');
        }

    }

    /**
     * Register the JavaScript for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts(){

        $is_elementor_exists = $this->ays_quiz_is_elementor();

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Quiz_Maker_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Quiz_Maker_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */
        if ( ! $is_elementor_exists ) {
            wp_enqueue_script("jquery-effects-core");
            wp_enqueue_script($this->plugin_name .'-select2js', plugin_dir_url(__FILE__) . 'js/quiz-maker-select2.min.js', array('jquery'), $this->version, true);
            wp_enqueue_script($this->plugin_name .'-sweetalert-js', plugin_dir_url(__FILE__) . 'js/quiz-maker-sweetalert2.all.min.js', array('jquery'), $this->version, true );
            wp_enqueue_script($this->plugin_name .'-rate-quiz', plugin_dir_url(__FILE__) . 'js/rating.min.js', array('jquery'), $this->version, true);


            wp_enqueue_script( $this->plugin_name . '-datatable-min', plugin_dir_url(__FILE__) . 'js/quiz-maker-datatable.min.js', array('jquery'), $this->version, true);

            wp_localize_script( $this->plugin_name . '-datatable-min', 'quizLangDataTableObj', array(
                "sEmptyTable"               => __( "No data available in table", 'quiz-maker' ),
                "sInfo"                     => __( "Showing _START_ to _END_ of _TOTAL_ entries", 'quiz-maker' ),
                "sInfoEmpty"                => __( "Showing 0 to 0 of 0 entries", 'quiz-maker' ),
                "sInfoFiltered"             => __( "(filtered from _MAX_ total entries)", 'quiz-maker' ),
                // "sInfoPostFix":              => __( "", 'quiz-maker' ),
                // "sInfoThousands":            => __( ",", 'quiz-maker' ),
                "sLengthMenu"               => __( "Show _MENU_ entries", 'quiz-maker' ),
                "sLoadingRecords"           => __( "Loading...", 'quiz-maker' ),
                "sProcessing"               => __( "Processing...", 'quiz-maker' ),
                "sSearch"                   => __( "Search:", 'quiz-maker' ),
                // "sUrl":                      => __( "", 'quiz-maker' ),
                "sZeroRecords"              => __( "No matching records found", 'quiz-maker' ),
                "sFirst"                    => __( "First", 'quiz-maker' ),
                "sLast"                     => __( "Last", 'quiz-maker' ),
                "sNext"                     => __( "Next", 'quiz-maker' ),
                "sPrevious"                 => __( "Previous", 'quiz-maker' ),
                "sSortAscending"            => __( ": activate to sort column ascending", 'quiz-maker' ),
                "sSortDescending"           => __( ": activate to sort column descending", 'quiz-maker' ),
            ) );

            wp_enqueue_script($this->plugin_name .'-functions.js', plugin_dir_url(__FILE__) . 'js/quiz-maker-functions.js', array('jquery'), $this->version, true);
            wp_enqueue_script($this->plugin_name .'-ajax-public', plugin_dir_url(__FILE__) . 'js/quiz-maker-public-ajax.js', array('jquery'), time(), true);
            wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/quiz-maker-public.js', array('jquery'), time(), true);
            wp_localize_script($this->plugin_name . '-ajax-public', 'quiz_maker_ajax_public', array(
                'nonce'         => wp_create_nonce('ays_quiz_nonce'),
                'ajax_url'      => admin_url('admin-ajax.php'),
                'warningIcon'   => plugin_dir_url(__FILE__) . "images/warning.svg",
                'checkingError' => __( 'Error checking answer', 'quiz-maker' ),
            ));
            wp_localize_script($this->plugin_name, 'quizLangObj', array(
                'notAnsweredText'               => $this->default_texts['notAnsweredQuestionText'],
                'areYouSure'                    => $this->default_texts['finishQuizText'],
                'selectPlaceholder'             => $this->default_texts['selectAnswerText'],
                'correctAnswerVariants'         => __( 'Variants of the correct answer', 'quiz-maker' ),
                'shareDialog'                   => __( 'Share Dialog', 'quiz-maker' ),
                'expiredMessage'                => __( 'The quiz has expired!', 'quiz-maker' ),
                'day'                           => __( 'day', 'quiz-maker' ),
                'days'                          => __( 'days', 'quiz-maker' ),
                'hour'                          => __( 'hour', 'quiz-maker' ),
                'hours'                         => __( 'hours', 'quiz-maker' ),
                'minute'                        => __( 'minute', 'quiz-maker' ),
                'minutes'                       => __( 'minutes', 'quiz-maker' ),
                'second'                        => __( 'second', 'quiz-maker' ),
                'seconds'                       => __( 'seconds', 'quiz-maker' ),
                'startButtonText'               => $this->buttons_texts['startButton'],
                'defaultStartButtonText'        => __( 'Start', 'quiz-maker' ),
                'loadResource'                  => __( "Can't load resource.", 'quiz-maker' ),
                'somethingWentWrong'            => __( "Maybe something went wrong.", 'quiz-maker' ),
                'passwordIsWrong'               => $this->default_texts['wrongPassword'],
                'requiredError'                 => __( 'This is a required question', 'quiz-maker' ),
                'show'                          => __( 'Show', 'quiz-maker' ),
                'hide'                          => __( 'Hide', 'quiz-maker' ),
                'emptyReportMessage'            => __( 'You cannot submit an empty report. Please add some details.', 'quiz-maker' ),
                'reportSentMessage'             => $this->default_texts['questionReportSubmittedText'],
            ) );
        }
    }

    public function ays_generate_quiz_method($attr){
        $id = (isset($attr['id']) && $attr['id'] != '') ? absint(intval($attr['id'])) : null;

        $is_training = isset($attr['training']) && sanitize_text_field($attr['training']) === 'true' ? true : ' ';
        $category_selective = (isset($attr['category_selective']) && sanitize_text_field($attr['category_selective']) === 'true') ? true : ' ';
        $title = isset($attr['title']) ? sanitize_text_field($attr['title']) : ' ';

        $this->default_texts = self::ays_set_quiz_default_texts( $this->plugin_name, array() );

        $this->set_prop( 'is_training', $is_training );
        $this->set_prop( 'category_selective', $category_selective );
        $this->set_prop( 'title', $title );

        if (is_null($id)) {
            $quiz_content = "<p class='wrong_shortcode_text' style='color:red;'>" . $this->default_texts['wrongShortcode'] . "</p>";
            return str_replace(array("\r\n", "\n", "\r"), "\n", $quiz_content);
        }

        $this->buttons_texts = $this->ays_set_quiz_texts($id);
        
        $this->enqueue_styles();
        $this->enqueue_scripts();
        
        $quiz_content = $this->show_quiz($id);
        return str_replace(array("\r\n", "\n", "\r"), "\n", $quiz_content);
    }
    
    public function ays_set_quiz_texts($id){

        /*
         * Get Quiz buttons texts from database
         */

        $quiz = $this->get_quiz_by_id($id);

        $options = ( isset($quiz['options']) && ( json_decode($quiz['options'], true) != null ) ) ? json_decode($quiz['options'], true) : array();

        // Enable custom texts for buttons
        $options['quiz_enable_custom_texts_for_buttons'] = isset($options['quiz_enable_custom_texts_for_buttons']) ? sanitize_text_field($options['quiz_enable_custom_texts_for_buttons']) : 'off';
        $quiz_enable_custom_texts_for_buttons = (isset($options['quiz_enable_custom_texts_for_buttons']) && $options['quiz_enable_custom_texts_for_buttons'] == 'on') ? true : false;

        $settings_buttons_texts = $this->settings->ays_get_setting('buttons_texts');
        if($settings_buttons_texts){
            $settings_buttons_texts = json_decode($settings_buttons_texts, true);
        }else{
            $settings_buttons_texts = array();
        }

        $ays_start_button           = (isset($settings_buttons_texts['start_button']) && $settings_buttons_texts['start_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['start_button'] ) ) : 'Start';
        $ays_next_button            = (isset($settings_buttons_texts['next_button']) && $settings_buttons_texts['next_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['next_button'] ) ) : 'Next';
        $ays_previous_button        = (isset($settings_buttons_texts['previous_button']) && $settings_buttons_texts['previous_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['previous_button'] ) ) : 'Prev';
        $ays_clear_button           = (isset($settings_buttons_texts['clear_button']) && $settings_buttons_texts['clear_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['clear_button'] ) ) : 'Clear';
        $ays_finish_button          = (isset($settings_buttons_texts['finish_button']) && $settings_buttons_texts['finish_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['finish_button'] ) ) : 'Finish';
        $ays_see_result_button      = (isset($settings_buttons_texts['see_result_button']) && $settings_buttons_texts['see_result_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['see_result_button'] ) ) : 'See Result';
        $ays_restart_quiz_button    = (isset($settings_buttons_texts['restart_quiz_button']) && $settings_buttons_texts['restart_quiz_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['restart_quiz_button'] ) ) : 'Restart quiz';
        $ays_send_feedback_button   = (isset($settings_buttons_texts['send_feedback_button']) && $settings_buttons_texts['send_feedback_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['send_feedback_button'] ) ) : 'Send feedback';
        $ays_load_more_button       = (isset($settings_buttons_texts['load_more_button']) && $settings_buttons_texts['load_more_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['load_more_button'] ) ) : 'Load more';
        $ays_exit_button            = (isset($settings_buttons_texts['exit_button']) && $settings_buttons_texts['exit_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['exit_button'] ) ) : 'Exit';
        $ays_check_button           = (isset($settings_buttons_texts['check_button']) && $settings_buttons_texts['check_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['check_button'] ) ) : 'Check';
        $ays_login_button           = (isset($settings_buttons_texts['login_button']) && $settings_buttons_texts['login_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['login_button'] ) ) : 'Log In';
        $ays_report_submit_button = (isset($settings_buttons_texts['report_submit_button']) && $settings_buttons_texts['report_submit_button'] != '') ? stripslashes( esc_attr( $settings_buttons_texts['report_submit_button'] ) ) : 'Submit';

        //////////////////////////
        //////////////////////////
        //////////////////////////

        // New Start button
        $quiz_custom_texts_start_button         = (isset($options['quiz_custom_texts_start_button']) && $options['quiz_custom_texts_start_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_start_button'] ) ) : $ays_start_button;
        $quiz_custom_texts_next_button          = (isset($options['quiz_custom_texts_next_button']) && $options['quiz_custom_texts_next_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_next_button'] ) ) : $ays_next_button;
        $quiz_custom_texts_prev_button          = (isset($options['quiz_custom_texts_prev_button']) && $options['quiz_custom_texts_prev_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_prev_button'] ) ) : $ays_previous_button;
        $quiz_custom_texts_clear_button         = (isset($options['quiz_custom_texts_clear_button']) && $options['quiz_custom_texts_clear_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_clear_button'] ) ) : $ays_clear_button;
        $quiz_custom_texts_finish_button        = (isset($options['quiz_custom_texts_finish_button']) && $options['quiz_custom_texts_finish_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_finish_button'] ) ) : $ays_finish_button;
        $quiz_custom_texts_see_results_button   = (isset($options['quiz_custom_texts_see_results_button']) && $options['quiz_custom_texts_see_results_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_see_results_button'] ) ) : $ays_see_result_button;
        $quiz_custom_texts_restart_quiz_button  = (isset($options['quiz_custom_texts_restart_quiz_button']) && $options['quiz_custom_texts_restart_quiz_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_restart_quiz_button'] ) ) : $ays_restart_quiz_button;
        $quiz_custom_texts_send_feedback_button = (isset($options['quiz_custom_texts_send_feedback_button']) && $options['quiz_custom_texts_send_feedback_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_send_feedback_button'] ) ) : $ays_send_feedback_button;
        $quiz_custom_texts_load_more_button     = (isset($options['quiz_custom_texts_load_more_button']) && $options['quiz_custom_texts_load_more_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_load_more_button'] ) ) : $ays_load_more_button;
        $quiz_custom_texts_exit_button          = (isset($options['quiz_custom_texts_exit_button']) && $options['quiz_custom_texts_exit_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_exit_button'] ) ) : $ays_exit_button;
        $quiz_custom_texts_check_button         = (isset($options['quiz_custom_texts_check_button']) && $options['quiz_custom_texts_check_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_check_button'] ) ) : $ays_check_button;
        $quiz_custom_texts_login_button         = (isset($options['quiz_custom_texts_login_button']) && $options['quiz_custom_texts_login_button'] != '') ? stripslashes( esc_attr( $options['quiz_custom_texts_login_button'] ) ) : $ays_login_button;

        if ($ays_start_button === 'Start') {
            $ays_start_button_text = __('Start', 'quiz-maker');
        }else{
            $ays_start_button_text = $ays_start_button;
        }

        if ($ays_next_button === 'Next') {
            $ays_next_button_text = __('Next', 'quiz-maker');
        }else{
            $ays_next_button_text = $ays_next_button;
        }

        if ($ays_previous_button === 'Prev') {
            $ays_previous_button_text = __('Prev', 'quiz-maker');
        }else{
            $ays_previous_button_text = $ays_previous_button;
        }

        if ($ays_clear_button === 'Clear') {
            $ays_clear_button_text = __('Clear', 'quiz-maker');
        }else{
            $ays_clear_button_text = $ays_clear_button;
        }
        
        if ($ays_finish_button === 'Finish') {
            $ays_finish_button_text = __('Finish', 'quiz-maker');
        }else{
            $ays_finish_button_text = $ays_finish_button;
        }

        if ($ays_see_result_button === 'See Result') {
            $ays_see_result_button_text = __('See Result', 'quiz-maker');
        }else{
            $ays_see_result_button_text = $ays_see_result_button;
        }

        if ($ays_restart_quiz_button === 'Restart quiz') {
            $ays_restart_quiz_button_text = __('Restart quiz', 'quiz-maker');
        }else{
            $ays_restart_quiz_button_text = $ays_restart_quiz_button;
        }

        if ($ays_send_feedback_button === 'Send feedback') {
            $ays_send_feedback_button_text = __('Send feedback', 'quiz-maker');
        }else{
            $ays_send_feedback_button_text = $ays_send_feedback_button;
        }

        if ($ays_load_more_button === 'Load more') {
            $ays_load_more_button_text = __('Load more', 'quiz-maker');
        }else{
            $ays_load_more_button_text = $ays_load_more_button;
        }

        if ($ays_exit_button === 'Exit') {
            $ays_exit_button_text = __('Exit', 'quiz-maker');
        }else{
            $ays_exit_button_text = $ays_exit_button;
        }

        if ($ays_check_button === 'Check') {
            $ays_check_button_text = __('Check', 'quiz-maker');
        }else{
            $ays_check_button_text = $ays_check_button;
        }

        if ($ays_login_button === 'Log In') {
            $ays_login_button_text = __('Log In', 'quiz-maker');
        }else{
            $ays_login_button_text = $ays_login_button;
        }

        if ($ays_report_submit_button === 'Submit') {
            $ays_report_submit_button_text = __('Submit', 'quiz-maker');
        }else{
            $ays_report_submit_button_text = $ays_report_submit_button;
        }

        ////////////////////////////////////////////
        ////////////////////////////////////////////
        ////////////////////////////////////////////

        if( $quiz_enable_custom_texts_for_buttons ){

            if ($quiz_custom_texts_start_button === 'Start') {
                $ays_start_button_text = __('Start', 'quiz-maker');
            }else{
                $ays_start_button_text = $quiz_custom_texts_start_button;
            }

            if ($quiz_custom_texts_next_button === 'Next') {
                $ays_next_button_text = __('Next', 'quiz-maker');
            }else{
                $ays_next_button_text = $quiz_custom_texts_next_button;
            }

            if ($quiz_custom_texts_prev_button === 'Prev') {
                $ays_previous_button_text = __('Prev', 'quiz-maker');
            }else{
                $ays_previous_button_text = $quiz_custom_texts_prev_button;
            }

            if ($quiz_custom_texts_clear_button === 'Clear') {
                $ays_clear_button_text = __('Clear', 'quiz-maker');
            }else{
                $ays_clear_button_text = $quiz_custom_texts_clear_button;
            }

            if ($quiz_custom_texts_finish_button === 'Finish') {
                $ays_finish_button_text = __('Finish', 'quiz-maker');
            }else{
                $ays_finish_button_text = $quiz_custom_texts_finish_button;
            }

            if ($quiz_custom_texts_see_results_button === 'See Result') {
                $ays_see_result_button_text = __('See Result', 'quiz-maker');
            }else{
                $ays_see_result_button_text = $quiz_custom_texts_see_results_button;
            }

            if ($quiz_custom_texts_restart_quiz_button === 'Restart quiz') {
                $ays_restart_quiz_button_text = __('Restart quiz', 'quiz-maker');
            }else{
                $ays_restart_quiz_button_text = $quiz_custom_texts_restart_quiz_button;
            }

            if ($quiz_custom_texts_send_feedback_button === 'Send feedback') {
                $ays_send_feedback_button_text = __('Send feedback', 'quiz-maker');
            }else{
                $ays_send_feedback_button_text = $quiz_custom_texts_send_feedback_button;
            }

            if ($quiz_custom_texts_load_more_button === 'Load more') {
                $ays_load_more_button_text = __('Load more', 'quiz-maker');
            }else{
                $ays_load_more_button_text = $quiz_custom_texts_load_more_button;
            }

            if ($quiz_custom_texts_exit_button === 'Exit') {
                $ays_exit_button_text = __('Exit', 'quiz-maker');
            }else{
                $ays_exit_button_text = $quiz_custom_texts_exit_button;
            }

            if ($quiz_custom_texts_check_button === 'Check') {
                $ays_check_button_text = __('Check', 'quiz-maker');
            }else{
                $ays_check_button_text = $quiz_custom_texts_check_button;
            }

            if ($quiz_custom_texts_login_button === 'Log In') {
                $ays_login_button_text = __('Log In', 'quiz-maker');
            }else{
                $ays_login_button_text = $quiz_custom_texts_login_button;
            }
        }

        $texts = array(
            'startButton'           => $ays_start_button_text,
            'nextButton'            => $ays_next_button_text,
            'previousButton'        => $ays_previous_button_text,
            'clearButton'           => $ays_clear_button_text,
            'finishButton'          => $ays_finish_button_text,
            'seeResultButton'       => $ays_see_result_button_text,
            'restartQuizButton'     => $ays_restart_quiz_button_text,
            'sendFeedbackButton'    => $ays_send_feedback_button_text,
            'loadMoreButton'        => $ays_load_more_button_text,
            'exitButton'            => $ays_exit_button_text,
            'checkButton'           => $ays_check_button_text,
            'loginButton'           => $ays_login_button_text,
            'reportSubmitButton'    => $ays_report_submit_button_text,
        );
        return $texts;
    }

    public function ays_set_quiz_fields_placeholders_texts(){

        /*
         * Get Quiz fields placeholders from database
         */

        $settings_placeholders_texts = $this->settings->ays_get_setting('fields_placeholders');
        if($settings_placeholders_texts){
            $settings_placeholders_texts = json_decode($settings_placeholders_texts, true);
        }else{
            $settings_placeholders_texts = array();
        }

        $quiz_fields_placeholder_name  = (isset($settings_placeholders_texts['quiz_fields_placeholder_name']) && $settings_placeholders_texts['quiz_fields_placeholder_name'] != '') ? stripslashes( esc_attr( $settings_placeholders_texts['quiz_fields_placeholder_name'] ) ) : 'Name';

        $quiz_fields_placeholder_eamil = (isset($settings_placeholders_texts['quiz_fields_placeholder_eamil']) && $settings_placeholders_texts['quiz_fields_placeholder_eamil'] != '') ? stripslashes( esc_attr( $settings_placeholders_texts['quiz_fields_placeholder_eamil'] ) ) : 'Email';

        $quiz_fields_placeholder_phone = (isset($settings_placeholders_texts['quiz_fields_placeholder_phone']) && $settings_placeholders_texts['quiz_fields_placeholder_phone'] != '') ? stripslashes( esc_attr( $settings_placeholders_texts['quiz_fields_placeholder_phone'] ) ) : 'Phone Number';

        $quiz_fields_label_name  = (isset($settings_placeholders_texts['quiz_fields_label_name']) && $settings_placeholders_texts['quiz_fields_label_name'] != '') ? stripslashes( esc_attr( $settings_placeholders_texts['quiz_fields_label_name'] ) ) : 'Name';

        $quiz_fields_label_eamil = (isset($settings_placeholders_texts['quiz_fields_label_eamil']) && $settings_placeholders_texts['quiz_fields_label_eamil'] != '') ? stripslashes( esc_attr( $settings_placeholders_texts['quiz_fields_label_eamil'] ) ) : 'Email';

        $quiz_fields_label_phone = (isset($settings_placeholders_texts['quiz_fields_label_phone']) && $settings_placeholders_texts['quiz_fields_label_phone'] != '') ? stripslashes( esc_attr( $settings_placeholders_texts['quiz_fields_label_phone'] ) ) : 'Phone Number';

        if ($quiz_fields_placeholder_name === 'Name') {
            $quiz_fields_placeholder_name_text = __('Name', 'quiz-maker');
        }else{
            $quiz_fields_placeholder_name_text = $quiz_fields_placeholder_name;
        }

        if ($quiz_fields_placeholder_eamil === 'Email') {
            $quiz_fields_placeholder_eamil_text = __('Email', 'quiz-maker');
        }else{
            $quiz_fields_placeholder_eamil_text = $quiz_fields_placeholder_eamil;
        }

        if ($quiz_fields_placeholder_phone === 'Phone Number') {
            $quiz_fields_placeholder_phone_text = __('Phone Number', 'quiz-maker');
        }else{
            $quiz_fields_placeholder_phone_text = $quiz_fields_placeholder_phone;
        }

        if ($quiz_fields_label_name === 'Name') {
            $quiz_fields_label_name_text = __('Name', 'quiz-maker');
        }else{
            $quiz_fields_label_name_text = $quiz_fields_label_name;
        }

        if ($quiz_fields_label_eamil === 'Email') {
            $quiz_fields_label_eamil_text = __('Email', 'quiz-maker');
        }else{
            $quiz_fields_label_eamil_text = $quiz_fields_label_eamil;
        }

        if ($quiz_fields_label_phone === 'Phone Number') {
            $quiz_fields_label_phone_text = __('Phone Number', 'quiz-maker');
        }else{
            $quiz_fields_label_phone_text = $quiz_fields_label_phone;
        }

        $texts = array(
            'namePlaceholder'       => $quiz_fields_placeholder_name_text,
            'emailPlaceholder'      => $quiz_fields_placeholder_eamil_text,
            'phonePlaceholder'      => $quiz_fields_placeholder_phone_text,
            'nameLabel'             => $quiz_fields_label_name_text,
            'emailLabel'            => $quiz_fields_label_eamil_text,
            'phoneLabel'            => $quiz_fields_label_phone_text,
        );

        return $texts;
    }

    public function ays_set_quiz_message_variables_data( $id, $quiz ){

        /*
         * Quiz message variables for Start Page
         */

        // Quiz options 
        $options = ( json_decode($quiz['options'], true) != null ) ? json_decode($quiz['options'], true) : array();


        // General Setting's Options
        $quiz_settings = $this->settings;
        $general_settings_options = ($quiz_settings->ays_get_setting('options') === false) ? json_encode(array()) : $quiz_settings->ays_get_setting('options');
        $settings_options = json_decode(stripcslashes($general_settings_options), true);

        // Do not store IP adressess 
        $disable_user_ip = (isset($settings_options['disable_user_ip']) && $settings_options['disable_user_ip'] == 'on') ? true : false;


        // Quiz Timer
        $quiz_timer = (isset( $options['timer'] ) && intval($options['timer']) != 0 && $options['timer'] != "") ? absint( sanitize_text_field( $options['timer'] ) ) : 100;

        // Quiz title
        $quiz_title = (isset( $quiz['title'] ) && $quiz['title'] != "") ? stripslashes($quiz['title']) : "";

        // Quiz create date
        $quiz_creation_date = (isset($options['create_date']) && $options['create_date'] != '') ? sanitize_text_field( $options['create_date'] ) : "";
        if( $quiz_creation_date != "" ){
            $quiz_creation_date = date_i18n( get_option( 'date_format' ), strtotime( $quiz_creation_date ) );
        }

        // Quiz Author ID
        if ( isset( $options['author'] ) && is_string($options['author']) ) {
            $quiz_current_author_data = (isset( $options['author'] ) && $options['author'] != '') ? json_decode($options['author'], true) : array();
        } else {
            $options_author = isset( $options['author'] ) ? (array)$options['author'] : array();
            $quiz_current_author_data = (is_array( $options_author ) && empty( $options_author )) ? $options_author : array();
        }

        if($disable_user_ip){
            $user_ip = '';
        }else{
            $user_ip = $this->get_user_ip();
        }

        $current_user_ip = $user_ip;

        $question_ids = $this->get_quiz_questions_count($id);

        $questions_count = 0;
        if ( ! empty( $question_ids ) ) {
            $questions_count = count($question_ids);
        }

        // WP home page url
        $home_main_url = home_url();
        $wp_home_page_url = '<a href="'.$home_main_url.'" target="_blank">'.$home_main_url.'</a>';

        $user_first_name        = '';
        $user_last_name         = '';
        $user_nickname          = '';
        $user_display_name      = '';
        $user_wordpress_email   = '';
        $user_wordpress_roles   = '';
        $user_id = get_current_user_id();
        if($user_id != 0){
            $usermeta = get_user_meta( $user_id );
            if($usermeta !== null){
                $user_first_name = (isset($usermeta['first_name'][0]) && sanitize_text_field( $usermeta['first_name'][0] != '') ) ? sanitize_text_field( $usermeta['first_name'][0] ) : '';
                $user_last_name  = (isset($usermeta['last_name'][0]) && sanitize_text_field( $usermeta['last_name'][0] != '') ) ? sanitize_text_field( $usermeta['last_name'][0] ) : '';
                $user_nickname   = (isset($usermeta['nickname'][0]) && sanitize_text_field( $usermeta['nickname'][0] != '') ) ? sanitize_text_field( $usermeta['nickname'][0] ) : '';
            }

            $current_user_data = get_userdata( $user_id );
            if ( ! is_null( $current_user_data ) && $current_user_data ) {
                $user_display_name = ( isset( $current_user_data->data->display_name ) && $current_user_data->data->display_name != '' ) ? sanitize_text_field( $current_user_data->data->display_name ) : "";

                $user_wordpress_email = ( isset( $current_user_data->data->user_email ) && $current_user_data->data->user_email != '' ) ? sanitize_text_field( $current_user_data->data->user_email ) : "";

                $user_wordpress_roles = ( isset( $current_user_data->roles ) && ! empty( $current_user_data->roles ) ) ? $current_user_data->roles : "";

                if ( !empty( $user_wordpress_roles ) && $user_wordpress_roles != "" ) {
                    if ( is_array( $user_wordpress_roles ) ) {
                        $user_wordpress_roles = implode(",", $user_wordpress_roles);
                    }
                }
            }
        }

        $current_quiz_author = __( "Unknown", 'quiz-maker' );
        $current_quiz_author_email = "";
        $current_quiz_author_nickname = "";
        $current_quiz_author_website_url = "";
        
        $super_admin_email = get_option('admin_email');

        if( !empty($quiz_current_author_data) ){
            if( !is_array($quiz_current_author_data) ){
                $quiz_current_author_data = json_decode($quiz_current_author_data, true);
            }

            $quiz_current_author = (isset($quiz_current_author_data['id']) && $quiz_current_author_data['id'] != "") ? absint(sanitize_text_field( $quiz_current_author_data['id'] )) : "";

            $current_quiz_user_data = get_userdata( $quiz_current_author );
            if ( ! is_null( $current_quiz_user_data ) && $current_quiz_user_data ) {
                $current_quiz_author            = ( isset( $current_quiz_user_data->data->display_name ) && $current_quiz_user_data->data->display_name != '' ) ? sanitize_text_field( $current_quiz_user_data->data->display_name ) : "";
                $current_quiz_author_email      = ( isset( $current_quiz_user_data->data->user_email ) && $current_quiz_user_data->data->user_email != '' ) ? sanitize_text_field( $current_quiz_user_data->data->user_email ) : "";
                $current_quiz_author_nickname   = ( isset( $current_quiz_user_data->data->user_nicename ) && $current_quiz_user_data->data->user_nicename != '' ) ? sanitize_text_field( $current_quiz_user_data->data->user_nicename ) : "";

                $current_quiz_author_website_url = ( isset( $current_quiz_user_data->user_url ) && ! empty( $current_quiz_user_data->user_url ) ) ? sanitize_url($current_quiz_user_data->user_url) : "";

                if( !empty( $current_quiz_author_website_url ) ){
                    $current_quiz_author_website_url = "<a href='". esc_url( $current_quiz_author_website_url ) ."' target='_blank' class='ays-quiz-user-website-link-a-tag'>". __( "Website", 'quiz-maker' ) ."</a>";
                }
            }
        }

        $get_site_title = get_bloginfo('name');
        $get_site_description = get_bloginfo('description');

        $message_data = array(
            'quiz_name'                             => $quiz_title,
            'time'                                  => $this->secondsToWords($quiz_timer),
            'user_first_name'                       => $user_first_name,
            'user_last_name'                        => $user_last_name,
            'questions_count'                       => $questions_count,
            'user_nickname'                         => $user_nickname,
            'user_display_name'                     => $user_display_name,
            'user_wordpress_email'                  => $user_wordpress_email,
            'user_wordpress_roles'                  => $user_wordpress_roles,
            'quiz_creation_date'                    => $quiz_creation_date,
            'current_quiz_author'                   => $current_quiz_author,
            'current_user_ip'                       => $current_user_ip,
            'current_quiz_author_email'             => $current_quiz_author_email,
            'current_quiz_author_nickname'          => $current_quiz_author_nickname,
            'admin_email'                           => $super_admin_email,
            'home_page_url'                         => $wp_home_page_url,
            'quiz_id'                               => $id,
            'user_id'                               => $user_id,
            'site_title'                            => $get_site_title,
            'current_quiz_author_website_url'       => $current_quiz_author_website_url,
            'site_description'                      => $get_site_description,
        );

        return $message_data;
    }

    /*
     * Get quiz default texts from database
     */
    public static function ays_set_quiz_default_texts( $plugin_name, $settings = array() ) {
        global $wpdb;

        $settings_table = $wpdb->prefix . "aysquiz_settings";
        $sql = "SELECT meta_value FROM " . $settings_table . " WHERE meta_key = 'default_texts'";
        $result = $wpdb->get_var($sql);
        $settings_static_texts = ($result == "") ? array() : json_decode($result, true);

        $wrong_shortcode_text = (isset($settings_static_texts['wrong_shortcode_text']) && $settings_static_texts['wrong_shortcode_text'] != '') ? stripslashes(esc_attr($settings_static_texts['wrong_shortcode_text'])) : 'Wrong shortcode initialized';
        $enter_password_text = (isset($settings_static_texts['enter_password_text']) && $settings_static_texts['enter_password_text'] != '') ? stripslashes(esc_attr($settings_static_texts['enter_password_text'])) : 'Please enter password';
        $wrong_password_text = (isset($settings_static_texts['wrong_password_text']) && $settings_static_texts['wrong_password_text'] != '') ? stripslashes(esc_attr($settings_static_texts['wrong_password_text'])) : 'Password is wrong!';
        $empty_results_text = (isset($settings_static_texts['empty_results_text']) && $settings_static_texts['empty_results_text'] != '') ? stripslashes(esc_attr($settings_static_texts['empty_results_text'])) : 'There are no results yet.';
        $not_answered_question_text = (isset($settings_static_texts['not_answered_question_text']) && $settings_static_texts['not_answered_question_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['not_answered_question_text'] ) ) : 'You have not answered this question';
        $finish_quiz_text = (isset($settings_static_texts['finish_quiz_text']) && $settings_static_texts['finish_quiz_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['finish_quiz_text'] ) ) : 'Do you want to finish the quiz? Are you sure?';
        $select_question_placeholder_text  = (isset($settings_static_texts['select_question_placeholder_text']) && $settings_static_texts['select_question_placeholder_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['select_question_placeholder_text'] ) ) : 'Select an answer';
        $no_more_reviews_text  = (isset($settings_static_texts['no_more_reviews_text']) && $settings_static_texts['no_more_reviews_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['no_more_reviews_text'] ) ) : 'No more reviews';

        $report_question_text  = (isset($settings_static_texts['report_question_text']) && $settings_static_texts['report_question_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['report_question_text'] ) ) : 'Report a question';
        $whats_wrong_report_question_text  = (isset($settings_static_texts['whats_wrong_report_question_text']) && $settings_static_texts['whats_wrong_report_question_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['whats_wrong_report_question_text'] ) ) : 'What’s wrong with this question?';

        $question_report_submitted_text  = (isset($settings_static_texts['question_report_submitted_text']) && $settings_static_texts['question_report_submitted_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['question_report_submitted_text'] ) ) : 'Report has been submitted successfully';
        $empty_report_text  = (isset($settings_static_texts['empty_report_text']) && $settings_static_texts['empty_report_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['empty_report_text'] ) ) : 'You cannot submit an empty report. Please add some details.';
        $question_report_icon_text  = (isset($settings_static_texts['question_report_icon_text']) && $settings_static_texts['question_report_icon_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['question_report_icon_text'] ) ) : 'Report Question';
        $start_button_loading_text  = (isset($settings_static_texts['start_button_loading_text']) && $settings_static_texts['start_button_loading_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['start_button_loading_text'] ) ) : 'Loading ...';
        $created_on_text  = (isset($settings_static_texts['created_on_text']) && $settings_static_texts['created_on_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['created_on_text'] ) ) : 'Created on';
        $logged_in_users_message_text  = (isset($settings_static_texts['logged_in_users_message_text']) && $settings_static_texts['logged_in_users_message_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['logged_in_users_message_text'] ) ) : 'You must log in to pass this quiz.';
        $quiz_schedule_start_message_text  = (isset($settings_static_texts['quiz_schedule_start_message_text']) && $settings_static_texts['quiz_schedule_start_message_text'] != '') ? stripslashes( esc_attr( $settings_static_texts['quiz_schedule_start_message_text'] ) ) : 'This Quiz will start on';

        if ($wrong_shortcode_text === 'Wrong shortcode initialized') {
            $wrong_shortcode_text = __('Wrong shortcode initialized', 'quiz-maker');
        }

        if ($enter_password_text === 'Please enter password') {
            $enter_password_text = __('Please enter password', 'quiz-maker');
        }

        if ($wrong_password_text === 'Password is wrong!') {
            $wrong_password_text = __('Password is wrong!', 'quiz-maker');
        }

        if ($empty_results_text === 'There are no results yet.') {
            $empty_results_text = __('There are no results yet.', 'quiz-maker');
        }

        if ($not_answered_question_text === 'You have not answered this question') {
            $not_answered_question_text = __('You have not answered this question', 'quiz-maker');
        }

        if ($finish_quiz_text === 'Do you want to finish the quiz? Are you sure?') {
            $finish_quiz_text = __('Do you want to finish the quiz? Are you sure?', 'quiz-maker');
        }

        if ($select_question_placeholder_text === 'Select an answer') {
            $select_question_placeholder_text = __('Select an answer', 'quiz-maker');
        }

        if ($no_more_reviews_text === 'No more reviews') {
            $no_more_reviews_text = __('No more reviews', 'quiz-maker');
        }

        if ($report_question_text === 'Report a question') {
            $report_question_text = __('Report a question', 'quiz-maker');
        }

        if ($whats_wrong_report_question_text === 'What’s wrong with this question?') {
            $whats_wrong_report_question_text = __('What’s wrong with this question?', 'quiz-maker');
        }

        if ($question_report_submitted_text === 'Report has been submitted successfully') {
            $question_report_submitted_text = __('Report has been submitted successfully', 'quiz-maker');
        }

        if ($empty_report_text === 'You cannot submit an empty report. Please add some details.') {
            $empty_report_text = __('You cannot submit an empty report. Please add some details.', 'quiz-maker');
        }

        if ($question_report_icon_text === 'Report Question') {
            $question_report_icon_text = __('Report Question', 'quiz-maker');
        }
        
        if ($start_button_loading_text === 'Loading ...') {
            $start_button_loading_text = __('Loading ...', 'quiz-maker');
        }
        
        if ($created_on_text === 'Created on') {
            $created_on_text = __('Created on', 'quiz-maker');
        }

        if ($logged_in_users_message_text === 'You must log in to pass this quiz.') {
            $logged_in_users_message_text = __('You must log in to pass this quiz.', 'quiz-maker');
        }

        if ($quiz_schedule_start_message_text === 'This Quiz will start on') {
            $quiz_schedule_start_message_text = __('This Quiz will start on', 'quiz-maker');
        }

        $texts = array(
            'wrongShortcode'                => $wrong_shortcode_text,
            'enterPassword'                 => $enter_password_text,
            'wrongPassword'                 => $wrong_password_text,
            'emptyResultsText'              => $empty_results_text,
            'notAnsweredQuestionText'       => $not_answered_question_text,
            'finishQuizText'                => $finish_quiz_text,
            'selectAnswerText'              => $select_question_placeholder_text,
            'noMoreReviewsText'             => $no_more_reviews_text,
            'reportQuestionText'            => $report_question_text,
            'whatsWrongReportQuestionText'  => $whats_wrong_report_question_text,
            'questionReportSubmittedText'   => $question_report_submitted_text,
            'emptyReportText'               => $empty_report_text,
            'questionReportIconText'        => $question_report_icon_text,
            'startButtonLoadingText'        => $start_button_loading_text,
            'createdOnText'                 => $created_on_text,
            'loggedInUsersMessageText'      => $logged_in_users_message_text,
            'quizScheduleStartMessageText'  => $quiz_schedule_start_message_text,
        );

        return $texts;
    }

    
    public function show_quiz($id){
        $quiz = $this->get_quiz_by_id($id);
        $this->default_texts = self::ays_set_quiz_default_texts( $this->plugin_name, array() );
        $content = '';
        
        if (is_null($quiz)) {
            $content = "<p class='wrong_shortcode_text' style='color:red;'>" . $this->default_texts['wrongShortcode'] . "</p>";
            return $content;
        }
        if (intval($quiz['published']) === 0) {
            return $content;
        }
        $options = ( json_decode($quiz['options'], true) != null ) ? json_decode($quiz['options'], true) : array();
        $options['quiz_theme'] = (array_key_exists('quiz_theme', $options)) ? $options['quiz_theme'] : '';
        
        // $this->buttons_texts = $this->ays_set_quiz_texts($id);
        $this->fields_placeholders = $this->ays_set_quiz_fields_placeholders_texts();
        $quiz_parts = $this->ays_quiz_parts($id);
        if ($quiz_parts === "" || empty($quiz_parts)) {
            return "";
        }
        
        $settings_for_theme = $this->settings;
        $buttons_texts_for_theme = $this->buttons_texts;
        
        switch ($options['quiz_theme']) {
            case 'elegant_dark':
                include_once('partials/class-quiz-theme-elegant-dark.php');
                $theme_obj = new Quiz_Theme_Elegant_Dark(AYS_QUIZ_NAME, AYS_QUIZ_NAME_VERSION, 'elegant_dark', $settings_for_theme, $buttons_texts_for_theme);
                $content = $theme_obj->ays_generate_quiz($quiz_parts);
                break;
            case 'elegant_light':
                include_once('partials/class-quiz-theme-elegant-light.php');
                $theme_obj = new Quiz_Theme_Elegant_Light(AYS_QUIZ_NAME, AYS_QUIZ_NAME_VERSION, 'elegant_light', $settings_for_theme, $buttons_texts_for_theme);
                $content = $theme_obj->ays_generate_quiz($quiz_parts);
                break;
            case 'rect_light':
                include_once('partials/class-quiz-theme-rect-light.php');
                $theme_obj = new Quiz_Theme_Rect_Light(AYS_QUIZ_NAME, AYS_QUIZ_NAME_VERSION, 'rect_light', $settings_for_theme, $buttons_texts_for_theme);
                $content = $theme_obj->ays_generate_quiz($quiz_parts);
                break;
            case 'rect_dark':
                include_once('partials/class-quiz-theme-rect-dark.php');
                $theme_obj = new Quiz_Theme_Rect_Dark(AYS_QUIZ_NAME, AYS_QUIZ_NAME_VERSION, 'rect_dark', $settings_for_theme, $buttons_texts_for_theme);
                $content = $theme_obj->ays_generate_quiz($quiz_parts);
                break;
            default:
                $content = $this->ays_generate_quiz($quiz_parts);
        }
        return $content;
    }

    public function ays_quiz_parts($id){
        
        global $wpdb;
        global $wp_embed;
        
    /*******************************************************************************************************/
        
        /*
         * Get Quiz data from database by id
         * Separation options from quiz data
         */
        $quiz = $this->get_quiz_by_id($id);
        $options = json_decode($quiz['options'], true);
        
        $settings_options = $this->settings->ays_get_setting('options');
        if($settings_options){
            $settings_options = json_decode(stripcslashes($settings_options), true);
        }else{
            $settings_options = array();
        }

        /*
         * Quiz message variables for Start Page
         */

        $message_variables_data = $this->ays_set_quiz_message_variables_data( $id, $quiz );

        /*
         * Gets the current theme obj
         */
        $current_user_theme_obj = wp_get_theme();

        
    /*******************************************************************************************************/
                
        $randomize_answers = false;
        $questions = null;
        $randomize_questions = false;
        $questions_ordering_by_cat = false;
        $quiz_questions_ids = "";
        $question_bank_cats = array();
        
        $arr_questions = ($quiz["question_ids"] == "") ? array() : explode(',', $quiz["question_ids"]);
        $arr_questions = (count($arr_questions) == 1 && $arr_questions[0] == '') ? array() : $arr_questions;

        if ( !empty($arr_questions) ) {
            $new_arr_questions = implode( ",", $arr_questions );
            $arr_questions = self::get_published_questions_id_arr($new_arr_questions);
        }

        if( $this->get_prop( 'is_training' ) === true ){
            $saved_passed_questions = apply_filters( 'ays_qm_front_end_training_passed_questions', array(), $id );
            $saved_wrong_questions = apply_filters( 'ays_qm_front_end_training_wrong_questions', array(), $id );
            $arr_questions = array_diff( $arr_questions, $saved_passed_questions );
            $arr_questions = array_values( $arr_questions );
        }

        if (isset($_COOKIE['ays_quiz_selected_categories-'.$id])) {
            $selected_questions = apply_filters( 'ays_qm_front_end_category_selective_get_questions', array(), $id );
            $arr_questions = $selected_questions;
        }

        $quiz_questions_ids = implode(',', $arr_questions);
        if (isset($options['randomize_questions']) && $options['randomize_questions'] == 'on') {
            $randomize_questions = true;
            shuffle($arr_questions);
        }
        if (isset($options['enable_question_bank']) && $options['enable_question_bank'] == 'on' && 
            isset($options['questions_count']) && intval($options['questions_count']) > 0 &&
            $options['questions_count'] <= count($arr_questions)) {
            $random_questions = array_rand($arr_questions, intval($options['questions_count']));
            if (!is_array($random_questions)) {
               $random_questions = array($random_questions);
            }       

            foreach ($random_questions as $key => $question) {
                $random_questions[$key] = strval($arr_questions[$question]);
            }
            $arr_questions = $random_questions;
            $quiz_questions_ids = join(',', $random_questions);
        }

        if(isset($options['enable_questions_ordering_by_cat']) && $options['enable_questions_ordering_by_cat'] == "on"){
            $questions_ordering_by_cat = true;
        }

        $quest_s = $this->get_quiz_questions_by_ids($arr_questions);
        $quests = array();
        foreach($quest_s as $quest){
            $quests[$quest['id']] = $quest;
        }

        $question_bank_categories = $this->get_question_bank_categories($quiz_questions_ids);

        if(count($arr_questions) > 0){
            if($questions_ordering_by_cat){
                $question_bank_questions = array();
                $quiz_questions_ids = array();

                foreach($arr_questions as $key => $val){
                    $question_bank_questions[$val] = $quests[$val];
                    if(isset($question_bank_categories[$quests[$val]['category_id']])){
                        $question_bank_cats[$quests[$val]['category_id']][] = strval($val);
                    }
                }

                if ($randomize_questions) {
                    $question_bank_cats = $this->ays_shuffle_assoc($question_bank_cats);

                    foreach ($question_bank_cats as $key => $value) {
                        shuffle($question_bank_cats[$key]);
                    }
                }

                $arr_questions = array();
                foreach($question_bank_cats as $key => $value){
                    $arr_questions = array_merge($arr_questions, $value);
                }

                $quiz_questions_ids = implode(',', $arr_questions);
            }
        }

        $questions_count = count($arr_questions);
        
        if (isset($options['randomize_answers']) && $options['randomize_answers'] == 'on') {
            $randomize_answers = true;
        }else{
            $randomize_answers = false;
        }

        if(isset($options['enable_correction']) && $options['enable_correction'] == "on"){
            $enable_correction = true;
        }else{
            $enable_correction = false;
        }

        // Waiting time
        $options['quiz_waiting_time'] = isset($options['quiz_waiting_time']) ? esc_attr($options['quiz_waiting_time']) : 'off';
        $quiz_waiting_time = (isset($options['quiz_waiting_time']) && $options['quiz_waiting_time'] == 'on') ? true : false;
        

    /*******************************************************************************************************/
        
        /*
         * Quiz information form fields
         *
         * Checking required filelds
         *
         * Creating HTML code for printing
         */
        
        $form_inputs = null;
        $show_form = null;
        $required_fields = (array_key_exists('required_fields', $options) && !is_null($options['required_fields'])) ? $options['required_fields'] : array();
        
        $name_required = (in_array('ays_user_name', $required_fields)) ? 'required' : '';
        $email_required = (in_array('ays_user_email', $required_fields)) ? 'required' : '';
        $phone_required = (in_array('ays_user_phone', $required_fields)) ? 'required' : '';
        
        $form_title = "";
        if(isset($options['form_title']) && $options['form_title'] != ''){
            $form_title = $this->replace_message_variables($options['form_title'], $message_variables_data);
            $form_title = $this->ays_autoembed($form_title);
        }

        // Display form fields labels
        $options['display_fields_labels'] = isset($options['display_fields_labels']) ? $options['display_fields_labels'] : 'on';
        $display_fields_labels = (isset($options['display_fields_labels']) && $options['display_fields_labels'] == 'on') ? true : false;

        if($options['form_name'] == "on"){
            $show_form = "show";
            if( $display_fields_labels ){
                $form_inputs .= "<label for='ays_form_field_user_name_". $id ."'>". $this->fields_placeholders['nameLabel'] ."</label>";
            }
            $form_inputs .= "<input type='text' id='ays_form_field_user_name_". $id ."' name='ays_user_name' placeholder='". $this->fields_placeholders['namePlaceholder'] ."' class='ays_quiz_form_input ays_animated_x5ms' " . $name_required . ">";
        }else{
            $form_inputs .= "<input type='hidden' name='ays_user_name' placeholder='". $this->fields_placeholders['namePlaceholder'] ."' value=''>";
        }
        if($options['form_email'] == "on"){
            $show_form = "show";
            if( $display_fields_labels ){
                $form_inputs .= "<label for='ays_form_field_user_email_". $id ."'>". $this->fields_placeholders['emailLabel'] ."</label>";
            }
            $form_inputs .= "<input type='text' id='ays_form_field_user_email_". $id ."' name='ays_user_email' placeholder='". $this->fields_placeholders['emailPlaceholder'] ."' class='ays_quiz_form_input ays_animated_x5ms' " . $email_required . ">";
        }else{
            $form_inputs .= "<input type='hidden' name='ays_user_email' placeholder='". $this->fields_placeholders['emailPlaceholder'] ."' value=''>";
        }
        if($options['form_phone'] == "on"){
            $show_form = "show";
            if( $display_fields_labels ){
                $form_inputs .= "<label for='ays_form_field_user_phone_". $id ."'>". $this->fields_placeholders['phoneLabel'] ."</label>";
            }
            $form_inputs .= "<input type='text' id='ays_form_field_user_phone_". $id ."' name='ays_user_phone' placeholder='". $this->fields_placeholders['phonePlaceholder'] ."' class='ays_quiz_form_input ays_animated_x5ms' " . $phone_required . ">";
        }else{
            $form_inputs .= "<input type='hidden' name='ays_user_phone' placeholder='". $this->fields_placeholders['phonePlaceholder'] ."' value=''>";
        }
        
        // Show information form to logged in users
        $options['show_information_form'] = isset($options['show_information_form']) ? $options['show_information_form'] : 'on';
        $show_information_form = (isset($options['show_information_form']) && $options['show_information_form'] == 'on') ? true : false;
        
    /*******************************************************************************************************/
        
        /*
         * Quiz colors
         * 
         * Quiz container colors
         */
        
        // Quiz container background color
        
        if(isset($options['bg_color']) && $options['bg_color'] != ''){
            $bg_color = $options['bg_color'];
        }else{
            $bg_color = "#fff";
        }
        
        // Color of elements inside quiz container
        
        if(isset($options['color']) && $options['color'] != ''){
            $color = $options['color'];
        }else{
            $color = "#27ae60";
        }
        
        // Color of text inside quiz container
        
        if(isset($options['text_color']) && $options['text_color'] != ''){
            $text_color = $options['text_color'];
        }else{
            $text_color = "#333";
        }
        
        // Color of text of buttons inside quiz container
        
        if(isset($options['buttons_text_color']) && $options['buttons_text_color'] != ''){
            $buttons_text_color = $options['buttons_text_color'];
        }else{
            $buttons_text_color = $text_color;
        }
        
        // Quiz container shadow color
        
        // CHecking exists box shadow option
        $options['enable_box_shadow'] = (!isset($options['enable_box_shadow'])) ? 'on' : $options['enable_box_shadow'];
        
        if(isset($options['box_shadow_color']) && $options['box_shadow_color'] != ''){
            $box_shadow_color = $options['box_shadow_color'];
        }else{
            $box_shadow_color = "#333";
        }
        
        // Quiz container border color
        
        if(isset($options['quiz_border_color']) && $options['quiz_border_color'] != ''){
            $quiz_border_color = $options['quiz_border_color'];
        }else{
            $quiz_border_color = '#000';
        }
                
        
    /*******************************************************************************************************/ 
        
        /*
         * Quiz styles
         *
         * Quiz container styles
         */

        $answer_view_class = "";
        
        
        // Quiz container minimal height
        
        if(isset($options['height']) && $options['height'] != ''){
            $quiz_height = $options['height'];
        }else{
            $quiz_height = '400';
        }
        
        // Quiz container width
        
        if(isset($options['width']) && $options['width'] != '' && absint( $options['width'] ) > 0){
            if (isset($options['quiz_width_by_percentage_px']) && $options['quiz_width_by_percentage_px'] == 'percentage') {
                if (absint(intval($options['width'])) > 100 ) {
                    $quiz_width = '100%';
                }else{
                    $quiz_width = $options['width'] . '%';
                }
            }else{
                $quiz_width = $options['width'] . 'px';
            }
        }else{
            $quiz_width = '100%';
        }
        
        
        // Quiz container max-width for mobile
        if(isset($options['mobile_max_width']) && $options['mobile_max_width'] != '' && absint( $options['mobile_max_width'] ) > 0){
            $mobile_max_width = $options['mobile_max_width'] . '%';
        }else{
            $mobile_max_width = '100%';
        }

        // Quiz content max-width
        $quiz_content_max_width = ( isset($options['quiz_content_max_width']) && $options['quiz_content_max_width'] != "" && intval($options['quiz_content_max_width']) > 0 ) ? intval(esc_attr($options['quiz_content_max_width'])) : 90;

        // Quiz content mobile max-width
        $quiz_content_mobile_max_width = ( isset($options['quiz_content_mobile_max_width']) && $options['quiz_content_mobile_max_width'] != "" && intval($options['quiz_content_mobile_max_width']) > 0 ) ? intval(esc_attr($options['quiz_content_mobile_max_width'])) : 90;

        // Quiz title transformation
        $quiz_title_transformation = (isset($options['quiz_title_transformation']) && sanitize_text_field( $options['quiz_title_transformation'] ) != "") ? sanitize_text_field( $options['quiz_title_transformation'] ) : 'uppercase';

        // Quiz title font size
        $quiz_title_font_size = (isset($options['quiz_title_font_size']) && ( $options['quiz_title_font_size'] ) != '' && ( $options['quiz_title_font_size'] ) != 0) ? esc_attr( absint( $options['quiz_title_font_size'] ) ) : 21;

        // Quiz title font size | On mobile
        $quiz_title_mobile_font_size = (isset($options['quiz_title_mobile_font_size']) && sanitize_text_field($options['quiz_title_mobile_font_size']) != '') ? esc_attr( absint($options['quiz_title_mobile_font_size']) ) : 21;

        // Quiz title text shadow
        $options['quiz_enable_title_text_shadow'] = isset($options['quiz_enable_title_text_shadow']) ? esc_attr($options['quiz_enable_title_text_shadow']) : 'off';
        $quiz_enable_title_text_shadow = (isset($options['quiz_enable_title_text_shadow']) && $options['quiz_enable_title_text_shadow'] == 'on') ? true : false;

        // Quiz title text shadow color
        $quiz_title_text_shadow_color = (isset($options['quiz_title_text_shadow_color']) && $options['quiz_title_text_shadow_color'] != '') ? esc_attr($options['quiz_title_text_shadow_color']) : '#333';

        // Quiz Title Text Shadow X offset
        $quiz_title_text_shadow_x_offset = (isset($options['quiz_title_text_shadow_x_offset']) && ( $options['quiz_title_text_shadow_x_offset'] ) != '' && ( $options['quiz_title_text_shadow_x_offset'] ) != 0) ? esc_attr( intval( $options['quiz_title_text_shadow_x_offset'] ) ) : 2;

        // Quiz Title Text Shadow Y offset
        $quiz_title_text_shadow_y_offset = (isset($options['quiz_title_text_shadow_y_offset']) && ( $options['quiz_title_text_shadow_y_offset'] ) != '' && ( $options['quiz_title_text_shadow_y_offset'] ) != 0) ? esc_attr( intval( $options['quiz_title_text_shadow_y_offset'] ) ) : 2;

        // Quiz Title Text Shadow Z offset
        $quiz_title_text_shadow_z_offset = (isset($options['quiz_title_text_shadow_z_offset']) && ( $options['quiz_title_text_shadow_z_offset'] ) != '' && ( $options['quiz_title_text_shadow_z_offset'] ) != 0) ? esc_attr( intval( $options['quiz_title_text_shadow_z_offset'] ) ) : 2;

        $title_text_shadow_offsets = $quiz_title_text_shadow_x_offset . 'px ' . $quiz_title_text_shadow_y_offset . 'px ' . $quiz_title_text_shadow_z_offset . 'px ';

        // Quiz image height
        $quiz_image_height = (isset($options['quiz_image_height']) && sanitize_text_field( $options['quiz_image_height'] ) != '') ? absint( sanitize_text_field( $options['quiz_image_height'] ) ) : '';

        
        // Quiz container border radius
        
        // Modified border radius for Pass count option and Rate avg option
        $quiz_modified_border_radius = "";
        
        if(isset($options['quiz_border_radius']) && $options['quiz_border_radius'] != ''){
            $quiz_border_radius = $options['quiz_border_radius'];
        }else{
            $quiz_border_radius = '3px';
        }
        
        // Quiz container shadow enabled/disabled
        
        if(isset($options['enable_box_shadow']) && $options['enable_box_shadow'] == 'on'){
            $enable_box_shadow = true;
        }else{
            $enable_box_shadow = false;
        }

        //  Box Shadow X offset
        $quiz_box_shadow_x_offset = (isset($options['quiz_box_shadow_x_offset']) && sanitize_text_field( $options['quiz_box_shadow_x_offset'] ) != '' && sanitize_text_field( $options['quiz_box_shadow_x_offset'] ) != 0) ? intval( sanitize_text_field( $options['quiz_box_shadow_x_offset'] ) ) : 0;

        //  Box Shadow Y offset
        $quiz_box_shadow_y_offset = (isset($options['quiz_box_shadow_y_offset']) && sanitize_text_field( $options['quiz_box_shadow_y_offset'] ) != '' && sanitize_text_field( $options['quiz_box_shadow_y_offset'] ) != 0) ? intval( sanitize_text_field( $options['quiz_box_shadow_y_offset'] ) ) : 0;

        //  Box Shadow Z offset
        $quiz_box_shadow_z_offset = (isset($options['quiz_box_shadow_z_offset']) && sanitize_text_field( $options['quiz_box_shadow_z_offset'] ) != '' && sanitize_text_field( $options['quiz_box_shadow_z_offset'] ) != 0) ? intval( sanitize_text_field( $options['quiz_box_shadow_z_offset'] ) ) : 15;

        $box_shadow_offsets = $quiz_box_shadow_x_offset . 'px ' . $quiz_box_shadow_y_offset . 'px ' . $quiz_box_shadow_z_offset . 'px ';
        
        // Quiz container background image
        
        if(isset($options['quiz_bg_image']) && $options['quiz_bg_image'] != ''){
            $ays_quiz_bg_image = $options['quiz_bg_image'];
        }else{
            $ays_quiz_bg_image = null;
        }

        // if( $ays_quiz_bg_image != "" && !is_null($ays_quiz_bg_image) ){
        //     if ( !(filter_var($ays_quiz_bg_image, FILTER_VALIDATE_URL) && wp_http_validate_url($ays_quiz_bg_image)) ) {
        //         // Invalid URL, handle accordingly
        //         $ays_quiz_bg_image = null;
        //     }
        // }

        // if( $ays_quiz_bg_image != "" && !is_null($ays_quiz_bg_image) ){
        //     $check_if_current_image_exists = Quiz_Maker_Admin::ays_quiz_check_if_current_image_exists($ays_quiz_bg_image);

        //     if( !$check_if_current_image_exists ){
        //         $ays_quiz_bg_image = "";
        //     }
        // }
        
        // Quiz container background image position
        $quiz_bg_image_position = "center center";

        if(isset($options['quiz_bg_image_position']) && $options['quiz_bg_image_position'] != ""){
            $quiz_bg_image_position = $options['quiz_bg_image_position'];
        }

		// Hide quiz background image on the result page
        $quiz_bg_img_in_finish_page = "false";

        if(isset($options['quiz_bg_img_in_finish_page']) && $options['quiz_bg_img_in_finish_page'] == "on"){
            $quiz_bg_img_in_finish_page = "true";
        }

        // Hide background image on start page
        $options['quiz_bg_img_on_start_page'] = isset($options['quiz_bg_img_on_start_page']) ? $options['quiz_bg_img_on_start_page'] : 'off';
        $quiz_bg_img_on_start_page = (isset($options['quiz_bg_img_on_start_page']) && $options['quiz_bg_img_on_start_page'] == 'on') ? true : false;

        $quiz_bg_img_class = '';
        if ( $quiz_bg_img_on_start_page ) {
            $quiz_bg_img_class = 'ays_quiz_hide_bg_on_start_page';
        }

        // Hide background image during the quiz
        $options['quiz_bg_img_during_the_quiz'] = isset($options['quiz_bg_img_during_the_quiz']) ? $options['quiz_bg_img_during_the_quiz'] : 'off';
        $quiz_bg_img_during_the_quiz = (isset($options['quiz_bg_img_during_the_quiz']) && $options['quiz_bg_img_during_the_quiz'] == 'on') ? true : false;

        
        /*
         * Quiz container border enabled/disabled
         *
         * Quiz container border width
         *
         * Quiz container border style
         */
        
        if(isset($options['enable_border']) && $options['enable_border'] == 'on'){
            $enable_border = true;
        }else{
            $enable_border = false;
        }
        
        if(isset($options['quiz_border_width']) && $options['quiz_border_width'] != ''){
            $quiz_border_width = $options['quiz_border_width'];
        }else{
            $quiz_border_width = '1';
        }
        
        if(isset($options['quiz_border_style']) && $options['quiz_border_style'] != ''){
            $quiz_border_style = $options['quiz_border_style'];
        }else{
            $quiz_border_style = 'solid';
        }
        
        // Questions image width, height and sizing
        
        // Image Width(px)
        $image_width = (isset($options['image_width']) && sanitize_text_field($options['image_width']) != '' && absint( sanitize_text_field($options['image_width']) ) > 0) ? absint( sanitize_text_field($options['image_width']) ) : '';

        // Quiz image width percentage/px
        $quiz_image_width_by_percentage_px = (isset($options['quiz_image_width_by_percentage_px']) && sanitize_text_field( $options['quiz_image_width_by_percentage_px'] ) != '') ? sanitize_text_field( $options['quiz_image_width_by_percentage_px'] ) : 'pixels';

        if($image_width != ''){
            if ($quiz_image_width_by_percentage_px == 'percentage') {
                if ($image_width > 100 ) {
                    $question_image_width = '100%';
                }else{
                    $question_image_width = $image_width . '%';
                }
            }else{
                $question_image_width = $image_width . 'px';
            }
        }else{
            $question_image_width = "100%";
        }

        if(isset($options['image_height']) && $options['image_height'] != ''){
            $question_image_height = $options['image_height'] . 'px';
        }else{
            $question_image_height = "auto";
        }
        
        if(isset($options['image_sizing']) && $options['image_sizing'] != ''){
            $question_image_sizing = $options['image_sizing'];
        }else{
            $question_image_sizing = "cover";
        }

        // Question Image border radius
        $quiz_question_image_border_radius = (isset($options[ 'quiz_question_image_border_radius' ]) && $options[ 'quiz_question_image_border_radius' ] != '') ? absint ( stripslashes( $options[ 'quiz_question_image_border_radius' ] ) ) : 0;


        /*
        ==========================================
            Answers styles
        ==========================================
        */

        // Answers view
        $answer_view_class = "list";
        if(isset($options['answers_view']) && $options['answers_view'] != ''){
            $answer_view_class = sanitize_text_field($options['answers_view']);
        }

        // Answer view mobile
        $answer_view_class_mobile = "list";
        if(isset($options['answers_view_mobile']) && $options['answers_view_mobile'] != ''){
            $answer_view_class_mobile = sanitize_text_field($options['answers_view_mobile']);
        }
        
        // Answers font size
        
        $answers_font_size = '15';
        if(isset($options['answers_font_size']) && $options['answers_font_size'] != ""){
            $answers_font_size = intval($options['answers_font_size']);
        }

        // Answers padding option
        $answers_padding = '5';
        if(isset($options['answers_padding']) && $options['answers_padding'] != ''){
            $answers_padding = intval($options['answers_padding']);
        }

        // Answer font size | On mobile
        $answers_mobile_font_size = ( isset($options['answers_mobile_font_size']) && $options['answers_mobile_font_size'] != "" ) ? absint( sanitize_text_field( $options['answers_mobile_font_size'] ) ) : 15;

        // Question Font Size
        
        $question_font_size = '16';
        if(isset($options['question_font_size']) && $options['question_font_size'] != ""){
            $question_font_size = $options['question_font_size'];
        }

        // Question font size | On mobile
        $question_mobile_font_size = ( isset($options['question_mobile_font_size']) && $options['question_mobile_font_size'] != "" ) ? absint( sanitize_text_field( $options['question_mobile_font_size'] ) ) : 16;

        // Font size for the wrong answer
        $wrong_answers_font_size = (isset($options['wrong_answers_font_size']) && $options['wrong_answers_font_size'] != '') ? absint(sanitize_text_field($options['wrong_answers_font_size'])) : '16';

        // Font size for the wrong answer | Mobile
        $wrong_answers_mobile_font_size = (isset($options['wrong_answers_mobile_font_size']) && $options['wrong_answers_mobile_font_size'] != '') ? absint(sanitize_text_field($options['wrong_answers_mobile_font_size'])) : $wrong_answers_font_size;

        // Wrong answer transform size
        $quiz_wrong_answer_text_transform = (isset($options[ 'quiz_wrong_answer_text_transform' ]) && $options[ 'quiz_wrong_answer_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_wrong_answer_text_transform' ] ) ) : 'none';

        // Wrong answer text transform size | Mobile
        $quiz_wrong_answer_mobile_text_transform = (isset($options[ 'quiz_wrong_answer_mobile_text_transform' ]) && $options[ 'quiz_wrong_answer_mobile_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_wrong_answer_mobile_text_transform' ] ) ) : $quiz_wrong_answer_text_transform;

        // Wrong answer text decoration
        $quiz_wrong_answers_text_decoration = (isset($options[ 'quiz_wrong_answers_text_decoration' ]) && $options[ 'quiz_wrong_answers_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_wrong_answers_text_decoration' ] ) ) : 'none';

        // Wrong answer text decoration | Mobile
        $quiz_wrong_answers_mobile_text_decoration = (isset($options[ 'quiz_wrong_answers_mobile_text_decoration' ]) && $options[ 'quiz_wrong_answers_mobile_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_wrong_answers_mobile_text_decoration' ] ) ) : $quiz_wrong_answers_text_decoration;

        // Letter spacing | Wrong answer
        $quiz_wrong_answers_letter_spacing = (isset($options[ 'quiz_wrong_answers_letter_spacing' ]) && $options[ 'quiz_wrong_answers_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_wrong_answers_letter_spacing' ] ) ) : 0;

        // Letter spacing | Wrong answer | Mobile
        $quiz_wrong_answers_mobile_letter_spacing = (isset($options[ 'quiz_wrong_answers_mobile_letter_spacing' ]) && $options[ 'quiz_wrong_answers_mobile_letter_spacing' ] != '') ? absint ( stripslashes( $options[ 'quiz_wrong_answers_mobile_letter_spacing' ] ) ) : $quiz_wrong_answers_letter_spacing;

        // Wrong answer font weight
        $quiz_wrong_answers_font_weight = (isset($options[ 'quiz_wrong_answers_font_weight' ]) && $options[ 'quiz_wrong_answers_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_wrong_answers_font_weight' ] ) ) : 'normal';

        // Wrong answer font weight | Mobile
        $quiz_wrong_answers_mobile_font_weight = (isset($options[ 'quiz_wrong_answers_mobile_font_weight' ]) && $options[ 'quiz_wrong_answers_mobile_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_wrong_answers_mobile_font_weight' ] ) ) : $quiz_wrong_answers_font_weight;

        // Font size for the question explanation
        $quest_explanation_font_size = (isset($options['quest_explanation_font_size']) && $options['quest_explanation_font_size'] != '') ? absint(sanitize_text_field($options['quest_explanation_font_size'])) : '16';

        // Font size for the question explanation | Mobile
        $quest_explanation_mobile_font_size = (isset($options['quest_explanation_mobile_font_size']) && $options['quest_explanation_mobile_font_size'] != '') ? absint(esc_attr($options['quest_explanation_mobile_font_size'])) : $quest_explanation_font_size;

        // Question explanation transform size
        $quiz_quest_explanation_text_transform = (isset($options[ 'quiz_quest_explanation_text_transform' ]) && $options[ 'quiz_quest_explanation_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_quest_explanation_text_transform' ] ) ) : 'none';

        // Question explanation transform size | Mobile
        $quiz_quest_explanation_mobile_text_transform = (isset($options[ 'quiz_quest_explanation_mobile_text_transform' ]) && $options[ 'quiz_quest_explanation_mobile_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_quest_explanation_mobile_text_transform' ] ) ) : $quiz_quest_explanation_text_transform;

        // Question Explanation text decoration
        $quiz_quest_explanation_text_decoration = (isset($options[ 'quiz_quest_explanation_text_decoration' ]) && $options[ 'quiz_quest_explanation_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_quest_explanation_text_decoration' ] ) ) : 'none';

        // Question Explanation text decoration | Mobile
        $quiz_quest_explanation_mobile_text_decoration = (isset($options[ 'quiz_quest_explanation_mobile_text_decoration' ]) && $options[ 'quiz_quest_explanation_mobile_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_quest_explanation_mobile_text_decoration' ] ) ) : $quiz_quest_explanation_text_decoration;

        // Letter spacing | Question Explanation
        $quiz_quest_explanation_letter_spacing = (isset($options[ 'quiz_quest_explanation_letter_spacing' ]) && $options[ 'quiz_quest_explanation_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_quest_explanation_letter_spacing' ] ) ) : 0;

        // Letter spacing | Question Explanation | Mobile
        $quiz_quest_explanation_mobile_letter_spacing = (isset($options[ 'quiz_quest_explanation_mobile_letter_spacing' ]) && $options[ 'quiz_quest_explanation_mobile_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_quest_explanation_mobile_letter_spacing' ] ) ) : $quiz_quest_explanation_letter_spacing;

        // Question Explanation font weight
        $quiz_quest_explanation_font_weight = (isset($options[ 'quiz_quest_explanation_font_weight' ]) && $options[ 'quiz_quest_explanation_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_quest_explanation_font_weight' ] ) ) : 'normal';

        // Question explanation Font weight | Mobile
        $quiz_quest_explanation_mobile_font_weight = (isset($options[ 'quiz_quest_explanation_mobile_font_weight' ]) && $options[ 'quiz_quest_explanation_mobile_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_quest_explanation_mobile_font_weight' ] ) ) : $quiz_quest_explanation_font_weight;

        // Font size for the right answer | PC
        $right_answers_font_size = (isset($options['right_answers_font_size']) && $options['right_answers_font_size'] != '') ? absint(sanitize_text_field($options['right_answers_font_size'])) : '16';

        // Font size for the right answer | Mobile
        $right_answers_mobile_font_size = (isset($options['right_answers_mobile_font_size']) && $options['right_answers_mobile_font_size'] != '') ? absint(esc_attr($options['right_answers_mobile_font_size'])) : $right_answers_font_size;

        // Right answer transform size
        $quiz_right_answer_text_transform = (isset($options[ 'quiz_right_answer_text_transform' ]) && $options[ 'quiz_right_answer_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_right_answer_text_transform' ] ) ) : 'none';

        // Right answer text transform size | Mobile
        $quiz_right_answer_mobile_text_transform = (isset($options[ 'quiz_right_answer_mobile_text_transform' ]) && $options[ 'quiz_right_answer_mobile_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_right_answer_mobile_text_transform' ] ) ) : $quiz_right_answer_text_transform;

        // Right answer text decoration
        $quiz_right_answers_text_decoration = (isset($options[ 'quiz_right_answers_text_decoration' ]) && $options[ 'quiz_right_answers_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_right_answers_text_decoration' ] ) ) : 'none';

        // Right answer text decoration | Mobile
        $quiz_right_answers_mobile_text_decoration = (isset($options[ 'quiz_right_answers_mobile_text_decoration' ]) && $options[ 'quiz_right_answers_mobile_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_right_answers_mobile_text_decoration' ] ) ) : $quiz_right_answers_text_decoration;

        // Letter spacing | Right answer
        $quiz_right_answers_letter_spacing = (isset($options[ 'quiz_right_answers_letter_spacing' ]) && $options[ 'quiz_right_answers_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_right_answers_letter_spacing' ] ) ) : 0;

        // Letter spacing | Right answer | Mobile
        $quiz_right_answers_mobile_letter_spacing = (isset($options[ 'quiz_right_answers_mobile_letter_spacing' ]) && $options[ 'quiz_right_answers_mobile_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_right_answers_mobile_letter_spacing' ] ) ) : $quiz_right_answers_letter_spacing;

        // Right answer font weight
        $quiz_right_answers_font_weight = (isset($options[ 'quiz_right_answers_font_weight' ]) && $options[ 'quiz_right_answers_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_right_answers_font_weight' ] ) ) : 'normal';

        // Right answer font weight | Mobile
        $quiz_right_answers_mobile_font_weight = (isset($options[ 'quiz_right_answers_mobile_font_weight' ]) && $options[ 'quiz_right_answers_mobile_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_right_answers_mobile_font_weight' ] ) ) : $quiz_right_answers_font_weight;

        // Font size for the Note text | PC
        $note_text_font_size = (isset($options['note_text_font_size']) && $options['note_text_font_size'] != '') ? absint(esc_attr($options['note_text_font_size'])) : '14';

        // Font size for the Note text | Mobile
        $note_text_mobile_font_size = (isset($options['note_text_mobile_font_size']) && $options['note_text_mobile_font_size'] != '') ? absint(esc_attr($options['note_text_mobile_font_size'])) : $note_text_font_size;

        // Note text transform size
        $quiz_admin_note_text_transform = (isset($options[ 'quiz_admin_note_text_transform' ]) && $options[ 'quiz_admin_note_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_admin_note_text_transform' ] ) ) : 'none';

        // Note text transform size | Mobile
        $quiz_admin_note_mobile_text_transform = (isset($options[ 'quiz_admin_note_mobile_text_transform' ]) && $options[ 'quiz_admin_note_mobile_text_transform' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_admin_note_mobile_text_transform' ] ) ) : $quiz_admin_note_text_transform;

        // Note text decoration
        $quiz_admin_note_text_decoration = (isset($options[ 'quiz_admin_note_text_decoration' ]) && $options[ 'quiz_admin_note_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_admin_note_text_decoration' ] ) ) : 'none';

        // Note text decoration | On mobile
        $quiz_admin_note_mobile_text_decoration = (isset($options[ 'quiz_admin_note_mobile_text_decoration' ]) && $options[ 'quiz_admin_note_mobile_text_decoration' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_admin_note_mobile_text_decoration' ] ) ) : $quiz_admin_note_text_decoration;

        // Note letter spacing
        $quiz_admin_note_letter_spacing = (isset($options[ 'quiz_admin_note_letter_spacing' ]) && $options[ 'quiz_admin_note_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_admin_note_letter_spacing' ] ) ) : 0;

        // Note letter spacing | Admin note | Mobile
        $quiz_admin_note_mobile_letter_spacing = (isset($options[ 'quiz_admin_note_mobile_letter_spacing' ]) && $options[ 'quiz_admin_note_mobile_letter_spacing' ] != '') ? stripslashes ( absint( $options[ 'quiz_admin_note_mobile_letter_spacing' ] ) ) : $quiz_admin_note_letter_spacing;

        // Admin Note font weight
        $quiz_admin_note_font_weight = (isset($options[ 'quiz_admin_note_font_weight' ]) && $options[ 'quiz_admin_note_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_admin_note_font_weight' ] ) ) : 'normal';

        // Admin Notice Font weight | Mobile
        $quiz_admin_note_mobile_font_weight = (isset($options[ 'quiz_admin_note_mobile_font_weight' ]) && $options[ 'quiz_admin_note_mobile_font_weight' ] != '') ? stripslashes ( esc_attr( $options[ 'quiz_admin_note_mobile_font_weight' ] ) ) : $quiz_admin_note_font_weight;

        // Disable answer hover
        $options['disable_hover_effect'] = isset($options['disable_hover_effect']) ? $options['disable_hover_effect'] : 'off';
        $disable_hover_effect = (isset($options['disable_hover_effect']) && $options['disable_hover_effect'] == "on") ? true : false;

        // Question text alignment
        $quiz_question_text_alignment = (isset($options['quiz_question_text_alignment']) && sanitize_text_field( $options['quiz_question_text_alignment'] ) != '') ? sanitize_text_field( $options['quiz_question_text_alignment'] ) : 'center';

        // Answers image options
        // Show answers caption
        $show_answers_caption = false;
        $options['show_answers_caption'] = isset($options['show_answers_caption']) ? stripslashes ( esc_attr($options['show_answers_caption']) ) : 'on';
        if(isset($options['show_answers_caption']) && $options['show_answers_caption'] == 'on'){
            $show_answers_caption = true;
        }

        $ans_img_height = "150px";
        if(isset($options['ans_img_height']) && $options['ans_img_height'] != ''){
            $ans_img_height = absint($options['ans_img_height']) . "px";
        }

        $ans_img_caption_position = 'bottom';
        if(isset($options['ans_img_caption_position']) && $options['ans_img_caption_position'] != ''){
            $ans_img_caption_position = sanitize_text_field($options['ans_img_caption_position']);
        }

        $answers_object_fit = 'cover';
        if(isset($options['answers_object_fit']) && $options['answers_object_fit'] != ''){
            $answers_object_fit = sanitize_text_field($options['answers_object_fit']);
        }

        $ans_image_caption_style = 'outside';
        $ans_img_caption_style = "column-reverse";
        if(isset($options['ans_img_caption_style']) && $options['ans_img_caption_style'] != ''){
            $ans_image_caption_style = sanitize_text_field($options['ans_img_caption_style']);
        }
        $ans_image_caption_position = $ans_img_caption_position;

        // Desktop styles
        if($answer_view_class == 'list'){
            if($ans_image_caption_style == 'outside'){
                $ans_img_caption_position = "position:initial;".$ans_img_caption_position.":0;";
            }elseif($ans_image_caption_style == 'inside'){
                $ans_img_caption_position = "position:absolute;".$ans_img_caption_position.":0;";
            }
            if($ans_image_caption_position == 'top'){
                $ans_img_caption_style = "row";
            }elseif($ans_image_caption_position == 'bottom'){
                $ans_img_caption_style = "row-reverse";
            }
        }elseif($answer_view_class == 'grid'){
            if($ans_image_caption_style == 'outside'){
                $ans_img_caption_position = "position:initial;".$ans_img_caption_position.":0;";
            }elseif($ans_image_caption_style == 'inside'){
                $ans_img_caption_position = "position:absolute;".$ans_img_caption_position.":0;";
            }
            if($ans_image_caption_position == 'top'){
                $ans_img_caption_style = "column";
            }elseif($ans_image_caption_position == 'bottom'){
                $ans_img_caption_style = "column-reverse";
            }
        }

        // Mobile styles
        // $ans_img_caption_position_mobile = $ans_img_caption_position;
        // if($answer_view_class_mobile == 'list'){
        //     if($ans_image_caption_style == 'outside'){
        //         $ans_img_caption_position_mobile = "position:initial;".$ans_img_caption_position.":0;";
        //     }elseif($ans_image_caption_style == 'inside'){
        //         $ans_img_caption_position_mobile = "position:absolute;".$ans_img_caption_position.":0;";
        //     }
        //     if($ans_image_caption_position == 'top'){
        //         $ans_img_caption_style_mobile = "row";
        //     }elseif($ans_image_caption_position == 'bottom'){
        //         $ans_img_caption_style_mobile = "row-reverse";
        //     }
        // }elseif($answer_view_class_mobile == 'grid'){
        //     if($ans_image_caption_style == 'outside'){
        //         $ans_img_caption_position_mobile = "position:initial;".$ans_img_caption_position.":0;";
        //     }elseif($ans_image_caption_style == 'inside'){
        //         $ans_img_caption_position_mobile = "position:absolute;".$ans_img_caption_position.":0;";
        //     }
        //     if($ans_image_caption_position == 'top'){
        //         $ans_img_caption_style_mobile = "column";
        //     }elseif($ans_image_caption_position == 'bottom'){
        //         $ans_img_caption_style_mobile = "column-reverse";
        //     }
        // }
        

        // Answers border options
        $options['answers_border'] = (isset($options['answers_border'])) ? $options['answers_border'] : 'on';
        $answers_border = false;
        if(isset($options['answers_border']) && $options['answers_border'] == 'on'){
            $answers_border = true;
        }
        $answers_border_width = '1';
        if(isset($options['answers_border_width']) && $options['answers_border_width'] != ''){
            $answers_border_width = $options['answers_border_width'];
        }
        $answers_border_style = 'solid';
        if(isset($options['answers_border_style']) && $options['answers_border_style'] != ''){
            $answers_border_style = $options['answers_border_style'];
        }
        $answers_border_color = '#444';
        if(isset($options['answers_border_color']) && $options['answers_border_color'] != ''){
            $answers_border_color = $options['answers_border_color'];
        }

        // Answers margin option
        $answers_margin = 10;
        if(isset($options['answers_margin']) && $options['answers_margin'] != ''){
            $answers_margin = intval( $options['answers_margin'] );
        }

        /* 
         * Quiz container background gradient
         * 
         */
        
        // Checking exists background gradient option
        $options['enable_background_gradient'] = (!isset($options['enable_background_gradient'])) ? "off" : $options['enable_background_gradient'];
        
        if(isset($options['background_gradient_color_1']) && $options['background_gradient_color_1'] != ''){
            $background_gradient_color_1 = $options['background_gradient_color_1'];
        }else{
            $background_gradient_color_1 = "#000";
        }

        if(isset($options['background_gradient_color_2']) && $options['background_gradient_color_2'] != ''){
            $background_gradient_color_2 = $options['background_gradient_color_2'];
        }else{
            $background_gradient_color_2 = "#fff";
        }

        if(isset($options['quiz_gradient_direction']) && $options['quiz_gradient_direction'] != ''){
            $quiz_gradient_direction = $options['quiz_gradient_direction'];
        }else{
            $quiz_gradient_direction = 'vertical';
        }
        switch($quiz_gradient_direction) {
            case "horizontal":
                $quiz_gradient_direction = "to right";
                break;
            case "diagonal_left_to_right":
                $quiz_gradient_direction = "to bottom right";
                break;
            case "diagonal_right_to_left":
                $quiz_gradient_direction = "to bottom left";
                break;
            default:
                $quiz_gradient_direction = "to bottom";
        }

        // Quiz container background gradient enabled/disabled
        
        if(isset($options['enable_background_gradient']) && $options['enable_background_gradient'] == "on"){
            $enable_background_gradient = true;
        }else{
            $enable_background_gradient = false;
        }

        // Answers box shadow
        $answers_box_shadow = false;
        $answers_box_shadow_color = '#000';
        if(isset($options['answers_box_shadow']) && $options['answers_box_shadow'] == 'on'){
            $answers_box_shadow = true;
        }
        if(isset($options['answers_box_shadow_color']) && $options['answers_box_shadow_color'] != ''){
            $answers_box_shadow_color = $options['answers_box_shadow_color'];
        }

        //  Box Shadow X offset
        $quiz_answer_box_shadow_x_offset = (isset($options['quiz_answer_box_shadow_x_offset']) && sanitize_text_field( $options['quiz_answer_box_shadow_x_offset'] ) != '' && sanitize_text_field( $options['quiz_answer_box_shadow_x_offset'] ) != 0) ? intval( sanitize_text_field( $options['quiz_answer_box_shadow_x_offset'] ) ) : 0;

        //  Box Shadow Y offset
        $quiz_answer_box_shadow_y_offset = (isset($options['quiz_answer_box_shadow_y_offset']) && sanitize_text_field( $options['quiz_answer_box_shadow_y_offset'] ) != '' && sanitize_text_field( $options['quiz_answer_box_shadow_y_offset'] ) != 0) ? intval( sanitize_text_field( $options['quiz_answer_box_shadow_y_offset'] ) ) : 0;

        //  Box Shadow Z offset
        $quiz_answer_box_shadow_z_offset = (isset($options['quiz_answer_box_shadow_z_offset']) && sanitize_text_field( $options['quiz_answer_box_shadow_z_offset'] ) != '' && sanitize_text_field( $options['quiz_answer_box_shadow_z_offset'] ) != 0) ? intval( sanitize_text_field( $options['quiz_answer_box_shadow_z_offset'] ) ) : 10;

        $answer_box_shadow_offsets = $quiz_answer_box_shadow_x_offset . 'px ' . $quiz_answer_box_shadow_y_offset . 'px ' . $quiz_answer_box_shadow_z_offset . 'px ';


        // Answers right/wrong icons
        $ans_right_wrong_icon = 'default';
        if(isset($options['ans_right_wrong_icon']) && $options['ans_right_wrong_icon'] != ''){
            $ans_right_wrong_icon = $options['ans_right_wrong_icon'];
        }


        // Buttons position
        $buttons_position = 'center';
        if(isset($options['buttons_position']) && $options['buttons_position'] != ''){
            $buttons_position = $options['buttons_position'];
        }

         /*
        ==========================================
            Buttons styles
        ==========================================
        */

        // Buttons font size
        $buttons_font_size = '17px';
        if(isset($options['buttons_font_size']) && $options['buttons_font_size'] != ''){
            $buttons_font_size = $options['buttons_font_size'] . 'px';
        }

        // Button font-size (px) | Mobile
        $buttons_mobile_font_size = (isset($options['buttons_mobile_font_size']) && $options['buttons_mobile_font_size'] != '') ? absint( esc_attr( $options['buttons_mobile_font_size'] ) ) : 17;

        // Buttons font size
        $buttons_width = '';
        if(isset($options['buttons_width']) && $options['buttons_width'] != ''){
            $buttons_width = $options['buttons_width'] . 'px';
        }

        $buttons_width_html = '';
        if( $buttons_width != ''){
            $buttons_width_html = "width:" . $buttons_width;
        }

        // Buttons Left / Right padding
        $buttons_left_right_padding = '20px';
        if(isset($options['buttons_left_right_padding']) && $options['buttons_left_right_padding'] != ''){
            $buttons_left_right_padding = $options['buttons_left_right_padding'] . 'px';
        }

        // Buttons Top / Bottom padding
        $buttons_top_bottom_padding = '10px';
        if(isset($options['buttons_top_bottom_padding']) && $options['buttons_top_bottom_padding'] != ''){
            $buttons_top_bottom_padding = $options['buttons_top_bottom_padding'] . 'px';
        }

        // Buttons border radius
        $buttons_border_radius = '8px';
        if(isset($options['buttons_border_radius']) && $options['buttons_border_radius'] != ''){
            $buttons_border_radius = $options['buttons_border_radius'] . 'px';
        }

        // Button font-size (px) | Mobile
        $buttons_mobile_border_radius = (isset($options['buttons_mobile_border_radius']) && $options['buttons_mobile_border_radius'] != '') ? absint( esc_attr( $options['buttons_mobile_border_radius'] ) ) : intval($buttons_border_radius);
        
    /*******************************************************************************************************/
        
        /*
         * Quiz start page
         *
         * Quiz title
         * Quiz desctiption
         * Quiz image
         *
         * Quiz Start button
         */
        
        $title = do_shortcode(stripslashes($quiz['title']));
        
        $description = $this->replace_message_variables($quiz['description'], $message_variables_data);
        $description = $this->ays_autoembed( $description );
        
        $quiz_image = (isset($quiz['quiz_image']) && $quiz['quiz_image'] != "") ? esc_url($quiz['quiz_image']) : "";

        // if( $quiz_image != "" ){
        //     if ( !(filter_var($quiz_image, FILTER_VALIDATE_URL) && wp_http_validate_url($quiz_image)) ) {
        //         // Invalid URL, handle accordingly
        //         $quiz_image = "";
        //     }
        // }

        // if( $quiz_image != "" ){
        //     $check_if_current_image_exists = Quiz_Maker_Admin::ays_quiz_check_if_current_image_exists($quiz_image);

        //     if( !$check_if_current_image_exists ){
        //         $quiz_image = "";
        //     }
        // }
        
        
        $quiz_rate_reports = '';
        $quiz_result_reports = '';
        
        
        if($questions_count == 0){
            $empty_questions_notification = '<p id="ays_no_questions_message" style="color:red">' . __('Add at least one question to start', 'quiz-maker') . '</p>';
            $empty_questions_button = "disabled";
        }else{
            $empty_questions_notification = "";
            $empty_questions_button = "";
        }

        if( $this->get_prop( 'is_training' ) === true && empty( $arr_questions ) ){
            $empty_questions_notification = "<button type='button' class='action-button ays_restart_training_button'>
                    <i class='ays_fa ays_fa_undo'></i>
                    <span>". $this->buttons_texts['restartQuizButton'] ."</span>
                </button>";;
        }

        $password_message = "";
        $start_button_disabled = "";
        $quiz_password_message_html = "";
        $password_message_with_toggle = "";

        // Password quiz
        $quiz_password = ( isset( $options['password_quiz']) && $options['password_quiz'] != '' ) ? stripslashes( $options['password_quiz'] ) : '';

        // Quiz password width
        $quiz_password_width = (isset($options['quiz_password_width']) && ( $options['quiz_password_width'] ) != '' && ( $options['quiz_password_width'] ) != 0) ? esc_attr( absint( $options['quiz_password_width'] ) ) : "";

        $quiz_password_width_css = "";
        if ( $quiz_password_width != "" ) {
            $quiz_password_width_css = $quiz_password_width . "px";
        } else {
            $quiz_password_width_css = "100%";
        }

        if(isset($options['enable_password']) && $options['enable_password'] == 'on' && $quiz_password != ""){

            // Password for passing quiz | Message
            $quiz_password_message = ( isset( $options['quiz_password_message']) && $options['quiz_password_message'] != '' ) ? stripslashes( $options['quiz_password_message'] ) : '';
            $quiz_password_message = $this->replace_message_variables($quiz_password_message, $message_variables_data);

            // Enable toggle password visibility
            $options['quiz_enable_password_visibility'] = isset($options['quiz_enable_password_visibility']) ? $options['quiz_enable_password_visibility'] : 'off';
            $quiz_enable_password_visibility = (isset($options['quiz_enable_password_visibility']) && $options['quiz_enable_password_visibility'] == 'on') ? true : false;

            if ( $quiz_password_message != '' ) {
                $quiz_password_message_html .= '<div class="ays-quiz-password-message-box">';
                    $quiz_password_message_html .= $this->ays_autoembed($quiz_password_message);
                $quiz_password_message_html .= '</div>';
            }

            $password_message = "<input type='password' autocomplete='no' id='ays_quiz_password_val_". $id ."' class='ays_quiz_password' placeholder='". $this->default_texts['enterPassword'] ."'>";

            if ( $quiz_enable_password_visibility ) {
                $password_message_with_toggle .= "<div class='ays-quiz-password-toggle-visibility-box'>";
                    $password_message_with_toggle .= $password_message;
                    $password_message_with_toggle .= "<img src='". AYS_QUIZ_PUBLIC_URL ."/images/quiz-maker-eye-visibility-off.svg' class='ays-quiz-password-toggle ays-quiz-password-toggle-visibility-off'>";
                    $password_message_with_toggle .= "<img src='". AYS_QUIZ_PUBLIC_URL ."/images/quiz-maker-eye-visibility.svg' class='ays-quiz-password-toggle ays-quiz-password-toggle-visibility ays_display_none'>";
                $password_message_with_toggle .= "</div>";

                $password_message = $password_message_with_toggle;
            }

            $start_button_disabled = " disabled='disabled' ";
        }

        // Checking confirmation box for leaving the page enabled or diabled
        if (isset($options['enable_leave_page']) && $options['enable_leave_page'] == 'on') {
            $enable_leave_page = 'data-enable-leave-page="false"';
        }elseif (! isset($options['enable_leave_page'])) {
            $enable_leave_page = 'data-enable-leave-page="false"';
        }
        else{
            $enable_leave_page = '';
        }

        // Enable lazy loading attribute for images
        $settings_options['quiz_enable_lazy_loading'] = isset($settings_options['quiz_enable_lazy_loading']) ? esc_attr( $settings_options['quiz_enable_lazy_loading'] ) : 'off';
        $quiz_enable_lazy_loading = (isset($settings_options['quiz_enable_lazy_loading']) && esc_attr( $settings_options['quiz_enable_lazy_loading'] ) == "on") ? true : false;

        // Disable answer hover
        $settings_options['enable_start_button_loader'] = isset($settings_options['enable_start_button_loader']) ? sanitize_text_field($settings_options['enable_start_button_loader']) : 'off';
        $enable_start_button_loader = (isset($settings_options['enable_start_button_loader']) && sanitize_text_field($settings_options['enable_start_button_loader']) == "on") ? true : false;

        //Enable keyboard navigation
        $options['quiz_enable_keyboard_navigation'] = isset($options['quiz_enable_keyboard_navigation']) ? $options['quiz_enable_keyboard_navigation'] : 'on';
        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == "on") ? true : false;
        $class_for_keyboard = "";
        $attributes_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){            
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
        }
        
        $quiz_start_button = "<input type='button' $empty_questions_button $start_button_disabled class='ays_next start_button action-button ".$class_for_keyboard."' value='". $this->buttons_texts['startButton'] ."' ". $enable_leave_page ." />" . $empty_questions_notification;


        $is_elementor_exists        = $this->ays_quiz_is_elementor();
        $is_editor_exists           = $this->ays_quiz_is_editor();
        $is_beaver_builder_exists   = $this->ays_quiz_is_beaver_builder();
        
        if ( $enable_start_button_loader ) {
            if ( $is_elementor_exists ) {
                $enable_start_button_loader = false;
            }

            if ( $is_editor_exists ) {
                $enable_start_button_loader = false;
            }

            if ( $is_beaver_builder_exists ) {
                $enable_start_button_loader = false;
            }
        }

        if ( $enable_start_button_loader ) {
            if ($questions_count != 0) {
                $quiz_start_butto_html = "<input type='button' $empty_questions_button class='ays_next start_button action-button ays_quiz_enable_loader ".$class_for_keyboard."' disabled='disabled' value='". esc_html($this->default_texts['startButtonLoadingText']) ."' ". $enable_leave_page ." />".$empty_questions_notification;

                $quiz_start_button = '
                <div class="ays-quiz-start-button-preloader">
                    '. $quiz_start_butto_html .'
                    <img src="'. AYS_QUIZ_ADMIN_URL .'/images/loaders/tail-spin.svg" class="ays_quiz_start_button_loader">
                </div>';
            }
        }

        $quiz_block_preview_message = "";
        if( $is_elementor_exists || $is_editor_exists || $is_beaver_builder_exists ){
            $quiz_block_preview_message = '
                <span class="ays_quiz_small_hint_text" style="color: #ccc;font-size: 15px;">'. esc_attr( __( "You're in the preview mode. Note: All elements work correctly on the front end." , "quiz-maker") ) .'</span>';
        }
        
        
        /*
         * Show quiz head information
         * Show quiz title and description
         */
        
        $options['show_quiz_title'] = isset($options['show_quiz_title']) ? $options['show_quiz_title'] : 'on';
        $options['show_quiz_desc'] = isset($options['show_quiz_desc']) ? $options['show_quiz_desc'] : 'on';
        $show_quiz_title = (isset($options['show_quiz_title']) && $options['show_quiz_title'] == "on") ? true : false;
        $show_quiz_desc = (isset($options['show_quiz_desc']) && $options['show_quiz_desc'] == "on") ? true : false;

        // Show Quiz Image on the front-end
        $options['show_quiz_image'] = isset($options['show_quiz_image']) ? esc_attr($options['show_quiz_image']) : 'on';
        $show_quiz_image = (isset($options['show_quiz_image']) && $options['show_quiz_image'] == "on") ? true : false;

        
        /* 
         * Quiz passed users count
         *
         * Generate HTML code
         */
        
        if(isset($options['enable_pass_count']) && $options['enable_pass_count'] == 'on'){
            $enable_pass_count = true;
            $quiz_result_reports = $this->get_quiz_results_count_by_id($id);
            $quiz_result_reports = "<qm_users_count class='ays_quizn_ancnoxneri_qanak'><i class='ays_fa ays_fa_users'></i> ".$quiz_result_reports['res_count']."</qm_users_count>";
            $quiz_modified_border_radius = "border-radius:" . $quiz_border_radius . "px " . $quiz_border_radius . "px 0px " . $quiz_border_radius . "px;";
        }else{
            $enable_pass_count = false;
        }
        
        
        
        /* 
         * Quiz average rate
         *
         * Generate HTML code
         */
        
        $quiz_rates_avg = round($this->ays_get_average_of_rates($id), 1);
        $quiz_rates_count = $this->ays_get_count_of_rates($id);
        if(isset($options['enable_rate_avg']) && $options['enable_rate_avg'] == 'on'){
            $enable_rate_avg = true;
            $quiz_rate_reports = "<div class='ays_quiz_rete_avg'>
                <div class='for_quiz_rate_avg ui star rating' data-rating='".round($quiz_rates_avg)."' data-max-rating='5'></div>
                <qm_votes>$quiz_rates_count " . __( "votes", 'quiz-maker' ) . ", $quiz_rates_avg " . __( "avg", 'quiz-maker' ) . "</qm_votes>
            </div>";
            $quiz_modified_border_radius = "border-radius:" . $quiz_border_radius . "px " . $quiz_border_radius . "px " . $quiz_border_radius . "px 0px;";
        }else{
            $enable_rate_avg = false;
        }
        
        
        
        /* 
         * Generate HTML code when passed users count and average rate both are enabled
         */
        
        if($enable_rate_avg && $enable_pass_count){
            $quiz_modified_border_radius = "border-radius:" . $quiz_border_radius . "px " . $quiz_border_radius . "px 0px 0px;";
            $ays_quiz_reports = "<div class='ays_quiz_reports'>$quiz_rate_reports $quiz_result_reports</div>";
        }else{
            $ays_quiz_reports = $quiz_rate_reports.$quiz_result_reports;
        }
        
        /* 
         * Generate HTML code when passed users count and average rate both are enabled
         * 
         * Show quiz author and create date
         */
        
        // Show quiz category
        if(isset($options['show_category']) && $options['show_category'] == "on"){
            $show_category = true;
        }else{
            $show_category = false;
        }

        // Show quiz category description
        $options['quiz_enable_quiz_category_description'] = isset($options['quiz_enable_quiz_category_description']) ? $options['quiz_enable_quiz_category_description'] : 'off';
        $quiz_enable_quiz_category_description = (isset($options['quiz_enable_quiz_category_description']) && $options['quiz_enable_quiz_category_description'] == 'on') ? true : false;
        
        // Show question category
        if(isset($options['show_question_category']) && $options['show_question_category'] == "on"){
            $show_question_category = true;
        }else{
            $show_question_category = false;
        }

        // Show question category description
        $options['quiz_enable_question_category_description'] = isset($options['quiz_enable_question_category_description']) ? $options['quiz_enable_question_category_description'] : 'off';
        $quiz_enable_question_category_description = (isset($options['quiz_enable_question_category_description']) && $options['quiz_enable_question_category_description'] == 'on') ? true : false;
        
        if(isset($options['show_create_date']) && $options['show_create_date'] == "on"){
            $show_create_date = true;
        }else{
            $show_create_date = false;
        }
        
        if(isset($options['show_author']) && $options['show_author'] == "on"){
            $show_author = true;
        }else{
            $show_author = false;
        }
        
        $show_cd_and_author = "<div class='ays_cb_and_a'>";
        if($show_create_date){
            $quiz_create_date = (isset($options['create_date']) && $options['create_date'] != '') ? $options['create_date'] : "0000-00-00 00:00:00";
            if(Quiz_Maker_Admin::validateDate($quiz_create_date)){
                $show_cd_and_author .= "<span>". esc_html($this->default_texts['createdOnText']) ." </span><strong><time>".date_i18n("F d, Y", strtotime($quiz_create_date))."</time></strong>";
            }else{
                $show_cd_and_author .= "";
            }
        }
        if($show_author){
            if(isset($options['author'])){
                if(is_array($options['author'])){
                    $author = $options['author'];
                }else{
                    $author = json_decode($options['author'], true);
                }
            }else{
                $author = array("name"=>"Unknown");
            }
            $user_id = 0;
            if(isset($author['id']) && intval($author['id']) != 0){
                $user_id = intval($author['id']);
            }
            $image = get_avatar($user_id, 32);
            if($author['name'] !== "Unknown"){
                if($show_create_date){
                    $text = __("By", 'quiz-maker');
                }else{
                    $text = __("Created by", 'quiz-maker');
                }
                $show_cd_and_author .= "<span>   ".$text." </span>".$image."<strong>".$author['name']."</strong>";
            }else{
                $show_cd_and_author .= "";
            }
        }
        if($show_category){
            $category_id = isset($quiz['quiz_category_id']) ? intval($quiz['quiz_category_id']) : null;
            if($category_id !== null){
                $quiz_category = $this->get_quiz_category_by_id($category_id);

                $quiz_category_description = isset($quiz_category['description']) && $quiz_category['description'] != "" ? $this->ays_autoembed($quiz_category['description']) : "";
                
                $show_cd_and_author .= "<p style='margin:0!important;'><strong>".$quiz_category['title']."</strong></p>";

                if( $quiz_enable_quiz_category_description ){
                    $show_cd_and_author .= $quiz_category_description;
                }
            }else{
                $show_cd_and_author .= "";
            }
        }
        $show_cd_and_author .= "</div>";
        
        if($show_create_date == false && $show_author == false && $show_category == false){
            $show_cd_and_author = "";
        }
        
        
        
    /*******************************************************************************************************/
        
        /* 
         * Quiz passing options
         *
         * Generate HTML code
         */
        
        $live_progress_bar = "";
        $timer_row = "";
        $correction_class = "";
        $ie_container_css = "";
        $rtl_style = "";
        $filling_type_wrap = '';
        $filling_type = '';
        $quiz_message_before_timer = '';

        // Progress live bar style
        $options['enable_live_progress_bar'] = isset($options['enable_live_progress_bar']) ? $options['enable_live_progress_bar'] : 'off';
        $enable_live_progress_bar = (isset($options['enable_live_progress_bar']) && $options['enable_live_progress_bar'] == 'on') ? true : false;
            
        
        /*
         * Generating Quiz timer
         *
         * Checking timer enabled or diabled
         */
        
        $timer_enabled = false;
        if (isset($options['enable_timer']) && $options['enable_timer'] == 'on') {
            $timer_enabled = true;
            $timer_text = (isset($options['timer_text'])) ? $options['timer_text'] : '';
            $timer_text = $this->replace_message_variables($timer_text, $message_variables_data);
            $timer_text = $this->ays_autoembed( $timer_text );
            $after_timer_text = (isset($options['after_timer_text'])) ? $options['after_timer_text'] : '';
            $after_timer_text = $this->replace_message_variables($after_timer_text, $message_variables_data);
            $after_timer_text = $this->ays_autoembed( $after_timer_text );

            // Message before timer
            $quiz_message_before_timer = (isset($options['quiz_message_before_timer']) && $options['quiz_message_before_timer'] != '') ? esc_attr( sanitize_text_field( $options['quiz_message_before_timer'] ) ) : '';

            $quiz_message_before_timer_class = '';
            if ( $quiz_message_before_timer != '' ) {
                $quiz_message_before_timer_class = 'ays-quiz-message-before-timer';
            }

            $hide_timer_cont = "";
            $empty_after_timer_text_class = "";
            if($timer_text == ""){
                $hide_timer_cont = " style='display:none;' ";
            }
            if($after_timer_text == ""){
                $empty_after_timer_text_class = " empty_after_timer_text ";
            }
            $timer_row = "<section {$hide_timer_cont} class='ays_quiz_timer_container'>
                <div class='ays-quiz-timer ". $quiz_message_before_timer_class ."' data-timer='" . $options['timer'] . "'>{$timer_text}</div>
                <div class='ays-quiz-after-timer ".$empty_after_timer_text_class."'>{$after_timer_text}</div>
                <hr style='height:1px;'>
            </section>";
        }
        
        /*
         * Quiz live progress bar
         *
         * Checking enabled or diabled
         *
         * Checking percent view or not
         */
        
        if($enable_live_progress_bar){
            $live_preview_view = isset($options['progress_live_bar_style']) && $options['progress_live_bar_style'] != '' ? $options['progress_live_bar_style'] : 'default';
            
            if(isset($options['enable_percent_view']) && $options['enable_percent_view'] == 'on'){
                $live_progress_bar_percent = "<span class='ays-live-bar-percent'>0</span>%";
            }else{
                $live_progress_bar_percent = "<span class='ays-live-bar-percent ays-live-bar-count'></span>/$questions_count";
            }
            switch ($live_preview_view) {
                case 'second':
                    $filling_type_wrap = 'ays-live-second-wrap';
                    $filling_type = 'ays-live-second';
                    break;
                case 'third':
                    $filling_type_wrap = 'ays-live-third-wrap';
                    $filling_type = 'ays-live-third';
                    break;
                case 'fourth':
                    $filling_type_wrap = 'ays-live-fourth-wrap';
                    $filling_type = 'ays-live-fourth';
                    break;
                default:
                    $filling_type_wrap = '';
                    $filling_type = '';
                    break;
            }
            
            $live_progress_bar = "<div class='ays-live-bar-wrap ". $filling_type_wrap ."'><div class='ays-live-bar-fill ". $filling_type ."' style='width: 0%;'><span>". $live_progress_bar_percent ."</span></div></div>";          
        }
        
        
        
        /*
         * Quiz questions answers view
         *
         * Generate HTML class for answers view
         */
        
        // if(isset($options['answers_view']) && $options['answers_view'] != ''){
        //     $answer_view_class = $options['answers_view'];
        // }
        
        
        /*
         * Get site url for social sharing buttons
         *
         * Generate HTML class for answers view
         */
        
        $actual_link = "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){
            $actual_link = "https" . $actual_link;
        }else{
            $actual_link = "http" . $actual_link;
        }        
        
        /*
         * Show correct answers
         *
         * Generate HTML class for answers view
         */
        
        if($enable_correction){
            $correction_class = "enable_correction";
        }

        /*
         * Answeres numbering 
         */
        $show_answers_numbering = (isset($options['show_answers_numbering']) && sanitize_text_field( $options['show_answers_numbering'] ) !== '') ?  sanitize_text_field( $options['show_answers_numbering'] ) : 'none';

        // Questions numbering
        $show_questions_numbering = (isset($options['show_questions_numbering']) && $options['show_questions_numbering'] !== '') ?  sanitize_text_field( $options['show_questions_numbering'] ) : 'none';
              
        
        /*
         * Show correct answers
         *
         * Generate HTML class for answers view
         */
        
        if(isset($options['enable_questions_counter']) && $options['enable_questions_counter'] == 'on'){
            $questions_counter = true;
        }else{
            $questions_counter = false;
        }

        if(isset($options['enable_questions_reporting']) && $options['enable_questions_reporting'] == 'on'){
            $questions_reporting = true;
            $send_email = 'off';

            $questions_reporting_modal = '<div class="ays-modal-reports" id="ays-quiz-question-report-modal-' . $id . '">
                                            <div class="ays-modal-content-reports">
                                                <span class="ays-close-reports-window"><img src="' . AYS_QUIZ_PUBLIC_URL . '/images/close-report-window.svg" title="close"></span>
                                                <h3 class="ays-quiz-question-report-title">' . esc_html($this->default_texts['reportQuestionText']) . '</h3>
                                                <form id="ays-quiz-question-report-form">
                                                    <label class="ays-quiz-question-report-textarea-label" for="ays-quiz-question-report-textarea">' . esc_html($this->default_texts['whatsWrongReportQuestionText']) . '</label>
                                                    <textarea id="ays-quiz-question-report-textarea" name="ays-quiz-question-report-textarea"></textarea>
                                                    <div class="ays-quiz-question-report-error">' . esc_html($this->default_texts['emptyReportText']) . '</div>
                                                    <input type="hidden" class="ays-quiz-report-question-id" value="">
                                                    <input type="hidden" class="ays-quiz-report-quiz-id" value="' . $id . '">
                                                    <input type="hidden" class="ays-quiz-report-question-send-email" value="' . $send_email . '">
                                                    <div class="ays-quiz-submit-question-report-box">
                                                        <input type="submit" class="ays-quiz-submit-question-report" value="'. $this->buttons_texts['reportSubmitButton'] .'">
                                                    </div>
                                                    <input type="hidden" class="ays_quiz_question_report_nonce" name="ays_quiz_question_report_nonce" value="'. esc_attr( wp_create_nonce( 'ays_quiz_question_report_nonce' ) ) .'">

                                                </form>
                                                <div class="ays-quiz-preloader" style="top:0">
                                                    <img src="' . AYS_QUIZ_ADMIN_URL . '/images/loaders/tail-spin.svg">
                                                </div>
                                            </div>
                                          </div>';

        }else{
            $questions_reporting = false;
            $questions_reporting_modal = '';
        }
           
        
        /*
         * Get Browser data for Internet Explorer
         */
        
        $useragent = htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES, 'UTF-8');
        if(preg_match('~MSIE|Internet Explorer~i', $useragent) || 
           (strpos($useragent, 'Trident/7.0; rv:11.0') !== false)){
            $ie_container_css = 'display:flex;flex-wrap:wrap;';
        }


        /*
         * Question hint value 
         */
        $questions_hint_type = (isset($options['questions_hint_icon_or_text']) && $options['questions_hint_icon_or_text'] != '' ) ? sanitize_text_field( $options['questions_hint_icon_or_text'] ) : 'default';
        $questions_hint_value = (isset($options['questions_hint_value']) && $options['questions_hint_value'] != '') ? stripslashes(esc_attr($options['questions_hint_value'])) : '';
        $questions_hint_button_value = (isset($options['questions_hint_button_value']) && $options['questions_hint_button_value'] != '') ? stripslashes(esc_attr($options['questions_hint_button_value'])) : '';

        $questions_hint_arr = array(
            'questionsHintType' => $questions_hint_type,
            'questionsHintValue' => $questions_hint_value,
            'questionsHintButtonValue' => $questions_hint_button_value,
        );
        
        /*
         * Quiz buttons
         * 
         * Next button
         * Previous button
         * Arrows instead buttons
         */
        if(isset($options['enable_previous_button']) && $options['enable_previous_button'] == "on"){
            $prev_button = true;
        }else{
            $prev_button = false;
        }
        
        if(isset($options['enable_next_button']) && $options['enable_next_button'] == "on"){
            $next_button = true;
        }else{
            $next_button = false;
        }
        
        if(isset($options['enable_arrows']) && $options['enable_arrows'] == "on"){
            $enable_arrows = true;
        }else{
            $enable_arrows = false;
        }
        
        // Quiz arrows option arrows
        if(isset($options['quiz_arrow_type']) && $options['quiz_arrow_type'] != ""){
            $quiz_arrow_type = $options['quiz_arrow_type'];
        }else{
            $quiz_arrow_type = 'default';
        }

        if(isset($options['enable_early_finish']) && $options['enable_early_finish'] == 'on'){
            $enable_early_finish = true;
        }else{
            $enable_early_finish = false;
        }
        
        if($enable_arrows){
            $arrows_visibility = "";
        }else{
            $arrows_visibility = 'ays_display_none';
        }
        
        if($prev_button && $enable_arrows){
            $prev_arrow_visibility = "";
        }else{
            $prev_arrow_visibility = 'ays_display_none';
        }
        
        if($prev_button && !$enable_arrows){
            $prev_button_visibility = "";
        }else{
            $prev_button_visibility = 'ays_display_none';
        }
        
        if($next_button && $enable_arrows){
            $next_arrow_visibility = "";
        }else{
            $next_arrow_visibility = 'ays_display_none';
        }
        
        if($next_button == true && $enable_arrows == false){
            $next_button_visibility = "";
        }else{
            $next_button_visibility = 'ays_display_none';
        }

        
        /*
         * Clear answer button
         */
        $enable_clear_answer = false;
        if(isset($options['enable_clear_answer']) && $options['enable_clear_answer'] == 'on'){
            $enable_clear_answer = true;
        }
        
        
        $buttons = array(
            "enableArrows"          => $enable_arrows,
            "arrows"                => $arrows_visibility,
            "nextArrow"             => $next_arrow_visibility,
            "prevArrow"             => $prev_arrow_visibility,
            "nextButton"            => $next_button_visibility,
            "prevButton"            => $prev_button_visibility,
            "earlyButton"           => $enable_early_finish,
            "clearAnswerButton"     => $enable_clear_answer,
            "quizArrowType"         => $quiz_arrow_type,
        );
        
        /*
         * Quiz restart button
         */
        $enable_restart_button = false;
        if(isset($options['enable_restart_button']) && $options['enable_restart_button'] == 'on'){
            $enable_restart_button = true;
        }

        if($enable_restart_button){
            $restart_button = "<button type='button' class='action-button ays_restart_button ".$class_for_keyboard."'>
                    <i class='ays_fa ays_fa_undo'></i>
                    <span>". $this->buttons_texts['restartQuizButton'] ."</span>
                </button>";
        }else{
            $restart_button = "";
        }

        
        /*
         * EXIT button in finish page
         */

        $enable_exit_button = false;
        $exit_redirect_url = null;
        if(isset($options['enable_exit_button']) && $options['enable_exit_button'] == 'on'){
            $enable_exit_button = true;
        }
        if(isset($options['exit_redirect_url']) && $options['exit_redirect_url'] != ''){
            $exit_redirect_url = $options['exit_redirect_url'];
        }


        if($enable_exit_button && $exit_redirect_url !== null){

            $exit_button = "<a style='width:auto;' href='".$exit_redirect_url."' class='action-button ays_restart_button ".$class_for_keyboard."' target='_top'>
                        <span>".$this->buttons_texts['exitButton']."</span>
                        <i class='ays_fa ays_fa_sign_out'></i>
                    </a>";
        }else{
            $exit_button = "";
        }
                        
        /*
         * Quiz questions per page count
         */
        
        if(isset($options['enable_rtl_direction']) && $options['enable_rtl_direction'] == "on"){
            $rtl_direction = true;
            $rtl_style = "
                #ays-quiz-container-" . $id . " p {
                    direction:rtl;
                    text-align:right;   
                }
                #ays-quiz-container-" . $id . " p.ays_score {
                    text-align: center;   
                }
                #ays-quiz-container-" . $id . " p.ays-question-counter {
                    right: unset;
                    left: 8px;
                }
                #ays-quiz-container-" . $id . " .ays_question_hint_container {
                    left:unset;
                    right:10px;
                }
                #ays-quiz-container-" . $id . " .ays_question_hint_text {
                    left:unset;
                    right:20px;
                }
                #ays-quiz-container-" . $id . " .select2-container--default .select2-results__option {
                    direction:rtl;
                    text-align:right;
                }
                #ays-quiz-container-" . $id . " .select2-container--default .select2-selection--single .select2-selection__placeholder,
                #ays-quiz-container-" . $id . " .select2-container--default .select2-selection--single .select2-selection__rendered {
                    direction:rtl;
                    text-align:right;
                    display: inline-block;
                    width: 100%;
                }

                #ays-quiz-container-" . $id . " .select2-container .select2-selection--single .select2-selection__rendered {
                    padding-right: 30px;
                }
                #ays-quiz-container-" . $id . " .ays-field.ays-select-field {
                    margin: 0;
                }

                #ays-quiz-container-" . $id . " label[for^=\"ays-answer-\"]{
                    direction:rtl;
                    text-align:right;
                    padding-left: 0px;
                    padding-right: 10px;
                    position: relative;
                    text-overflow: ellipsis;
                }                        
                #ays-quiz-container-" . $id . " label[for^=\"ays-answer-\"]:last-child {
                    padding-right: 0;
                }
                #ays-quiz-container-" . $id . " label[for^=\"ays-answer-\"]::before {
                    margin-left: 5px;
                    margin-right: 5px;
                }
                #ays-quiz-container-" . $id . " label[for^=\"ays-answer-\"]::after {
                    margin-left: 0px;
                    margin-right: 10px;
                }
                ";
        }else{
            $rtl_direction = false;
        }
        
        
        
        /*
         * Quiz background music 
         */
        
        $enable_bg_music = false;
        $quiz_bg_music = "";
        $ays_quiz_music_html = "";
        $ays_quiz_music_sound = "";
        
        if(isset($options['enable_bg_music']) && $options['enable_bg_music'] == "on"){
            $enable_bg_music = true;
        }
        
        if(isset($options['quiz_bg_music']) && $options['quiz_bg_music'] != ""){
            $quiz_bg_music = $options['quiz_bg_music'];
        }

        if($enable_bg_music && $quiz_bg_music != ""){
            $ays_quiz_music_html = "<audio id='ays_quiz_music_".$id."' loop class='ays_quiz_music' src='".$quiz_bg_music."'></audio>";
            $with_timer = "";
            if($timer_enabled){
                $with_timer = " ays_sound_with_timer ";
            }
            $ays_quiz_music_sound = "<span class='ays_music_sound ".$with_timer." ays_sound_active ays_display_none'><i class='ays_fa ays_fa_volume_up'></i></span>";
        }
        
        /*
         * Quiz Right / Wrong answers sounds 
         */
        
        $enable_rw_asnwers_sounds = false;
        $rw_answers_sounds_status = false;
        $right_answer_sound_status = false;
        $wrong_answer_sound_status = false;
        $right_answer_sound = "";
        $wrong_answer_sound = "";
        $rw_asnwers_sounds_html = "";


        if(isset($settings_options['right_answer_sound']) && $settings_options['right_answer_sound'] != ''){
            $right_answer_sound_status = true;
            $right_answer_sound = esc_url($settings_options['right_answer_sound']);
        }

        if(isset($settings_options['wrong_answer_sound']) && $settings_options['wrong_answer_sound'] != ''){
            $wrong_answer_sound_status = true;
            $wrong_answer_sound = esc_url($settings_options['wrong_answer_sound']);
        }

        if($right_answer_sound_status && $wrong_answer_sound_status){
            $rw_answers_sounds_status = true;
        }
        
        if(isset($options['enable_rw_asnwers_sounds']) && $options['enable_rw_asnwers_sounds'] == "on"){
            if($rw_answers_sounds_status){
                $enable_rw_asnwers_sounds = true;
            }
        }

        if($enable_rw_asnwers_sounds){
            $rw_asnwers_sounds_html = "<audio id='ays_quiz_right_ans_sound_".$id."' class='ays_quiz_right_ans_sound' src='".esc_url($right_answer_sound)."'></audio>";
            $rw_asnwers_sounds_html .= "<audio id='ays_quiz_wrong_ans_sound_".$id."' class='ays_quiz_wrong_ans_sound' src='".esc_url($wrong_answer_sound)."'></audio>";
        }


        /*
         * Text quetion type
         * Textarea height (public)
         */

        // Textarea height (public)
        $quiz_textarea_height = (isset($settings_options['quiz_textarea_height']) && $settings_options['quiz_textarea_height'] != '' && $settings_options['quiz_textarea_height'] != 0) ? absint( sanitize_text_field($settings_options['quiz_textarea_height']) ) : 100;

        // Show question explanation
        $show_questions_explanation = (isset($options['show_questions_explanation']) && $options['show_questions_explanation'] != '') ? sanitize_text_field( $options['show_questions_explanation'] ) : 'on_results_page';

        // Show messages for right/wrong answers
        $answers_rw_texts = (isset($options['answers_rw_texts']) && $options['answers_rw_texts'] != '') ? sanitize_text_field( $options['answers_rw_texts'] ) : 'on_passing';

        
    /*******************************************************************************************************/
        
        /* 
         * Quiz finish page
         *
         * Generating some HTML code for finish page
         */
        
        $progress_bar = false;
        $progress_bar_style = "first";
        $progress_bar_html = "";
        $show_average = "";
        $show_score_html = "";
        $enable_questions_result = "";
        $rate_form_title = "";
        $quiz_rate_html = "";
        $ays_social_buttons = "";
        $pass_score_html = "";
        
        /*
         * Quiz progress bar for finish page
         *
         * Checking enabled or diabled
         */
        
        if(isset($options['enable_progress_bar']) && $options['enable_progress_bar'] == 'on'){
            $progress_bar = true;
        }

        if(isset($options['progress_bar_style']) && $options['progress_bar_style'] != ""){
            $progress_bar_style = $options['progress_bar_style'];
        }

        if($progress_bar){
            $progress_bar_html = "<div class='ays-progress " . $progress_bar_style . "'>
                <span class='ays-progress-value " . $progress_bar_style . "'>0%</span>
                <div class='ays-progress-bg " . $progress_bar_style . "'>
                    <div class='ays-progress-bar " . $progress_bar_style . "' style='width:0%;'></div>
                </div>
            </div>";
        }


        /*
         * Average statistical of quiz
         *
         * Checking enabled or diabled
         */
        if (isset($options['enable_average_statistical']) && $options['enable_average_statistical'] == "on") {
            $sql = "SELECT AVG(`score`) FROM {$wpdb->prefix}aysquiz_reports WHERE quiz_id= $id";

            $quiz_avg_result = $wpdb->get_var($sql);
            if ( is_null( $quiz_avg_result ) || empty( $quiz_avg_result ) ) {
                $result = 0;
            } else { 
                $result = round($wpdb->get_var($sql));
            }
            $show_average = "<p class='ays_average'>" . __('The average score is', 'quiz-maker') . " " . $result . "%</p>";
        }
        
        
        /*
         * Passed quiz score
         *
         * Checking enabled or diabled
         */
                
        if(array_key_exists('hide_score',$options) && $options['hide_score'] != 'on'){
            $show_score_html = "<p class='ays_score ays_score_display_none animated'>" . __( 'Your score is', 'quiz-maker' ) . "</p>";
        }
        
        
        /*
         * Show quiz results after passing quiz
         *
         * Checking enabled or diabled
         */
              
        if(isset($options['enable_questions_result']) && $options['enable_questions_result'] == 'on'){
            $enable_questions_result = 'enable_questions_result';
        }

        // Add all reviews link
        $options['quiz_make_all_review_link'] = isset($options['quiz_make_all_review_link']) ? sanitize_text_field($options['quiz_make_all_review_link']) : 'off';
        $quiz_make_all_review_link = (isset($options['quiz_make_all_review_link']) && $options['quiz_make_all_review_link'] == 'on') ? true : false;
        
        $all_review_link_html = '';
        if ( $quiz_make_all_review_link ) {
            if ( $this->ays_get_count_of_reviews(0, 5, $id) > 0 ) {
                $all_review_link_html = "
                <div class='ays-quiz-rate-link-box'>
                    <span class='ays-quiz-rate-link'>". __( "See review", 'quiz-maker' ) ."</span>
                </div>";
            }
        }

        // Enable quiz assessment | Placeholder text
        $quiz_review_placeholder_text = (isset($options['quiz_review_placeholder_text']) && $options['quiz_review_placeholder_text'] != '') ? stripslashes( esc_attr( $options['quiz_review_placeholder_text'] ) ) : "";

        // Make review required
        $options['quiz_make_review_required'] = isset($options['quiz_make_review_required']) ? sanitize_text_field($options['quiz_make_review_required']) : 'off';
        $quiz_make_review_required = (isset($options['quiz_make_review_required']) && $options['quiz_make_review_required'] == 'on') ? "true" : "false";

        // Enable users' anonymous assessment
        $options['quiz_enable_user_cհoosing_anonymous_assessment'] = isset($options['quiz_enable_user_cհoosing_anonymous_assessment']) ? sanitize_text_field($options['quiz_enable_user_cհoosing_anonymous_assessment']) : 'off';
        $quiz_enable_user_cհoosing_anonymous_assessment = (isset($options['quiz_enable_user_cհoosing_anonymous_assessment']) && $options['quiz_enable_user_cհoosing_anonymous_assessment'] == 'on') ? true : false;

        /*
         * Passed or Failed quiz score html
         */
        $pass_score_html = "<div class='ays_score_message'></div>";
        
        
        /*
         * Quiz rate
         *
         * Generating HTML code
         */
        
        if(isset($options['rate_form_title'])){
            $rate_form_title = $this->ays_autoembed($options['rate_form_title']);
            $rate_form_title = $this->replace_message_variables($rate_form_title, $message_variables_data);
        }
        
        if(isset($options['enable_quiz_rate']) && $options['enable_quiz_rate'] == 'on'){

            // Thank you message | Review
            $quiz_review_thank_you_message = (isset($options['quiz_review_thank_you_message']) && $options['quiz_review_thank_you_message'] != '') ? $this->ays_autoembed( $options['quiz_review_thank_you_message'] ) : "";
            $quiz_review_thank_you_message = $this->replace_message_variables($quiz_review_thank_you_message, $message_variables_data);

            // Enable Comment Field
            $options['quiz_review_enable_comment_field'] = isset($options['quiz_review_enable_comment_field']) ? sanitize_text_field($options['quiz_review_enable_comment_field']) : 'on';
            $quiz_review_enable_comment_field = (isset($options['quiz_review_enable_comment_field']) && $options['quiz_review_enable_comment_field'] == 'on') ? true : false;

            $review_thank_you_message = "";
            if ( $quiz_review_thank_you_message != "" ) {
                $review_thank_you_message = "<div class='ays-quiz-review-thank-you-message ays_display_none'>". $quiz_review_thank_you_message ."</div>";
            }

            $review_comment_field_html = "";
            if ( $quiz_review_enable_comment_field ) {
                $review_comment_field_html = "<textarea id='quiz_rate_reason_".$id."' class='quiz_rate_reason' data-required='". $quiz_make_review_required ."' placeholder='". $quiz_review_placeholder_text ."'></textarea>";
            }

            $enable_user_cհoosing_anonymous_assessment_html = "";
            if( $quiz_enable_user_cհoosing_anonymous_assessment ){
                $enable_user_cհoosing_anonymous_assessment_html = "<div class='ays-quiz-user-cհoosing-anonymous-assessment'>
                <label for='ays-quiz-user-cհoosing-anonymous-assessment-{$id}'>". __("Anonymous feedback", 'quiz-maker') ."</label>
                <input type='checkbox' name='ays_quiz_user_cհoosing_anonymous_assessment' id='ays-quiz-user-cհoosing-anonymous-assessment-{$id}' class='ays-quiz-user-cհoosing-anonymous-assessment'value='on'/></div>";
            }

            $quiz_rate_html = "<div class='ays_quiz_rete'>
                <div>$rate_form_title</div>
                $enable_user_cհoosing_anonymous_assessment_html
                <div class='for_quiz_rate ui huge star rating' data-rating='0' data-max-rating='5'></div>
                <div class='ays-quiz-lds-spinner-box'><div class='lds-spinner-none'><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
                ". $all_review_link_html ."
                ". $review_thank_you_message ."
                <div class='for_quiz_rate_reason'>
                    ". $review_comment_field_html ."
                    <div class='ays_feedback_button_div'>
                        <button type='button' class='action-button ".$class_for_keyboard."'>". $this->buttons_texts['sendFeedbackButton'] ."</button>
                    </div>
                </div>
                <div><div class='lds-spinner2-none'><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
                <div class='quiz_rate_reasons_body'></div>
                <input type='hidden' class='ays_quiz_rate_reason_nonce' name='ays_quiz_rate_reason_nonce' value='". esc_attr( wp_create_nonce( 'ays_quiz_rate_reason_nonce' ) ) ."'>
            </div>";
        }
        
        
        
        /*
         * Quiz social sharing buttons
         *
         * Generating HTML code
         */

        // Heading for social buttons
        $social_buttons_heading = (isset($options['social_buttons_heading']) && $options['social_buttons_heading'] != '') ? stripslashes( wpautop( $options['social_buttons_heading'] ) ) : "";

        // Enable Linkedin button
        $options['quiz_enable_linkedin_share_button'] = isset($options['quiz_enable_linkedin_share_button']) ? sanitize_text_field($options['quiz_enable_linkedin_share_button']) : 'on';
        $quiz_enable_linkedin_share_button = (isset($options['quiz_enable_linkedin_share_button']) && $options['quiz_enable_linkedin_share_button'] == 'on') ? true : false;

        // Enable Facebook button
        $options['quiz_enable_facebook_share_button'] = isset($options['quiz_enable_facebook_share_button']) ? sanitize_text_field($options['quiz_enable_facebook_share_button']) : 'on';
        $quiz_enable_facebook_share_button = (isset($options['quiz_enable_facebook_share_button']) && $options['quiz_enable_facebook_share_button'] == 'on') ? true : false;

        // Enable Twitter button
        $options['quiz_enable_twitter_share_button'] = isset($options['quiz_enable_twitter_share_button']) ? sanitize_text_field($options['quiz_enable_twitter_share_button']) : 'on';
        $quiz_enable_twitter_share_button = (isset($options['quiz_enable_twitter_share_button']) && $options['quiz_enable_twitter_share_button'] == 'on') ? true : false;

        // Enable Whatsapp button
        $options['quiz_enable_whatsapp_share_button'] = isset($options['quiz_enable_whatsapp_share_button']) ? sanitize_text_field($options['quiz_enable_whatsapp_share_button']) : 'off';
        $quiz_enable_whatsapp_share_button = (isset($options['quiz_enable_whatsapp_share_button']) && $options['quiz_enable_whatsapp_share_button'] == 'on') ? true : false;

        // Enable VKontakte button
        $options['quiz_enable_vkontakte_share_button'] = isset($options['quiz_enable_vkontakte_share_button']) ? sanitize_text_field($options['quiz_enable_vkontakte_share_button']) : 'on';
        $quiz_enable_vkontakte_share_button = (isset($options['quiz_enable_vkontakte_share_button']) && $options['quiz_enable_vkontakte_share_button'] == 'on') ? true : false;

        if ( ! $quiz_enable_linkedin_share_button && ! $quiz_enable_facebook_share_button && ! $quiz_enable_twitter_share_button && ! $quiz_enable_whatsapp_share_button && ! $quiz_enable_vkontakte_share_button) {
            $quiz_enable_linkedin_share_button = true;
            $quiz_enable_facebook_share_button = true;
            $quiz_enable_twitter_share_button = true;
            $quiz_enable_whatsapp_share_button = true;
            $quiz_enable_vkontakte_share_button = true;
        }
        
        $ays_social_twitter_x_icon = "
            <svg xmlns='https://www.w3.org/2000/svg' viewBox='0 0 512 512' width='14' height='14' style='fill: #fff;'>
                <path d='M389.2 48h70.6L305.6 224.2 487 464H345L233.7 318.6 106.5 464H35.8L200.7 275.5 26.8 48H172.4L272.9 180.9 389.2 48zM364.4 421.8h39.1L151.1 88h-42L364.4 421.8z'></path>
            </svg>";
        
        if(isset($options['enable_social_buttons']) && $options['enable_social_buttons'] == 'on'){
            $ays_social_buttons .= "<div class='ays-quiz-social-shares'>";
                $ays_social_buttons .= "<div class='ays-quiz-social-shares-heading'>";
                    $ays_social_buttons .= $social_buttons_heading;
                $ays_social_buttons .= "</div>";
            
            if ( $quiz_enable_linkedin_share_button ) {
                $ays_social_buttons .= "
                    <!-- Branded LinkedIn button -->
                    <a class='ays-share-btn ays-to-share ays-share-btn-branded ays-share-btn-linkedin'
                       href='https://www.linkedin.com/shareArticle?mini=true&url=" . $actual_link . "'
                       title='". esc_attr__('Share on LinkedIn', 'quiz-maker') ."'>
                        <span class='ays-quiz-share-btn-icon'></span>
                        <span class='ays-share-btn-text'>LinkedIn</span>
                    </a>";
            }

            if ( $quiz_enable_facebook_share_button ) {
                $ays_social_buttons .= "
                    <!-- Branded Facebook button -->
                    <a class='ays-share-btn ays-to-share ays-share-btn-branded ays-share-btn-facebook'
                       href='https://www.facebook.com/sharer/sharer.php?u=" . $actual_link . "'
                       title='". esc_attr__('Share on Facebook', 'quiz-maker') ."'>
                        <span class='ays-quiz-share-btn-icon'></span>
                        <span class='ays-share-btn-text'>Facebook</span>
                    </a>";
            }

            if ( $quiz_enable_twitter_share_button ) {
                $ays_social_buttons .= "
                <!-- Branded X button -->
                <a class='ays-share-btn ays-to-share ays-share-btn-branded ays-share-btn-twitter'
                   href='https://x.com/share?url=" . $actual_link . "'
                   title='". esc_attr__('Share on X', 'quiz-maker') ."'>
                    <span class='ays-quiz-share-btn-icon'>". $ays_social_twitter_x_icon ."</span>
                    <!-- <span class='ays-share-btn-text'></span> -->
                </a>";
            }

            if ( $quiz_enable_whatsapp_share_button ) {
                $ays_social_buttons .= "
                <!-- Branded Whatsapp button -->
                <a class='ays-share-btn ays-to-share ays-share-btn-branded ays-share-btn-whatsapp'
                   href='https://api.whatsapp.com/send?text=" . rawurlencode( $actual_link ) . "'
                   title='". esc_attr__('Share on Whatsapp', 'quiz-maker') ."'>
                    <span class='ays-quiz-share-btn-icon'></span>
                    <span class='ays-share-btn-text'>Whatsapp</span>
                </a>";
            }

            if ( $quiz_enable_vkontakte_share_button ) {
                $ays_social_buttons .= "
                <!-- Branded VK button -->
                <a class='ays-share-btn ays-to-share ays-share-btn-branded ays-share-btn-vkontakte'
                   href='https://vk.com/share.php?url=" . $actual_link . "'
                   title='". esc_attr__('Share on VKontakte', 'quiz-maker') ."'>
                    <span class='ays-quiz-share-btn-icon'></span>
                    <span class='ays-share-btn-text'>VKontakte</span>
                </a>";
            }

            $ays_social_buttons .= "</div>";
        }
        
        
        
        /*
         * Quiz social media links
         *
         * Generating HTML code
         */

        // Heading for social media links
        $social_links_heading = (isset($options['social_links_heading']) && $options['social_links_heading'] != '') ? stripslashes( wpautop( $options['social_links_heading'] ) ) : "";

        // Social Media links

        $enable_social_links = (isset($options['enable_social_links']) && $options['enable_social_links'] == "on") ? true : false;
        $social_links = (isset($options['social_links'])) ? $options['social_links'] : array(
            'linkedin_link'     => '',
            'facebook_link'     => '',
            'twitter_link'      => '',
            'vkontakte_link'    => '',
            'instagram_link'    => '',
            'youtube_link'      => '',
            'behance_link'      => '',
        );
        $ays_social_links_array = array();

        $linkedin_link = isset($social_links['linkedin_link']) && $social_links['linkedin_link'] != '' ? esc_url($social_links['linkedin_link']) : '';
        $facebook_link = isset($social_links['facebook_link']) && $social_links['facebook_link'] != '' ? esc_url($social_links['facebook_link']) : '';
        $twitter_link = isset($social_links['twitter_link']) && $social_links['twitter_link'] != '' ? esc_url($social_links['twitter_link']) : '';
        $vkontakte_link = isset($social_links['vkontakte_link']) && $social_links['vkontakte_link'] != '' ? esc_url($social_links['vkontakte_link']) : '';
        $instagram_link = isset($social_links['instagram_link']) && $social_links['instagram_link'] != '' ? esc_url($social_links['instagram_link']) : '';
        $youtube_link = isset($social_links['youtube_link']) && $social_links['youtube_link'] != '' ? esc_url($social_links['youtube_link']) : '';
        $behance_link = isset($social_links['behance_link']) && $social_links['behance_link'] != '' ? esc_url($social_links['behance_link']) : '';

        if($linkedin_link != ''){
            $ays_social_links_array['Linkedin'] = $linkedin_link;
        }
        if($facebook_link != ''){
            $ays_social_links_array['Facebook'] = $facebook_link;
        }
        if($twitter_link != ''){
            $ays_social_links_array['Twitter'] = $twitter_link;
        }
        if($vkontakte_link != ''){
            $ays_social_links_array['VKontakte'] = $vkontakte_link;
        }
        if($instagram_link != ''){
            $ays_social_links_array['Instagram'] = $instagram_link;
        }
        if($youtube_link != ''){
            $ays_social_links_array['Youtube'] = $youtube_link;
        }
        if($behance_link != ''){
            $ays_social_links_array['Behance'] = $behance_link;
        }
        $ays_social_links = '';
        
        if($enable_social_links){
            $ays_social_links .= "<div class='ays-quiz-social-shares'>";
            
            if( $social_links_heading != "" ) {
                $ays_social_links .= "<div class='ays-quiz-social-links-heading'>";
                    $ays_social_links .= $social_links_heading;
                $ays_social_links .= "</div>";
            }

            foreach($ays_social_links_array as $media => $link){

                $icon_html = "";
                $custom_media = $media;
                if( strtolower($media) == 'twitter' ){
                    $icon_html = $ays_social_twitter_x_icon;
                    $custom_media = 'X';
                }

                $ays_social_links .= "<!-- Branded " . $media . " button -->
                    <a class='ays-share-btn ays-share-btn-branded ays-share-btn-rounded ays-share-btn-" . esc_attr(strtolower($media)) . "'
                        href='" . esc_url($link) . "'
                        target='_blank'
                        title='" . esc_attr($custom_media) . " link'>
                        <span class='ays-quiz-share-btn-icon'>". $icon_html ."</span>
                    </a>";
            }
                    
                    // "<!-- Branded Facebook button -->
                    // <a class='ays-share-btn ays-share-btn-branded ays-share-btn-facebook'
                    //     href='" . . "'
                    //     title='Share on Facebook'>
                    //     <span class='ays-quiz-share-btn-icon'></span>
                    // </a>
                    // <!-- Branded Twitter button -->
                    // <a class='ays-share-btn ays-share-btn-branded ays-share-btn-twitter'
                    //     href='" . . "'
                    //     title='Share on Twitter'>
                    //     <span class='ays-quiz-share-btn-icon'></span>
                    // </a>";
            $ays_social_links .= "</div>";
        }
        
        
        /*
         * Quiz loader
         *
         * Generating HTML code
         */
                
        $quiz_loader = 'default';
        
        if(isset($options['quiz_loader']) && $options['quiz_loader'] != ''){
            $quiz_loader = $options['quiz_loader'];
        }
        // Custom Text
        $quiz_loader_text_value = (isset($options['quiz_loader_text_value']) && $options['quiz_loader_text_value'] != '') ? stripslashes($options['quiz_loader_text_value']) : '';

        // Custom Gif
        $quiz_loader_custom_gif = (isset($options['quiz_loader_custom_gif']) && $options['quiz_loader_custom_gif'] != '') ? stripslashes($options['quiz_loader_custom_gif']) : '';

        //  Quiz loader custom gif width
        $quiz_loader_custom_gif_width = (isset($options['quiz_loader_custom_gif_width']) && $options['quiz_loader_custom_gif_width'] != '') ? absint( intval( $options['quiz_loader_custom_gif_width'] ) ) : 100;

        $quiz_loader_custom_gif_width_css = '';
        if ( $quiz_loader_custom_gif_width != '' ) {
            $quiz_loader_custom_gif_width_css = 'width: '. $quiz_loader_custom_gif_width .'px; height: auto; max-width: 100%;';
        }
        
        switch($quiz_loader){
            case 'default':
                $quiz_loader_html = "<div data-class='lds-ellipsis' data-role='loader' class='ays-loader'><div></div><div></div><div></div><div></div></div>";
                break;
            case 'circle':
                $quiz_loader_html = "<div data-class='lds-circle' data-role='loader' class='ays-loader'></div>";
                break;
            case 'dual_ring':
                $quiz_loader_html = "<div data-class='lds-dual-ring' data-role='loader' class='ays-loader'></div>";
                break;
            case 'facebook':
                $quiz_loader_html = "<div data-class='lds-facebook' data-role='loader' class='ays-loader'><div></div><div></div><div></div></div>";
                break;
            case 'hourglass':
                $quiz_loader_html = "<div data-class='lds-hourglass' data-role='loader' class='ays-loader'></div>";
                break;
            case 'ripple':
                $quiz_loader_html = "<div data-class='lds-ripple' data-role='loader' class='ays-loader'><div></div><div></div></div>";
                break;
            case 'text':
                if ($quiz_loader_text_value != '') {
                    $quiz_loader_html = "
                    <div class='ays-loader' data-class='text' data-role='loader'>
                        <p class='ays-loader-content'>". $quiz_loader_text_value ."</p>
                    </div>";
                }else{
                    $quiz_loader_html = "<div data-class='lds-ellipsis' data-role='loader' class='ays-loader'><div></div><div></div><div></div><div></div></div>";
                }
                break;
            case 'custom_gif':
                if ($quiz_loader_custom_gif != '') {
                    $quiz_loader_html = "
                    <div class='ays-loader' data-class='text' data-role='loader' style='text-align: center;'>
                        <img src='". $quiz_loader_custom_gif ."' class='ays-loader-content ays-loader-custom-gif-content' style='". $quiz_loader_custom_gif_width_css ."'>
                    </div>";
                }else{
                    $quiz_loader_html = "<div data-class='lds-ellipsis' data-role='loader' class='ays-loader'><div></div><div></div><div></div><div></div></div>";
                }
                break;    
            default:
                $quiz_loader_html = "<div data-class='lds-ellipsis' data-role='loader' class='ays-loader'><div></div><div></div><div></div><div></div></div>";
                break;
        }
        
        
    /*******************************************************************************************************/
        
        /*
         * Quiz limitations
         *
         * Blocking content
         *
         * Generating HTML code
         */
        
        $limit_users_html = "";
        $limit_users = null;
        
        /*
         * Quiz timer in tab title
         */
        
        if(isset($options['quiz_timer_in_title']) && $options['quiz_timer_in_title'] == "on"){
            $show_timer_in_title = "true";
        }else{
            $show_timer_in_title = "false";
        }

        // Timer Warning text color
        $quiz_timer_warning_text_color = (isset($options['quiz_timer_warning_text_color']) && $options['quiz_timer_warning_text_color'] != '') ? stripslashes ( esc_attr( $options[ 'quiz_timer_warning_text_color' ] ) ) : '#ff0000';
        
        /*
         * Quiz one time passing
         *
         * Generating HTML code
         */        

        // Limit users by option
        $limit_users_by = 'ip';
        $limit_users_attr = array();
        $check_cookie = null;

        if(isset($options['limit_users_by']) && $options['limit_users_by'] != ''){
            $limit_users_by = $options['limit_users_by'];
        }

        $limit_users_attr = array(
            'id' => $id,
            'name' => 'ays_quiz_cookie_',
            'title' => $title,
        );
        $check_cookie = $this->ays_quiz_check_cookie( $limit_users_attr );

        if (isset($options['limit_users']) && $options['limit_users'] == "on") {

            switch ( $limit_users_by ) {
                case 'ip':
                    $result = $this->get_user_by_ip($id);
                    if ( $check_cookie ) {
                        $remove_cookie = $this->ays_quiz_remove_cookie( $limit_users_attr );
                    }
                    break;
                case 'user_id':
                    if(is_user_logged_in()){
                        $user_id = get_current_user_id();
                        $result = $this->get_limit_user_by_id($id, $user_id);
                    }else{
                        $result = 0;
                    }

                    if ( $check_cookie ) {
                        $remove_cookie = $this->ays_quiz_remove_cookie( $limit_users_attr );
                    }
                    break;
                case 'cookie':
                    if ( ! $check_cookie ) {
                        $result = 0;
                    }else{
                        $result = 1;
                    }
                    break;
                case 'ip_cookie':
                    $check_user_by_ip = $this->get_user_by_ip($id);
                    if($check_cookie || $check_user_by_ip > 0){
                        $result = 1;
                    }
                    elseif(! $check_cookie || $check_user_by_ip <= 0){
                        $result = 0;
                    }
                    break;
                default:
                    $result = 0;
                    if ( $check_cookie ) {
                        $remove_cookie = $this->ays_quiz_remove_cookie( $limit_users_attr );
                    }
                    break;
            }


            if ($result != 0) {
                $limit_users = true;
                $timer_row = "";
                if(isset($options['redirection_delay']) && $options['redirection_delay'] != ''){
                    if(isset($options['redirect_url']) && $options['redirect_url'] != ''){
                        $timer_row = "<qm_rurl class='ays_redirect_url' style='display:none'>" . 
                                $options['redirect_url'] . 
                            "</qm_rurl>                                
                            <div class='ays-quiz-timer' data-show-in-title='".$show_timer_in_title."' data-timer='" . $options['redirection_delay'] . "'>". 
                                __( "Redirecting after", 'quiz-maker' ). " " . 
                                $this->secondsToWords($options['redirection_delay']) . 
                                "<EXTERNAL_FRAGMENT></EXTERNAL_FRAGMENT>                                
                            </div>";
                    }
                }

                $limitation_message = (isset($options['limitation_message']) && $options['limitation_message'] != '') ? $this->ays_autoembed($options['limitation_message']) : __( 'You have already passed this quiz.', 'quiz-maker' );
                $limitation_message = $this->replace_message_variables($limitation_message, $message_variables_data);
                
                $limit_users_html = $timer_row . "<div style='color:" . $text_color . "' class='ays_block_content'>" .  $limitation_message . "</div><style>form{min-height:0 !important;}</style>";
            }
        }else{
            $limit_users = false;
            if ( $check_cookie ) {
                $remove_cookie = $this->ays_quiz_remove_cookie( $limit_users_attr );
            }
        }
        
        
        
        /*
         * Quiz only for logged in users
         *
         * Generating HTML code
         */  
        
        // Show login form for not logged in users
        $options['show_login_form'] = isset($options['show_login_form']) ? $options['show_login_form'] : 'off';
        $show_login_form = (isset($options['show_login_form']) && $options['show_login_form'] == "on") ? true : false;
        $quiz_login_form = "";
        if($show_login_form){
            $ays_login_button_text = $this->buttons_texts['loginButton'];
            $args = array(
                'echo'          => false,
                'id_username'   => 'ays_user_login',
                'id_password'   => 'ays_user_pass',
                'id_remember'   => 'ays_rememberme',
                'id_submit'     => 'ays-submit',
                'label_log_in'  => $ays_login_button_text,
            );
            $quiz_login_form = "<div class='ays_quiz_login_form'>" . wp_login_form( $args ) . "</div>";
        }

        global $wp_roles;
        
        if(isset($options['enable_logged_users']) && $options['enable_logged_users'] == 'on' && !is_user_logged_in()){
            $enable_logged_users = 'only_logged_users';
            if(isset($options['enable_logged_users_message']) && $options['enable_logged_users_message'] != ""){
                $logged_users_message = $this->ays_autoembed($options['enable_logged_users_message']);
                $logged_users_message = $this->replace_message_variables($logged_users_message, $message_variables_data);
            }else{
                $logged_users_message = $this->default_texts['loggedInUsersMessageText'];
                $logged_users_message = $this->ays_autoembed($logged_users_message);
                $logged_users_message = $this->replace_message_variables($logged_users_message, $message_variables_data);
            }
            if($logged_users_message !== null){
                $user_massage = '<div class="logged_in_message">' . $logged_users_message . '</div>';
            }else{
                $user_massage = null;
            }
        }else{
            $user_massage = null;
            $enable_logged_users = '';
            $user_role = (isset($options['user_role']) && $options['user_role'] != '') ? $options['user_role'] : ''; 
            if (isset($options['enable_restriction_pass']) && $options['enable_restriction_pass'] == 'on' && !empty( $user_role )) {
                $user = wp_get_current_user();
                $user_roles   = $wp_roles->role_names;
                $message = (isset($options['restriction_pass_message']) && $options['restriction_pass_message'] != '') ? $options['restriction_pass_message'] : __('Permission Denied', 'quiz-maker');
                $user_massage = '<div class="logged_in_message">' . $this->ays_autoembed($message) . '</div>';
                $user_massage = $this->replace_message_variables($user_massage, $message_variables_data);
                
                if (is_array($user_role)) {
                    foreach($user_role as $key => $role){
                        if(in_array($role, $user_roles)){
                            $user_role[$key] = array_search($role, $user_roles);
                        }                        
                    }
                }else{
                    if(in_array($user_role, $user_roles)){
                        $user_role = array_search($user_role, $user_roles);
                    }
                }

                if(is_array($user_role)){
                    foreach($user_role as $role){                        
                        if (in_array(strtolower($role), (array)$user->roles)) {
                            $user_massage = null;
                            break;
                        }
                    }                    
                }else{
                    if (in_array(strtolower($user_role), (array)$user->roles)) {
                        $user_massage = null;
                    }
                }
            }
        }
        
        if($user_massage !== null){
            if( !is_user_logged_in() ){
                $user_massage .= $quiz_login_form;
            }
        }

        // Limitation tackers of quiz
        $enable_tackers_count = false;
        $tackers_count = 0;
        $tackers_message = "<div style='padding:50px;'><p>" . __( "This quiz is expired!", 'quiz-maker' ) . "</p></div>";
        $options['enable_tackers_count'] = !isset($options['enable_tackers_count']) ? 'off' : $options['enable_tackers_count'];
        if(isset($options['enable_tackers_count']) && $options['enable_tackers_count'] == 'on'){
            $enable_tackers_count = true;
        }
        if(isset($options['tackers_count']) && $options['tackers_count'] != ''){
            $tackers_count = intval($options['tackers_count']);
        }

        // Quiz takers message
        $quiz_tackers_message = ( isset($options['quiz_tackers_message']) && $options['quiz_tackers_message'] != '' ) ? stripslashes( wpautop( $options['quiz_tackers_message'] ) ) : __( "This quiz is expired!", 'quiz-maker' );
        $quiz_tackers_message = $this->replace_message_variables($quiz_tackers_message, $message_variables_data);

        if ( $quiz_tackers_message != __( "This quiz is expired!", 'quiz-maker' ) ) {
            $tackers_message = "<div class='ays-quiz-limitation-count-of-takers'>". $quiz_tackers_message ."</div>";
        } else {
            $tackers_message = "<div class='ays-quiz-limitation-count-of-takers'><p>" . __( "This quiz is expired!", 'quiz-maker' ) . "</p></div>";
        }
        
        
    /*******************************************************************************************************/
        
        /*
         * Schedule quiz
		 * Check is quiz expired
         */
        
        $is_expired = false;
        $active_date_check = false;
        $UTC_seconds = null;
        $startDate = '';
        $endDate = '';
        $startDate_atr = '';
        $endDate_atr = '';
        $current_time = strtotime(current_time( "Y:m:d H:i:s" ));
        $activeInterval = isset( $options['activeInterval'] ) && $options['activeInterval'] != '' ? $options['activeInterval'] : current_time( 'mysql' );
        $deactiveInterval = isset( $options['deactiveInterval'] ) && $options['deactiveInterval'] != '' ? $options['deactiveInterval'] : current_time( 'mysql' );
        $startDate = strtotime( $activeInterval );
        $endDate   = strtotime( $deactiveInterval );

        // Timezone | Schedule the quiz
        $ays_quiz_schedule_timezone = (isset($options['quiz_schedule_timezone']) && $options['quiz_schedule_timezone'] != '') ? sanitize_text_field( $options['quiz_schedule_timezone'] ) : get_option( 'timezone_string' );

        if ( class_exists( 'DateTimeZone' )) {

            // Remove old Etc mappings. Fallback to gmt_offset.
            if ( strpos( $ays_quiz_schedule_timezone, 'Etc/GMT' ) !== false ) {
                $ays_quiz_schedule_timezone = '';
            }

            $current_offset = get_option( 'gmt_offset' );
            if ( empty( $ays_quiz_schedule_timezone ) ) { // Create a UTC+- zone if no timezone string exists.

                if ( 0 == $current_offset ) {
                    $ays_quiz_schedule_timezone = 'UTC+0';
                } elseif ( $current_offset < 0 ) {
                    $ays_quiz_schedule_timezone = 'UTC' . $current_offset;
                } else {
                    $ays_quiz_schedule_timezone = 'UTC+' . $current_offset;
                }
            }

            $if_timezone_UTC = false;
            if ( strpos($ays_quiz_schedule_timezone, 'UTC+') !== false ) {
                $if_timezone_UTC = true;

                $UTC_val_arr = explode('+', $ays_quiz_schedule_timezone );

                $UTC_val     = ( isset( $UTC_val_arr[1] ) && $UTC_val_arr[1] != '' ) ? $UTC_val_arr[1] : 0;

                $UTC_seconds = (int) ($UTC_val * 3600);

            } elseif ( strpos($ays_quiz_schedule_timezone, 'UTC-') !== false ) {
                $if_timezone_UTC = true;

                $UTC_val_arr = explode('-', $ays_quiz_schedule_timezone );

                $UTC_val     = ( isset( $UTC_val_arr[1] ) && $UTC_val_arr[1] != '' ) ? $UTC_val_arr[1] : 0;

                $UTC_seconds =  (int) ( -1 * ( $UTC_val * 3600 ) );
            }

            if (in_array( $ays_quiz_schedule_timezone , DateTimeZone::listIdentifiers()) && ! $if_timezone_UTC ) {

                $Date_Time_Zone = new DateTime("now", new DateTimeZone( $ays_quiz_schedule_timezone ));
                $current_time   = strtotime( $Date_Time_Zone->format( "Y:m:d H:i:s" ) );
            } else {
                if ( ! is_null( $UTC_seconds ) && ! empty( $UTC_seconds ) ) {
                    $Date_Time_Zone = new DateTime("now", new DateTimeZone( 'UTC' ));
                    $current_time   = strtotime( $Date_Time_Zone->format( "Y:m:d H:i:s" ) ) + ( $UTC_seconds );
                } else {
                    $current_time = strtotime(current_time( "Y:m:d H:i:s" ));
                }
            }
        }

        $expired_quiz_message = "<p class='ays-fs-subtitle'>" . __('The quiz has expired.', 'quiz-maker') . "</p>";

        if (isset($options['active_date_check']) && $options['active_date_check'] == "on") {
            $active_date_check = true;

            if (isset($options['activeInterval']) && !empty($options['activeInterval'])) {
                $startDate_atr = $startDate - $current_time;
            }elseif (isset($options['deactiveInterval']) && !empty($options['deactiveInterval'])) {
                $endDate_atr = $endDate - $current_time;
            }

            // show timer
            $activeDateCheck =  isset($options['active_date_check']) && !empty($options['active_date_check']) ? true : false;
            $activeDeactiveDateCheck =  isset($options['deactiveInterval']) && !empty($options['deactiveInterval']) ? true : false;
            $show_timer_type = isset($options['show_timer_type']) && !empty($options['show_timer_type']) ? $options['show_timer_type'] : 'countdown';
            $activeActiveDateCheck =  isset($options['activeInterval']) && !empty($options['activeInterval']) ? true : false;

            $options['quiz_dont_show_quiz'] = isset($options['quiz_dont_show_quiz']) ? $options['quiz_dont_show_quiz'] : 'off';
            $quiz_dont_show_quiz = (isset($options['quiz_dont_show_quiz']) && $options['quiz_dont_show_quiz'] == 'on') ? true : false;

            $show_timer = '';
            if ($activeDateCheck && $activeActiveDateCheck && $active_date_check) {
                if (isset($options['show_schedule_timer']) && $options['show_schedule_timer'] == 'on') {
                    $show_timer .= "<div class='ays_quiz_show_timer'>";
                    if ($show_timer_type == 'countdown') {
                        $show_timer .= '<p class="show_timer_countdown" data-timer_countdown="'.$startDate_atr.'"></p>';
                    }else if ($show_timer_type == 'enddate') {
                        $show_timer .= '<p class="show_timer_countdown">' . esc_html($this->default_texts['quizScheduleStartMessageText']);
                        $show_timer .= ' ' . date_i18n('H:i:s F jS, Y', intval($startDate));
                        $show_timer .= '</p>';
                    }
                    $show_timer .= "</div>";
                }
            }

            if ($startDate > $current_time) {
                $is_expired = true;
                if( !$is_elementor_exists && !$is_editor_exists && !$is_beaver_builder_exists ){
                    if ($quiz_dont_show_quiz) {
                        return "";
                    }
                }
                if(isset($options['active_date_pre_start_message'])){

                    $active_date_pre_start_message = isset( $options['active_date_pre_start_message'] ) && $options['active_date_pre_start_message'] != "" ? $this->ays_autoembed($options['active_date_pre_start_message']) : "";
                    $active_date_pre_start_message = $this->replace_message_variables($active_date_pre_start_message, $message_variables_data);

                    $expired_quiz_message = "<div class='step active-step'>
                        <div class='ays-abs-fs'>
                            ".$show_timer."
                            " . $active_date_pre_start_message . "
                        </div>
                    </div>";
                }else{
                    $expired_quiz_message = "<div class='step active-step'>
                        <div class='ays-abs-fs'>
                            ".$show_timer."
                            <p class='ays-fs-subtitle'>" . __('The quiz will be available soon.', 'quiz-maker') . "</p>
                        </div>
                    </div>";
                }
            }elseif ($endDate < $current_time) {
                $is_expired = true;
                if( !$is_elementor_exists && !$is_editor_exists && !$is_beaver_builder_exists ){
                    if ($quiz_dont_show_quiz) {
                        return "";
                    }
                }
                if(isset($options['active_date_message']) && $options['active_date_message'] != ''){
                    $expired_quiz_message = "<div class='step active-step' data-message-exist='true'>
                        <div class='ays-abs-fs'>
                            " . $this->ays_autoembed( $options['active_date_message'] ) . "
                        </div>
                    </div>";
                }else{
                    $expired_quiz_message = "<div class='step active-step'>
                        <div class='ays-abs-fs'>
                            <p class='ays-fs-subtitle'>" . __('The quiz has expired.', 'quiz-maker') . "</p>
                        </div>
                    </div>";
                }
            }
        }
        
        
    /*******************************************************************************************************/

        /*
         * Quiz main content
         *
         * Generating HTML code
         */
        
        
        if($show_quiz_image && $quiz_image != ""){
            $quiz_image_alt_text = $this->ays_quiz_get_image_id_by_url($quiz_image);

            $quiz_image = "<img src='{$quiz_image}' alt='". $quiz_image_alt_text ."' class='ays_quiz_image'>";
        }else{
            $quiz_image = "";
        }

        $ays_protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
         
        $quiz_current_page_link = esc_url( $ays_protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
        
        if($show_quiz_title){
            $title = "<p class='ays-fs-title'>" . $title . "</p>";
        }else{
            $title = "";
        }

        if($show_quiz_desc){
            $description = "<div class='ays-fs-subtitle'>" . $description . "</div>";
        }else{
            $description = "";
        }


        $main_content_first_part = "{$timer_row}
            {$rw_asnwers_sounds_html}
            {$ays_quiz_music_sound}
            <div class='step active-step'>
                <div class='ays-abs-fs ays-start-page'>
                    {$show_cd_and_author}
                    {$quiz_image}
                    {$title}
                    {$description}
                    <input type='hidden' name='ays_quiz_id' value='{$id}'/>
                    <input type='hidden' name='ays_quiz_finish_nonce' value='". esc_attr( wp_create_nonce( 'ays_quiz_finish_nonce' ) ) ."'>
                    <input type='hidden' name='ays_quiz_curent_page_link' class='ays-quiz-curent-page-link' value='{$quiz_current_page_link}'/>
                    " . (isset($quiz_questions_ids) ? "<input type='hidden' name='ays_quiz_questions' value='{$quiz_questions_ids}'>" : "") . "
                    {$quiz_password_message_html}
                    {$password_message}
                    {$quiz_start_button}
                    {$quiz_block_preview_message}
                    </div>
                </div>";
        
        if($limit_users === false || $limit_users === null){
            $restart_button_html = $restart_button;
        }else{
            $restart_button_html = "";
        }
            
        $main_content_last_part = "<div class='step ays_thank_you_fs'>
            <div class='ays-abs-fs ays-end-page'>".
            $quiz_loader_html .
            "<div class='ays_quiz_results_page'>".
                $pass_score_html .
                "<div class='ays_message'></div>" .
                $show_score_html .
                $show_average .
                $ays_social_buttons .
                $ays_social_links .
                $progress_bar_html .
                "<p class='ays_restart_button_p'>".
                    $restart_button_html .
                    $exit_button .
                "</p>".
                $quiz_rate_html .
                "</div>
            </div>
        </div>";

        if (! $show_information_form) {
            if(is_user_logged_in()){
                $show_form = null;
            }
        }

        switch( $quiz_arrow_type ){
            case 'default':
                $quiz_arrow_type_class_right = "ays_fa_arrow_right";
                break;
            case 'long_arrow':
                $quiz_arrow_type_class_right = "ays_fa_long_arrow_right";
                break;
            case 'arrow_circle_o':
                $quiz_arrow_type_class_right = "ays_fa_arrow_circle_o_right";
                break;
            case 'arrow_circle':
                $quiz_arrow_type_class_right = "ays_fa_arrow_circle_right";
                break;
            default:
                $quiz_arrow_type_class_right = "ays_fa_arrow_right";
                break;
        }

        $quiz_theme_type = isset($options['quiz_theme']) && $options['quiz_theme'] != "" ? esc_attr( stripslashes($options['quiz_theme']) ) : "";

        if($show_form != null){
            if ($options['information_form'] == "after") {
                $main_content_last_part = "<div class='step'>
                                <div class='ays-abs-fs ays-end-page information_form'>
                                <div class='ays-form-title'>{$form_title}</div>
                                    " . $form_inputs . "
                                    <div class='ays_buttons_div'>
                                        <i class='" . ($enable_arrows ? '' : 'ays_display_none') . " ays_fa ays_fa_flag_checkered ays_finish action-button ays_arrow ays_next_arrow ".$class_for_keyboard."'></i>
                                        <input type='submit' name='ays_finish_quiz' class='" . ($enable_arrows ? 'ays_display_none' : '') . " ays_next ays_finish action-button ".$class_for_keyboard."' value='" . $this->buttons_texts['seeResultButton'] . "'/>
                                    </div>
                                </div>
                              </div>" . $main_content_last_part;

            } elseif ($options['information_form'] == "before") {
                $main_content_first_part = $main_content_first_part . "<div class='step'>
                                    <div class='ays-abs-fs ays-start-page information_form'>
                                    <div class='ays-form-title'>{$form_title}</div>
                                        " . $form_inputs . "
                                        <div class='ays_buttons_div'>
                                            <i class='ays_fa " . $quiz_arrow_type_class_right . " ays_next action-button ays_arrow ays_next_arrow ".$class_for_keyboard." " . ($enable_arrows ? '' : 'ays_display_none') . "'></i>
                                            <input type='button' class='ays_next action-button ".$class_for_keyboard." " . ($enable_arrows ? 'ays_display_none' : '') . "' value='" . $this->buttons_texts['nextButton'] . "' />
                                        </div>
                                    </div>
                                  </div>" ;

            }
        }else{
            $options['information_form'] = "disable";
        }
        
        
    /*******************************************************************************************************/
        
        /*
         * Script for getting quiz options
         *
         * Script for question type dropdown
         *
         * Generating HTML code
         */
        
        
        if(isset($options['submit_redirect_delay'])){
            if($options['submit_redirect_delay'] == ''){
                $options['submit_redirect_delay'] = 0;
            }

            if( $options['submit_redirect_delay'] === 0 ){
                $options['submit_redirect_after'] = 0;
            } else {
                $options['submit_redirect_after'] = $this->secondsToWords( absint($options['submit_redirect_delay']) );
            }
        }
        
        $options['rw_answers_sounds'] = $enable_rw_asnwers_sounds;
        
        $quiz_content_script = "<script>";
        unset($quiz['options']);
        $quiz_options = $options;
        foreach($quiz as $k => $q){
            $quiz_options[$k] = $q;
        }

        // Animation Top (px)
        $quiz_animation_top = (isset($settings_options['quiz_animation_top']) && $settings_options['quiz_animation_top'] != 0) ? absint(intval($settings_options['quiz_animation_top'])) : 100;
        $settings_options['quiz_enable_animation_top'] = isset($settings_options['quiz_enable_animation_top']) ? $settings_options['quiz_enable_animation_top'] : 'on';
        $quiz_enable_animation_top = (isset($settings_options['quiz_enable_animation_top']) && $settings_options['quiz_enable_animation_top'] == "on") ? 'on' : 'off';

        $quiz_options['quiz_animation_top'] = $quiz_animation_top;
        $quiz_options['quiz_enable_animation_top'] = $quiz_enable_animation_top;
        
        if ($limit_users) {
            if($limit_users_by == 'ip'){
                $result = $this->get_user_by_ip($id);
            }elseif($limit_users_by == 'user_id'){
                if(is_user_logged_in()){
                    $user_id = get_current_user_id();
                    $result = $this->get_limit_user_by_id($id, $user_id);
                }else{
                    $result = 0;
                }
            }else{
                $result = 0;
            }
            if ($result == 0) {
                $quiz_content_script .= "
                    if(typeof aysQuizOptions === 'undefined'){
                        var aysQuizOptions = [];
                    }
                    aysQuizOptions['".$id."']  = '" . base64_encode(json_encode($quiz_options)) . "';";
            }
        }else{
            $quiz_content_script .= "
                if(typeof aysQuizOptions === 'undefined'){
                    var aysQuizOptions = [];
                }
                aysQuizOptions['".$id."']  = '" . base64_encode(json_encode($quiz_options)) . "';";
        }
        $quiz_content_script .= "
        </script>";
        
    /*******************************************************************************************************/
        
        /*
         * Styles for quiz
         *
         * Generating HTML code
         */
        
        
        $quest_animation = 'none';
        
        if(isset($options['quest_animation']) && $options['quest_animation'] != ''){
            $quest_animation = $options['quest_animation'];
        }
        
        $quiz_styles = "<style>
            div#ays-quiz-container-" . $id . " * {
                box-sizing: border-box;
            }

            #ays-quiz-container-" . $id . " [id^='ays_finish_quiz_'] div.step div.ays-abs-fs {
                width: ". $quiz_content_max_width ."%;
            }

            /* Styles for Internet Explorer start */
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " {
                " . $ie_container_css . "
            }";

        if($ie_container_css != ''){
            // $quiz_styles .= "#ays-quiz-container-" . $id . " .ays_next.action-button,
            //                 #ays-quiz-container-" . $id . " .ays_previous.action-button{
            //                     margin: 10px 5px;
            //                 }";

            $quiz_styles .= "
            /*
            #ays-quiz-container-" . $id . " .ays_next.action-button,
            #ays-quiz-container-" . $id . " .ays_previous.action-button{
                margin: 10px 5px;
            }
            */

            #ays-quiz-container-" . $id . " .ays_block_content{
                margin: 0 auto;
                word-break: break-all;
            }

            ";

        }
                
        $quiz_styles .= "

            /* Styles for Quiz container */
            #ays-quiz-container-" . $id . "{
                min-height: " . $quiz_height . "px;
                width:" . $quiz_width . ";
                background-color:" . $bg_color . ";
                background-position:" . $quiz_bg_image_position . ";";

        if($ays_quiz_bg_image !== null){
            $quiz_styles .=  "background-image: url('$ays_quiz_bg_image');";
        } elseif($enable_background_gradient) {
            $quiz_styles .=  "background-image: linear-gradient($quiz_gradient_direction, $background_gradient_color_1, $background_gradient_color_2);";
        }

        if($quiz_modified_border_radius != ""){
            $quiz_styles .= $quiz_modified_border_radius;
        }else{
            $quiz_styles .=  "border-radius:" . $quiz_border_radius . "px;";
        }

        if($enable_box_shadow){
            $quiz_styles .=  "box-shadow: ". $box_shadow_offsets ." 1px " . $this->hex2rgba($box_shadow_color, '0.4') . ";";
        }else{
            $quiz_styles .=  "box-shadow: none;";
        }
        if($enable_border){
            $quiz_styles .=  "border-width: " . $quiz_border_width.'px;'.
                           "border-style: " . $quiz_border_style.';'.
                           "border-color: " . $quiz_border_color.';';
        }else{
            $quiz_styles .=  "border: none;";
        }

        $quiz_styles .= "}

            /* Styles for questions */
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " div.step {
                min-height: " . $quiz_height . "px;
            }

            /* Styles for text inside quiz container */
            #ays-quiz-container-" . $id . " .ays-start-page *:not(input):not([class^='enlighter']),
            #ays-quiz-container-" . $id . " .ays_question_hint,
            #ays-quiz-container-" . $id . " label[for^=\"ays-answer-\"],
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " p,
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-fs-title,
            #ays-quiz-container-" . $id . " .ays-fs-subtitle,
            #ays-quiz-container-" . $id . " .logged_in_message,
            #ays-quiz-container-" . $id . " .ays_score_message,
            #ays-quiz-container-" . $id . " .ays_message{
               color: " . $text_color . ";
               outline: none;
            }

            div#ays-quiz-container-" . $id . " .ays_question_hint.ays-quiz-keyboard-active,
            div#ays-quiz-container-" . $id . " .ays_restart_button.ays-quiz-keyboard-active {
                outline: revert;
            }

            #ays-quiz-container-" . $id . " .ays-quiz-password-message-box,
            #ays-quiz-container-" . $id . " .ays-quiz-question-note-message-box,
            #ays-quiz-container-" . $id . " .ays_quiz_question,
            #ays-quiz-container-" . $id . " .ays-quiz-question-category-box,
            #ays-quiz-container-" . $id . " .ays_quiz_question *:not([class^='enlighter']) {
                color: " . $text_color . ";
            }

            #ays-quiz-container-" . $id . " .ays-quiz-question-category-box em {
                color: " . $text_color . ";
            }

            #ays-quiz-container-" . $id . " textarea,
            #ays-quiz-container-" . $id . " input::first-letter,
            #ays-quiz-container-" . $id . " select::first-letter,
            #ays-quiz-container-" . $id . " option::first-letter {
                color: initial !important;
            }
            
            #ays-quiz-container-" . $id . " p::first-letter:not(.ays_no_questions_message) {
                color: " . $text_color . " !important;
                background-color: transparent !important;
                font-size: inherit !important;
                font-weight: inherit !important;
                float: none !important;
                line-height: inherit !important;
                margin: 0 !important;
                padding: 0 !important;
            }
            
            
            #ays-quiz-container-" . $id . " .select2-container,
            #ays-quiz-container-" . $id . " .ays-field * {
                font-size: ".$answers_font_size."px !important;
            }
    
            #ays-quiz-container-" . $id . " .ays_quiz_question p {
                font-size: ".$question_font_size."px;
                text-align: inherit;
            }

            #ays-quiz-container-" . $id . " .ays-fs-subtitle p {
                text-align:  ". $quiz_question_text_alignment ." ;
            }

            #ays-quiz-container-" . $id . " .ays_quiz_question {
                text-align:  ". $quiz_question_text_alignment ." ;
                margin-bottom: 10px;
            }

            #ays-quiz-container-" . $id . " .ays_quiz_question pre {
                max-width: 100%;
                white-space: break-spaces;
            }

            /* Mango theme (Version: 1.4.202301041836) | tid=32449 | Start */
            #ays-quiz-container-" . $id . " .ays_quiz_question img {
                float: unset;
                margin: 0;
            }
            /* Mango theme (Version: 1.4.202301041836) | End */

            #ays-quiz-container-" . $id . " .ays-quiz-timer p {
                font-size: 16px;
            }

            #ays-quiz-container-" . $id . " section.ays_quiz_redirection_timer_container hr,
            #ays-quiz-container-" . $id . " section.ays_quiz_timer_container hr {
                margin: 0;
            }

            #ays-quiz-container-" . $id . " section.ays_quiz_timer_container.ays_quiz_timer_red_warning .ays-quiz-timer {
                color: ". $quiz_timer_warning_text_color .";
            }

            #ays-quiz-container-" . $id . " .ays_thank_you_fs p {
                text-align: center;
            }

            #ays-quiz-container-" . $id . " .ays_quiz_results_page .ays_score span {
                visibility: visible;
            }

            #ays-quiz-container-" . $id . " input[type='button'],
            #ays-quiz-container-" . $id . " input[type='submit'] {
                color: " . $buttons_text_color . " !important;
            }

            #ays-quiz-container-" . $id . " input[type='button']{
                outline: none;
            }

            #ays-quiz-container-" . $id . " .information_form input[type='text'],
            #ays-quiz-container-" . $id . " .information_form input[type='url'],
            #ays-quiz-container-" . $id . " .information_form input[type='number'],
            #ays-quiz-container-" . $id . " .information_form input[type='email'],
            #ays-quiz-container-" . $id . " .information_form input[type='checkbox'],
            #ays-quiz-container-" . $id . " .information_form input[type='tel'],
            #ays-quiz-container-" . $id . " .information_form textarea,
            #ays-quiz-container-" . $id . " .information_form select,
            #ays-quiz-container-" . $id . " .information_form option {
                color: initial !important;
                outline: none;
                background-image: unset;
            }

            #ays-quiz-container-" . $id . " .wrong_answer_text{
                color:#ff4d4d;
            }
            #ays-quiz-container-" . $id . " .right_answer_text{
                color:#33cc33;
            }

            #ays-quiz-container-" . $id . " .wrong_answer_text p {
                font-size:" . $wrong_answers_font_size . "px;
            }

            #ays-quiz-container-" . $id . " .ays_questtion_explanation p {
                font-size:" . $quest_explanation_font_size . "px;
            }

            #ays-quiz-container-" . $id . " .wrong_answer_text *:not(strong) {
                text-transform:" . $quiz_wrong_answer_text_transform . ";
                text-decoration: ". $quiz_wrong_answers_text_decoration .";
                letter-spacing: ". $quiz_wrong_answers_letter_spacing ."px;
                font-weight: ". $quiz_wrong_answers_font_weight .";
            }

            #ays-quiz-container-" . $id . " .ays_questtion_explanation *:not(strong) {
                text-transform:" . $quiz_quest_explanation_text_transform . ";
                text-decoration: ". $quiz_quest_explanation_text_decoration .";
                letter-spacing: ". $quiz_quest_explanation_letter_spacing ."px;
                font-weight: ". $quiz_quest_explanation_font_weight .";
            }

            #ays-quiz-container-" . $id . " .right_answer_text *:not(strong) {
                text-transform:" . $quiz_right_answer_text_transform . ";
                text-decoration: ". $quiz_right_answers_text_decoration .";
                letter-spacing: ". $quiz_right_answers_letter_spacing ."px;
                font-weight: ". $quiz_right_answers_font_weight .";
            }

            #ays-quiz-container-" . $id . " .right_answer_text p {
                font-size:" . $right_answers_font_size . "px;
            }

            #ays-quiz-container-" . $id . " .ays-quiz-question-note-message-box p {
                font-size:" . $note_text_font_size . "px;
            }

            #ays-quiz-container-" . $id . " .ays-quiz-question-note-message-box *:not(strong) {
                text-transform:" . $quiz_admin_note_text_transform . ";
                text-decoration: ". $quiz_admin_note_text_decoration .";
                letter-spacing: ". $quiz_admin_note_letter_spacing ."px;
                font-weight: ". $quiz_admin_note_font_weight .";
            }
            
            #ays-quiz-container-" . $id . " .ays_cb_and_a,
            #ays-quiz-container-" . $id . " .ays_cb_and_a * {
                color: " . $this->hex2rgba($text_color) . ";
                text-align: center;
            }

            /* Quiz textarea height */
            #ays-quiz-container-" . $id . " textarea {
                height: ". $quiz_textarea_height ."px;
                min-height: ". $quiz_textarea_height ."px;
            }

            /* Quiz rate and passed users count */
            #ays-quiz-container-" . $id . " .ays_quizn_ancnoxneri_qanak,
            #ays-quiz-container-" . $id . " .ays_quiz_rete_avg {
                color:" . $bg_color . " !important;
                background-color:" . $text_color . ";   
            }

            #ays-quiz-container-" . $id . " .ays-questions-container > .ays_quizn_ancnoxneri_qanak {
                padding: 5px 20px;
            }
            #ays-quiz-container-" . $id . " div.for_quiz_rate.ui.star.rating .icon {
                color: " . $this->hex2rgba($text_color, '0.35') . ";
            }
            #ays-quiz-container-" . $id . " .ays_quiz_rete_avg div.for_quiz_rate_avg.ui.star.rating .icon {
                color: " . $this->hex2rgba($bg_color, '0.5') . ";
            }

            #ays-quiz-container-" . $id . " .ays_quiz_rete .ays-quiz-rate-link-box .ays-quiz-rate-link {
                color: " . $text_color . ";
            }

            /* Loaders */            
            #ays-quiz-container-" . $id . " div.lds-spinner,
            #ays-quiz-container-" . $id . " div.lds-spinner2 {
                color: " . $text_color . ";
            }
            #ays-quiz-container-" . $id . " div.lds-spinner div:after,
            #ays-quiz-container-" . $id . " div.lds-spinner2 div:after {
                background-color: " . $text_color . ";
            }
            #ays-quiz-container-" . $id . " .lds-circle,
            #ays-quiz-container-" . $id . " .lds-facebook div,
            #ays-quiz-container-" . $id . " .lds-ellipsis div{
                background: " . $text_color . ";
            }
            #ays-quiz-container-" . $id . " .lds-ripple div{
                border-color: " . $text_color . ";
            }
            #ays-quiz-container-" . $id . " .lds-dual-ring::after,
            #ays-quiz-container-" . $id . " .lds-hourglass::after{
                border-color: " . $text_color . " transparent " . $text_color . " transparent;
            }

            /* Stars */
            #ays-quiz-container-" . $id . " .ui.rating .icon,
            #ays-quiz-container-" . $id . " .ui.rating .icon:before {
                font-family: Rating !important;
            }

            /* Progress bars */
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-progress {
                border-color: " . $this->hex2rgba($text_color, '0.8') . ";
            }
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-progress-bg {
                background-color: " . $this->hex2rgba($text_color, '0.3') . ";
            }";

        if( $quiz_theme_type == 'elegant_dark' || $quiz_theme_type == 'elegant_light' ){
            $quiz_styles .= "
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-progress-bg,
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-progress-bg {
                border: 1px solid " . $this->hex2rgba($text_color, '0.3') . ";
            }";
        }

        if ($enable_live_progress_bar) {
            $quiz_styles .= "
            #ays-quiz-container-" . $id . " ." . $filling_type . " {
                background-color: " . $color . ";
            }
            #ays-quiz-container-" . $id . " ." . $filling_type_wrap . " {
                background-color: " . $text_color . ";
            }";
        }

        if ($quiz_image_height != '' && $quiz_image_height > 0) {
            $quiz_styles .= "
            /* Quiz image */
            #ays-quiz-container-" . $id . " .ays_quiz_image{
                height: " . $quiz_image_height . "px;
            }";
        }

        if ($quiz_bg_img_on_start_page) {
            if($enable_background_gradient) {
                $ays_quiz_bg_style_value = "background-image: linear-gradient(". $quiz_gradient_direction .", ". $background_gradient_color_1 .", ". $background_gradient_color_2 .");";
            }else {
                $ays_quiz_bg_style_value = "background-image: unset";
            }

            $quiz_styles .= "
            div#ays-quiz-container-" . $id . ".ays_quiz_hide_bg_on_start_page {
                " . $ays_quiz_bg_style_value . ";
            }";
        }
        
        if ($quiz_bg_img_during_the_quiz) {
            if($enable_background_gradient) {
                $ays_quiz_bg_during_quiz_style_value = "background-image: linear-gradient(". $quiz_gradient_direction .", ". $background_gradient_color_1 .", ". $background_gradient_color_2 .");";
            }else {
                $ays_quiz_bg_during_quiz_style_value = "background-image: unset";
            }

            $quiz_styles .= "
            div#ays-quiz-container-" . $id . ".ays_quiz_hide_bg_during_quiz {
                " . $ays_quiz_bg_during_quiz_style_value . ";
            }";
        }

        $quiz_styles .= "    
            #ays-quiz-container-" . $id . " .ays-progress-value {
                color: " . $text_color . ";
                text-align: center;
            }
            #ays-quiz-container-" . $id . " .ays-progress-bar {
                background-color: " . $color . ";
            }
            #ays-quiz-container-" . $id . " .ays-question-counter .ays-live-bar-wrap {
                direction:ltr !important;
            }
            #ays-quiz-container-" . $id . " .ays-live-bar-fill{
                color: " . $text_color . ";
                border-bottom: 2px solid " . $this->hex2rgba($text_color, '0.8') . ";
                text-shadow: 0px 0px 5px " . $bg_color . ";
            }
            #ays-quiz-container-" . $id . " .ays-live-bar-fill.ays-live-fourth,
            #ays-quiz-container-" . $id . " .ays-live-bar-fill.ays-live-third,
            #ays-quiz-container-" . $id . " .ays-live-bar-fill.ays-live-second {
                text-shadow: unset;
            }
            #ays-quiz-container-" . $id . " .ays-live-bar-percent{
                display:none;
            }
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays_average {
                text-align: center;
            }
            
            /* Music, Sound */
            #ays-quiz-container-" . $id . " .ays_music_sound {
                color:" . $this->hex2rgba($text_color) . ";
            }

            /* Dropdown questions scroll bar */
            #ays-quiz-container-" . $id . " blockquote {
                border-left-color: " . $text_color . " !important;                                      
            }

            /* Quiz Password */
            #ays-quiz-container-" . $id . " .ays-start-page > input[id^='ays_quiz_password_val_'],
            #ays-quiz-container-" . $id . " .ays-quiz-password-toggle-visibility-box {
                width: ". $quiz_password_width_css .";
            }


            /* Question hint */
            #ays-quiz-container-" . $id . " .ays_question_hint_container .ays_question_hint_text {
                background-color:" . $bg_color . ";
                box-shadow: 0 0 15px 3px " . $this->hex2rgba($box_shadow_color, '0.6') . ";
                max-width: 270px;
            }

            #ays-quiz-container-" . $id . " .ays_question_hint_container .ays_question_hint_text p {
                max-width: unset;
            }

            #ays-quiz-container-" . $id . " .ays-quiz-additonal-box {
                display: flex;
                justify-content: flex-end;
                align-items: center;
                position: relative;  
            }

            #ays-quiz-container-" . $id . " .ays_questions_hint_max_width_class {
                max-width: 80%;
            }

            /* Information form */
            #ays-quiz-container-" . $id . " .ays-form-title{
                color:" . $this->hex2rgba($text_color) . ";
            }

            /* Quiz timer */
            #ays-quiz-container-" . $id . " div.ays-quiz-redirection-timer,
            #ays-quiz-container-" . $id . " div.ays-quiz-timer{
                color: " . $text_color . ";
                text-align: center;
            }

            #ays-quiz-container-" . $id . " div.ays-quiz-timer.ays-quiz-message-before-timer:before {
                font-weight: 500;
            }

            /* Quiz title / transformation */
            #ays-quiz-container-" . $id . " .ays-fs-title{
                text-transform: " . $quiz_title_transformation . ";
                font-size: " . $quiz_title_font_size . "px;
                text-align: center;";

            if($quiz_enable_title_text_shadow){
                $quiz_styles .= "
                    text-shadow: " . $title_text_shadow_offsets . " " . $quiz_title_text_shadow_color . ";";
            }else{
                $quiz_styles .= "
                    text-shadow: none;";
            }

            if($questions_reporting){
                $quiz_styles .= "
                #ays-quiz-container-" . $id . " .ays-questions-container {
                    perspective: unset !important;
                }

                .elementor-sticky--active{
                    z-index:999999;
                }";
            }

            $quiz_styles .= "
            }
            
            /* Quiz buttons */
            #ays-quiz-container-" . $id . " .ays_arrow {
                color:". $buttons_text_color ."!important;
            }
            #ays-quiz-container-" . $id . " input#ays-submit,
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button,
            div#ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button.ays_restart_button,
            #ays-quiz-container-" . $id . " + .ays-quiz-category-selective-main-container .ays-quiz-category-selective-restart-bttn,
            #ays-quiz-container-" . $id . " .ays-quiz-category-selective-submit-bttn {
                background: none;
                background-color: " . $color . ";
                color:" . $buttons_text_color . ";
                font-size: " . $buttons_font_size . ";
                padding: " . $buttons_top_bottom_padding . " " . $buttons_left_right_padding . ";
                border-radius: " . $buttons_border_radius . ";
                height: auto;
                letter-spacing: 0;
                box-shadow: unset;
                width: auto;
                /* border: unset; */
                min-height: unset;
                line-height: normal;
                text-shadow: unset;
            }
            #ays-quiz-container-" . $id . " input#ays-submit,
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " input.action-button,
            #ays-quiz-container-" . $id . " + .ays-quiz-category-selective-main-container .ays-quiz-category-selective-restart-bttn,
            #ays-quiz-container-" . $id . " .ays-quiz-category-selective-submit-bttn {
                " . $buttons_width_html . "
            }

            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button.ays_check_answer {
                padding: 5px 10px;
                font-size: " . $buttons_font_size . " !important;
            }
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button.ays_restart_button {
                white-space: nowrap;
                padding: 5px 10px;
                white-space: normal;
            }
            #ays-quiz-container-" . $id . " input#ays-submit:hover,
            #ays-quiz-container-" . $id . " input#ays-submit:focus,
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button:hover,
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button:focus,
            #ays-quiz-container-" . $id . " + .ays-quiz-category-selective-main-container .ays-quiz-category-selective-restart-bttn:hover,
            #ays-quiz-container-" . $id . " .ays-quiz-category-selective-submit-bttn:hover {
                background: none;
                box-shadow: 0 0 0 2px $buttons_text_color;
                background-color: " . $color . ";
            }
            #ays-quiz-container-" . $id . " .ays_restart_button {
                color: " . $buttons_text_color . ";
            }
            
            #ays-quiz-container-" . $id . " .ays_restart_button_p,
            #ays-quiz-container-" . $id . " .ays_buttons_div {
                justify-content: " . $buttons_position . ";
            }";

            if($buttons_position == "flex-start"){
                $quiz_styles .= "
                #ays-quiz-container-" . $id . " .action-button.start_button{
                    float: left;
                    clear: both;
                }";
            }elseif ($buttons_position == "flex-end") {
                $quiz_styles .= "
                #ays-quiz-container-" . $id . " .action-button.start_button{
                    float: right;
                    clear: both;
                }";
            }

            if(!empty($current_user_theme_obj) && !empty($current_user_theme_obj->name)){
                if ( 'Educational' == $current_user_theme_obj->name || 'Educational' == $current_user_theme_obj->parent_theme ) {
                    $quiz_styles .= "
                    #ays-quiz-container-" . $id . " input#ays-submit,
                    #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button,
                    div#ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button.ays_restart_button,
                    #ays-quiz-container-" . $id . " + .ays-quiz-category-selective-main-container .ays-quiz-category-selective-restart-bttn,
                    #ays-quiz-container-" . $id . " .ays-quiz-category-selective-submit-bttn {
                        font-size: " . $buttons_font_size . " !important;
                        padding: " . $buttons_top_bottom_padding . " " . $buttons_left_right_padding . " !important;
                        border-radius: " . $buttons_border_radius . " !important;
                    }";
                }

                if( 'Colibri WP' == $current_user_theme_obj->name || 'Colibri WP' == $current_user_theme_obj->parent_theme ) {
                    $quiz_styles .= "
                    .ays-quiz-question-report-popup-container.swal2-popup .swal2-icon {
                        width: 88px;
                        height: 88px;
                    }";
                }
            }

            $quiz_styles .= "

            #ays-quiz-container-" . $id . " .ays_finish.action-button{
                margin: 10px 5px;
            }

            #ays-quiz-container-" . $id . " .ays-share-btn.ays-share-btn-branded {
                color: #fff;
                display: inline-block;
            }

            #ays-quiz-container-" . $id . " .ays_quiz_results .ays-field.checked_answer_div.correct_div input:checked+label {
                background-color: transparent;
            }
                        
            /* Question answers */
            #ays-quiz-container-".$id." .ays-field {";

            if($answers_border){
                $quiz_styles .= "
                    border-color: " . $answers_border_color . ";
                    border-style: " . $answers_border_style . ";
                    border-width: " . $answers_border_width . "px;";
            }else{
                $quiz_styles .= "
                    border-color: transparent;
                    border-style: none;
                    border-width: 0;";
            }

            if($answers_box_shadow){
                $quiz_styles .= "
                    box-shadow: " . $answer_box_shadow_offsets . " 1px " . $answers_box_shadow_color . ";";
            }else{
                $quiz_styles .= "
                    box-shadow: none;";
            }

            $quiz_styles .=
                "flex-direction: ".$ans_img_caption_style.";
            }
            ";

            $quiz_styles .= "

            /* Answer maximum length of a text field */
            #ays-quiz-container-" . $id . " .ays_quiz_question_text_message{
                color: " . $text_color . ";
                text-align: left;
                font-size: 12px;
            }

            div#ays-quiz-container-" . $id . " div.ays_quiz_question_text_error_message {
                color: #ff0000;
            }

            /* Questions answer image */
            #ays-quiz-container-" . $id . " .ays-answer-image {
                width:" . ($answer_view_class == "grid" ? "100%" : "15em") . ";
                height:" . $ans_img_height . ";
                object-fit: " . $answers_object_fit . ";
            }
            ";

        if (! $disable_hover_effect) {
            $quiz_styles .= "
            #ays-quiz-container-" . $id . " .ays-quiz-answers .ays-field:hover:not(.ays-answered-text-input){
                opacity: 1;
            }";
        } else{
            $quiz_styles .= "
            #ays-quiz-container-" . $id . " .ays-quiz-answers .ays-field:hover:not(.ays-answered-text-input),
            #ays-quiz-container-" . $id . " .ays-quiz-answers .ays-field{
                opacity: 1;
            }

            #ays-quiz-container-" . $id . ".ays_quiz_elegant_light .ays-quiz-answers .ays-field:hover,
            #ays-quiz-container-" . $id . ".ays_quiz_elegant_light .ays-quiz-answers .ays-field,
            #ays-quiz-container-" . $id . ".ays_quiz_elegant_dark .ays-quiz-answers .ays-field:hover,
            #ays-quiz-container-" . $id . ".ays_quiz_elegant_dark .ays-quiz-answers .ays-field{
                opacity: 0.6;
            }";
        }

        $quiz_styles .= "
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field label.ays_answer_caption[for^='ays-answer-'] {
                z-index: 1;
                " . $ans_img_caption_position;

        if(! $show_answers_caption ){
            $quiz_styles .= "display: none !important;";
        }
        $quiz_styles .= "}";

        if( $ans_image_caption_position == 'top' && $answer_view_class == 'list' ){
            $quiz_styles .= "
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field.ays_list_view_item label.ays_answer_image[for^='ays-answer-'] {
                display: flex !important;
                justify-content: flex-end;
            }";
        }

        if($ans_image_caption_style == 'inside'){
            $quiz_styles .= "
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field.ays_grid_view_item label.ays_answer_caption[for^='ays-answer-'] {
                background-color: " . $this->hex2rgba($color, '0.6') . ";
            }
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field.ays_grid_view_item label.ays_answer_caption[for^='ays-answer-']:hover {
                background-color: " . $this->hex2rgba($color, '1') . ";
            }";
        }

        $quiz_styles .= "
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field input~label[for^='ays-answer-'] {
                padding: " . $answers_padding . "px;
            }

            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field {
                margin-bottom: " . ($answers_margin) . "px;
            }
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field.ays_grid_view_item {
                width: calc(50% - " . ($answers_margin / 2) . "px);
            }
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field.ays_grid_view_item:nth-child(odd) {
                margin-right: " . ($answers_margin / 2) . "px;
            }
            
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field input:checked+label:before {
                border-color: " . $color . ";
                background: " . $color . ";
                background-clip: content-box;
            }
            #ays-quiz-container-" . $id . " .ays-quiz-answers div.ays-text-right-answer {
                color: " . $text_color . ";
            }
                        
            /* Questions answer right/wrong icons */
            ";
        if($ans_right_wrong_icon == 'default'){
            $quiz_styles .= "#ays-quiz-container-" . $id . " .ays-field input~label.answered.correct:after{
                content: url('".AYS_QUIZ_PUBLIC_URL."/images/correct.png');          }
            #ays-quiz-container-" . $id . " .ays-field input~label.answered.wrong:after{
                content: url('".AYS_QUIZ_PUBLIC_URL."/images/wrong.png');
            }";
        } elseif( $ans_right_wrong_icon == "none" ){
            $quiz_styles .= "#ays-quiz-container-" . $id . " .ays-field input~label.answered.correct:after{
                content: '';          }
            #ays-quiz-container-" . $id . " .ays-field input~label.answered.wrong:after{
                content: '';
            }";
        } else{
            $quiz_styles .= "#ays-quiz-container-" . $id . " .ays-field input~label.answered.correct::after{
                content: url('".AYS_QUIZ_PUBLIC_URL."/images/correct-".$ans_right_wrong_icon.".png');
            }
            #ays-quiz-container-" . $id . " .ays-field input~label.answered.wrong::after{
                content: url('".AYS_QUIZ_PUBLIC_URL."/images/wrong-".$ans_right_wrong_icon.".png');
            }";
            
            if ( $ans_right_wrong_icon == 'style-9' ) {
               $quiz_styles .= "#ays-quiz-container-" . $id . " .ays-field input+label.answered:after{
                    width: unset;
                    height: unset;
                }";
            }
        }

        if($ans_right_wrong_icon == 'style-9'){
            $quiz_styles .= "
                #ays-quiz-container-" . $id . " .ays-field label.answered::after{
                    height: auto;
                }";
        }
        $quiz_styles .= "
            #ays-quiz-container-" . $id . " .ays-field label.answered:last-of-type:after{
                display: none;
                height: auto;
                left: " . ($answers_padding+5) . "px;";
        if($ans_image_caption_position == 'top'){
            $quiz_styles .= "bottom: " . ($answers_padding+5) . "px;";
        }else{
            $quiz_styles .= "top: " . ($answers_padding+5) . "px;";
        }
        $quiz_styles .= "}";

        $quiz_styles .= "
            /* Dropdown questions */            
            #ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .ays-field .select2-container--default .select2-selection--single {
                border-bottom: 2px solid " . $color . ";
                background-color: " . $color . ";
            }
            
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__placeholder,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__rendered,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__arrow {
                color: " . $this->ays_color_inverse( $text_color ) . ";
            }

            #ays-quiz-container-" . $id . " .select2-container--default .select2-search--dropdown .select2-search__field:focus,
            #ays-quiz-container-" . $id . " .select2-container--default .select2-search--dropdown .select2-search__field {
                outline: unset;
                padding: 0.75rem;
            }

            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__rendered,
            #ays-quiz-container-" . $id . " .select2-container--default .select2-results__option--highlighted[aria-selected] {
                background-color: " . $color . ";
            }

            #ays-quiz-container-" . $id . " .ays-field .select2-container--default,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .selection,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .dropdown-wrapper,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__rendered,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__rendered .select2-selection__placeholder,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__arrow,
            #ays-quiz-container-" . $id . " .ays-field .select2-container--default .select2-selection--single .select2-selection__arrow b[role='presentation'] {
                font-size: 16px !important;
            }

            #ays-quiz-container-" . $id . " .select2-container--default .select2-results__option {
                padding: 6px;
            }
            
            /* Dropdown questions scroll bar */
            #ays-quiz-container-" . $id . " .select2-results__options::-webkit-scrollbar {
                width: 7px;
            }
            #ays-quiz-container-" . $id . " .select2-results__options::-webkit-scrollbar-track {
                background-color: " . $this->hex2rgba($bg_color, '0.35') . ";
            }
            #ays-quiz-container-" . $id . " .select2-results__options::-webkit-scrollbar-thumb {
                transition: .3s ease-in-out;
                background-color: " . $this->hex2rgba($text_color, '0.55') . ";
            }
            #ays-quiz-container-" . $id . " .select2-results__options::-webkit-scrollbar-thumb:hover {
                transition: .3s ease-in-out;
                background-color: " . $this->hex2rgba($text_color, '0.85') . ";
            }

            /* Audio / Video */
            #ays-quiz-container-" . $id . " .mejs-container .mejs-time{
                box-sizing: unset;
            }
            #ays-quiz-container-" . $id . " .mejs-container .mejs-time-rail {
                padding-top: 15px;
            }

            #ays-quiz-container-" . $id . " .mejs-container .mejs-mediaelement video {
                margin: 0;
            }

            /* Limitation */
            #ays-quiz-container-" . $id . " .ays-quiz-limitation-count-of-takers {
                padding: 50px;
            }

            #ays-quiz-container-" . $id . " div.ays-quiz-results-toggle-block span.ays-show-res-toggle.ays-res-toggle-show,
            #ays-quiz-container-" . $id . " div.ays-quiz-results-toggle-block span.ays-show-res-toggle.ays-res-toggle-hide{
                color: ". $text_color .";
            }

            #ays-quiz-container-" . $id . " div.ays-quiz-results-toggle-block input:checked + label.ays_switch_toggle {
                border: 1px solid ". $text_color .";
            }

            #ays-quiz-container-" . $id . " div.ays-quiz-results-toggle-block input:checked + label.ays_switch_toggle {
                border: 1px solid ". $text_color .";
            }

            #ays-quiz-container-" . $id . " div.ays-quiz-results-toggle-block input:checked + label.ays_switch_toggle:after{
                background: ". $text_color .";
            }

            #ays-quiz-container-" . $id . ".ays_quiz_elegant_dark div.ays-quiz-results-toggle-block input:checked + label.ays_switch_toggle:after,
            #ays-quiz-container-" . $id . ".ays_quiz_rect_dark div.ays-quiz-results-toggle-block input:checked + label.ays_switch_toggle:after{
                background: #000;
            }

            /* Hestia theme (Version: 3.0.16) | Start */
            #ays-quiz-container-" . $id . " .mejs-container .mejs-inner .mejs-controls .mejs-button > button:hover,
            #ays-quiz-container-" . $id . " .mejs-container .mejs-inner .mejs-controls .mejs-button > button {
                box-shadow: unset;
                background-color: transparent;
            }
            #ays-quiz-container-" . $id . " .mejs-container .mejs-inner .mejs-controls .mejs-button > button {
                margin: 10px 6px;
            }
            /* Hestia theme (Version: 3.0.16) | End */

            /* Go theme (Version: 1.4.3) | Start */
            #ays-quiz-container-" . $id . " label[for^='ays-answer']:before,
            #ays-quiz-container-" . $id . " label[for^='ays-answer']:before {
                -webkit-mask-image: unset;
                mask-image: unset;
            }

            #ays-quiz-container-" . $id . ".ays_quiz_classic_light .ays-field input:checked+label.answered.correct:before,
            #ays-quiz-container-" . $id . ".ays_quiz_classic_dark .ays-field input:checked+label.answered.correct:before {
                background-color: ". $color ." !important;
                background-color: rgba(39,174,96, 1) !important;
            }
            /* Go theme (Version: 1.4.3) | End */

            #ays-quiz-container-" . $id . " .ays_quiz_results fieldset.ays_fieldset .ays_quiz_question .wp-video {
                width: 100% !important;
                max-width: 100%;
            }

            /* Classic Dark / Classic Light */
            /* Dropdown questions right/wrong styles */
            #ays-quiz-container-" . $id . ".ays_quiz_classic_dark .correct_div,
            #ays-quiz-container-" . $id . ".ays_quiz_classic_light .correct_div{
                border-color:green !important;
                opacity: 1 !important;
                background-color: rgba(39,174,96,0.4) !important;
            }
            #ays-quiz-container-" . $id . ".ays_quiz_classic_dark .correct_div .selected-field,
            #ays-quiz-container-" . $id . ".ays_quiz_classic_light .correct_div .selected-field {
                padding: 0px 10px 0px 10px;
                color: green !important;
            }

            #ays-quiz-container-" . $id . ".ays_quiz_classic_dark .wrong_div,
            #ays-quiz-container-" . $id . ".ays_quiz_classic_light .wrong_div{
                border-color:red !important;
                opacity: 1 !important;
                background-color: rgba(243,134,129,0.4) !important;
            }
            #ays-quiz-container-" . $id . ".ays_quiz_classic_dark .ays-field.checked_answer_div.wrong_div input:checked~label,
            #ays-quiz-container-" . $id . ".ays_quiz_classic_light .ays-field.checked_answer_div.wrong_div input:checked~label {
                background-color: rgba(243,134,129,0.4) !important;
            }
            #ays-quiz-container-" . $id . ".ays_quiz_classic_dark .ays-field,
            #ays-quiz-container-" . $id . ".ays_quiz_classic_light .ays-field {
                text-align: left;
                /*margin-bottom: 10px;*/
                padding: 0;
                transition: .3s ease-in-out;
            }

            #ays-quiz-container-" . $id . " .ays-quiz-close-full-screen {
                fill: $text_color;
            }

            #ays-quiz-container-" . $id . " .ays-quiz-open-full-screen {
                fill: $text_color;
            }

            #ays-quiz-container-" . $id . " .ays_quiz_login_form p{
                color: $text_color;
            }

            /* report questions modal start */
            .ays-modal-reports {
                display: none;
                position: fixed;
                z-index: 9999;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                overflow: auto;
                background-color: rgba(0, 0, 0, 0.4);
                animation-duration: .5s;
                background-color: rgba(0, 0, 0, 0.4);
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-modal-content-reports {
                background-color: #fefefe;
                margin: 10% auto;
                border: 1px solid #888;
                max-width: 500px;
                width: 100%;
                border-radius: 10px;
                padding: 20px;
                box-sizing: border-box;
                position: relative;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-modal-content-reports label{
                display: block;
                margin-bottom: 10px;
                font-weight: bold;
                letter-spacing: normal;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-modal-content-reports textarea {
                display: block;
                width: 100%;
                height: 100px;
                padding: 5px;
                box-sizing: border-box;
                border-radius: 5px;
                border: 1px solid #ccc;
                max-width: 100%;
                max-height: 500px;
                resize: vertical;
                transition: unset;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-modal-content-reports input.ays-quiz-submit-question-report {
                background-color: #0073aa;
                color: #fff !important;
                border: none;
                border-radius: 5px;
                padding: 10px 20px;
                cursor: pointer;
                margin-top: 20px;
                line-height: normal;
                letter-spacing: normal;
                box-shadow: unset;
                background-image: unset;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-close-reports-window {
                color: #aaa;
                font-weight: bold;
                position: absolute;
                top: 1%;
                right: 1%;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-close-reports-window img {
                box-shadow: unset;
            }
            
            #ays-quiz-question-report-modal-" . $id . " .ays-quiz-preloader img {
                box-shadow: unset;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-close-reports-window img:hover,
            #ays-quiz-question-report-modal-" . $id . " .ays-close-reports-window img:focus {
                cursor: pointer;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-quiz-question-report-error {
                display: none;
                font-size: 13px;
                color: #f00;
                text-align: left;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-quiz-question-report-textarea-label,
            #ays-quiz-question-report-modal-" . $id . " .ays-quiz-question-report-title {
                text-align: left;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays_quiz_modal_overlay {
                width: 100%;
                height: 100%;
                position: fixed;
                top: 0;
                left: 0;
                position: absolute;
                z-index: 1000000000;
                flex-direction: column;
            }

            #ays-quiz-question-report-modal-" . $id . " .ays-quiz-container .ays-quiz-user-cհoosing-anonymous-assessment {
                margin: 10px 0;
            }

            /* report questions modal end */

            @media screen and (max-width: 768px){
                #ays-quiz-container-" . $id . "{
                    max-width: $mobile_max_width;
                }

                div#ays-quiz-container-" . $id . " [id^='ays_finish_quiz_'] div.step div.ays-abs-fs {
                    width: ". $quiz_content_mobile_max_width ."%;
                }

                #ays-quiz-container-" . $id . " .ays_quiz_question p {
                    font-size: ".$question_mobile_font_size."px;
                }

                #ays-quiz-container-" . $id . " .select2-container,
                #ays-quiz-container-" . $id . " .ays-field * {
                    font-size: ".$answers_mobile_font_size."px !important;
                }

                div#ays-quiz-container-" . $id . " input#ays-submit,
                div#ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button,
                div#ays-quiz-container-" . $id . " #ays_finish_quiz_" . $id . " .action-button.ays_restart_button,
                #ays-quiz-container-" . $id . " + .ays-quiz-category-selective-main-container .ays-quiz-category-selective-restart-bttn,
                #ays-quiz-container-" . $id . " .ays-quiz-category-selective-submit-bttn {
                    font-size: ".$buttons_mobile_font_size."px;
                    border-radius: " . $buttons_mobile_border_radius . "px;
                }

                /* Quiz title / mobile font size */
                div#ays-quiz-container-" . $id . " .ays-fs-title {
                    font-size: " . $quiz_title_mobile_font_size . "px;
                }

                /* Question explanation / mobile font size */
                #ays-quiz-container-" . $id . " .ays_questtion_explanation p {
                    font-size:" . $quest_explanation_mobile_font_size . "px;
                }

                /* Wrong answers / mobile font size */
                #ays-quiz-container-" . $id . " .wrong_answer_text p {
                    font-size:" . $wrong_answers_mobile_font_size . "px;
                }

                /* Right answers / mobile font size */
                #ays-quiz-container-" . $id . " .right_answer_text p {
                    font-size:" . $right_answers_mobile_font_size . "px;
                }

                /* Note text / mobile font size */
                #ays-quiz-container-" . $id . " .ays-quiz-question-note-message-box p {
                    font-size:" . $note_text_mobile_font_size . "px;
                }

                div#ays-quiz-container-" . $id . " .ays-quiz-question-note-message-box *:not(strong) {
                    text-transform:" . $quiz_admin_note_mobile_text_transform . ";
                    text-decoration: ". $quiz_admin_note_mobile_text_decoration .";
                    letter-spacing: ". $quiz_admin_note_mobile_letter_spacing ."px;
                    font-weight: ". $quiz_admin_note_mobile_font_weight .";
                }

                div#ays-quiz-container-" . $id . " .ays_questtion_explanation *:not(strong) {
                    text-transform:" . $quiz_quest_explanation_mobile_text_transform . ";
                    text-decoration: ". $quiz_quest_explanation_mobile_text_decoration .";
                    letter-spacing: ". $quiz_quest_explanation_mobile_letter_spacing ."px;
                    font-weight: ". $quiz_quest_explanation_mobile_font_weight .";
                }

                div#ays-quiz-container-" . $id . " .right_answer_text *:not(strong) {
                    text-transform:" . $quiz_right_answer_mobile_text_transform . ";
                    text-decoration: ". $quiz_right_answers_mobile_text_decoration .";
                    letter-spacing: ". $quiz_right_answers_mobile_letter_spacing ."px;
                    font-weight: ". $quiz_right_answers_mobile_font_weight .";
                }

                div#ays-quiz-container-" . $id . " .wrong_answer_text *:not(strong) {
                    text-transform:" . $quiz_wrong_answer_mobile_text_transform . ";
                    text-decoration: ". $quiz_wrong_answers_mobile_text_decoration .";
                    letter-spacing: ". $quiz_wrong_answers_mobile_letter_spacing ."px;
                    font-weight: ". $quiz_wrong_answers_mobile_font_weight .";
                }
            }
            /* Custom css styles */
            " . stripslashes( htmlspecialchars_decode( $options['custom_css'] ) ) . "
            
            /* RTL direction styles */
            " . $rtl_style . "
        </style>";


        
    /*******************************************************************************************************/
        
        /*
         * Quiz container
         *
         * Generating HTML code
         */
        
        $quiz_theme = "";
        $options['quiz_theme'] = (array_key_exists('quiz_theme', $options)) ? $options['quiz_theme'] : '';
        switch ($options['quiz_theme']) {
            case 'elegant_dark':
                $quiz_theme = "ays_quiz_elegant_dark";
                break;
            case 'elegant_light':
                $quiz_theme = "ays_quiz_elegant_light";
                break;
            case 'rect_dark':
                $quiz_theme = "ays_quiz_rect_dark";
                break;
            case 'rect_light':
                $quiz_theme = "ays_quiz_rect_light";
                break;
            case 'classic_dark':
                $quiz_theme = "ays_quiz_classic_dark";
                break;
            case 'classic_light':
                $quiz_theme = "ays_quiz_classic_light";
                break;
        }
        
        $custom_class = isset($options['custom_class']) && $options['custom_class'] != "" ? esc_attr( stripslashes($options['custom_class']) ) : "";
		$quiz_gradient = '';
		if($enable_background_gradient){
			$quiz_gradient = " data-bg-gradient='linear-gradient($quiz_gradient_direction, $background_gradient_color_1, $background_gradient_color_2)' ";
		}

        // Question Image Zoom
        $options['quiz_enable_question_image_zoom'] = isset($options['quiz_enable_question_image_zoom']) ? $options['quiz_enable_question_image_zoom'] : 'off';
        $quiz_enable_question_image_zoom = (isset($options['quiz_enable_question_image_zoom']) && $options['quiz_enable_question_image_zoom'] == "on") ? true : false;

        // Display Messages before the buttons
        $options['quiz_display_messages_before_buttons'] = isset($options['quiz_display_messages_before_buttons']) ? esc_attr($options['quiz_display_messages_before_buttons']) : 'off';
        $quiz_display_messages_before_buttons = (isset($options['quiz_display_messages_before_buttons']) && $options['quiz_display_messages_before_buttons'] == 'on') ? true : false;

        // Enable questions ordering by category
        $options['enable_questions_ordering_by_cat'] = isset($options['enable_questions_ordering_by_cat']) ? $options['enable_questions_ordering_by_cat'] : 'off';
        $enable_questions_ordering_by_cat = (isset($options['enable_questions_ordering_by_cat']) && $options['enable_questions_ordering_by_cat'] == "on") ? true : false;

        // Enable questions numbering by category
        $options['quiz_questions_numbering_by_category'] = isset($options['quiz_questions_numbering_by_category']) ? sanitize_text_field($options['quiz_questions_numbering_by_category']) : 'off';
        $quiz_questions_numbering_by_category = (isset($options['quiz_questions_numbering_by_category']) && $options['quiz_questions_numbering_by_category'] == 'on') ? true : false;


        $options['enable_full_screen_mode'] = isset($options['enable_full_screen_mode']) ? $options['enable_full_screen_mode'] : 'off';
        $enable_full_screen_mode = (isset($options['enable_full_screen_mode']) && $options['enable_full_screen_mode'] == "on") ? true : false;

        $fullcsreen_mode = '';

        if($enable_full_screen_mode){
            $fullcsreen_mode = '<div class="ays-quiz-full-screen-wrap">
                <a class="ays-quiz-full-screen-container">
                    <svg xmlns="http://www.w3.org/2000/svg" height="24" fill="#fff" viewBox="0 0 24 24" width="24" class="ays-quiz-close-full-screen">
                        <path d="M0 0h24v24H0z" fill="none"/>
                        <path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/>
                    </svg>
                    <svg xmlns="http://www.w3.org/2000/svg" height="24" fill="#fff" viewBox="0 0 24 24" width="24" class="ays-quiz-open-full-screen">
                        <path d="M0 0h24v24H0z" fill="none"/>
                        <path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/>
                    </svg>
                </a>
            </div>';
        }else {
            $fullcsreen_mode = '';
        }

        $quiz_container_first_part = "
            <div class='ays-quiz-container ".$quiz_theme." ".$quiz_bg_img_class." ". esc_attr($custom_class) ." ".$class_for_keyboard."' data-quest-effect='".$quest_animation."' ".$quiz_gradient." data-hide-bg-image='".$quiz_bg_img_in_finish_page."' id='ays-quiz-container-" . $id . "'>
                {$live_progress_bar}
                {$ays_quiz_music_html}
                <div class='ays-questions-container'>
                    {$fullcsreen_mode}
                    $ays_quiz_reports
                    " . $questions_reporting_modal . "
                    <form action='' method='post' id='ays_finish_quiz_" . $id . "' 
                        class='ays-quiz-form " . $correction_class . " " . $enable_questions_result . " " . $enable_logged_users . "'
                    >";
        
        $quiz_container_first_part .= "
            <input type='hidden' value='" . $answer_view_class . "' class='answer_view_class'>
            <input type='hidden' value='" . $enable_arrows . "' class='ays_qm_enable_arrows'>";
        
        $quiz_container_middle_part = "";
        if($is_expired){
            $quiz_container_middle_part = $expired_quiz_message;
            $main_content_first_part = "";
            $main_content_last_part = "";
        }
        if($enable_tackers_count){
            $quiz_tackers_count = $this->get_quiz_tackers_count($id);
            if($quiz_tackers_count >= $tackers_count){
                $quiz_container_middle_part = $tackers_message;
                $main_content_first_part = "";
                $main_content_last_part = "";
            }
        }
        if($limit_users === true){
            $quiz_container_middle_part = $limit_users_html;
            $main_content_first_part = "";
            $main_content_last_part = "";
        }
        if($user_massage !== null){
            $quiz_container_middle_part = "<!-- This content is empty -->";
            $main_content_first_part = "";
            $main_content_last_part = "";
        }
        

        $quiz_container_last_part = $quiz_content_script;

        if( $this->get_prop( 'is_training' ) === true ){
            $quiz_container_last_part .= "<input type='hidden' name='is_training' value='true' />";
        }

        $cat_selective_start_page = "";
        $cat_selective_restart_bttn = "";
        if( $this->get_prop( 'category_selective' ) === true ){
            if (isset($_COOKIE['ays_quiz_selected_categories-'.$id])) {
                $selected_questions = apply_filters( 'ays_qm_front_end_category_selective_get_questions', array(), $id );
                $arr_questions = $selected_questions;
                $cat_selective_restart_bttn = apply_filters( 'ays_qm_front_end_category_selective_restart_button', "", $id );
            } else {
                $title = $this->get_prop( 'title' ) != null ? sanitize_text_field($this->get_prop( 'title' )) : '';
                $category_selective_quiz_data = array(
                    'title' => $title,
                    'quiz_id' => $id,
                );
                $cat_selective_start_page = "
                <div class='ays-quiz-wrap'>
                    <div class='ays-quiz-container ".$quiz_theme." ".$quiz_bg_img_class." ".$custom_class." ".$class_for_keyboard."'
                        data-quest-effect='".$quest_animation."' ".$quiz_gradient."
                        data-hide-bg-image='".$quiz_bg_img_in_finish_page."'
                        id='ays-quiz-container-" . $id . "'
                        style='padding:30px 0'>
                            ".apply_filters( 'ays_qm_front_end_category_selective_start_page', "", $category_selective_quiz_data )."
                    </div>
                </div>";
            }
        }
        
        $quiz_container_last_part .= "
                    <input type='hidden' name='quiz_id' value='" . $id . "'/>
                    <input type='hidden' name='start_date' class='ays-start-date'/>
                </form>";
        if($user_massage !== null){
            $quiz_container_last_part .= $user_massage;
        }

        $quiz_container_last_part .= "</div>
                            </div>
                            {$cat_selective_restart_bttn}";
        
        
    /*******************************************************************************************************/
        
        /*
         * Generating Quiz parts array
         */
        
        $quiz_parts = array(
            "container_first_part"                      => $quiz_container_first_part,
            "main_content_first_part"                   => $main_content_first_part,
            "main_content_middle_part"                  => $quiz_container_middle_part,
            "main_content_last_part"                    => $main_content_last_part,
            "quiz_styles"                               => $quiz_styles,
            "quiz_additional_styles"                    => "",
            "container_last_part"                       => $quiz_container_last_part,
            "cat_selective_start_page"                  => $cat_selective_start_page,
        );
        
        $quizOptions = array(
            'buttons'                                   => $buttons,
            'correction'                                => $enable_correction,
            'randomizeAnswers'                          => $randomize_answers,
            'questionImageWidth'                        => $question_image_width,
            'questionImageHeight'                       => $question_image_height,
            'questionImageSizing'                       => $question_image_sizing,
            'questionImageBorderRadius'                 => $quiz_question_image_border_radius,
            'questionsCounter'                          => $questions_counter,
            'informationForm'                           => $options['information_form'],
            'answersViewClass'                          => $answer_view_class,
            'quizTheme'                                 => $options['quiz_theme'],
            'rtlDirection'                              => $rtl_direction,
            'showQuestionCategory'                      => $show_question_category,
            'showQuestionCategoryDescription'           => $quiz_enable_question_category_description,
            'questionsHint'                             => $questions_hint_arr,
            'disable_hover_effect'                      => $disable_hover_effect,
            'show_answers_numbering'                    => $show_answers_numbering,
            'show_questions_numbering'                  => $show_questions_numbering,
            'show_questions_explanation'                => $show_questions_explanation,
            'answers_rw_texts'                          => $answers_rw_texts,
            'quiz_waiting_time'                         => $quiz_waiting_time,
            'quiz_enable_lazy_loading'                  => $quiz_enable_lazy_loading,
            'quiz_enable_question_image_zoom'           => $quiz_enable_question_image_zoom,
            'quiz_display_messages_before_buttons'      => $quiz_display_messages_before_buttons,
            'enable_questions_ordering_by_cat'          => $enable_questions_ordering_by_cat,
            'quiz_questions_numbering_by_category'      => $quiz_questions_numbering_by_category,
            'question_bank_cats'                        => $question_bank_cats,
            'quiz_enable_keyboard_navigation'           => $quiz_enable_keyboard_navigation,
            'questionsReporting'                        => $questions_reporting,
            'ans_right_wrong_icon'                      => $ans_right_wrong_icon,
        );
        
        $ays_quiz = (object)array(
            "quizID"            => $id,
            "quizOptions"       => $quizOptions,
            "questions"         => $arr_questions,
            "questionsCount"    => $questions_count,
            "quizParts"         => $quiz_parts,
            "quizColors"        => array(
                "Color"             => $color,
                "textColor"         => $text_color,
                "bgColor"           => $bg_color,
                "boxShadowColor"    => $box_shadow_color,
                "borderColor"       => $quiz_border_color
            )
        );
            
        return $ays_quiz;
    }

    public function ays_generate_quiz($quiz){
        
        $quiz_id = $quiz->quizID;
        $arr_questions = $quiz->questions;
        $questions_count = $quiz->questionsCount;
        $options = $quiz->quizOptions;

        if (isset($quiz->quizParts['cat_selective_start_page']) && $quiz->quizParts['cat_selective_start_page'] != "") {
            return $quiz->quizParts['cat_selective_start_page'].$quiz->quizParts['quiz_styles'];
        }

        // Disable answer hover
        $options['disable_hover_effect'] = isset($options['disable_hover_effect']) ? $options['disable_hover_effect'] : 'off';
        $disable_hover_effect = (isset($options['disable_hover_effect']) && $options['disable_hover_effect'] == "on") ? true : false;

        $questions = "";
        $questions = $this->get_quiz_questions($arr_questions, $quiz_id, $options, false);
        
        if($quiz->quizParts['main_content_middle_part'] == ""){
            $quiz->quizParts['main_content_middle_part'] = $questions;
        }
        $additional_css = "
            <style>
                #ays-quiz-container-" . $quiz_id . " p {
                    margin: 0.625em;
                }
                
                #ays-quiz-container-" . $quiz_id . " .ays-field.checked_answer_div input:checked~label {
                    background-color: " . $this->hex2rgba($quiz->quizColors['Color'], '0.6') . ";
                }

                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_light  .ays_quiz_results .ays-field.checked_answer_div input:checked~label,
                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_dark  .ays_quiz_results .ays-field.checked_answer_div input:checked~label,
                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_light  .enable_correction .ays-field.checked_answer_div input:checked~label,
                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_dark  .enable_correction .ays-field.checked_answer_div input:checked~label {
                    background-color: transparent;
                }

                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_light  .ays_quiz_results .not_influence_to_score .ays-field.checked_answer_div input:checked~label,
                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_dark  .ays_quiz_results .not_influence_to_score .ays-field.checked_answer_div input:checked~label,
                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_light  .enable_correction .not_influence_to_score .ays-field.checked_answer_div input:checked~label,
                #ays-quiz-container-" . $quiz_id . ".ays_quiz_classic_dark  .enable_correction .not_influence_to_score .ays-field.checked_answer_div input:checked~label {
                    background-color: " . $this->hex2rgba($quiz->quizColors['Color'], '0.6') . ";
                }
                ";
        if (! $disable_hover_effect) {
            $additional_css .= "
                #ays-quiz-container-" . $quiz_id . " .ays-field.checked_answer_div input:checked~label:hover {
                    background-color: " . $this->hex2rgba($quiz->quizColors['Color'], '0.8') . ";
                }

                #ays-quiz-container-" . $quiz_id . ".ays-quiz-container.ays_quiz_classic_light .ays-questions-container .ays-field:hover label[for^='ays-answer-'],
                #ays-quiz-container-" . $quiz_id . " .ays-field:hover:not(.ays-answered-text-input){
                    background: " . $this->hex2rgba($quiz->quizColors['Color'], '0.8') . ";
                    /* border-radius: 4px; */
                    color: #fff;
                    transition: all .3s;
                }";
        }

        $additional_css .= "
                #ays-quiz-container-" . $quiz_id . " #ays_finish_quiz_" . $quiz_id . " .action-button:hover,
                #ays-quiz-container-" . $quiz_id . " #ays_finish_quiz_" . $quiz_id . " .action-button:focus,
                #ays-quiz-container-" . $quiz_id . " + .ays-quiz-category-selective-main-container .ays-quiz-category-selective-restart-bttn:hover,
                #ays-quiz-container-" . $quiz_id . " .ays-quiz-category-selective-submit-bttn:focus {
                    box-shadow: 0 0 0 2px white, 0 0 0 3px " . $quiz->quizColors['Color'] . ";
                    background: " . $quiz->quizColors['Color'] . ";
                }
            </style>";
        
        $quiz->quizParts['quiz_additional_styles'] = $additional_css;
        
        $container = implode("", $quiz->quizParts);
        
        return $container;
    }

    public function get_quiz_by_id($id){
        global $wpdb;

        $sql = "SELECT *
                FROM {$wpdb->prefix}aysquiz_quizes
                WHERE id=" . intval($id);

        $quiz = $wpdb->get_row($sql, 'ARRAY_A');

        return $quiz;
    }
    
    public static function get_quiz_category_by_id($id){
        global $wpdb;

        $sql = "SELECT *
                FROM {$wpdb->prefix}aysquiz_quizcategories
                WHERE id=" . intval($id);

        $category = $wpdb->get_row($sql, 'ARRAY_A');

        return $category;
    }
    
    public static function get_question_category_by_id($id){
        global $wpdb;

        $sql = "SELECT *
                FROM {$wpdb->prefix}aysquiz_categories
                WHERE id=" . intval($id);

        $category = $wpdb->get_row($sql, 'ARRAY_A');

        return $category;
    }
    
    public function get_quiz_results_count_by_id($id){
        global $wpdb;

        $sql = "SELECT COUNT(*) AS res_count
                FROM {$wpdb->prefix}aysquiz_reports
                WHERE quiz_id=" . intval($id);

        $quiz = $wpdb->get_row($sql, 'ARRAY_A');

        return $quiz;
    }

    public function get_quiz_attributes_by_id($id){
        global $wpdb;
        $quiz_attrs = isset(json_decode($this->get_quiz_by_id($id)['options'])->quiz_attributes) ? json_decode($this->get_quiz_by_id($id)['options'])->quiz_attributes : array();
        $quiz_attributes = implode(',', $quiz_attrs);
        if (!empty($quiz_attributes)) {
            $sql = "SELECT * FROM {$wpdb->prefix}aysquiz_attributes WHERE `id` in ($quiz_attributes)";
            $results = $wpdb->get_results($sql);
            return $results;
        }
        return array();

    }

    public function get_quiz_questions($ids, $quiz_id, $options, $per_page){
        
        $container = $this->ays_questions_parts($ids, $quiz_id, $options, $per_page);
        $questions_container = array();
        foreach($container as $key => $question){
            $answer_container = '';
            $use_html = $this->in_question_use_html($question['questionID']);
            switch ($question["questionType"]) {
                case "select":
                    $ans_options = array(
                        'correction' => $options['correction'],
                        'show_answers_numbering' => $options['show_answers_numbering'],
                    );
                    $answer_container .= $this->ays_dropdown_answer_html($question['questionID'], $quiz_id, $question['questionAnswers'], $ans_options);
                    break;
                case "text":
                    $question_max_length_array = $this->ays_quiz_get_question_max_length_array($question['questionID']);
                    $ans_options = array(
                        'correction' => $options['correction'],
                        'questionMaxLengthArray' => $question_max_length_array,
                        'enable_case_sensitive_text' => $question['enable_case_sensitive_text'],
                    );
                    $answer_container .= $this->ays_text_answer_html($question['questionID'], $quiz_id, $question['questionAnswers'], $ans_options);
                    break;
                case "short_text":
                    $question_max_length_array = $this->ays_quiz_get_question_max_length_array($question['questionID']);
                    $ans_options = array(
                        'correction' => $options['correction'],
                        'questionMaxLengthArray' => $question_max_length_array,
                        'enable_case_sensitive_text' => $question['enable_case_sensitive_text'],
                    );
                    $answer_container .= $this->ays_short_text_answer_html($question['questionID'], $quiz_id, $question['questionAnswers'], $ans_options);
                    break;
                case "number":
                    $question_max_length_array = $this->ays_quiz_get_question_max_length_array($question['questionID']);
                    $ans_options = array(
                        'correction' => $options['correction'],
                        'questionMaxLengthArray' => $question_max_length_array,
                    );
                    $answer_container .= $this->ays_number_answer_html($question['questionID'], $quiz_id, $question['questionAnswers'], $ans_options);
                    break;
                case "date":
                    $ans_options = array(
                        'correction' => $options['correction']
                    );
                    $answer_container .= $this->ays_date_answer_html($question['questionID'], $quiz_id, $question['questionAnswers'], $ans_options);
                    break;
                case "true_or_false":
                default:
                    $ans_options = array(
                        'correction'                        => $options['correction'],
                        'rtlDirection'                      => $options['rtlDirection'],
                        'questionType'                      => $question["questionType"],
                        'answersViewClass'                  => $options['answersViewClass'],
                        'show_answers_numbering'            => $options['show_answers_numbering'],
                        'quiz_enable_keyboard_navigation'   => $options['quiz_enable_keyboard_navigation'],
                        'useHTML'                           => $use_html,
                        'enable_max_selection_number'       => $question['enable_max_selection_number'],
                        'max_selection_number'              => $question['max_selection_number'],
                        'enable_min_selection_number'       => $question['enable_min_selection_number'],
                        'min_selection_number'              => $question['min_selection_number'],
                        'quiz_enable_lazy_loading'          => $options['quiz_enable_lazy_loading'],
                        'ans_right_wrong_icon'              => $options['ans_right_wrong_icon'],
                        'key_number'                        => $key,
                    );
                    $answer_container .= $this->ays_default_answer_html($question['questionID'], $quiz_id, $question['questionAnswers'], $ans_options);
                    break;
            }
            $question['questionParts']['question_middle_part'] = $answer_container;
            $questions_container[] = implode("", $question['questionParts']);
        }
        $container = implode("", $questions_container);
        return $container;
    }
    
    public function ays_questions_parts($ids, $quiz_id, $options, $per_page){
        global $wpdb;
        $total = count($ids);
        $container = array();
        $buttons = $options['buttons'];
        $enable_arrows = $buttons['enableArrows'];
        $quiz_arrow_type = $buttons['quizArrowType'];
        $settings_buttons_texts = $this->buttons_texts;
        $quiz_waiting_time = $options['quiz_waiting_time'];
        $quiz_enable_lazy_loading = $options['quiz_enable_lazy_loading'];
        $quiz_enable_question_image_zoom = $options['quiz_enable_question_image_zoom'];
        $quiz_display_messages_before_buttons = $options['quiz_display_messages_before_buttons'];
        $enable_questions_ordering_by_cat = $options['enable_questions_ordering_by_cat'];
        $quiz_questions_numbering_by_category = $options['quiz_questions_numbering_by_category'];
        $question_bank_cats = $options['question_bank_cats'];

        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == 'on') ? true : false;
        $attributes_for_keyboard = "";
        $class_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
        }

        foreach($ids as $key => $id){
            $current = $key + 1;
            if($total == $current){
                $last = true;
            }else{
                $last = false;
            }
            $sql = "SELECT * FROM {$wpdb->prefix}aysquiz_questions WHERE id = " . intval($id);
            $question = $wpdb->get_row($sql, 'ARRAY_A');
            
            if (!empty($question)) {
                $answers = $this->get_answers_with_question_id($question["id"]);
                // $question_options = (isset($question['options']) && sanitize_text_field( $question['options'] ) != '') ? json_decode( $question['options'], true ) : array();

                $question_options = array();
                if (isset($question['options']) && is_string($question['options']) && sanitize_text_field($question['options']) != '') {
                    $decoded_options = json_decode($question['options'], true);
                    if (is_array($decoded_options)) {
                        $question_options = $decoded_options;
                    }
                }

                $question_image = '';
                $question_image_style = '';
                $question_category = '';
                $question_category_description = '';
                $question_category_description_html = '';
                $show_question_category = $options['showQuestionCategory'];
                $show_question_category_description = $options['showQuestionCategoryDescription'];
                $show_questions_explanation = $options['show_questions_explanation'];
                $show_answers_rw_texts = $options['answers_rw_texts'];
                if($show_question_category){
                    $question_category_data = self::get_question_category_by_id($question['category_id']);
                    $question_category = ( isset( $question_category_data['title'] ) && $question_category_data['title'] != "" ) ? $question_category_data['title'] : "";
                    $question_category_description = ( isset( $question_category_data['description'] ) && $question_category_data['description'] != "" ) ? $question_category_data['description'] : "";
                    
                    $question_category = "<p class='ays-quiz-question-category-box' style='margin:0!important;text-align:left;'>
                        <em style='font-style:italic;font-size:0.8em;'>". __("Category", 'quiz-maker') .":</em>
                        <strong style='font-size:0.8em;'>{$question_category}</strong>
                    </p>";

                    if ( $show_question_category_description && $question_category_description != "" ) {
                        $question_category_description_html .= '<div class="ays-quiz-category-description-box">';
                            $question_category_description_html .= $this->ays_autoembed($question_category_description);
                        $question_category_description_html .= '</div>';

                        $question_category .= $question_category_description_html;
                    }
                }

                if ( $question["type"] == 'true_or_false' ) {
                    $question["type"] = 'radio';
                }
                
                $question['not_influence_to_score'] = ! isset($question['not_influence_to_score']) ? 'off' : $question['not_influence_to_score'];
                $not_influence_to_score = (isset($question['not_influence_to_score']) && $question['not_influence_to_score'] == 'on') ? true : false;

                // Hide question text on the front-end
                $question_options['quiz_hide_question_text'] = isset($question_options['quiz_hide_question_text']) ? sanitize_text_field( $question_options['quiz_hide_question_text'] ) : 'off';
                $quiz_hide_question_text = (isset($question_options['quiz_hide_question_text']) && $question_options['quiz_hide_question_text'] == 'on') ? true : false;

                // Question Image border radius
                $quiz_question_image_border_radius = absint ( stripslashes( $options[ 'questionImageBorderRadius' ] ) );

                $question_image_style = "style='width:{$options['questionImageWidth']};height:{$options['questionImageHeight']};object-fit:{$options['questionImageSizing']};border-radius:{$quiz_question_image_border_radius}px;object-position:center center;'";

                // Enable maximum selection number
                $question_options['enable_max_selection_number'] = isset($question_options['enable_max_selection_number']) ? sanitize_text_field( $question_options['enable_max_selection_number'] ) : 'off';
                $enable_max_selection_number = (isset($question_options['enable_max_selection_number']) && sanitize_text_field( $question_options['enable_max_selection_number'] ) == 'on') ? true : false;

                // Max value
                $max_selection_number = ( isset($question_options['max_selection_number']) && $question_options['max_selection_number'] != '' ) ? intval( sanitize_text_field( $question_options['max_selection_number'] ) ) : '';

                // Enable minimum selection number
                $question_options['enable_min_selection_number'] = isset($question_options['enable_min_selection_number']) ? sanitize_text_field( $question_options['enable_min_selection_number'] ) : 'off';
                $enable_min_selection_number = (isset($question_options['enable_min_selection_number']) && sanitize_text_field( $question_options['enable_min_selection_number'] ) == 'on') ? true : false;

                // Min value
                $min_selection_number = ( isset($question_options['min_selection_number']) && $question_options['min_selection_number'] != '' ) ? intval( sanitize_text_field( $question_options['min_selection_number'] ) ) : '';

                $max_selection_number_class = '';
                $min_selection_number_class = '';
                if ( $question["type"] == 'checkbox' ) {

                    if ( $enable_max_selection_number && ! empty( $max_selection_number ) && $max_selection_number != 0 ) {
                        $max_selection_number_class = 'enable_max_selection_number';
                    }
                    if ( $enable_min_selection_number && ! empty( $min_selection_number ) && $min_selection_number != 0 ) {
                        $min_selection_number_class = 'enable_min_selection_number';
                    }
                }

                $enable_case_sensitive_text = false;
                if ( $question["type"] == 'text' || $question["type"] == 'short_text' ) {

                    // Enable case sensitive text
                    $question_options['enable_case_sensitive_text'] = isset($question_options['enable_case_sensitive_text']) ? sanitize_text_field( $question_options['enable_case_sensitive_text'] ) : 'off';
                    $enable_case_sensitive_text = (isset($question_options['enable_case_sensitive_text']) && sanitize_text_field( $question_options['enable_case_sensitive_text'] ) == 'on') ? true : false;
                }

                // Enable strip slashes for questions
                $question_options['quiz_enable_question_stripslashes'] = isset($question_options['quiz_enable_question_stripslashes']) ? sanitize_text_field( $question_options['quiz_enable_question_stripslashes'] ) : 'off';
                $quiz_enable_question_stripslashes = (isset($question_options['quiz_enable_question_stripslashes']) && $question_options['quiz_enable_question_stripslashes'] == 'on') ? true : false;

                // if( isset($question['question_image']) && $question['question_image'] != "" && !is_null($question['question_image']) ){
                //     if ( !(filter_var($question['question_image'], FILTER_VALIDATE_URL) && wp_http_validate_url($question['question_image'])) ) {
                //         // Invalid URL, handle accordingly
                //         $question['question_image'] = null;
                //     }
                // }

                // if( isset($question['question_image']) && $question['question_image'] != "" && !is_null($question['question_image']) ){
                //     $check_if_current_image_exists = Quiz_Maker_Admin::ays_quiz_check_if_current_image_exists($question['question_image']);

                //     if( !$check_if_current_image_exists ){
                //         $question['question_image'] = null;
                //     }
                // }
                
                if ($question['question_image'] != NULL && $question['question_image'] != "") {
                    $question_image_alt_text = $this->ays_quiz_get_image_id_by_url($question['question_image']);

                    $question_image_lazy_loading_attr = "";
                    if ( $quiz_enable_lazy_loading ) {
                        if( $key != 0 ){
                            $question_image_lazy_loading_attr = 'loading="lazy"';
                        }
                    }

                    $quiz_question_image_zoom_class = "";
                    $quiz_question_full_size_url_attr = "";
                    if ( $quiz_enable_question_image_zoom ) {
                        $quiz_question_image_zoom_class = "ays-quiz-question-image-zoom";
                        $quiz_question_full_size_url = $this->ays_quiz_get_image_full_size_url_by_url($question['question_image']);

                        if ( $quiz_question_full_size_url && $quiz_question_full_size_url != "" ) {
                            $quiz_question_full_size_url_attr = ' data-ays-src="'. esc_url( $quiz_question_full_size_url ) .'" ';
                        } elseif ( $quiz_question_full_size_url == "" ) {
                            $quiz_question_full_size_url_attr = ' data-ays-src="'. esc_url( $question['question_image'] ) .'" ';
                        }
                    }

                    $question_image .= '<div class="ays-image-question-img">';
                        $question_image .= '<img src="' . esc_url($question['question_image']) . '" '. $quiz_question_full_size_url_attr .' '. $question_image_lazy_loading_attr .' alt="'. $question_image_alt_text .'" ' . $question_image_style . ' class="'. $quiz_question_image_zoom_class .' '. $class_for_keyboard .'" '. $attributes_for_keyboard .'>';
                    $question_image .= '</div>';
                }
                $answer_view_class = "";
                $question_hint = '';
                $user_explanation = "";
                if ($options['randomizeAnswers']) {
                    shuffle($answers);
                }
                if (isset($question['question_hint']) && strlen($question['question_hint']) !== 0) {
                    $question_hint_arr = $options['questionsHint'];
                    $questions_hint_type = $options['questionsHint']['questionsHintType'];
                    $question_text_value = $options['questionsHint']['questionsHintValue'];
                    $questions_hint_button_value = $options['questionsHint']['questionsHintButtonValue'];

                    $questions_hint_content = "<i class='ays_fa ays_fa_info_circle ays_question_hint ". $class_for_keyboard."' ". $attributes_for_keyboard ." aria-hidden='true'></i>";
                    $questions_hint_max_width_class = '';
                    switch ( $questions_hint_type ) {
                        case 'text':
                            if ($question_text_value != '') {
                                $questions_hint_content = '<p class="ays_question_hint">'. $question_text_value .'</p>';
                            }
                            break;
                        case 'button':
                            if ($questions_hint_button_value != '') {
                                $questions_hint_max_width_class = 'ays_questions_hint_max_width_class';

                                $questions_hint_content = '<button class="ays_question_hint action-button ays_question_hint_button_type '. $class_for_keyboard.' ">'. $questions_hint_button_value .'</button>';
                            }
                            break;
                        case 'hide':
                            $questions_hint_content = '';
                            break;
                        case 'default':
                        default:
                            $questions_hint_content = "<i class='ays_fa ays_fa_info_circle ays_question_hint ". $class_for_keyboard."' ". $attributes_for_keyboard ." aria-hidden='true'></i>";
                            break;
                    }

                    $question_hint = $this->ays_autoembed($question['question_hint']);
                    $question_hint = "
                    <div class='ays_question_hint_container ". $questions_hint_max_width_class ."'>
                        ".$questions_hint_content."
                        <span class='ays_question_hint_text'>" . $question_hint . "</span>
                    </div>";

                    if ( $questions_hint_type == "hide" ) {
                        $question_hint = "";
                    }
                }
                if(isset($question['user_explanation']) && $question['user_explanation'] == 'on'){
                    $user_explanation = "<div class='ays_user_explanation'>
                        <textarea placeholder='".__('You can enter your answer explanation','quiz-maker')."' class='ays_user_explanation_text' name='user-answer-explanation[{$id}]'></textarea>
                    </div>";
                }

                if($question['wrong_answer_text'] == ''){
                    $wrong_answer_class = 'ays_do_not_show';
                }else{
                    $wrong_answer_class = '';
                }
                if($question['right_answer_text'] == ''){
                    $right_answer_class = 'ays_do_not_show';
                }else{
                    $right_answer_class = '';
                }

                // Note text
                $quiz_question_note_message = ( isset( $question_options['quiz_question_note_message']) && $question_options['quiz_question_note_message'] != '' ) ? stripslashes( $question_options['quiz_question_note_message'] ) : '';

                $quiz_question_note_message_html = '';
                if ( $quiz_question_note_message != '' ) {
                    $quiz_question_note_message_html .= '<div class="ays-quiz-question-note-message-box">';
                        $quiz_question_note_message_html .= $this->ays_autoembed($quiz_question_note_message);
                    $quiz_question_note_message_html .= '</div>';
                }

                $quiz_waiting_time_html = '';
                // Waiting time
                if ( $quiz_waiting_time ) {
                    $quiz_waiting_time_html .= '<div class="ays-quiz-question-waiting-time-box">';
                    $quiz_waiting_time_html .= '</div>';
                }
                
                if($options['questionsCounter']){
                    $questions_counter = "<p class='ays-question-counter animated'>{$current} / {$total}</p>";
                }else{
                    $questions_counter = "";
                }

                if (!empty($options['questionsReporting']) && $options['questionsReporting']) {
                    $questions_reporting = ' <div class="ays_question_report">
                                                <span title="' . esc_html($this->default_texts['questionReportIconText']) . '">
                                                <svg class="ays-quiz-open-report-window '. $class_for_keyboard .'" '. $attributes_for_keyboard .' fill="#000000" width="18px" height="18px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M3.25 4a.25.25 0 00-.25.25v12.5c0 .138.112.25.25.25h2.5a.75.75 0 01.75.75v3.19l3.427-3.427A1.75 1.75 0 0111.164 17h9.586a.25.25 0 00.25-.25V4.25a.25.25 0 00-.25-.25H3.25zm-1.75.25c0-.966.784-1.75 1.75-1.75h17.5c.966 0 1.75.784 1.75 1.75v12.5a1.75 1.75 0 01-1.75 1.75h-9.586a.25.25 0 00-.177.073l-3.5 3.5A1.457 1.457 0 015 21.043V18.5H3.25a1.75 1.75 0 01-1.75-1.75V4.25zM12 6a.75.75 0 01.75.75v4a.75.75 0 01-1.5 0v-4A.75.75 0 0112 6zm0 9a1 1 0 100-2 1 1 0 000 2z"/></svg>
                                                </span>
                                            </div>';                
                } else {
                    $questions_reporting = '';
                }
                
                $early_finish = "";                
                if($buttons['earlyButton']){
                    $early_finish = "<i class='" . ($enable_arrows ? '' : 'ays_display_none'). " ays_fa ays_fa_flag_checkered ays_early_finish action-button ays_arrow ".$class_for_keyboard."' ". $attributes_for_keyboard ."></i><input type='button' name='next' class='" . ($enable_arrows ? 'ays_display_none' : '') . " ays_early_finish action-button ".$class_for_keyboard."' ". $attributes_for_keyboard ." value='" . $settings_buttons_texts['finishButton'] . "'/>";
                }
                
                $clear_answer = "";                
                if($buttons['clearAnswerButton']){
                    $clear_answer = "<i class='" . ($enable_arrows ? '' : 'ays_display_none'). " ays_fa ays_fa_eraser ays_clear_answer action-button ays_arrow ".$class_for_keyboard."' ". $attributes_for_keyboard ."></i><input type='button' name='next' class='" . ($enable_arrows ? 'ays_display_none' : '') . " ays_clear_answer action-button ". $class_for_keyboard."' ". $attributes_for_keyboard ." value='" . $settings_buttons_texts['clearButton'] . "'/>";
                }
                if($options['correction']){
                    $clear_answer = "";
                }
                
                switch( $quiz_arrow_type ){
                    case 'default':
                        $quiz_arrow_type_class_right = "ays_fa_arrow_right";
                        $quiz_arrow_type_class_left = "ays_fa_arrow_left";
                        break;
                    case 'long_arrow':
                        $quiz_arrow_type_class_right = "ays_fa_long_arrow_right";
                        $quiz_arrow_type_class_left = "ays_fa_long_arrow_left";
                        break;
                    case 'arrow_circle_o':
                        $quiz_arrow_type_class_right = "ays_fa_arrow_circle_o_right";
                        $quiz_arrow_type_class_left = "ays_fa_arrow_circle_o_left";
                        break;
                    case 'arrow_circle':
                        $quiz_arrow_type_class_right = "ays_fa_arrow_circle_right";
                        $quiz_arrow_type_class_left = "ays_fa_arrow_circle_left";
                        break;
                    default:
                        $quiz_arrow_type_class_right = "ays_fa_arrow_right";
                        $quiz_arrow_type_class_left = "ays_fa_arrow_left";
                        break;
                }

                $buttons_order = array();

                if ($last) {
                    switch($options['informationForm']){
                        case "disable":
                            $input = "<i class='" . $buttons['nextArrow'] . " ays_fa ays_fa_flag_checkered ays_finish action-button ays_arrow ays_next_arrow ".$class_for_keyboard."' ". $attributes_for_keyboard ."></i><input type='submit' name='ays_finish_quiz' class=' " . $buttons['nextButton'] . " ays_next ays_finish action-button ".$class_for_keyboard."' value='" . $settings_buttons_texts['seeResultButton'] . "'/>";
                            break;
                        case "before":
                            $input = "<i class='" . $buttons['nextArrow'] . " ays_fa ays_fa_flag_checkered ays_finish action-button ays_arrow ays_next_arrow ".$class_for_keyboard."' ". $attributes_for_keyboard ."></i><input type='submit' name='ays_finish_quiz' class=' " . $buttons['nextButton'] . " ays_next ays_finish action-button ".$class_for_keyboard."' value='" . $settings_buttons_texts['seeResultButton'] . "'/>";
                            break;
                        case "after":
                            $input = "<i class='" . $buttons['nextArrow'] . " ays_fa ". $quiz_arrow_type_class_right ." ays_finish action-button ays_arrow ays_next_arrow ".$class_for_keyboard."' ". $attributes_for_keyboard ."></i><input type='button' class=' " . $buttons['nextButton'] . " ays_next action-button ".$class_for_keyboard."' value='" . $settings_buttons_texts['finishButton'] . "' />";
                            break;
                        default:
                            $input = "<i class='" . $buttons['nextArrow'] . " ays_fa ays_fa_flag_checkered ays_finish action-button ays_arrow ays_next_arrow ".$class_for_keyboard."' ". $attributes_for_keyboard ."></i><input type='submit' name='ays_finish_quiz' class=' " . $buttons['nextButton'] . " ays_next ays_finish action-button ".$class_for_keyboard."' value='" . $settings_buttons_texts['seeResultButton'] . "'/>";
                            break;                        
                    }
                    
                    $buttons_order['finish'] = $input;
                    $buttons_order['prev'] ="<i class=\"ays_fa ". $quiz_arrow_type_class_left ." ays_previous action-button ".$class_for_keyboard." ays_arrow " . $buttons['prevArrow'] . "\" ". $attributes_for_keyboard ."></i>
                            <input type='button' name='next' class='ays_previous action-button ".$class_for_keyboard." " . $buttons['prevButton'] . "'  value='". $settings_buttons_texts['previousButton'] ."' />";

                    $buttons_order['clear'] = $clear_answer;

                    $buttons_div = "<div class='ays_buttons_div'>
                            {$clear_answer}
                            <i class=\"ays_fa ". $quiz_arrow_type_class_left ." ays_previous action-button ays_arrow ".$class_for_keyboard." " . $buttons['prevArrow'] . "\" ". $attributes_for_keyboard ."></i>
                            <input type='button' class='ays_previous action-button ".$class_for_keyboard." " . $buttons['prevButton'] . "'  value='".$settings_buttons_texts['previousButton']."' />
                            {$input}
                        </div>";
                }else{
                    $buttons_order['finish'] = $early_finish;
                    $buttons_order['clear'] = $clear_answer;
                    $buttons_order['prev'] = "<i class=\"ays_fa ". $quiz_arrow_type_class_left ." ays_previous action-button ays_arrow ".$class_for_keyboard." " . $buttons['prevArrow'] . "\" ". $attributes_for_keyboard ."></i>
                        <input type='button' name='next' class='ays_previous action-button ".$class_for_keyboard." " . $buttons['prevButton'] . "' value='". $settings_buttons_texts['previousButton'] ."' />";
                    
                    $buttons_order['next'] = "<i class=\"ays_fa ". $quiz_arrow_type_class_right ." ays_next action-button ays_arrow ays_next_arrow ".$class_for_keyboard." " . $buttons['nextArrow'] . "\" ". $attributes_for_keyboard ."></i>
                        <input type='button' name='next' class='ays_next action-button ".$class_for_keyboard." " . $buttons['nextButton'] . "' value='" . $settings_buttons_texts['nextButton'] . "' />";

                    $buttons_div = "<div class='ays_buttons_div'>
                        {$clear_answer}
                        <i class=\"ays_fa ". $quiz_arrow_type_class_left ." ays_previous action-button ays_arrow ".$class_for_keyboard." " . $buttons['prevArrow'] . "\" ". $attributes_for_keyboard ."></i>
                        <input type='button' class='ays_previous action-button ".$class_for_keyboard." " . $buttons['prevButton'] . "' value='".$settings_buttons_texts['previousButton']."' />
                        " . $early_finish . "
                        <i class=\"ays_fa ". $quiz_arrow_type_class_right ." ays_next action-button ays_arrow ays_next_arrow ".$class_for_keyboard." " . $buttons['nextArrow'] . "\" ". $attributes_for_keyboard ."></i>
                        <input type='button' class='ays_next action-button ".$class_for_keyboard." " . $buttons['nextButton'] . "' value='" . $settings_buttons_texts['nextButton'] . "' />
                    </div>";
                }

                $buttons_settings_order = Quiz_Maker_Data::ays_quiz_get_buttons_order();

                $buttons_div = "<div class='ays_buttons_div'>";
                if(!empty($buttons_settings_order) && isset($buttons_settings_order['desktop']) && !empty($buttons_settings_order['desktop'])) {
                    foreach($buttons_settings_order['desktop'] as $bin=>$key){
                        $buttons_div .= isset($buttons_order[$bin]) ? $buttons_order[$bin] : '';
                    }
                }
                $buttons_div .= "</div>";

                if(wp_is_mobile()){

                    $buttons_div = "<div class='ays_buttons_div'>";
                    if(!empty($buttons_settings_order) && isset($buttons_settings_order['mobile']) && !empty($buttons_settings_order['mobile'])) {
                        foreach($buttons_settings_order['mobile'] as $bin=>$key){
                            $buttons_div .= isset($buttons_order[$bin]) ? $buttons_order[$bin] : '';
                        }
                    }
                    $buttons_div .= "</div>";
                }
                
                $additional_css = "";
                $answer_view_class = $options['answersViewClass'];

                $show_questions_numbering = $options['show_questions_numbering'];
                $question_numering_type = $this->ays_question_numbering( $show_questions_numbering, $total );

                $question_bg_image = (isset($question_options['bg_image']) && $question_options['bg_image'] != "") ? $question_options['bg_image'] : null;
                $question_bg_class = ($question_bg_image !== null) ? "ays-quiz-question-with-bg" : "";

                $question_title = $question['question'];

                if ( $quiz_enable_question_stripslashes ) {
                    $question_title = stripslashes( $question_title );
                }
                
                $question_numering_value = "";
                if( isset( $question_numering_type[$key] ) && $question_numering_type[$key] != '' ){

                    if( $enable_questions_ordering_by_cat && $quiz_questions_numbering_by_category ){
                        if( !empty( $question_bank_cats ) ){
                            $question_bank_cat_key = null;
                            foreach ($question_bank_cats as $question_bank_cat_id => $question_bank_cat_arr) {
                                $question_bank_cat_key_index = array_search($id, $question_bank_cat_arr);
                                if( is_numeric( $question_bank_cat_key_index ) && !is_bool( $question_bank_cat_key_index ) ){
                                    $question_bank_cat_key = $question_bank_cat_key_index;
                                    break;
                                }
                            }
                            if( !is_null( $question_bank_cat_key ) ){
                                $question_numering_value = $question_numering_type[$question_bank_cat_key] . " ";
                                $question_title = $question_numering_value . $question['question'];
                            }
                        }
                    } else {
                        $question_numering_value = $question_numering_type[$key] . " ";
                        $question_title = $question_numering_value . $question['question'];
                    }
                }
                
                $question_content = $this->ays_autoembed( $question_title );

                if ( $quiz_hide_question_text ) {
                    $question_content = '';
                }

                switch ($options['quizTheme']) {
                    case 'elegant_dark':
                    case 'elegant_light':
                    case 'rect_dark':
                    case 'rect_light':
                        $question_html = "<div class='ays_quiz_question'>
                                " . $question_content . "
                            </div>
                            {$question_image}";
                        $answer_view_class = "ays_".$answer_view_class."_view_container";
                        if($question_bg_image !== null){
                            $additional_css = "<style>
                                #ays-quiz-container-" . $quiz_id . " div.step[data-question-id='". absint($question["id"]) ."'] {
                                    background-image:url('". esc_url($question_bg_image) ."');
                                }
                            </style>";
                        }
                        break;
                    default:
                        $question_html = "<div class='ays_quiz_question'>
                                " . $question_content . "
                            </div>
                            {$question_image}";
                        $answer_view_class = "ays_".$answer_view_class."_view_container";
                        if($question_bg_image !== null){
                            $additional_css = "<style>
                                #ays-quiz-container-" . $quiz_id . " div.step[data-question-id='". absint($question["id"]) ."'] {
                                    background-image:url('". esc_url($question_bg_image) ."');
                                }
                            </style>";
                        }
                        break;
                }
                $not_influence_to_score_class = $not_influence_to_score ? 'not_influence_to_score' : '';
                $container_first_part = "<div class='step ".$question_bg_class." ".$not_influence_to_score_class."' data-question-id='" . $question["id"] . "' data-type='" . $question["type"] . "'>
                    {$question_hint}
                    {$quiz_waiting_time_html}
                    {$questions_counter}
                    <div class='ays-abs-fs'>
                        {$question_category}
                        {$question_html}
                        <div class='ays-quiz-answers $answer_view_class $max_selection_number_class $min_selection_number_class'>";

                $required_question_message = '';
                if( $enable_min_selection_number ){
                    $required_question_message = '<div class="ays-quiz-question-validation-error" role="alert"></div>';
                }

                $ays_questtion_explanation_html = "";
                if ( $show_questions_explanation != "" && $show_questions_explanation != "disable") {
                    $ays_questtion_explanation_html = $this->ays_autoembed($question["explanation"]);
                }

                $wrong_answer_text_html = "";
                $right_answer_text_html = "";
                if ( $show_answers_rw_texts != "" && $show_answers_rw_texts != "disable") {
                    $wrong_answer_text_html = $this->ays_autoembed($question["wrong_answer_text"]);
                    $right_answer_text_html = $this->ays_autoembed($question["right_answer_text"]);
                }

                $new_buttons_div_html = "";
                if( $quiz_display_messages_before_buttons ) {
                    $new_buttons_div_html = $buttons_div;
                    $buttons_div = "";
                }
                                            
                $container_last_part = "</div>                        
                        {$quiz_question_note_message_html}
                        {$user_explanation}
                        {$buttons_div}
                        {$required_question_message}
                        <div class='wrong_answer_text $wrong_answer_class' style='display:none'>
                            " . $wrong_answer_text_html . "
                        </div>
                        <div class='right_answer_text $right_answer_class' style='display:none'>
                            " . $right_answer_text_html . "
                        </div>
                        <div class='ays_questtion_explanation' style='display:none'>
                            " . $ays_questtion_explanation_html . "
                        </div>
                        <div class='ays-quiz-additonal-box'>
                            {$questions_reporting}
                        </div>   
                        {$new_buttons_div_html}
                        {$additional_css}
                    </div>
                </div>";
                
                $container[] = array(
                    'quizID'                        => $quiz_id,
                    'questionID'                    => $question['id'],
                    'questionAnswers'               => $answers,
                    'questionType'                  => $question["type"],
                    'enable_max_selection_number'   => $enable_max_selection_number,
                    'max_selection_number'          => $max_selection_number,
                    'enable_min_selection_number'   => $enable_min_selection_number,
                    'min_selection_number'          => $min_selection_number,
                    'enable_case_sensitive_text'    => $enable_case_sensitive_text,
                    'questionParts'                 => array(
                        'question_first_part'   => $container_first_part,
                        'question_middle_part'  => "",
                        'question_last_part'    => $container_last_part
                    )
                );
            }
        }
        return $container;
    }
    
    protected function get_answers_with_question_id($id){
        global $wpdb;

        $sql = "SELECT *
                FROM {$wpdb->prefix}aysquiz_answers
                WHERE question_id=" . absint($id);

        $answer = $wpdb->get_results($sql, 'ARRAY_A');

        return $answer;
    }

    protected function get_answers_with_question_ids($q_ids){
        global $wpdb;

        $new_ids_arr = array();
        if (!empty( $q_ids )) {
            foreach ($q_ids as $key => $q_id) {
                if( !is_null($q_id) && $q_id != "" && absint( $q_id ) > 0  ){
                    $new_ids_arr[] = absint($q_id);
                }
            }
        }

        if( empty( $new_ids_arr ) ){
            return array();
        }

        $in_values = $new_ids_arr;

        $in_pholders = implode( ',', array_fill( 0, count( $in_values ), '%d' ) );

        $sql = $wpdb->prepare( 
            "SELECT id, answer AS question_answer, correct, question_id
                FROM {$wpdb->prefix}aysquiz_answers c
                WHERE question_id IN ( $in_pholders )",
            $in_values
        );

        $result = $wpdb->get_results( $sql, 'ARRAY_A');
        $questions = array();
        
        foreach($result as $res){
            $questions[$res['question_id']][$res['id']] = $res;
        }

        return $questions;
    }

    public function get_quiz_questions_count($id){
        global $wpdb;

        $sql = "SELECT `question_ids`
                FROM {$wpdb->prefix}aysquiz_quizes
                WHERE id=" . absint($id);

        $questions_str = $wpdb->get_row($sql, 'ARRAY_A');
        $questions = explode(',', $questions_str['question_ids']);
        return $questions;
    }

    public function get_question_bank_categories($q_ids){
        global $wpdb;

        $q_ids = isset( $q_ids ) && $q_ids != '' ? sanitize_text_field($q_ids) : '';
        
        if($q_ids == '' || empty( $q_ids )){
            return array();
        }

        if( is_string($q_ids) ){
            $q_ids = explode(',', $q_ids);
        }

        $new_ids_arr = array();
        if (!empty( $q_ids )) {
            foreach ($q_ids as $key => $q_id) {
                if( !is_null($q_id) && $q_id != "" && absint( $q_id ) > 0  ){
                    $new_ids_arr[] = absint($q_id);
                }
            }
        }

        if( empty( $new_ids_arr ) ){
            return array();
        }

        $in_values = $new_ids_arr;

        $in_pholders = implode( ',', array_fill( 0, count( $in_values ), '%d' ) );

        $sql = $wpdb->prepare( 
            "SELECT DISTINCT c.id, c.title 
                FROM {$wpdb->prefix}aysquiz_categories c
                JOIN {$wpdb->prefix}aysquiz_questions q
                ON c.id = q.category_id
                WHERE q.id IN ( $in_pholders )",
            $in_values
        );

        $result = $wpdb->get_results( $sql, 'ARRAY_A');
        $cats = array();
        
        foreach($result as $res){
            $cats[$res['id']] = $res['title'];
        }
        
        return $cats;
    }

    public static function get_quiz_tackers_count($id){
        global $wpdb;

        $sql = "SELECT COUNT(*)
                FROM {$wpdb->prefix}aysquiz_reports
                WHERE quiz_id=" . absint($id);

        $count = intval($wpdb->get_var($sql));

        return $count;
    }

    public function sort_array_keys_by_array($array, $orderArray) {
        $ordered = array();
        foreach ($orderArray as $key) {
            if (array_key_exists('ays-question-'.$key, $array)) {
                $ordered['ays-question-'.$key] = $array['ays-question-'.$key];
                unset($array['ays-question-'.$key]);
            }
        }
        return $ordered + $array;
    }

    public function ays_finish_quiz(){
        ob_start();
        $quiz_id = isset($_REQUEST['ays_quiz_id']) ? absint( sanitize_text_field( $_REQUEST['ays_quiz_id'] ) ) : 0;

        $ays_quiz_finish_nonce = isset( $_REQUEST['ays_quiz_finish_nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['ays_quiz_finish_nonce'] ) ) : '';

        if ( ! wp_verify_nonce( $ays_quiz_finish_nonce, 'ays_quiz_finish_nonce' ) ) {

            echo json_encode( array(
                'status'  => false,
                'message' => __( 'Invalid request', 'quiz-maker' ),
            ) );
            wp_die();
        }

        if($quiz_id === 0){            
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array("status" => false, "message" => "No no no" ));
            wp_die();
        } else {
            global $wpdb;

            // $quiz_id = absint(intval($_REQUEST['ays_quiz_id']));
            $questions_answers = (isset($_REQUEST["ays_questions"])) ? Quiz_Maker_Admin::recursive_sanitize_text_field_public( $_REQUEST['ays_questions'] ) : array();
            $is_training = isset( $_REQUEST["is_training"] ) && sanitize_text_field( $_REQUEST['is_training'] ) === 'true' ? true : false;

            $questions_ids = preg_split('/,/', sanitize_text_field( $_REQUEST['ays_quiz_questions'] ) );
            $questions_answers = $this->sort_array_keys_by_array($questions_answers, $questions_ids);

            $quiz = $this->get_quiz_by_id($quiz_id);
            $quiz_intervals_data = (isset( $quiz['intervals'] ) && $quiz['intervals'] != "") ? $quiz['intervals'] : "";
            $quiz_intervals = array();
            if ( $quiz_intervals_data != "" ) {
                $quiz_intervals = json_decode($quiz_intervals_data);
            }
            $options = json_decode($quiz['options']);
            $quiz_questions_count = $this->get_quiz_questions_count($quiz_id);

            if (isset($options->enable_question_bank) && $options->enable_question_bank == 'on' && isset($options->questions_count) && intval($options->questions_count) > 0 && count($quiz_questions_count) > intval($options->questions_count) || $is_training) {
                $question_ids = preg_split('/,/', sanitize_text_field( $_REQUEST['ays_quiz_questions'] ) );
            } else {
                $question_ids = $this->get_quiz_questions_count($quiz_id);
            }
            // Strong calculation of checkbox answers
            $options->checkbox_score_by = ! isset($options->checkbox_score_by) ? 'on' : $options->checkbox_score_by;
            $strong_count_checkbox = (isset($options->checkbox_score_by) && $options->checkbox_score_by == "on") ? true : false;
            
            // Calculate the score
            $options->calculate_score = ! isset($options->calculate_score) ? 'by_correctness' : $options->calculate_score;
            $calculate_score = (isset($options->calculate_score) && $options->calculate_score != "") ? $options->calculate_score : 'by_correctness';

            // Disable store data 
            $options->disable_store_data = ! isset( $options->disable_store_data ) ? 'off' : $options->disable_store_data;
            $disable_store_data = (isset($options->disable_store_data) && $options->disable_store_data == 'off') ? true : false;

            // Display score option
            $display_score = (isset($options->display_score) && $options->display_score != "") ? $options->display_score : 'by_percantage';

            //Pass score count
            $pass_score_count = (isset($options->pass_score) && $options->pass_score != '') ? absint(intval($options->pass_score)) : 0;
            $enable_pass_score = (isset($options->enable_pass_score) && $options->enable_pass_score == 'on') || (!isset($options->enable_pass_score) && $pass_score_count > 0);

            // Information form
            $information_form = (isset($options->information_form) && $options->information_form != '') ? $options->information_form : 'disable';

            // Show information form to logged in users
            $options->show_information_form = isset($options->show_information_form) ? $options->show_information_form : 'on';
            $show_information_form = (isset($options->show_information_form) && $options->show_information_form == 'on') ? true : false;

            // Pass Score Text
            $pass_score_message = '';
            if(isset($options->pass_score_message) && $options->pass_score_message != ''){
                $pass_score_message = $this->ays_autoembed($options->pass_score_message);
            }else{
                // $pass_score_message = '<h4 style="text-align: center;">'. __("Congratulations!", 'quiz-maker') .'</h4><p style="text-align: center;">'. __("You passed the quiz!", 'quiz-maker') .'</p>';
            }

            // Fail Score Text
            $fail_score_message = '';
            if(isset($options->fail_score_message) && $options->fail_score_message != ''){
                $fail_score_message = $this->ays_autoembed($options->fail_score_message);
            }else{
                // $fail_score_message = '<h4 style="text-align: center;">'. __("Oops!", 'quiz-maker') .'</h4><p style="text-align: center;">'. __("You have not passed the quiz! <br> Try again!", 'quiz-maker') .'</p>';
            }

            if(! $show_information_form){
                if($information_form !== 'disable'){
                    $user = wp_get_current_user();
                    if($user->ID != 0){
                        $_REQUEST['ays_user_email'] = $user->data->user_email;
                        $_REQUEST['ays_user_name'] = $user->data->display_name;
                    }
                }
            }

            // Quiz create date
            $quiz_creation_date = (isset($options->create_date) && $options->create_date != '') ? sanitize_text_field( $options->create_date ) : "";

            // Quiz Author ID
            if ( isset( $options->author ) && is_string($options->author) ) {
                $quiz_current_author_data = (isset( $options->author ) && $options->author != '') ? json_decode($options->author, true) : array();
            } else {
                $options_author = isset( $options->author ) ? (array)$options->author : array();
                $quiz_current_author_data = (is_array( $options_author ) && empty( $options_author )) ? $options_author : array();
            }

            // General Setting's Options
            $quiz_settings = $this->settings;
            $general_settings_options = ($quiz_settings->ays_get_setting('options') === false) ? json_encode(array()) : $quiz_settings->ays_get_setting('options');
            $settings_options = json_decode(stripcslashes($general_settings_options), true);

            // Do not store IP adressess 
            $disable_user_ip = (isset($settings_options['disable_user_ip']) && $settings_options['disable_user_ip'] == 'on') ? true : false;

            // Limit user
            $options->limit_users = isset($options->limit_users) ? $options->limit_users : 'off';
            $limit_users = (isset($options->limit_users) && $options->limit_users == 'on') ? true : false;

            // Limit user by
            $limit_users_by = (isset($options->limit_users_by) && $options->limit_users_by != '') ? $options->limit_users_by : 'ip';

            // Quiz Title
            $quiz_title = (isset($quiz['title']) && $quiz['title'] != '') ? stripslashes( $quiz['title'] ) : '';

            $limit_users_attr = array(
                'id'     => $quiz_id,
                'name'   => 'ays_quiz_cookie_',
                'title'  => $quiz_title,
            );
            $check_cookie = $this->ays_quiz_check_cookie( $limit_users_attr );
            $return_false_status_arr = array(
                "status" => false,
                "flag"   => false,
                "text"   => __( 'You have already passed this quiz.', 'quiz-maker' ),
            );

            if ( $check_cookie ) {
                echo json_encode( $return_false_status_arr );
                wp_die();
            }
            if ( $limit_users ) {
                switch ( $limit_users_by ) {
                    case 'ip':
                        break;
                    case 'user_id':
                        break;
                    case 'cookie':
                        if ( ! $check_cookie ) {
                            $set_cookie = $this->ays_quiz_set_cookie( $limit_users_attr );
                        }
                        break;
                    case 'ip_cookie':
                        $check_user_by_ip = $this->get_user_by_ip($quiz_id);
                        if ( ! $check_cookie || $check_user_by_ip <= 0 ) {
                            if ( ! $check_cookie ) {
                                $set_cookie = $this->ays_quiz_set_cookie( $limit_users_attr );
                            }
                        } else {
                            echo json_encode( $return_false_status_arr );
                            wp_die();
                        }
                        break;
                    default:
                        break;
                }
            }

            $user_explanation = array();
            if ( isset( $_REQUEST['user-answer-explanation'] ) && is_array( $_REQUEST['user-answer-explanation'] ) ) {
                $raw_user_explanation = wp_unslash( $_REQUEST['user-answer-explanation'] );

                foreach ( $raw_user_explanation as $u_question_id => $u_explanation ) {
                    $u_question_id = absint( $u_question_id );

                    if ( $u_question_id === 0 ) {
                        continue;
                    }

                    $user_explanation[ $u_question_id ] = sanitize_textarea_field( $u_explanation );
                }
            }

            $questions_count = count($question_ids);
            $correctness = array();
            $user_answered = array();
            $correctness_results = array();
            $answer_max_weights = array();
            if (is_array($questions_answers)) {
                $quests = array();
                $questions_cats = array();
                $quiz_questions_ids = array();
                $question_bank_by_categories1 = array();

                foreach($questions_answers as $key => $val){
                    $question_id = explode('-', $key)[2];
                    if( absint($question_id) > 0 ){
                        $quiz_questions_ids[] = strval( absint($question_id) );
                    }
                }

                $questions_categories = $this->get_questions_categories( implode( ',', $quiz_questions_ids ) );
                $quest_s = $this->get_quiz_questions_by_ids($quiz_questions_ids);
                foreach($quest_s as $quest){
                    $quests[$quest['id']] = $quest;
                }

                foreach($quiz_questions_ids as $key => $question_id){
                    if( isset($quests[$question_id]) ){
                        $questions_cats[$quests[$question_id]['category_id']][$question_id] = null;
                    }
                }
                foreach ($questions_answers as $key => $questions_answer) {

                    $questions_answer_for_db = $questions_answer;
                    if (is_array($questions_answer_for_db)) {
                        $questions_answer_for_db = array_map('intval', $questions_answer_for_db);
                    } else {
                        $questions_answer_for_db = sanitize_text_field(stripslashes($questions_answer_for_db) );
                    }

                    $continue = false;
                    $question_id = explode('-', $key)[2];
                    if($this->is_question_not_influence($question_id)){
                        $questions_count--;
                        $continue = true;
                    }
                    $multiple_correctness = array();
                    $has_multiple = $this->has_multiple_correct_answers($question_id);
                    $answer_max_weights[] = $this->get_answers_max_weight($question_id, $has_multiple);
                    
                    $user_answered["question_id_" . $question_id] = $questions_answer_for_db;
                    if ($has_multiple) {                        
                        if (is_array($questions_answer)) {
                            foreach ($questions_answer as $answer_id) {
                                $answer_id = intval($answer_id);
                                $multiple_correctness[] = $this->check_answer_correctness($question_id, $answer_id, $calculate_score);
                            }
                            
                            if($calculate_score == 'by_points'){
                                if(!$continue){
                                    $correctness[$question_id] = array_sum($multiple_correctness);
                                }
                                $correctness_results["question_id_" . $question_id] = array_sum($multiple_correctness);
                                continue;
                            }
                            
                            if($strong_count_checkbox === false){
                                if(!$continue){
                                    $correctness[$question_id] = $this->isHomogenousStrong($multiple_correctness, $question_id);
                                }
                                $correctness_results["question_id_" . $question_id] = $this->isHomogenousStrong($multiple_correctness, $question_id);
                            }else{
                                if ($this->isHomogenous($multiple_correctness, $question_id)) {
                                    if(!$continue){
                                        $correctness[$question_id] = true;
                                    }
                                    $correctness_results["question_id_" . $question_id] = true;
                                } else {
                                    if(!$continue){
                                        $correctness[$question_id] = false;
                                    }
                                    $correctness_results["question_id_" . $question_id] = false;
                                }
                            }
                        } else {
                            if($calculate_score == 'by_points'){
                                $questions_answer = intval($questions_answer);
                                if(!$continue){
                                    $correctness[$question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                                }
                                $correctness_results["question_id_" . $question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                                continue;
                            }
                            if($strong_count_checkbox === false){
                                $questions_answer = intval($questions_answer);
                                if($this->check_answer_correctness($question_id, $questions_answer, $calculate_score)){
                                    if(!$continue){
                                        $correctness[$question_id] = 1 / intval($this->count_multiple_correct_answers($question_id));
                                    }
                                }else{
                                    if(!$continue){
                                        $correctness[$question_id] = false;
                                    }
                                }
                                $correctness_results["question_id_" . $question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                            }else{
                                if(!$continue){
                                    $correctness[$question_id] = false;
                                }
                                $correctness_results["question_id_" . $question_id] = false;
                            }
                        }
                    } elseif($this->has_text_answer($question_id)) {
                        $quests_data = ( isset( $quests[$question_id] ) && ! empty( $quests[$question_id] ) ) ? $quests[$question_id] : array();
                        $quests_data_options = isset( $quests_data['options'] ) ? json_decode( $quests_data['options'], true ) : array(); 
                        if(!$continue){
                            $correctness[$question_id] = $this->check_text_answer_correctness($question_id, $questions_answer, $calculate_score, $quests_data_options);
                        }
                        $correctness_results["question_id_" . $question_id] = $this->check_text_answer_correctness($question_id, $questions_answer, $calculate_score, $quests_data_options);
                    } else {
                        $questions_answer = intval($questions_answer);
                        if(!$continue){
                            $correctness[$question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                        }
                        $correctness_results["question_id_" . $question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                    }
                }

                $question_answer_data = array();
                if( !empty($quiz_questions_ids) ){
                    $question_answer_data = $this->get_answers_with_question_ids( $quiz_questions_ids );
                }

                $new_correctness = array();
                $quiz_weight = array();
                $corrects_count = 0;
                $quiz_weight_correctness = array();
                $corrects_count_by_cats = array();
                foreach($questions_cats as $cat_id => &$q_ids){
                    $corrects_count_by_cats[$cat_id] = 0;
                    foreach($correctness as $question_id => $item){
                        if( array_key_exists( strval($question_id), $q_ids ) ){
                            switch($calculate_score){
                                case "by_correctness":
                                    if($item){
                                        $corrects_count_by_cats[$cat_id]++;
                                    }
                                break;
                                default:
                                    if($item){
                                        $corrects_count_by_cats[$cat_id]++;
                                    }
                                break;
                            }
                        }
                    }
                }


                foreach($correctness as $question_id => $item){
                    $question_weight = $this->get_question_weight($question_id);
                    $new_correctness[strval($question_id)] = $question_weight * floatval($item);
                    $quiz_weight[] = $question_weight;
                    $quiz_weight_correctness[strval($question_id)] = $question_weight;
                    switch($calculate_score){
                        case "by_correctness":
                            if($item){
                                $corrects_count++;
                            }
                        break;
                        default:
                            if($item){
                                $corrects_count++;
                            }
                        break;
                    }
                }


                $quiz_weight_new_correctness_by_cats = array();
                $quiz_weight_correctness_by_cats = array();

                $questions_count_by_cats = array();
                foreach($questions_cats as $cat_id => &$q_ids){
                    foreach($q_ids as $q_id => &$val){
                        $val = array_key_exists($q_id, $new_correctness) ? $new_correctness[$q_id] : false;
                        $quiz_weight_new_correctness_by_cats[$cat_id][$q_id] = $val;
                        if( $this->is_question_not_influence($q_id) ){
                            continue;
                        }

                        if ( isset( $quiz_weight_correctness[$q_id] ) && sanitize_text_field( $quiz_weight_correctness[$q_id] ) != '' ) {
                            $quiz_weight_correctness_by_cats[$cat_id][$q_id] = $quiz_weight_correctness[$q_id];
                        }
                    }
                    $questions_count_by_cats[$cat_id] = count($q_ids);
                }

                $final_score_by_cats = array();
                $quiz_weight_cats = array();
                $correct_answered_count_cats = array();
                foreach($quiz_weight_new_correctness_by_cats as $cat_id => $q_ids){

                    if ( ! isset( $quiz_weight_correctness_by_cats[$cat_id] ) ) {
                        continue;
                    }
                    $quiz_weight_correctness_by_cats[$cat_id] = array_filter($quiz_weight_correctness_by_cats[$cat_id], "strlen");

                    switch($calculate_score){
                        case "by_correctness":
                            $quiz_weight_cat = array_sum($quiz_weight_correctness_by_cats[$cat_id]);
                            $quiz_weight_cats[$cat_id] = array_sum($quiz_weight_correctness_by_cats[$cat_id]);
                        break;
                        default:
                            $quiz_weight_cat = array_sum($quiz_weight_correctness_by_cats[$cat_id]);
                            $quiz_weight_cats[$cat_id] = array_sum($quiz_weight_correctness_by_cats[$cat_id]);
                        break;
                    }

                    $correct_answered_count_cat = array_sum($q_ids);

                    if($quiz_weight_cat == 0){
                        $final_score_by_cats[$cat_id] = floatval(0);
                    }else{
                        $final_score_by_cats[$cat_id] = floatval(floor(($correct_answered_count_cat / $quiz_weight_cat) * 100));
                    }
                }
                // $average_percent = 100 / $questions_count;
                
                switch($calculate_score){
                    case "by_correctness":
                        $quiz_weight = array_sum($quiz_weight);
                    break;
                    case "by_points":
                        $quiz_weight = array_sum($answer_max_weights);
                    break;
                    default:
                        $quiz_weight = array_sum($quiz_weight);
                    break;
                }
                $correct_answered_count = array_sum($new_correctness);
                
                if( $quiz_weight > 0 ){
                    $final_score = intval(floor(($correct_answered_count / $quiz_weight) * 100));
                }else{
                    $final_score = 0;
                }

                $current_quiz_question_categories_count = 0;
                if( !empty( $final_score_by_cats ) && is_array( $final_score_by_cats ) ){
                    $current_quiz_question_categories_count = count($final_score_by_cats);
                }

                $score_by_cats = array();
                foreach($final_score_by_cats as $cat_id => $cat_score){

                    $questions_categorie_title = isset( $questions_categories[$cat_id] ) && $questions_categories[$cat_id] != "" ? esc_attr( $questions_categories[$cat_id] ) : "";

                    switch($display_score){
                        case "by_correctness":
                            $score_by_cats[$cat_id] = array(
                                'score' => $corrects_count_by_cats[$cat_id] . " / " . $questions_count_by_cats[$cat_id],
                                'categoryName' => $questions_categorie_title,
                            );
                        break;
                        case "by_percentage":
                            $score_by_cats[$cat_id] = array(
                                'score' => $cat_score . "%",
                                'categoryName' => $questions_categorie_title,
                            );
                        break;
                        default:
                            $score_by_cats[$cat_id] = array(
                                'score' => $cat_score . "%",
                                'categoryName' => $questions_categorie_title,
                            );
                        break;
                    }
                }

                if(empty($score_by_cats)){
                    $result_score_by_categories = '';
                }else{
                    $result_score_by_categories = '<div class="ays_result_by_cats">';
                    foreach($score_by_cats as $cat_id => $cat){
                        $result_score_by_categories .= '<p class="ays_result_by_cat">
                            <strong class="ays_result_by_cat_name">'. $cat['categoryName'] .':</strong>
                            <span class="ays_result_by_cat_score">'. $cat['score'] .'</span>
                        </p>';
                    }
                    $result_score_by_categories .= '</div>';
                    $result_score_by_categories = str_replace(array("\r\n", "\n", "\r"), "", $result_score_by_categories);
                } 

                switch($display_score){
                    case "by_correctness":
                        $score = $corrects_count . " / " . $questions_count;
                    break;
                    case "by_percentage":
                        $score = $final_score . "%";
                    break;
                    default:
                        $score = $final_score . "%";
                    break;
                }

                $wrong_answered_count = $questions_count - $corrects_count;

                $skipped_questions_count = 0;
                foreach ($user_answered as $q_id => $user_answered_val) {
                    $question_id_val = explode('_', $q_id)[2];
                    if($this->is_question_not_influence($question_id_val)){
                        continue;
                    }

                    if ( $user_answered_val == '') {
                        $skipped_questions_count++;
                    }
                }

                $only_wrong_answers_count = $questions_count - ( $corrects_count + $skipped_questions_count );

                $answered_questions_count = $questions_count - $skipped_questions_count;
                $user_failed_questions_count = $corrects_count + ( $questions_count - ($corrects_count + $skipped_questions_count) );

                if ( ! empty( $user_failed_questions_count ) || $user_failed_questions_count != 0) {
                    $score_by_answered_questions = round( ( $corrects_count * 100 ) / $user_failed_questions_count , 1 );
                } else {
                    $score_by_answered_questions = 0;
                }

                $user_first_name        = '';
                $user_last_name         = '';
                $user_nickname          = '';
                $user_display_name      = '';
                $user_wordpress_email   = '';
                $user_wordpress_roles   = '';
                $user_wordpress_website = '';
                $user_id = get_current_user_id();
                if($user_id != 0){
                    $usermeta = get_user_meta( $user_id );
                    if($usermeta !== null){
                        $user_first_name = (isset($usermeta['first_name'][0]) && sanitize_text_field( $usermeta['first_name'][0] != '') ) ? sanitize_text_field( $usermeta['first_name'][0] ) : '';
                        $user_last_name  = (isset($usermeta['last_name'][0]) && sanitize_text_field( $usermeta['last_name'][0] != '') ) ? sanitize_text_field( $usermeta['last_name'][0] ) : '';
                        $user_nickname   = (isset($usermeta['nickname'][0]) && sanitize_text_field( $usermeta['nickname'][0] != '') ) ? sanitize_text_field( $usermeta['nickname'][0] ) : '';
                    }

                    $current_user_data = get_userdata( $user_id );
                    if ( ! is_null( $current_user_data ) && $current_user_data ) {
                        $user_display_name    = ( isset( $current_user_data->data->display_name ) && $current_user_data->data->display_name != '' ) ? sanitize_text_field( $current_user_data->data->display_name ) : "";
                        $user_wordpress_email = ( isset( $current_user_data->data->user_email ) && $current_user_data->data->user_email != '' ) ? sanitize_text_field( $current_user_data->data->user_email ) : "";

                        $user_wordpress_roles = ( isset( $current_user_data->roles ) && ! empty( $current_user_data->roles ) ) ? $current_user_data->roles : "";

                        if ( !empty( $user_wordpress_roles ) && $user_wordpress_roles != "" ) {
                            if ( is_array( $user_wordpress_roles ) ) {
                                $user_wordpress_roles = implode(",", $user_wordpress_roles);
                            }
                        }

                        $user_wordpress_website_url = ( isset( $current_user_data->user_url ) && ! empty( $current_user_data->user_url ) ) ? sanitize_url($current_user_data->user_url) : "";

                        if( !empty( $user_wordpress_website_url ) ){
                            $user_wordpress_website = "<a href='". esc_url( $user_wordpress_website_url ) ."' target='_blank' class='ays-quiz-user-website-link-a-tag'>". __( "Website", 'quiz-maker' ) ."</a>";
                        }

                    }
                }

                $current_quiz_author = __( "Unknown", 'quiz-maker' );
                $current_quiz_author_nickname = "";
                $current_quiz_author_email = "";
                $current_quiz_author_website_url = "";
                
                $super_admin_email = get_option('admin_email');

                if( !empty($quiz_current_author_data) ){
                    if( !is_array($quiz_current_author_data) ){
                        $quiz_current_author_data = json_decode($quiz_current_author_data, true);
                    }

                    $quiz_current_author = (isset($quiz_current_author_data['id']) && $quiz_current_author_data['id'] != "") ? absint(sanitize_text_field( $quiz_current_author_data['id'] )) : "";

                    $current_quiz_user_data = get_userdata( $quiz_current_author );
                    if ( ! is_null( $current_quiz_user_data ) && $current_quiz_user_data ) {
                        $current_quiz_author            = ( isset( $current_quiz_user_data->data->display_name ) && $current_quiz_user_data->data->display_name != '' ) ? sanitize_text_field( $current_quiz_user_data->data->display_name ) : "";
                        $current_quiz_author_email      = ( isset( $current_quiz_user_data->data->user_email ) && $current_quiz_user_data->data->user_email != '' ) ? sanitize_text_field( $current_quiz_user_data->data->user_email ) : "";
                        $current_quiz_author_nickname   = ( isset( $current_quiz_user_data->data->user_nicename ) && $current_quiz_user_data->data->user_nicename != '' ) ? sanitize_text_field( $current_quiz_user_data->data->user_nicename ) : "";

                        $current_quiz_author_website_url = ( isset( $current_quiz_user_data->user_url ) && ! empty( $current_quiz_user_data->user_url ) ) ? sanitize_url($current_quiz_user_data->user_url) : "";

                        if( !empty( $current_quiz_author_website_url ) ){
                            $current_quiz_author_website_url = "<a href='". esc_url( $current_quiz_author_website_url ) ."' target='_blank' class='ays-quiz-user-website-link-a-tag'>". __( "Website", 'quiz-maker' ) ."</a>";
                        }
                    }
                }

                // $correct_answered_count = array_sum($correctness);

                // $final_score = floor(($average_percent * $correct_answered_count));

                if($disable_user_ip){
                    $user_ip = '';
                }else{
                    $user_ip = $this->get_user_ip();
                }

                $current_user_ip = $user_ip;
                
                $correctness_and_answers = array(
                    'correctness' => $correctness_results,
                    'user_answered' => $user_answered
                );
                $ays_user_name = isset( $_REQUEST['ays_user_name'] ) && $_REQUEST['ays_user_name'] != '' ? trim( esc_sql( sanitize_text_field( $_REQUEST['ays_user_name'] ) ) ) : '';
                $ays_user_email = isset( $_REQUEST['ays_user_email'] ) && $_REQUEST['ays_user_email'] != '' ? esc_sql( sanitize_email( $_REQUEST['ays_user_email'] ) ) : '';
                $ays_user_phone = isset( $_REQUEST['ays_user_phone'] ) && $_REQUEST['ays_user_phone'] != '' ? esc_sql( sanitize_text_field( $_REQUEST['ays_user_phone'] ) ) : '';
                $start_date = isset( $_REQUEST['start_date'] ) && $_REQUEST['start_date'] != '' ? sanitize_text_field( $_REQUEST['start_date'] ) : current_time( 'mysql' );
                $end_date = isset( $_REQUEST['end_date'] ) && $_REQUEST['end_date'] != '' ? sanitize_text_field( $_REQUEST['end_date'] ) : current_time( 'mysql' );

                $quiz_curent_page_link = isset( $_REQUEST['ays_quiz_curent_page_link'] ) && $_REQUEST['ays_quiz_curent_page_link'] != '' ? sanitize_url( $_REQUEST['ays_quiz_curent_page_link'] ) : "";

                $quiz_current_page_link_html = "<a href='". esc_sql( $quiz_curent_page_link ) ."' target='_blank' class='ays-quiz-curent-page-link-a-tag'>". __( "Quiz link", 'quiz-maker' ) ."</a>";

                // WP home page url
                $home_main_url = home_url();
                $wp_home_page_url = '<a href="'.$home_main_url.'" target="_blank">'.$home_main_url.'</a>';

                $get_site_title = get_bloginfo('name');
                $get_site_description = get_bloginfo('description');
                
                $message_data = array(
                    'quiz_name'                                 => stripslashes($quiz['title']),
                    'user_name'                                 => $ays_user_name,
                    'user_email'                                => $ays_user_email,
                    'user_phone'                                => $ays_user_phone,
                    'score'                                     => $final_score . "%",
                    'current_date'                              => date_i18n( get_option( 'date_format' ), strtotime( $end_date ) ),
                    'results_by_cats'                           => $result_score_by_categories,
                    'avg_score'                                 => $this->ays_get_average_of_scores($quiz_id) . "%",
                    'avg_rate'                                  => round($this->ays_get_average_of_rates($quiz_id), 1),
                    'user_pass_time'                            => $this->get_time_difference( $start_date, $end_date ),
                    'quiz_time'                                 => $this->secondsToWords($options->timer),
                    'user_corrects_count'                       => $corrects_count,
                    'wrong_answers_count'                       => $wrong_answered_count,
                    'skipped_questions_count'                   => $skipped_questions_count,
                    'answered_questions_count'                  => $answered_questions_count,
                    'score_by_answered_questions'               => $score_by_answered_questions,
                    'user_first_name'                           => $user_first_name,
                    'user_last_name'                            => $user_last_name,
                    'questions_count'                           => $questions_count,
                    'only_wrong_answers_count'                  => $only_wrong_answers_count,
                    'user_nickname'                             => $user_nickname,
                    'user_display_name'                         => $user_display_name,
                    'user_wordpress_email'                      => $user_wordpress_email,
                    'user_wordpress_roles'                      => $user_wordpress_roles,
                    'user_wordpress_website'                    => $user_wordpress_website,
                    'quiz_creation_date'                        => date_i18n( get_option( 'date_format' ), strtotime( $quiz_creation_date ) ),
                    'current_quiz_author'                       => $current_quiz_author,
                    'current_quiz_page_link'                    => $quiz_current_page_link_html,
                    'current_user_ip'                           => $current_user_ip,
                    'current_quiz_author_email'                 => $current_quiz_author_email,
                    'current_quiz_author_nickname'              => $current_quiz_author_nickname,
                    'admin_email'                               => $super_admin_email,
                    'home_page_url'                             => $wp_home_page_url,
                    'quiz_id'                                   => $quiz_id,
                    'user_id'                                   => $user_id,
                    'current_quiz_question_categories_count'    => $current_quiz_question_categories_count,
                    'site_title'                                => $get_site_title,
                    'current_quiz_author_website_url'           => $current_quiz_author_website_url,
                    'site_description'                          => $get_site_description,
                );

                $data = array(
                    'user_ip'               => $user_ip,
                    'user_name'             => $ays_user_name,
                    'user_email'            => $ays_user_email,
                    'user_phone'            => $ays_user_phone,
                    'start_date'            => esc_sql( $start_date ),
                    'end_date'              => esc_sql( $end_date ),
                    'answered'              => $correctness_and_answers,
                    'score'                 => $final_score,
                    'user_explanation'      => $user_explanation,
                    'user_corrects_count'   => $corrects_count,
                    'questions_count'       => $questions_count,
                    'calc_method'           => $calculate_score,
                    'quiz_id'               => $quiz_id
                );

                if( $is_training === true ){
                    $disable_store_data = false;
                }

                // Disabling store data in DB
                if($disable_store_data){
                    $result = $this->add_results_to_db($data);
                }else{
                    $result = true;
                }

                $last_result_id = $wpdb->insert_id;

                $message_data['avg_score_by_category'] = $this->ays_get_average_score_by_category($quiz_id);
                $message_data['result_id'] = $last_result_id;

                $quiz_show_results_based_pass_score_flag = false;
                if ($final_score >= $pass_score_count) {
                    $score_message = $pass_score_message;
                    $quiz_show_results_based_pass_score_flag = true;
                }else{
                    $score_message = $fail_score_message;
                }

                $final_score_message = "";
                if($enable_pass_score && $pass_score_count > 0){
                    $final_score_message = $this->replace_message_variables($score_message, $message_data);
                }

                $result_text = '';
                if(isset($options->result_text) && $options->result_text != ''){
                    $result_text = $this->ays_autoembed($options->result_text);
                }

                $result_text = $this->replace_message_variables($result_text, $message_data);

                $heading_for_share_buttons = '';
                if( isset($options->enable_social_buttons) && $options->enable_social_buttons ){
                    $heading_for_share_buttons = isset( $options->social_buttons_heading ) ? $options->social_buttons_heading : "";
                    $heading_for_share_buttons = $this->replace_message_variables($heading_for_share_buttons, $message_data);
                    $heading_for_share_buttons = $this->ays_autoembed($heading_for_share_buttons);
                }

                $heading_for_social_links = '';
                if( isset($options->enable_social_links) && $options->enable_social_links ){
                    if( isset( $options->social_links_heading ) && $options->social_links_heading != "" ){
                        $heading_for_social_links = isset( $options->social_links_heading ) ? $options->social_links_heading : "";
                        $heading_for_social_links = $this->replace_message_variables($heading_for_social_links, $message_data);
                        $heading_for_social_links = $this->ays_autoembed($heading_for_social_links);
                    }

                }

                if( $is_training === true ) {
                    if ( has_action( 'ays_qm_front_end_trainings_save' ) ) {
                        $correct_questions = array();
                        $wrong_questions = array();
                        foreach ( $correctness_results as $q_key => $is_correct ){
                            $qid = explode( '_', $q_key )[2];
                            if( $is_correct === true ){
                                $correct_questions[] = absint( $qid );
                            }else{
                                $wrong_questions[] = absint( $qid );
                            }
                        }
                        do_action( "ays_qm_front_end_trainings_save", $quiz_id, $correct_questions, $wrong_questions );
                    }
                }else{
                    if ( has_action( 'ays_qm_front_end_integrations' ) ) {
                        $integration_args          = array();
                        $integration_options       = (array) $options;
                        $integration_options['id'] = $quiz_id;
                        $integration_options['ays_quiz_final_score'] = $final_score;
                        // $integration_options['ays_quiz_score_by'] = $score_by;
                        // $integration_options['quiz_attributes_information'] = $quiz_attributes_information_slag;
                        $integrations_data         = apply_filters( 'ays_qm_front_end_integrations_options', $integration_args, $integration_options );
                        $data["quiz_message_variables_data"] = $message_data;
                        $data["gamipress_interval"] = array();
                        $data["gamipress_questions_count"] = $questions_count;
                        $data["gamipress_correct_answered_count"] = $correct_answered_count;
                        do_action( "ays_qm_front_end_integrations", $integrations_data, $integration_options, $data );
                    }
                }

                ob_end_clean();
                $ob_get_clean = ob_get_clean();
                if ($result) {
                    echo json_encode(array(
                        "status"                => true,
                        "score"                 => $score,
                        "scoreMessage"          => $final_score_message,
                        "displayScore"          => $display_score,
                        "text"                  => $result_text,
                        "result_id"             => $last_result_id,
                        // "result_id"             => $last_result_id,
                        "socialHeading"         => $heading_for_share_buttons,
                        "socialLinksHeading"    => $heading_for_social_links,
                        "pass_score_flag"       => $quiz_show_results_based_pass_score_flag,
                        "question_answer_data"  => $question_answer_data,
                    ));
                    wp_die();                
                }else{
                    echo json_encode(array(
                        "status" => false, 
                        "text"   => "No no no"
                    ));
                    wp_die();
                }

            } else {
                $admin_mails = get_option('admin_email');
                
                ob_end_clean();
                $ob_get_clean = ob_get_clean();
                
                echo json_encode(array(
                    "status"     => false, 
                    "text"       => "No no no", 
                    "admin_mail" => $admin_mails 
                ));
                wp_die();
            }
        }
    }

    public function replace_message_variables($content, $data){
        foreach($data as $variable => $value){
            if( !is_array( $value ) ){
                $content = str_replace("%%".$variable."%%", $value, $content);
            }
        }
        return $content;
    }
    
    public function get_answers_max_weight($question_id, $has_multiple){
        global $wpdb;
        //$answers_table = $wpdb->prefix . "aysquiz_answers";
        //$question_id = absint(intval($question_id));
        //$query_part = "MAX(weight)";
        //if($has_multiple){
        //    $query_part = "SUM(weight)";
        //}
        //$sql = "SELECT {$query_part} FROM {$answers_table} WHERE question_id={$question_id}";
        //$checks = $wpdb->get_var($sql);
        //$answer_weight = floatval($checks);
        
        return 0;
    }

    public function check_answer_correctness($question_id, $answer_id, $calc_method){
        global $wpdb;
        $answers_table = $wpdb->prefix . "aysquiz_answers";
        $question_id = absint(intval($question_id));
        $answer_id = absint(intval($answer_id));
        $checks = $wpdb->get_row("SELECT * FROM {$answers_table} WHERE question_id={$question_id} AND id={$answer_id}", "ARRAY_A");
        if( empty( $checks ) ){
            return false;
        }
        $answer = false;
        switch($calc_method){
            case "by_correctness":
                if (absint(intval($checks["correct"])) == 1)
                    $answer = true;
                else
                    $answer = false;
            break;
            case "by_points":
                $answer_weight = isset($checks['weight']) ? floatval($checks['weight']) : 0;
                $answer = $answer_weight;
            break;
            default:
                if (absint(intval($checks["correct"])) == 1)
                    $answer = true;
                else
                    $answer = false;
            break;
        }
        return $answer;
    }

    public function check_text_answer_correctness($question_id, $answer, $calc_method, $options = array()){
        global $wpdb;
        $answers_table = $wpdb->prefix . "aysquiz_answers";
        $question_id = absint(intval($question_id));
        $checks = $wpdb->get_row("SELECT COUNT(*) AS qanak, answer, weight FROM {$answers_table} WHERE question_id={$question_id}", ARRAY_A);

        $checks['answer'] = (isset( $checks['answer'] ) && $checks['answer'] != "") ? $checks['answer'] : "";

        $correct_answers = mb_strtolower($checks['answer']);

        // Disable strip slashes for answers
        $options['quiz_disable_answer_stripslashes'] = isset($options['quiz_disable_answer_stripslashes']) ? sanitize_text_field( $options['quiz_disable_answer_stripslashes'] ) : 'off';
        $quiz_disable_answer_stripslashes = (isset($options['quiz_disable_answer_stripslashes']) && $options['quiz_disable_answer_stripslashes'] == 'on') ? true : false;

        if ( !$quiz_disable_answer_stripslashes ) {
            $answer = stripslashes($answer);
        }

        $correct = false;
        $text_type = $this->text_answer_is($question_id);

        // Enable case sensitive text
        $options['enable_case_sensitive_text'] = isset($options['enable_case_sensitive_text']) ? sanitize_text_field( $options['enable_case_sensitive_text'] ) : 'off';
        $enable_case_sensitive_text = (isset($options['enable_case_sensitive_text']) && sanitize_text_field( $options['enable_case_sensitive_text'] ) == 'on') ? true : false;

        if( $text_type == 'text' || $text_type == 'short_text' ){
            if ( $enable_case_sensitive_text ) {
                $correct_answers = $checks['answer'];
            }
        }

        if($text_type == 'date'){
            // if(Quiz_Maker_Admin::validateDate($answer, 'Y-m-d')){
                if(date('Y-m-d', strtotime($correct_answers)) == date('Y-m-d', strtotime($answer))){
                    $correct = true;
                }
            // }
        }elseif($text_type != 'number'){
            $correct_answers = explode('%%%', $correct_answers);
            foreach($correct_answers as $c){
                if ($enable_case_sensitive_text) {
                    if(trim($c) === trim($answer)){
                        $correct = true;
                        break;
                    }
                } else {
                    if(trim($c) === mb_strtolower(trim($answer))){
                        $correct = true;
                        break;
                    }
                }
            }
        }else{
            if($correct_answers == mb_strtolower(trim($answer))){
                $correct = true;
            }
        }
        $answer_res = false;
        switch($calc_method){
            case "by_correctness":
                if($correct)
                    $answer_res = true;
                else
                    $answer_res = false;
            break;
            case "by_points":
                $answer_weight = floatval($checks['weight']);
                if($correct)
                    $answer_res = $answer_weight;
                else
                    $answer_res = 0;
            break;
            default:
                if($correct)
                    $answer_res = true;
                else
                    $answer_res = false;
            break;
        }
        return $answer_res;
    }

    public function get_correct_and_wrong_answers($question_id, $question_type){
        $question_id = absint(sanitize_text_field($question_id));

        $answers = $this->get_answers_with_question_id($question_id);

        if( empty( $answers ) ){
            return false;
        }

        $question_answer = array();
        switch ($question_type) {
            case "text":
            case "short_text":
            case "number":
            case "date":
                foreach ($answers as $key => $answer) {
                    $question_answer = htmlspecialchars_decode(stripslashes($answer["answer"]), ENT_QUOTES);
                }
                break;
            case "radio":
            case "checkbox":
            case "true_or_false":
            case "select":
            default:
                foreach ($answers as $key => $answer) {
                    $question_answer[ $answer["id"] ] = htmlspecialchars_decode(stripslashes($answer["correct"]), ENT_QUOTES);
                }
                break;
        }

        return $question_answer;
    }

    public function get_correct_text_answer($question_id, $answer, $calc_method, $options = array()){
        global $wpdb;
        $answers_table = $wpdb->prefix . "aysquiz_answers";
        $question_id = absint(intval($question_id));
        $checks = $wpdb->get_row("SELECT COUNT(*) AS qanak, answer, weight FROM {$answers_table} WHERE question_id={$question_id}", ARRAY_A);

        $checks['answer'] = (isset( $checks['answer'] ) && $checks['answer'] != "") ? $checks['answer'] : "";

        $correct_answers = mb_strtolower($checks['answer']);

        // Disable strip slashes for answers
        $options['quiz_disable_answer_stripslashes'] = isset($options['quiz_disable_answer_stripslashes']) ? sanitize_text_field( $options['quiz_disable_answer_stripslashes'] ) : 'off';
        $quiz_disable_answer_stripslashes = (isset($options['quiz_disable_answer_stripslashes']) && $options['quiz_disable_answer_stripslashes'] == 'on') ? true : false;

        if ( !$quiz_disable_answer_stripslashes ) {
            $answer = stripslashes($answer);
        }

        $correct = false;
        $text_type = $this->text_answer_is($question_id);

        // Enable case sensitive text
        $options['enable_case_sensitive_text'] = isset($options['enable_case_sensitive_text']) ? sanitize_text_field( $options['enable_case_sensitive_text'] ) : 'off';
        $enable_case_sensitive_text = (isset($options['enable_case_sensitive_text']) && sanitize_text_field( $options['enable_case_sensitive_text'] ) == 'on') ? true : false;

        if( $text_type == 'text' || $text_type == 'short_text' ){
            if ( $enable_case_sensitive_text ) {
                $correct_answers = $checks['answer'];
            }
        }

        return $correct_answers;
    }

    public function count_multiple_correct_answers($question_id){
        global $wpdb;
        $answers_table = $wpdb->prefix . "aysquiz_answers";
        $question_id = absint(intval($question_id));

        $get_answers = $wpdb->get_var("SELECT COUNT(*) FROM {$answers_table} WHERE question_id={$question_id} AND correct=1");        
        return $get_answers;
    }
    
    public function has_multiple_correct_answers($question_id){
        global $wpdb;
        $answers_table = $wpdb->prefix . "aysquiz_answers";
        $question_id = absint(intval($question_id));

        $get_answers = $wpdb->get_var("SELECT COUNT(*) FROM {$answers_table} WHERE question_id={$question_id} AND correct=1");

        if (intval($get_answers) > 1) {
            return true;
        }
        return false;
    }
    
    public function is_question_not_influence($question_id){
        global $wpdb;
        $questions_table = $wpdb->prefix . "aysquiz_questions";
        $question_id = absint(intval($question_id));

        $question = $wpdb->get_row("SELECT * FROM {$questions_table} WHERE id={$question_id};", "ARRAY_A");
        $question['not_influence_to_score'] = ! isset($question['not_influence_to_score']) ? 'off' : $question['not_influence_to_score'];
        if(isset($question['not_influence_to_score']) && $question['not_influence_to_score'] == 'on'){
            return true;
        }
        return false;
    }
    
    public function in_question_use_html($question_id){
        global $wpdb;
        $questions_table = $wpdb->prefix . "aysquiz_questions";
        $question_id = absint(intval($question_id));

        $question = $wpdb->get_row("SELECT * FROM {$questions_table} WHERE id={$question_id};", "ARRAY_A");
        $options = ! isset($question['options']) ? array() : json_decode($question['options'], true);
        if(isset($options['use_html']) && $options['use_html'] == 'on'){
            return true;
        }
        return false;
    }

    public function has_text_answer($question_id){
        global $wpdb;
        $questions_table = $wpdb->prefix . "aysquiz_questions";
        $question_id = absint(intval($question_id));

        $text_types = array('text', 'short_text', 'number', 'date');
        $get_answers = $wpdb->get_var("SELECT type FROM {$questions_table} WHERE id={$question_id}");

        if (in_array($get_answers, $text_types)) {
            return true;
        }
        return false;
    }

    public function text_answer_is($question_id){
        global $wpdb;
        $questions_table = $wpdb->prefix . "aysquiz_questions";
        $question_id = absint(intval($question_id));

        $text_types = array('text', 'short_text', 'number', 'date');
        $get_answers = $wpdb->get_var("SELECT type FROM {$questions_table} WHERE id={$question_id}");

        if (in_array($get_answers, $text_types)) {
            return $get_answers;
        }
        return false;
    }
    
    public function ays_default_answer_html($question_id, $quiz_id, $answers, $options){
        $answer_container = "";
        $show_answers_numbering = (isset($options['show_answers_numbering']) && $options['show_answers_numbering'] != '') ? $options['show_answers_numbering'] : 'none';
        $numbering_type_arr = $this->ays_answer_numbering($show_answers_numbering);
        $numbering_type = '';

        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == 'on') ? true : false;
        $quiz_enable_lazy_loading = (isset($options['quiz_enable_lazy_loading']) && $options['quiz_enable_lazy_loading'] === true) ? true : false;
        $question_key_number = intval($options['key_number']);
        $attributes_for_keyboard = "";
        $class_for_keyboard = "";
        $class_label_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
            $class_label_for_keyboard = "ays-quiz-keyboard-label";
        }

        $answer_container_script    = '';
        $answer_container_script_html = '';
        $script_data_arr = array();
        $question_answer = array();
        foreach ($answers as $key => $answer) {
            $answer_image = (isset($answer['image']) && $answer['image'] != '') ? "<img src='{$answer["image"]}' alt='answer_image' class='ays-answer-image'>" : "";

            $answer_image = "";
            if(isset($answer['image']) && $answer['image'] != ''){
                $ans_img = esc_url($answer['image']);
                $answer_image_alt_text = $this->ays_quiz_get_image_id_by_url($ans_img);

                $question_image_lazy_loading_attr = "";
                if ( $quiz_enable_lazy_loading ) {
                    if( $question_key_number != 0 ){
                        $question_image_lazy_loading_attr = 'loading="lazy"';
                    }
                }

                $answer_image = "<img src='". esc_url($ans_img) ."' alt='". esc_attr($answer_image_alt_text) ."' ". esc_attr($question_image_lazy_loading_attr) ." class='ays-answer-image'>";
            }

            $ays_field_style = "";
            $answer_label_style = "";

            // if($answer_image == ""){
            //     $ays_field_style = "";
            //     $answer_label_style = "";
            // }else{
            //     if($options['rtlDirection']){
            //         $ays_flex_dir = 'unset';
            //         $ays_border_dir = "right";
            //     }else{
            //         $ays_flex_dir = 'row-reverse';
            //         $ays_border_dir = "left";
            //     }
            //     if($options['answersViewClass'] == 'grid'){
            //         $ays_field_style = "style='display: block; height: fit-content; margin-bottom: 10px; width:200px;'";
            //         $answer_label_style = "style='margin-bottom: 0; box-shadow: 0px 0px 10px; line-height: 1.5'";
            //     }else{
            //         $ays_field_style = "style='margin-bottom: 10px; border-radius: 4px 4px 0 0; overflow: hidden; display: flex; box-shadow: 0px 0px 10px; flex-direction: {$ays_flex_dir};'";
            //         $answer_label_style = "style='border-radius:0; border-{$ays_border_dir}:1px solid #ccc; line-height: 100px'";
            //     }
            // }

            if($answer_image == ""){
                $answer_label_class = "";
                $answer_img_label_class = " ays_position_initial ";
            }else{
                if($options['answersViewClass'] == 'grid'){
                    $answer_label_class = " ays_empty_before_content ";
                }else{
                    $answer_label_class = "";
                }

                if( !empty($options['ans_right_wrong_icon']) && $options['ans_right_wrong_icon'] == 'none'){
                    $answer_img_label_class = " ays_answer_caption ays_without_after_content ";
                } else {
                    $answer_img_label_class = " ays_answer_caption ";
                }
            }

            if ( $options["questionType"] == 'checkbox' ) {

                $enable_max_selection_number = ( isset( $options['enable_max_selection_number'] ) && $options["enable_max_selection_number"] == 'on' ) ? true : false;
                $max_selection_number        = ( isset( $options["max_selection_number"] ) && $options["max_selection_number"] != '' ) ? absint($options["max_selection_number"]) : '';

                $enable_min_selection_number = ( isset( $options['enable_min_selection_number'] ) && $options["enable_min_selection_number"] == 'on' ) ? true : false;
                $min_selection_number        = ( isset( $options["min_selection_number"] ) && $options["min_selection_number"] != '' ) ? absint($options["min_selection_number"]) : '';

                if ( ( $enable_max_selection_number && ! empty( $max_selection_number ) && $max_selection_number != 0 ) || ( $enable_min_selection_number && ! empty( $min_selection_number ) && $min_selection_number != 0 ) ) {

                    $script_data_arr['enable_max_selection_number'] = $enable_max_selection_number;
                    $script_data_arr['max_selection_number'] = $max_selection_number;
                    $script_data_arr['enable_min_selection_number'] = $enable_min_selection_number;
                    $script_data_arr['min_selection_number'] = $min_selection_number;
                }
            }

            $correct_answer_flag = 'ays_answer_image_class';
            if( isset($_GET['ays_quiz_answers']) && sanitize_key( $_GET['ays_quiz_answers'] ) == 'error404' && $answer["correct"] == 1 ){
                $correct_answer_flag = 'ays_anser_image_class';
            }

            $label = "";
            $answer_content = "";
            if( $answer["answer"] != "" ){
                if($options['useHTML']){
                    $answer_content = do_shortcode((stripslashes($answer["answer"])));
                }else{
                    $answer_content = do_shortcode(htmlspecialchars(stripslashes($answer["answer"])));
                }

                $question_answer[ $answer["id"] ] = htmlspecialchars_decode(stripslashes($answer["correct"]), ENT_QUOTES);

                if ( ! empty($numbering_type_arr) ) {
                    $numbering_type = (isset($numbering_type_arr[$key]) && $numbering_type_arr[$key] != '') ? $numbering_type_arr[$key] :'';
                    $numbering_type = $numbering_type . ' ';
                }

                // $label .= "<label for='ays-answer-{$answer["id"]}-{$quiz_id}' class='ays_answer_image {$correct_answer_flag} $class_label_for_keyboard' $answer_label_style>" . $numbering_type  . $answer_content . "</label>";
            }
            if( $answer_image != "" ){
                // $label .= "<label for='ays-answer-{$answer["id"]}-{$quiz_id}' style='border-radius:0;margin:0;padding:0;height:100px;'>{$answer_image}</label>";
                // $label .= "<label for='ays-answer-{$answer["id"]}-{$quiz_id}' class='ays_answer_image {$correct_answer_flag} ays_empty_before_content'>{$answer_image}</label>";
            }

            $label .= "<label for='ays-answer-{$answer["id"]}-{$quiz_id}' class='$answer_label_class $answer_img_label_class $class_label_for_keyboard'>" . $numbering_type . $answer_content . "</label>";
            $label .= "<label for='ays-answer-{$answer["id"]}-{$quiz_id}' class='ays_answer_image {$correct_answer_flag} ays_empty_before_content'>{$answer_image}</label>";

            $answer_container .= "
            <div class='ays-field ays_" . $options['answersViewClass'] . "_view_item ".$class_for_keyboard."' ".$attributes_for_keyboard." $ays_field_style>
                <input type='hidden' name='ays_answer_correct[]' value='0'/>

                <input type='{$options["questionType"]}' name='ays_questions[ays-question-{$question_id}]' id='ays-answer-{$answer["id"]}-{$quiz_id}' value='{$answer["id"]}'/>

                {$label}

            </div>";
        }

        // $script_data_arr['question_answer'] = $question_answer;

        $answer_container_script_html .= '<script>';
        $answer_container_script_html .= "
            if(typeof window.quizOptions_$quiz_id === 'undefined'){
                window.quizOptions_$quiz_id = [];
            }
            window.quizOptions_".$quiz_id."['".$question_id."'] = '" . base64_encode(json_encode($script_data_arr)) . "';";
        $answer_container_script_html .= '</script>';

        $answer_container .= $answer_container_script_html;
        
        return $answer_container;
    }
    
    protected function ays_text_answer_html($question_id, $quiz_id, $answers, $options){
        $enable_correction = $options['correction'] ? "display:inline-block;white-space: nowrap;" : "display:none";
        $enable_correction_textarea = $options['correction'] ? "width:100%;" : "width:100%;";
        $is_enable_question_max_length = $this->ays_quiz_is_enable_question_max_length( $question_id , 'text' );

        $question_not_influence_class  = "";
        if ( $this->is_question_not_influence( $question_id ) ) {
            $question_not_influence_class  = "ays_display_none";
            $enable_correction_textarea    = "width:100%;";
        }

        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == 'on') ? true : false;
        $attributes_for_keyboard = "";
        $class_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
        }

        $question_text_max_length_array = (isset($options['questionMaxLengthArray']) && ! empty($options['questionMaxLengthArray'])) ? $options['questionMaxLengthArray'] : array();

        $ays_question_limit_length_class = '';
        $ays_quiz_question_text_message_html = '';
        $enable_question_text_max_length = false;
        $question_text_max_length = '';
        $question_limit_text_type = 'characters';
        $question_enable_text_message = false;
        if (! empty($question_text_max_length_array) ) {

            $enable_question_text_max_length = $question_text_max_length_array['enable_question_text_max_length'];

            $question_text_max_length = $question_text_max_length_array['question_text_max_length'];

            $question_limit_text_type = $question_text_max_length_array['question_limit_text_type'];

            $question_enable_text_message = $question_text_max_length_array['question_enable_text_message'];
        }

        if( $is_enable_question_max_length ){
            $ays_question_limit_length_class = 'ays_question_limit_length';

            if ($question_enable_text_message && $question_text_max_length != 0 && $question_text_max_length != '') {
                $ays_quiz_question_text_message_html .= '<div class="ays_quiz_question_text_conteiner">';
                    $ays_quiz_question_text_message_html .= '<div class="ays_quiz_question_text_message">';
                        $ays_quiz_question_text_message_html .= '<span class="ays_quiz_question_text_message_span">'. $question_text_max_length . '</span> ' . $question_limit_text_type . ' ' . __( 'left' , 'quiz-maker' );
                    $ays_quiz_question_text_message_html .= '</div>';
                $ays_quiz_question_text_message_html .= '</div>';
            }
        }

        // Enable case sensitive text
        $enable_case_sensitive_text = ( isset($options['enable_case_sensitive_text']) && $options['enable_case_sensitive_text'] != '' ) ? $options['enable_case_sensitive_text'] : false;

        $answer_container = "<div class='ays-field ays-text-field'>";
            foreach ($answers as $answer) {
                $placeholder = isset($answer["placeholder"]) && $answer["placeholder"] != '' ? stripslashes(htmlentities($answer["placeholder"], ENT_QUOTES)) : '';
                $answer_image = (isset($answer['image']) && $answer['image'] != '') ? $answer["image"] : "";
                $answer_id = (isset($answer['id']) && $answer['id'] != '') ? intval($answer["id"]) : 0;

                $answer_container .= "<textarea style='$enable_correction_textarea' type='text' placeholder='$placeholder' class='ays-text-input ". $ays_question_limit_length_class ."' autocomplete='off' name='ays_questions[ays-question-{$question_id}]' data-question-id='". $question_id ."' data-answer-id='" . $answer_id . "'></textarea>
                <input type='hidden' name='ays_answer_correct[]' value='0'/>
                <button type='button' style='$enable_correction' class='ays_check_answer action-button ".$class_for_keyboard." ". $question_not_influence_class ."'>".$this->buttons_texts['checkButton']."</button>";
                $answer_container .= "<script>
                        if(typeof window.quizOptions_$quiz_id === 'undefined'){
                            window.quizOptions_$quiz_id = [];
                        }
                        window.quizOptions_".$quiz_id."['".$question_id."'] = '" . base64_encode(json_encode(array(
                            'question_type'                     => 'text',
                            // 'question_answer'                   => htmlspecialchars_decode(stripslashes($answer["answer"]), ENT_QUOTES),
                            'enable_question_text_max_length'   => $enable_question_text_max_length,
                            'question_text_max_length'          => $question_text_max_length,
                            'question_limit_text_type'          => $question_limit_text_type,
                            'question_enable_text_message'      => $question_enable_text_message,
                            'enable_case_sensitive_text'        => $enable_case_sensitive_text,
                        ))) . "';
                    </script>";
            }

        $answer_container .= "</div>";
        $answer_container .= $ays_quiz_question_text_message_html;
        return $answer_container;
    }
    
    protected function ays_short_text_answer_html($question_id, $quiz_id, $answers, $options){
        $enable_correction = $options['correction'] ? "display:inline-block;white-space: nowrap;" : "display:none";
        $enable_correction_textarea = $options['correction'] ? "width:100%;" : "width:100%;";
        $is_enable_question_max_length = $this->ays_quiz_is_enable_question_max_length( $question_id , 'short_text' );
        
        $question_not_influence_class  = "";
        if ( $this->is_question_not_influence( $question_id ) ) {
            $question_not_influence_class  = "ays_display_none";
            $enable_correction_textarea    = "width:100%;";
        }

        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == 'on') ? true : false;
        $attributes_for_keyboard = "";
        $class_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
        }

        $question_text_max_length_array = (isset($options['questionMaxLengthArray']) && ! empty($options['questionMaxLengthArray'])) ? $options['questionMaxLengthArray'] : array();

        $ays_question_limit_length_class = '';
        $ays_quiz_question_text_message_html = '';
        $enable_question_text_max_length = false;
        $question_text_max_length = '';
        $question_limit_text_type = 'characters';
        $question_enable_text_message = false;
        if (! empty($question_text_max_length_array) ) {

            $enable_question_text_max_length = $question_text_max_length_array['enable_question_text_max_length'];

            $question_text_max_length = $question_text_max_length_array['question_text_max_length'];

            $question_limit_text_type = $question_text_max_length_array['question_limit_text_type'];

            $question_enable_text_message = $question_text_max_length_array['question_enable_text_message'];
        }

        if( $is_enable_question_max_length ){
            $ays_question_limit_length_class = 'ays_question_limit_length';

            if ($question_enable_text_message && $question_text_max_length != 0 && $question_text_max_length != '') {
                $ays_quiz_question_text_message_html .= '<div class="ays_quiz_question_text_conteiner">';
                    $ays_quiz_question_text_message_html .= '<div class="ays_quiz_question_text_message">';
                        $ays_quiz_question_text_message_html .= '<span class="ays_quiz_question_text_message_span">'. $question_text_max_length . '</span> ' . $question_limit_text_type . ' ' . __( 'left' , 'quiz-maker' );
                    $ays_quiz_question_text_message_html .= '</div>';
                $ays_quiz_question_text_message_html .= '</div>';
            }
        }

        // Enable case sensitive text
        $enable_case_sensitive_text = ( isset($options['enable_case_sensitive_text']) && $options['enable_case_sensitive_text'] != '' ) ? $options['enable_case_sensitive_text'] : false;

        $answer_container = "<div class='ays-field ays-text-field'>";
            foreach ($answers as $answer) {
                $placeholder = isset($answer["placeholder"]) && $answer["placeholder"] != '' ? stripslashes(htmlentities($answer["placeholder"], ENT_QUOTES)) : '';
                $answer_image = (isset($answer['image']) && $answer['image'] != '') ? $answer["image"] : "";
                $answer_id = (isset($answer['id']) && $answer['id'] != '') ? intval($answer["id"]) : 0;

                $answer_container .= "<input style='$enable_correction_textarea' type='text' placeholder='$placeholder' class='ays-text-input ". $ays_question_limit_length_class ."' autocomplete='off' name='ays_questions[ays-question-{$question_id}]' data-question-id='". $question_id ."' data-answer-id='" . $answer_id . "'>
                <input type='hidden' name='ays_answer_correct[]' value='0'/>
                <button type='button' style='$enable_correction' class='ays_check_answer action-button ". $class_for_keyboard." ". $question_not_influence_class ."'>".$this->buttons_texts['checkButton']."</button>";
                $answer_container .= "<script>
                        if(typeof window.quizOptions_$quiz_id === 'undefined'){
                            window.quizOptions_$quiz_id = [];
                        }
                        window.quizOptions_".$quiz_id."['".$question_id."'] = '" . base64_encode(json_encode(array(
                            'question_type'                     => 'short_text',
                            // 'question_answer'                   => htmlspecialchars_decode(stripslashes($answer["answer"]), ENT_QUOTES),
                            'enable_question_text_max_length'   => $enable_question_text_max_length,
                            'question_text_max_length'          => $question_text_max_length,
                            'question_limit_text_type'          => $question_limit_text_type,
                            'question_enable_text_message'      => $question_enable_text_message,
                            'enable_case_sensitive_text'        => $enable_case_sensitive_text
                        ))) . "';
                    </script>";
            }
        
        $answer_container .= "</div>";
        $answer_container .= $ays_quiz_question_text_message_html;
        return $answer_container;
    }
    
    protected function ays_number_answer_html($question_id, $quiz_id, $answers, $options){
        $enable_correction = $options['correction'] ? "display:inline-block;white-space: nowrap;" : "display:none";
        $enable_correction_textarea = $options['correction'] ? "width:100%;" : "width:100%;";
        $is_enable_question_max_length = $this->ays_quiz_is_enable_question_max_length( $question_id , 'number' );

        $question_not_influence_class  = "";
        if ( $this->is_question_not_influence( $question_id ) ) {
            $question_not_influence_class  = "ays_display_none";
            $enable_correction_textarea    = "width:100%;";
        }

        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == 'on') ? true : false;
        $attributes_for_keyboard = "";
        $class_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
        }

        $question_text_max_length_array = (isset($options['questionMaxLengthArray']) && ! empty($options['questionMaxLengthArray'])) ? $options['questionMaxLengthArray'] : array();

        $ays_question_limit_length_class        = '';
        $ays_quiz_question_number_message_html  = '';
        $question_number_min_message_html       = '';
        $question_number_error_message          = '';
        $question_number_error_message_html     = '';

        $enable_question_number_max_length      = false;
        $enable_question_number_min_length      = false;
        $enable_question_number_error_message   = false;

        $question_number_max_length = '';
        $question_number_min_length = '';

        if (! empty($question_text_max_length_array) ) {

            $enable_question_number_max_length      = $question_text_max_length_array['enable_question_number_max_length'];
            $question_number_max_length             = $question_text_max_length_array['question_number_max_length'];

            $enable_question_number_min_length      = $question_text_max_length_array['enable_question_number_min_length'];
            $question_number_min_length             = $question_text_max_length_array['question_number_min_length'];

            $enable_question_number_error_message   = $question_text_max_length_array['enable_question_number_error_message'];
            $question_number_error_message          = $question_text_max_length_array['question_number_error_message'];
        }

        if( $is_enable_question_max_length ){
            $ays_question_limit_length_class = 'ays_question_number_limit_length';

            if ($question_number_max_length != 0 && $question_number_max_length != '') {
                $ays_quiz_question_number_message_html .= 'max="'. $question_number_max_length .'"';
            }
        }

        if ( $enable_question_number_min_length ) {
            $ays_question_limit_length_class = 'ays_question_number_limit_length';

            if ($question_number_min_length != 0 && $question_number_min_length != '') {
                $question_number_min_message_html .= 'min="'. $question_number_min_length .'"';
            }
        }

        if ( $enable_question_number_error_message ) {
            $ays_question_limit_length_class = 'ays_question_number_limit_length';

            if ( $question_number_error_message != "" ) {
                $question_number_error_message_html .= "<div class='ays-quiz-number-error-message ays_display_none'>";
                    $question_number_error_message_html .= $question_number_error_message;
                $question_number_error_message_html .= "</div>";
            }
        }

        $answer_container = "<div class='ays-field ays-text-field'>";
            foreach ($answers as $answer) {
                $placeholder = isset($answer["placeholder"]) && $answer["placeholder"] != '' ? stripslashes(htmlentities($answer["placeholder"], ENT_QUOTES)) : '';
                $answer_image = (isset($answer['image']) && $answer['image'] != '') ? $answer["image"] : "";
                $answer_id = (isset($answer['id']) && $answer['id'] != '') ? intval($answer["id"]) : 0;

                $answer_container .= "<input style='$enable_correction_textarea' type='number' placeholder='$placeholder' class='ays-text-input ". $ays_question_limit_length_class ."' ". $ays_quiz_question_number_message_html ." ". $question_number_min_message_html ." name='ays_questions[ays-question-{$question_id}]' data-question-id='". $question_id ."' data-answer-id='" . $answer_id . "'>
                <input type='hidden' name='ays_answer_correct[]' value='0'/>
                <button type='button' style='$enable_correction' class='ays_check_answer action-button ". $class_for_keyboard." ". $question_not_influence_class ."'>".$this->buttons_texts['checkButton']."</button>";
                $answer_container .= "<script>
                        if(typeof window.quizOptions_$quiz_id === 'undefined'){
                            window.quizOptions_$quiz_id = [];
                        }
                        window.quizOptions_".$quiz_id."['".$question_id."'] = '" . base64_encode(json_encode(array(
                            'question_type'                         => 'text',
                            // 'question_answer'                       => htmlspecialchars(stripslashes($answer["answer"])),
                            'enable_question_number_max_length'     => $enable_question_number_max_length,
                            'question_number_max_length'            => $question_number_max_length,
                            'enable_question_number_min_length'     => $enable_question_number_min_length,
                            'question_number_min_length'            => $question_number_min_length,
                            'enable_question_number_error_message'  => $enable_question_number_error_message,
                            'question_number_error_message'         => $question_number_error_message,
                        ))) . "';
                    </script>";
            }
        
        $answer_container .= "</div>";

        $answer_container .= $question_number_error_message_html;

        return $answer_container;
    }
    
    protected function ays_date_answer_html($question_id, $quiz_id, $answers, $options){
        $enable_correction = $options['correction'] ? "display:inline-block;white-space: nowrap;" : "display:none";
        $enable_correction_textarea = $options['correction'] ? "width:100%;" : "width:100%;";

        $question_not_influence_class  = "";
        if ( $this->is_question_not_influence( $question_id ) ) {
            $question_not_influence_class  = "ays_display_none";
            $enable_correction_textarea    = "width:100%;";
        }

        $quiz_enable_keyboard_navigation = (isset($options['quiz_enable_keyboard_navigation']) && $options['quiz_enable_keyboard_navigation'] == 'on') ? true : false;
        $attributes_for_keyboard = "";
        $class_for_keyboard = "";
        if($quiz_enable_keyboard_navigation){
            $class_for_keyboard = "ays-quiz-keyboard-active";
            $attributes_for_keyboard = "tabindex='0'";
        }

        $answer_container = "<div class='ays-field ays-text-field'>";
            foreach ($answers as $answer) {
                $placeholder = isset($answer["placeholder"]) && $answer["placeholder"] != '' ? stripslashes(htmlentities($answer["placeholder"], ENT_QUOTES)) : '';
                $answer_image = (isset($answer['image']) && $answer['image'] != '') ? $answer["image"] : "";
                $answer_id = (isset($answer['id']) && $answer['id'] != '') ? intval($answer["id"]) : 0;
                
                $answer_container .= "<input style='$enable_correction_textarea' type='date' autocomplete='off' placeholder='$placeholder' class='ays-text-input' name='ays_questions[ays-question-{$question_id}]' data-answer-id='" . $answer_id . "'>
                <input type='hidden' name='ays_answer_correct[]' value='0'/>
                <button type='button' style='$enable_correction' class='ays_check_answer action-button ". $class_for_keyboard."  ". $question_not_influence_class ."'>".$this->buttons_texts['checkButton']."</button>";
                $answer_container .= "<script>
                        if(typeof window.quizOptions_$quiz_id === 'undefined'){
                            window.quizOptions_$quiz_id = [];
                        }
                        window.quizOptions_".$quiz_id."['".$question_id."'] = '" . base64_encode(json_encode(array(
                            'question_type'     => 'date',
                            // 'question_answer'   => htmlspecialchars(stripslashes($answer["answer"]))
                        ))) . "';
                    </script>";
            }
        
        $answer_container .= "</div>";
        return $answer_container;
    }

    protected function ays_dropdown_answer_html($question_id, $quiz_id, $answers, $options){
        $show_answers_numbering = (isset($options['show_answers_numbering']) && $options['show_answers_numbering'] != '') ? $options['show_answers_numbering'] : 'none';
        $numbering_type_arr = $this->ays_answer_numbering($show_answers_numbering);
        $numbering_type = ''; 
        $script_data_arr = array();
        $question_answer = array();
        $answer_container_script_html = "";
        $answer_container = "<div class='ays-field ays-select-field'>            
            <select class='ays-select'>                
                <option value=''>".__('Select an answer', 'quiz-maker')."</option>";
            foreach ($answers as $key => $answer) {
                if ( ! empty($numbering_type_arr) ) {
                    $numbering_type = (isset($numbering_type_arr[$key]) && $numbering_type_arr[$key] != '') ? $numbering_type_arr[$key] :'';
                    $numbering_type = $numbering_type . ' ';
                }

                $correct_answer_flag = 'ays_answer_image_class';
                if( isset($_GET['ays_quiz_answers']) && sanitize_key( $_GET['ays_quiz_answers'] ) == 'error404' && $answer["correct"] == 1 ){
                    $correct_answer_flag = 'ays_anser_image_class';
                }

                $question_answer[ $answer["id"] ] = htmlspecialchars_decode(stripslashes($answer["correct"]), ENT_QUOTES);

                $answer_image = (isset($answer['image']) && $answer['image'] != '') ? esc_url($answer["image"]) : "";
                $answer_container .= "<option data-nkar='{$answer_image}' data-chisht='0' class='ays_answer_image {$correct_answer_flag}' value='{$answer["id"]}'>" . $numbering_type . do_shortcode(htmlspecialchars(stripslashes($answer["answer"]))) . "</option>";
            }
        $answer_container .= "</select>";
        $answer_container .= "<input class='ays-select-field-value' type='hidden' name='ays_questions[ays-question-{$question_id}]' value=''/>";

            foreach ($answers as $answer) {
                $answer_container .= "<input type='hidden' name='ays_answer_correct[]' data-id='{$answer["id"]}' value='0'/>";
            }
        $answer_container .= "</div>";

        // $script_data_arr['question_answer'] = $question_answer;

        $answer_container_script_html .= '<script>';
        $answer_container_script_html .= "
            if(typeof window.quizOptions_$quiz_id === 'undefined'){
                window.quizOptions_$quiz_id = [];
            }
            window.quizOptions_".$quiz_id."['".$question_id."'] = '" . base64_encode(json_encode($script_data_arr)) . "';";
        $answer_container_script_html .= '</script>';

        $answer_container .= $answer_container_script_html;
        
        return $answer_container;
    }

    protected function isHomogenousStrong($arr, $question_id){
        $arr_count = count( $arr );
        $arr_sum = array_sum( $arr );
        $count_correct = intval( $this->count_multiple_correct_answers($question_id) );
        $a = $arr_count - $arr_sum;
        $b = $arr_sum - $a;
        
        return $b / $count_correct;
    }
    
    protected function isHomogenous($arr, $question_id){
        $mustBe = true;
        $count = 0;
        foreach ($arr as $val) {
            if ($mustBe !== $val) {
                return false;
            }
            $count++;
        }
        $count_correct = intval( $this->count_multiple_correct_answers($question_id) );
        if($count !== $count_correct){
            return false;
        }
        return true;
    }
    
    protected function get_question_weight($id){
        //global $wpdb;
        //$sql = "SELECT weight FROM {$wpdb->prefix}aysquiz_questions WHERE id = $id";
        //$result = $wpdb->get_var($sql);
        //return intval($result);
        return 1;
    }

    protected function hex2rgba($color, $opacity = false){

        $default = 'rgb(0,0,0)';

        //Return default if no color provided
        if (empty($color))
            return $default;

        //Sanitize $color if "#" is provided
        if ($color[0] == '#') {
            $color = substr($color, 1);
        }else{
            return $color;
        }

        //Check if color has 6 or 3 characters and get values
        if (strlen($color) == 6) {
            $hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
        } elseif (strlen($color) == 3) {
            $hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
        } else {
            return $default;
        }

        //Convert hexadec to rgb
        $rgb = array_map('hexdec', $hex);

        //Check if opacity is set(rgba or rgb)
        if ($opacity) {
            if (abs($opacity) > 1)
                $opacity = 1.0;
            $output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')';
        } else {
            $output = 'rgb(' . implode(",", $rgb) . ')';
        }

        //Return rgb(a) color string
        return $output;
    }

    public static function secondsToWords($seconds){
        $ret = "";
        $seconds = absint($seconds);
        
        /*** get the days ***/
        $days = (int)($seconds / 86400);
        if ($days > 0) {
            $ret .= sprintf( __('%s days', 'quiz-maker' ), $days ) . " ";
        }

        /*** get the hours ***/
        $hours = (int)(($seconds - ($days * 86400)) / 3600);
        if ($hours > 0) {
            $ret .= sprintf( __('%s hours', 'quiz-maker' ), $hours ) . " ";
        }

        /*** get the minutes ***/
        $minutes = (int)(($seconds - $days * 86400 - $hours * 3600) / 60);
        if ($minutes > 0) {
            $ret .= sprintf( __('%s minutes', 'quiz-maker' ), $minutes ) . " ";
        }

        /*** get the seconds ***/
        $seconds = (int)($seconds - ($days * 86400) - ($hours * 3600) - ($minutes * 60));
        if ($seconds > 0) {
            $ret .= sprintf( __('%s seconds', 'quiz-maker' ), $seconds );
        }

        return $ret;
    }

    protected function add_results_to_db($data){
        global $wpdb;
        $results_table = $wpdb->prefix . 'aysquiz_reports';

        $user_ip = $data['user_ip'];
        $user_name = $data['user_name'];
        $user_email = $data['user_email'];
        $user_phone = $data['user_phone'];
        $quiz_id = $data['quiz_id'];
        $start_date = $data['start_date'];
        $end_date = $data['end_date'];
        $duration = strtotime($end_date) - strtotime($start_date);
        $score = $data['score'];
        $options = array();
        $options = $data['answered'];
        $calc_method = $data['calc_method'];
        $options['passed_time'] = $this->get_time_difference($start_date, $end_date);
        $options['calc_method'] = $calc_method;
        $user_corrects_count = !empty($data['user_corrects_count']) ? intval($data['user_corrects_count']) : 0;
        $questions_count = !empty($data['questions_count']) ? intval($data['questions_count']) : 0;

        $user_explanation = ( !empty($data['user_explanation']) && count($data['user_explanation']) > 0) ?  json_encode($data['user_explanation']) : '';
        
        $quiz_attributes_information = array();
        $quiz_attributes = $this->get_quiz_attributes_by_id($quiz_id);
        
        $options['attributes_information'] = $quiz_attributes_information;
        $results = $wpdb->insert(
            $results_table,
            array(
                'quiz_id'           => absint(intval($quiz_id)),
                'user_id'           => get_current_user_id(),
                'user_name'         => $user_name,
                'user_email'        => $user_email,
                'user_phone'        => $user_phone,
                'user_ip'           => $user_ip,
                'start_date'        => $start_date,
                'end_date'          => $end_date,
                'duration'          => $duration,
                'score'             => $score,
                'corrects_count'    => $user_corrects_count,
                'questions_count'   => $questions_count,
                'user_explanation'  => $user_explanation,
                'options'           => json_encode($options)
            ),
            array(
                '%d', // quiz_id
                '%d', // user_id
                '%s', // user_name
                '%s', // user_email
                '%s', // user_phone
                '%s', // user_ip
                '%s', // start_date
                '%s', // end_date
                '%s', // duration
                '%d', // score
                '%s', // user_corrects_count
                '%s', // questions_count
                '%s', // user_explanation
                '%s', // options
            )
        );

        if ($results >= 0) {
            return true;
        }

        return false;
    }
    
    protected function ays_get_count_of_rates($id){
        global $wpdb;

        $id = absint($id);

        $sql = $wpdb->prepare(
            "SELECT COUNT(`id`) AS count
             FROM {$wpdb->prefix}aysquiz_rates
             WHERE quiz_id = %d",
            $id
        );

        $result = $wpdb->get_var($sql);
        return $result;
    }
    
    protected function ays_get_count_of_reviews($start, $limit, $quiz_id){
        global $wpdb;

        $start   = absint($start);
        $limit   = absint($limit);
        $quiz_id = absint($quiz_id);

        $sql = $wpdb->prepare(
            "SELECT COUNT(`id`) AS count
             FROM {$wpdb->prefix}aysquiz_rates
             WHERE (review <> '' OR options <> '')
             AND quiz_id = %d
             ORDER BY id DESC
             LIMIT %d, %d",
            $quiz_id,
            $start,
            $limit
        );

        $result = $wpdb->get_var($sql);
        return $result;
    }
    
    protected function ays_set_rate_id_of_result($id, $last_result_id){
        global $wpdb;
        $results_table = $wpdb->prefix . 'aysquiz_reports';
        $sql = "SELECT * FROM $results_table WHERE id = ".intval($last_result_id);
        $res = $wpdb->get_row($sql, ARRAY_A);
        
        $results = false;
        if( !empty($res) && !empty($res['options']) ){
            $options = json_decode($res['options'], true);
            $options['rate_id'] = $id;
            $results = $wpdb->update(
                $results_table,
                array( 'options' => json_encode($options) ),
                array( 'id' => intval($last_result_id) ),
                array( '%s' ),
                array( '%d' )
            );
        } elseif ( isset($id) && isset($last_result_id) && $last_result_id == 0 ){
            $results = true;
        }

        if($results !== false){
            return true;
        }
        return false;
    }
    
    protected function ays_get_average_of_rates($id){
        global $wpdb;

        $id = absint($id);

        $sql = $wpdb->prepare(
            "SELECT AVG(`score`) AS avg_score
             FROM {$wpdb->prefix}aysquiz_rates
             WHERE quiz_id = %d",
            $id
        );

        $result = $wpdb->get_var($sql);

        if ( is_null( $result ) || empty( $result ) ) {
            $result = 0;
        }

        return $result;
    }

    protected function ays_get_average_of_scores($id){
        global $wpdb;

        $id = absint($id);

        $sql = $wpdb->prepare(
            "SELECT AVG(`score`) 
             FROM {$wpdb->prefix}aysquiz_reports 
             WHERE quiz_id = %d",
            $id
        );

        $avg_result = $wpdb->get_var($sql);
        if ( is_null( $avg_result ) || empty( $avg_result ) ) {
            $avg_result = 0;
        }

        $result = round($avg_result);
        return $result;
    }
    
    protected function ays_get_reasons_of_rates($start, $limit, $quiz_id){
        global $wpdb;

        $start   = absint($start);
        $limit   = absint($limit);
        $quiz_id = absint($quiz_id);

        $sql = $wpdb->prepare(
            "SELECT * 
             FROM {$wpdb->prefix}aysquiz_rates 
             WHERE quiz_id = %d 
             AND (review <> '' OR options <> '') 
             ORDER BY id DESC 
             LIMIT %d, %d",
            $quiz_id,
            $start,
            $limit
        );

        $result = $wpdb->get_results($sql, "ARRAY_A");

        return $result;
    }
    
    protected function ays_get_full_reasons_of_rates($start, $limit, $quiz_id, $zuyga){
        $quiz_rate_reasons = $this->ays_get_reasons_of_rates($start, $limit, $quiz_id);
        $quiz_rate_html = '';

        foreach($quiz_rate_reasons as $key => $reasons){

            $user_name = ! empty( $reasons['user_name'] ) ? '<span>' . esc_html( $reasons['user_name'] ) . '</span>' : '';

            if ( $this->isJSON( $reasons['options'] ) ) {
                $options = json_decode( $reasons['options'], true );
                $reason  = isset( $options['reason'] ) ? (string) $options['reason'] : '';
            } elseif ( ! empty( $reasons['options'] ) ) {
                $reason = (string) $reasons['options'];
            } else {
                $reason = isset( $reasons['review'] ) ? (string) $reasons['review'] : '';
            }

            if ( intval( $reasons['user_id'] ) !== 0 ) {
                $user_img = esc_url( get_avatar_url( intval( $reasons['user_id'] ) ) );
            } else {
                $user_img = esc_url( AYS_QUIZ_PUBLIC_URL . '/images/avatar_2x.png' );
            }

            $score     = absint( $reasons['score'] );
            $commented = esc_html( date_i18n( 'M j, Y', strtotime( $reasons['rate_date'] ) ) );

            if ( $zuyga == 1 ) {
                $row_reverse = ( $key % 2 == 0 ) ? 'row_reverse' : '';
            } else {
                $row_reverse = ( $key % 2 == 0 ) ? '' : 'row_reverse';
            }

            $quiz_rate_html .= "
                <div class='quiz_rate_reasons'>
                    <div class='rate_comment_row " . esc_attr( $row_reverse ) . "'>
                        <div class='rate_comment_user'>
                            <div class='thumbnail'>
                                <img class='img-responsive user-photo' src='" . esc_url( $user_img ) . "' alt=''>
                            </div>
                        </div>
                        <div class='rate_comment'>
                            <div class='panel panel-default'>
                                <div class='panel-heading'>
                                    <i class='ays_fa ays_fa_user'></i> <strong>" . $user_name . "</strong><br/>
                                    <i class='ays_fa ays_fa_clock_o'></i> " . $commented . "<br/>
                                    " . esc_html__( 'Rated', 'quiz-maker' ) . " <i class='ays_fa ays_fa_star'></i> " . esc_html( $score ) . "
                                </div>
                                <div class='panel-body'>
                                    <div>" . nl2br( esc_html( $reason ) ) . "</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>";
        }

        return $quiz_rate_html;
    }
    
    public function ays_get_rate_last_reviews(){


        if( empty($_REQUEST['_ajax_nonce']) ){
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                'status'    => false,
                'quiz_rate_html' => ''
            ));
            wp_die();
        }

        // Run a security check.
        check_ajax_referer( 'ays_quiz_rate_reason_nonce', sanitize_key( wp_unslash($_REQUEST['_ajax_nonce']) ) );
        
        $quiz_id = absint( sanitize_text_field( $_REQUEST["quiz_id"] ) );
        $this->buttons_texts = $this->ays_set_quiz_texts($quiz_id);
        $ays_load_more_button_text = $this->buttons_texts['loadMoreButton'];
        
        $quiz_rate_html = "<div class='quiz_rate_reasons_container'>";
        $quiz_rate_html .= $this->ays_get_full_reasons_of_rates(0, 5, $quiz_id, 0);
        $quiz_rate_html .= "</div>";
        if($this->ays_get_count_of_reviews(0, 5, $quiz_id) / 5 > 1){
            $quiz_rate_html .= "<div class='quiz_rate_load_more'>
                <div>
                    <div data-class='lds-spinner' data-role='loader' class='ays-loader'><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
                </div>
                <button type='button' zuyga='1' startfrom='5' class='action-button ays_load_more_review'><i class='ays_fa ays_fa_chevron_circle_down'></i> ".$ays_load_more_button_text."</button>
            </div>";
        }
        ob_end_clean();
        $ob_get_clean = ob_get_clean();
        echo json_encode(array(
            'status'         => true,
            'quiz_rate_html' => $quiz_rate_html
        ));
        wp_die();
    }
    
    public function ays_load_more_reviews(){
        $quiz_id = absint( sanitize_text_field( $_REQUEST["quiz_id"] ) );
        $start = absint( sanitize_text_field( $_REQUEST["start_from"] ) );
        $zuyga = absint( sanitize_text_field( $_REQUEST["zuyga"] ) );
        $limit = 5;
        $quiz_rate_html = "";
        $quiz_rate_html .= $this->ays_get_full_reasons_of_rates($start, $limit, $quiz_id, $zuyga);
        if($quiz_rate_html == ""){
            if(is_null( $this->default_texts )){
                $this->default_texts = self::ays_set_quiz_default_texts( 'quiz-maker', array() );
            }
            
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo "<p class='ays_no_more'>" . esc_html($this->default_texts['noMoreReviewsText']) . "</p>";
            wp_die();
        }else{
            ob_end_clean();
            $ob_get_clean = ob_get_clean();     
            $quiz_rate_html = "<div class='quiz_rate_more_review'>".$quiz_rate_html."</div>";            
            echo $quiz_rate_html;
            wp_die();
        }
    }

    public function ays_shuffle_assoc($list) { 
        if (!is_array($list)) return $list; 

        $keys = array_keys($list); 
        shuffle($keys); 
        $random = array(); 
        foreach ($keys as $key) { 
            $random[$key] = $list[$key]; 
        }
        return $random; 
    }
    
    public function ays_rate_the_quiz(){

        if( empty($_REQUEST['_ajax_nonce']) ){
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                'status'    => false,
            ));
            wp_die();
        }

        // Run a security check.
        check_ajax_referer( 'ays_quiz_rate_reason_nonce', sanitize_key( wp_unslash($_REQUEST['_ajax_nonce']) ) );

        global $wpdb;
        $rates_table   = $wpdb->prefix . 'aysquiz_rates';
        $quizzes_table = $wpdb->prefix . 'aysquiz_quizes';

        $quiz_id = isset( $_REQUEST["quiz_id"] ) ? absint( sanitize_text_field($_REQUEST["quiz_id"]) ) : 0;

        if ( empty($quiz_id) ) {
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                'status'    => false,
            ));
            wp_die();
        }

        $quiz_exists = $wpdb->get_row(
            $wpdb->prepare(
                "SELECT id, options FROM {$quizzes_table} WHERE id = %d",
                $quiz_id
            ),
            ARRAY_A
        );

        if ( empty($quiz_exists) ) {
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                'status'    => false,
            ));
            wp_die();
        }

        $options = ( isset($quiz_exists['options']) && ( json_decode($quiz_exists['options'], true) != null ) ) ? json_decode($quiz_exists['options'], true) : array();

        $user_ip = $this->get_user_ip();

        $one_minute_ago = date('Y-m-d H:i:s', current_time('timestamp') - 60);

        $recent_rates = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(id)
                 FROM {$rates_table}
                 WHERE user_ip = %s
                 AND rate_date > %s",
                $user_ip,
                $one_minute_ago
            )
        );

        if ( $recent_rates > 3 ) {
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                'status'    => false,
            ));
            wp_die();
        }

        $user_id = get_current_user_id();
        
        $report_id = (isset($_REQUEST['last_result_id']) && sanitize_text_field($_REQUEST['last_result_id']) != '' && ! is_null( sanitize_text_field($_REQUEST['last_result_id']) ) ) ? intval( sanitize_text_field( $_REQUEST['last_result_id'] ) ) : 0;

        $options['disable_store_data'] = !isset($options['disable_store_data']) ? 'off' : sanitize_text_field($options['disable_store_data']);
        $disable_store_data = (isset($options['disable_store_data']) && $options['disable_store_data'] == 'on') ? true : false;

        if( !$disable_store_data ){
            if ( empty($report_id) ) {
                ob_end_clean();
                $ob_get_clean = ob_get_clean();
                echo json_encode(array(
                    'status'    => false,
                ));
                wp_die();
            }

            if ($report_id > 0) {

                $result_exists = $wpdb->get_var(
                    $wpdb->prepare(
                        "SELECT id
                         FROM {$wpdb->prefix}aysquiz_reports
                         WHERE id = %d
                         AND quiz_id = %d",
                        $report_id,
                        $quiz_id
                    )
                );

                if (!$result_exists) {
                    ob_end_clean();
                    $ob_get_clean = ob_get_clean();
                    echo json_encode(array(
                        'status'    => false,
                    ));
                    wp_die();
                }
            }
        }

        // Make responses anonymous
        $quiz_make_responses_anonymous = (isset($_REQUEST['quiz_make_responses_anonymous']) && sanitize_text_field($_REQUEST['quiz_make_responses_anonymous']) == 'true' ) ? true : false;

        // Make responses anonymous
        $quiz_enable_user_cհoosing_anonymous_assessment = (isset($_REQUEST['quiz_enable_user_cհoosing_anonymous_assessment']) && sanitize_text_field($_REQUEST['quiz_enable_user_cհoosing_anonymous_assessment']) == 'true' ) ? true : false;
        $quiz_enable_user_cհoosing_anonymous_assessment_checkbox_flag = (isset($_REQUEST['quiz_enable_user_cհoosing_anonymous_assessment_checkbox_flag']) && sanitize_text_field($_REQUEST['quiz_enable_user_cհoosing_anonymous_assessment_checkbox_flag']) == 'true' ) ? true : false;

        $user_ip = $this->get_user_ip();
        if(isset($_REQUEST['ays_user_name']) && sanitize_text_field( $_REQUEST['ays_user_name'] ) != ''){
            $user_name = esc_sql( sanitize_text_field( $_REQUEST['ays_user_name'] ) );
        }elseif(is_user_logged_in()){
            $user = wp_get_current_user();
            $user_name = $user->data->display_name;
        }else{
            $user_name = __( 'Anonymous' , 'quiz-maker' );
        }
        $user_email = isset($_REQUEST['ays_user_email']) ? esc_sql( sanitize_email( $_REQUEST['ays_user_email'] ) ) : '';
        $user_phone = isset($_REQUEST['ays_user_phone']) ? esc_sql( sanitize_text_field( $_REQUEST['ays_user_phone'] ) ) : '';
        $score = (isset($_REQUEST['rate_score']) && $_REQUEST['rate_score'] != "") ? esc_sql( absint( sanitize_text_field( $_REQUEST['rate_score'] ) ) ) : 5;
        $rate_date = current_time('mysql'); // For Security // esc_sql( sanitize_text_field( $_REQUEST['rate_date'] ) );
        // $rate_reason = (isset($_REQUEST['rate_reason']) && $_REQUEST['rate_reason'] != "") ? stripslashes( sanitize_textarea_field( $_REQUEST['rate_reason'] ) ) : '';
        $rate_reason = isset($_REQUEST['rate_reason']) ? sanitize_textarea_field( wp_unslash( $_REQUEST['rate_reason'] ) ) : '';

        switch ($score) {
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
                $score = $score;
                break;
            default:
                $score = 5;
                break;
        }

        if ( $quiz_make_responses_anonymous ) {
            $user_id     = 0;
            $user_ip     = '';
            $user_name   = __( 'Anonymous' , 'quiz-maker' );
            $user_email  = '';
            $user_phone  = '';
        }

        if ( $quiz_enable_user_cհoosing_anonymous_assessment && $quiz_enable_user_cհoosing_anonymous_assessment_checkbox_flag ) {
            $user_id     = 0;
            $user_ip     = '';
            $user_name   = __( 'Anonymous' , 'quiz-maker' );
            $user_email  = '';
            $user_phone  = '';
        }

        $results = $wpdb->insert(
            $rates_table,
            array(
                'quiz_id'     => $quiz_id,
                'user_id'     => $user_id,
                'user_ip'     => $user_ip,
                'user_name'   => $user_name,
                'user_email'  => $user_email,
                'user_phone'  => $user_phone,
                'score'       => $score,
                'review'      => $rate_reason,
                'options'     => '',
                'rate_date'   => $rate_date,
            ),
            array(
                '%d', //quiz_id
                '%d', //user_id
                '%s', //user_ip
                '%s', //user_name
                '%s', //user_email
                '%s', //user_phone
                '%s', //score
                '%s', //review
                '%s', //options
                '%s', //rate_date
            )
        );
        $rate_id = $wpdb->insert_id;
        $avg_score = $this->ays_get_average_of_rates($quiz_id);
        if ($results >= 0 && $this->ays_set_rate_id_of_result( $rate_id , $report_id )) {
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                //'rate_id'     => $rate_id,
                //'result'      => $this->ays_set_rate_id_of_result($rate_id),
                'quiz_id'       => $quiz_id,
                'status'        => true,
                'avg_score'     => round($avg_score, 1),
                'score'         => intval(round($score)),
                'rates_count'   => $this->ays_get_count_of_rates($quiz_id),
            ));
            wp_die();
        }
        ob_end_clean();
        $ob_get_clean = ob_get_clean();
        echo json_encode(array(
            'status'    => false,
        ));
        wp_die();
    }

    protected function get_user_by_ip($id) {
        global $wpdb;
        $user_ip = $this->get_user_ip();

        $id = absint($id);
        $user_ip = sanitize_text_field($user_ip);

        $sql = $wpdb->prepare(
            "SELECT COUNT(*) 
             FROM `{$wpdb->prefix}aysquiz_reports` 
             WHERE `user_ip` = %s AND `quiz_id` = %d",
            $user_ip,
            $id
        );

        return $wpdb->get_var($sql);
    }

    protected function get_limit_user_by_id($quiz_id, $user_id) {
        global $wpdb;

        $quiz_id = absint($quiz_id);
        $user_id = absint($user_id);

        $sql = $wpdb->prepare(
            "SELECT COUNT(*) 
             FROM `{$wpdb->prefix}aysquiz_reports` 
             WHERE `user_id` = %d AND `quiz_id` = %d",
            $user_id,
            $quiz_id
        );

        return intval($wpdb->get_var($sql));
    }

    protected function get_user_ip(){
        $ipaddress = '';
        if (getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        elseif (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if (getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if (getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if (getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if (getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else
            $ipaddress = 'UNKNOWN';
        return sanitize_text_field($ipaddress);
    }

    protected function get_time_difference($strStart, $strEnd){
        $dteStart = new DateTime($strStart);
        $dteEnd = new DateTime($strEnd);
        $texts = array(
            'year'      => __( "year", 'quiz-maker' ),
            'years'     => __( "years", 'quiz-maker' ),
            'month'     => __( "month", 'quiz-maker' ),
            'months'    => __( "months", 'quiz-maker' ),
            'day'       => __( "day", 'quiz-maker' ),
            'days'      => __( "days", 'quiz-maker' ),
            'hour'      => __( "hour", 'quiz-maker' ),
            'hours'     => __( "hours", 'quiz-maker' ),
            'minute'    => __( "minute", 'quiz-maker' ),
            'minutes'   => __( "minutes", 'quiz-maker' ),
            'second'    => __( "second", 'quiz-maker' ),
            'seconds'   => __( "seconds", 'quiz-maker' ),
        );
        $interval = $dteStart->diff($dteEnd);
        $return = '';

        if ($v = $interval->y >= 1) $return .= $interval->y ." ". $texts[$this->pluralize_new($interval->y, 'year')] . ' ';
        if ($v = $interval->m >= 1) $return .= $interval->m ." ". $texts[$this->pluralize_new($interval->m, 'month')] . ' ';
        if ($v = $interval->d >= 1) $return .= $interval->d ." ". $texts[$this->pluralize_new($interval->d, 'day')] . ' ';
        if ($v = $interval->h >= 1) $return .= $interval->h ." ". $texts[$this->pluralize_new($interval->h, 'hour')] . ' ';
        if ($v = $interval->i >= 1) $return .= $interval->i ." ". $texts[$this->pluralize_new($interval->i, 'minute')] . ' ';

        $return .= $interval->s ." ". $texts[$this->pluralize_new($interval->s, 'second')];

        return $return;
    }
    
    protected function pluralize($count, $text){
        return $count . (($count == 1) ? (" $text") : (" {$text}s"));
    }

    protected function pluralize_new($count, $text){
        return ($count == 1) ? $text."" : $text."s";
    }
    
    protected function isJSON($string){
       return is_string($string) && is_array(json_decode($string, true)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
    }
    
    public function ays_get_user_information() {
        if ( ! check_ajax_referer( 'ays_quiz_nonce', '_ajax_nonce', false ) ) {
            wp_send_json_error( array( 'message' => 'Invalid request' ), 403 );
        }

        if ( ! is_user_logged_in() ) {
            wp_send_json_error( array( 'message' => 'User not logged in' ), 401 );
        }

        $user = wp_get_current_user();

        wp_send_json_success( array(
            'display_name' => $user->display_name,
            'user_email'   => $user->user_email,
        ) );
    }

    public static function ays_quiz_set_cookie($attr){
        $cookie_name = $attr['name'].$attr['id'];
        $cookie_value = $attr['title'];
        $cookie_expiration =  current_time('timestamp') + (1 * 365 * 24 * 60 * 60);
        setcookie($cookie_name, $cookie_value, $cookie_expiration, '/');
    }

    public static function ays_quiz_remove_cookie($attr){
        $cookie_name = $attr['name'].$attr['id'];
        if(isset($_COOKIE[$cookie_name])){
            unset($_COOKIE[$cookie_name]);
            $cookie_expiration =  current_time('timestamp') - 1;   
            setcookie($cookie_name, null, $cookie_expiration, '/');
        }
    }

    public static function ays_quiz_check_cookie($attr){
        $cookie_name = $attr['name'].$attr['id'];
        if(isset($_COOKIE[$cookie_name])){
            return true;
        }
        return false;
    }
    
    public static function ays_autoembed( $content ) {
        global $wp_embed;

        if ( is_null( $content ) ) {
            return $content;
        }

        $content = stripslashes( wpautop( $content ) );
        $content = $wp_embed->autoembed( $content );
        if ( strpos( $content, '[embed]' ) !== false ) {
            $content = $wp_embed->run_shortcode( $content );
        }
        $content = do_shortcode( $content );
        return $content;
    }

    public static function get_questions_categories($q_ids){
        global $wpdb;

        $q_ids = isset( $q_ids ) && $q_ids != '' ? sanitize_text_field($q_ids) : '';

        if($q_ids == '' || empty( $q_ids )){
            return array();
        }

        if( is_string($q_ids) ){
            $q_ids = explode(',', $q_ids);
        }

        $new_ids_arr = array();
        if (!empty( $q_ids )) {
            foreach ($q_ids as $key => $q_id) {
                if( !is_null($q_id) && $q_id != "" && absint( $q_id ) > 0  ){
                    $new_ids_arr[] = absint($q_id);
                }
            }
        }

        if( empty( $new_ids_arr ) ){
            return array();
        }

        $in_values = $new_ids_arr;

        $in_pholders = implode( ',', array_fill( 0, count( $in_values ), '%d' ) );

        $sql = $wpdb->prepare( 
            "SELECT DISTINCT c.id, c.title
                FROM {$wpdb->prefix}aysquiz_categories c
                JOIN {$wpdb->prefix}aysquiz_questions q
                ON c.id = q.category_id
                WHERE q.id IN ( $in_pholders )",
            $in_values
        );

        $result = $wpdb->get_results( $sql, 'ARRAY_A');
        $cats = array();

        foreach($result as $res){
            $cats[$res['id']] = $res['title'];
        }

        return $cats;
    }

    public static function get_quiz_questions_by_ids($ids){

        global $wpdb;

        $results = array();
        if(!empty($ids)){

            $new_ids_arr = array();
            if (!empty( $ids )) {
                foreach ($ids as $key => $q_id) {
                    if( !is_null($q_id) && $q_id != "" && absint( $q_id ) > 0  ){
                        $new_ids_arr[] = absint($q_id);
                    }
                }
            }

            if( empty( $new_ids_arr ) ){
                return array();
            }

            $in_values = $new_ids_arr;

            $in_pholders = implode( ',', array_fill( 0, count( $in_values ), '%d' ) );

            $sql = $wpdb->prepare( 
                "SELECT * FROM {$wpdb->prefix}aysquiz_questions WHERE id IN ( $in_pholders )",
                $in_values
            );

            $results = $wpdb->get_results( $sql, "ARRAY_A");

        }

        return $results;

    }

    public static function ays_get_average_score_by_category($id){
        global $wpdb;
        $user_id = get_current_user_id();

        if( is_null($user_id) || $user_id == 0 ){
            return "";
        }

        $id = isset( $id ) && $id != '' && intval($id) > 0 ? absint($id) : null;

        if( is_null($id) || $id <= 0 || $id == '' ){
            return '';
        }

        $quizes_table = $wpdb->prefix . 'aysquiz_quizes';
        $quizes_questions_table = $wpdb->prefix . 'aysquiz_questions';
        $quizes_questions_cat_table = $wpdb->prefix . 'aysquiz_categories';
        $sql = "SELECT question_ids FROM {$quizes_table} WHERE id = ". absint($id);
        $results = $wpdb->get_var( $sql);
        $questions_ids = array();
        $questions_counts = array();
        $questions_cat_list = array();
        if($results != ''){
            $results = explode("," , $results);
            foreach ($results as $key){
                $questions_ids[$key] = 0;
                $questions_counts[$key] = 0;

                $sql = "SELECT q.category_id, c.title
                        FROM {$quizes_questions_table} AS q
                        JOIN {$quizes_questions_cat_table} AS c
                            ON q.category_id = c.id
                        WHERE q.id = {$key}; ";
                $questions_cat_list[$key] = $wpdb->get_row($sql);
            }
        }

        $quizes_reports_table = $wpdb->prefix . 'aysquiz_reports';
        $sql = "SELECT options
                FROM {$quizes_reports_table}
                WHERE quiz_id =".absint($id);

        if( isset($user_id) && $user_id > 0 ){
            $sql .= " AND user_id =".absint($user_id);
        }

        $report = $wpdb->get_results( $sql, ARRAY_A );
        if(! empty($report)){
            foreach ($report as $key){
                $report = json_decode($key["options"]);
                $questions = isset( $report->correctness ) ? $report->correctness : array();
                foreach ($questions as $i => $v){
                    $q = (int) substr($i ,12);
                    if(isset($questions_ids[$q])) {
                        if ($v) {
                            $questions_ids[$q]++;
                        }

                        $questions_counts[$q]++;
                    }
                }
            }
        }

        $q_cat_list = array();
        $q_cat_title = array();
        foreach ($questions_cat_list as $key_id => $val ) {
            $val_arr = (array) $val;
            if(isset($val_arr['category_id'])){
                if (!array_key_exists($val_arr['category_id'], $q_cat_list)) {
                    $q_cat_list[$val_arr['category_id']][] = $key_id;
                    $q_cat_title[$val_arr['category_id']] = $val_arr['title'];
                }else{
                    $q_cat_list[$val_arr['category_id']][] = $key_id;
                    $q_cat_title[$val_arr['category_id']] = $val_arr['title'];
                }
            }
        }

        $q_cat_lists = array('percent'=>'', 'cat_name'=>'');
        $q_cats_lists = array();
        foreach ($q_cat_list as $key1 => $value1) {
            $sum_min = 0;
            $sum_max = 0;
            foreach ($value1 as $key2 => $value2) {
                $sum_min += $questions_ids[$value2];
                $sum_max += $questions_counts[$value2];
            }
            if($sum_max == 0){
                $persentage = 0;
            }else{
                $persentage = round(($sum_min*100)/$sum_max, 1);
            }

            $passed_users_count = "SELECT COUNT(*) FROM {$wpdb->prefix}aysquiz_reports WHERE quiz_id=". absint($id);
            $passed_users_count_res = $wpdb->get_var($passed_users_count);
            $avg_score_by_cat = "0%";
            if ($passed_users_count_res > 0) {
                $avg_score_by_cat = round( $persentage , 1 ) . '%';
            }
            $q_cat_lists['percent'] = $avg_score_by_cat;
            $q_cat_lists['cat_name'] = $q_cat_title[$key1];
            $q_cats_lists[] = $q_cat_lists;

        }

        $avg_category = '';
        foreach ($q_cats_lists as $key => $q_cats_list) {
            $avg_category .= '<p class="">
                                <strong class="">'.$q_cats_list['cat_name']  .':</strong>
                                <span class="">'.$q_cats_list['percent'].'</span>
                             </p>';
        }
        return $avg_category;
    }

    public function ays_quiz_is_elementor(){
        // if ( defined( 'ELEMENTOR_PATH' ) && class_exists( 'Elementor\Widget_Base' ) ) {
        //     if ( class_exists( 'Elementor\Plugin' ) ) {
        //         if ( is_callable( 'Elementor\Plugin', 'instance' ) ) {
        //             $elementor = Elementor\Plugin::instance();
        //             if ( isset( $elementor->preview ) ) {
        //                 return \Elementor\Plugin::$instance->preview->is_preview_mode();
        //             }
        //         }
        //     }
        // }
        $is_elementor = isset( $_GET['action'] ) && $_GET['action'] == 'elementor' ? true : false;
        if ( ! $is_elementor ) {
            $is_elementor = isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ? true : false;
        }
        return $is_elementor;
    }

    public function ays_quiz_is_editor(){
        $is_editor = false;
        if( isset( $_GET['action'] ) && ( $_GET['action'] == 'add' || $_GET['action'] == 'edit' ) ){
            if ( isset( $_GET['post'] ) && absint( $_GET['post'] ) > 0 ) {
                $is_editor = true;
            }
        } elseif ( isset( $_GET['context'] ) && ( $_GET['context'] == 'add' || $_GET['context'] == 'edit' ) ) {
            if( isset( $_GET['post_id'] ) & absint( $_GET['post_id'] ) > 0 ){
                $is_editor = true;
            }
        }

        return $is_editor;
    }

    public function ays_quiz_is_beaver_builder(){
        $is_editor = false;
        
        if( isset( $_GET['fl_builder'] ) || isset( $_GET['fl_builder_ui'] ) || isset( $_GET['fl_builder_ui_iframe'] ) ){
            $is_editor = true;
        }

        return $is_editor;
    }

    public static function ays_quiz_is_enable_question_max_length( $question_id , $question_type = 'text' ){
        global $wpdb;
        $questions_table = $wpdb->prefix . "aysquiz_questions";
        $question_id = absint(intval($question_id));

        $question = $wpdb->get_row("SELECT * FROM {$questions_table} WHERE id={$question_id};", "ARRAY_A");
        $options = ! isset($question['options']) ? array() : json_decode($question['options'], true);

        $res = false;
        switch ( $question_type ) {
            case 'number':
                if(isset($options['enable_question_number_max_length']) && sanitize_text_field( $options['enable_question_number_max_length'] ) == 'on'){
                    $res = true;
                }
                break;            
            default:
                if(isset($options['enable_question_text_max_length']) && sanitize_text_field( $options['enable_question_text_max_length'] ) == 'on'){
                    $res = true;
                }
                break;
        }
        return $res;
    }

    public static function ays_quiz_get_question_max_length_array( $question_id ){
        global $wpdb;
        $questions_table = $wpdb->prefix . "aysquiz_questions";
        $question_id = absint(intval($question_id));

        $question = $wpdb->get_row("SELECT * FROM {$questions_table} WHERE id={$question_id};", "ARRAY_A");
        $options = ! isset($question['options']) ? array() : json_decode($question['options'], true);

        $res = array();

        // Maximum length of a text field
        $options['enable_question_text_max_length'] = isset($options['enable_question_text_max_length']) ? sanitize_text_field( $options['enable_question_text_max_length'] ) : 'off';
        $res['enable_question_text_max_length'] = (isset($options['enable_question_text_max_length']) && sanitize_text_field( $options['enable_question_text_max_length'] ) == 'on') ? true : false;

        // Length
        $res['question_text_max_length'] = ( isset($options['question_text_max_length']) && sanitize_text_field( $options['question_text_max_length'] ) != '' ) ? absint( intval( sanitize_text_field( $options['question_text_max_length'] ) ) ) : '';

        // Limit by
        $res['question_limit_text_type'] = ( isset($options['question_limit_text_type']) && sanitize_text_field( $options['question_limit_text_type'] ) != '' ) ? sanitize_text_field( $options['question_limit_text_type'] ) : 'characters';

        // Show the counter-message
        $options['question_enable_text_message'] = isset($options['question_enable_text_message']) ? sanitize_text_field( $options['question_enable_text_message'] ) : 'off';
        $res['question_enable_text_message'] = (isset($options['question_enable_text_message']) && $options['question_enable_text_message'] == 'on') ? true : false;

        // Maximum length of a number field
        $options['enable_question_number_max_length'] = isset($options['enable_question_number_max_length']) ? sanitize_text_field( $options['enable_question_number_max_length'] ) : 'off';
        $res['enable_question_number_max_length'] = (isset($options['enable_question_number_max_length']) && sanitize_text_field( $options['enable_question_number_max_length'] ) == 'on') ? true : false;

        // Length
        $res['question_number_max_length'] = ( isset($options['question_number_max_length']) && sanitize_text_field( $options['question_number_max_length'] ) != '' ) ? intval( sanitize_text_field( $options['question_number_max_length'] ) ) : '';

        // Minimum length of a number field
        $options['enable_question_number_min_length'] = isset($options['enable_question_number_min_length']) ? sanitize_text_field( $options['enable_question_number_min_length'] ) : 'off';
        $res['enable_question_number_min_length'] = (isset($options['enable_question_number_min_length']) && sanitize_text_field( $options['enable_question_number_min_length'] ) == 'on') ? true : false;

        // Length
        $res['question_number_min_length'] = ( isset($options['question_number_min_length']) && sanitize_text_field( $options['question_number_min_length'] ) != '' ) ? intval( sanitize_text_field( $options['question_number_min_length'] ) ) : '';

        // Show error message
        $options['enable_question_number_error_message'] = isset($options['enable_question_number_error_message']) ? sanitize_text_field( $options['enable_question_number_error_message'] ) : 'off';
        $res['enable_question_number_error_message'] = (isset($options['enable_question_number_error_message']) && sanitize_text_field( $options['enable_question_number_error_message'] ) == 'on') ? true : false;

        // Message
        $res['question_number_error_message'] = ( isset($options['question_number_error_message']) && sanitize_text_field( $options['question_number_error_message'] ) != '' ) ? stripslashes( sanitize_text_field( $options['question_number_error_message'] ) ) : '';
            
        return $res;
    }

    public function ays_answer_numbering($numbering){
        $keyword_arr = array();
        switch ($numbering) {
            case '1.':
                $char_min_val = '1';
                $char_max_val = '100';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .".";
                }
                break;
            case '1)':
                $char_min_val = '1';
                $char_max_val = '100';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .")";
                }
                break;
            case 'A.':
                $char_min_val = 'A';
                $char_max_val = 'Z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .".";
                }
                break;
            case 'A)':
                $char_min_val = 'A';
                $char_max_val = 'Z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .")";
                }
                break;
            case 'a.':
                $char_min_val = 'a';
                $char_max_val = 'z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .".";
                }
                break;
            case 'a)':
                $char_min_val = 'a';
                $char_max_val = 'z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .")";
                }
                break;
            default:
                break;
        }
        return $keyword_arr;
    }

    public static function ays_question_numbering( $numbering , $total ){
        $keyword_arr = array();
        switch ($numbering) {
            case '1.':
                $char_min_val = '1';
                $char_max_val = $total;
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .".";
                }

                break;
            case '1)':
                $char_min_val = '1';
                $char_max_val = $total;
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .")";
                }
                break;
            case 'A.':

                $char_min_val = 'A';
                $char_max_val = 'Z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .".";
                }

                break;
            case 'A)':

                $char_min_val = 'A';
                $char_max_val = 'Z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .")";
                }

                break;
            case 'a.':
                $char_min_val = 'a';
                $char_max_val = 'z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .".";
                }

                break;
            case 'a)':

                $char_min_val = 'a';
                $char_max_val = 'z';
                for($x = $char_min_val; $x <= $char_max_val; $x++){
                    $keyword_arr[] = $x .")";
                }

                break;

            default:

                break;
        }

        return $keyword_arr;
    }
    
    public function ays_color_inverse( $color ){
        $color = str_replace( '#', '', $color );
        if ( strlen( $color ) == 3 ){
            $color_short = str_split( $color );
            foreach( $color_short as $k => $c_short ){
                $color_short[$k] = $c_short . $c_short;
            }
            $color = implode( $color_short );
        }

        if ( strlen( $color ) != 6 ){
            return '#000000';
        }

        $rgb = '';
        for ( $x = 0; $x < 3; $x++ ){
            $c = 255 - hexdec( substr( $color, ( 2 * $x ), 2 ) );
            $c = ( $c < 0 ) ? 0 : dechex( $c );
            $rgb .= ( strlen( $c ) < 2 ) ? '0' . $c : $c;
        }

        return '#'.$rgb;
    }

    // Retrieves the attachment ID from the file URL
    public function ays_quiz_get_image_id_by_url( $image_url ) {
        global $wpdb;

        $image_alt_text = "";
        if ( !empty( $image_url ) ) {

            $re = '/-\d+[Xx]\d+\./';
            $subst = '.';

            $image_url = preg_replace($re, $subst, $image_url, 1);

            $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
            if ( !is_null( $attachment ) && !empty( $attachment ) ) {

                $image_id = (isset( $attachment[0] ) && $attachment[0] != "") ? absint(  $attachment[0] ) : "";
                if ( $image_id != "" ) {
                    $image_alt_text = $this->ays_quiz_get_image_alt_text_by_id( $image_id );
                }
            }
        }

        return $image_alt_text; 
    }

    public function ays_quiz_get_image_alt_text_by_id( $image_id ) {

        $image_data = "";
        if ( $image_id != "" ) {

            $result = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
            if ( $result && $result != "" ) {
                $image_data = esc_attr( $result );
            }
        }

        return $image_data; 
    }

    public function ays_quiz_get_image_full_size_url_by_url( $image_url ) {
        global $wpdb;

        $image_full_size = "";
        if ( !empty( $image_url ) ) {

            $re = '/-\d+[Xx]\d+\./';
            $subst = '.';

            $image_url = preg_replace($re, $subst, $image_url, 1);

            $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
            if ( !is_null( $attachment ) && !empty( $attachment ) ) {

                $image_id = (isset( $attachment[0] ) && $attachment[0] != "") ? absint(  $attachment[0] ) : "";
                if ( $image_id != "" ) {
                    $image_full_size = $this->ays_quiz_get_image_full_size_by_id( $image_id );
                }
            } 
            elseif( !is_null( $attachment ) && empty( $attachment ) ){
                $image_full_size = $image_url;
            }
        }

        return $image_full_size; 
    }

    public function ays_quiz_get_image_full_size_by_id( $image_id ) {

        $image_data = "";
        if ( $image_id != "" ) {

            $result = wp_get_attachment_image_src( $image_id, 'full' );
            if ( !is_null( $result ) && $result && !empty($result) ) {
                $image_data = (isset( $result[0] ) && $result[0] != "") ? esc_url($result[0]) : "";
            }
        }

        return $image_data; 
    }

    public static function get_published_questions_id_arr($ids) {
        global $wpdb;

        $ids = sanitize_text_field($ids);

        $sql = "SELECT id FROM {$wpdb->prefix}aysquiz_questions WHERE id IN ({$ids}) AND published = 1 ORDER BY find_in_set(id,'".$ids."');";

        if( isset($_SERVER['HTTP_HOST']) && sanitize_text_field($_SERVER['HTTP_HOST']) == "playground.wordpress.net" ){
            $sql = "SELECT id FROM {$wpdb->prefix}aysquiz_questions WHERE id IN ({$ids}) AND published = 1;";
        }

        $results = $wpdb->get_results( $sql, 'ARRAY_A' );

        if ( !is_null( $results ) && !empty($results) ) {
            $new_question_ids = array();
            foreach ($results as $key => $value) {
                $published_question_id = (isset( $value['id'] ) && intval( $value['id'] ) > 0 && $value['id'] != "") ? sanitize_text_field($value['id']) : null;

                if ( !is_null( $published_question_id ) ) {
                    $new_question_ids[] = $published_question_id;
                }
            }

            if ( !empty( $new_question_ids ) ) {
                $results = $new_question_ids;
            } else {
                $results = explode(",", $ids);
            }
        } else {
            if( !empty($ids) && ( is_null( $results ) || empty($results) ) ){
                $results = array();
            } else {
                $results = explode(",", $ids);
            }
        }

        return $results;
    }

    public function set_prop( $prop, $value ){
        if( property_exists( $this, $prop ) && ! empty( $value ) ){
            $this->$prop = $value;
        }
    }

    public function get_prop( $prop ){
        if( isset( $this->$prop ) ){
            return $this->$prop;
        }

        return null;
    }

    /**
     * Fix [ays_quiz] shortcode inside Elementor text-editor widgets
     * tid=30764,30758
     */
    public function ays_quiz_elementor_remove_tags_before_randering_shortcode( $widget ) {
        if ( 'text-editor' === $widget->get_name() ) {
            $settings = $widget->get_settings();

            if ( ! empty( $settings['editor'] ) && strpos( $settings['editor'], '[ays_quiz' ) !== false ) {
                $content = $settings['editor'];

                // Remove Gutenberg shortcode comments
                $content = preg_replace( '/<!--\s*wp:shortcode\s*-->/', '', $content );
                $content = preg_replace( '/<!--\s*\/wp:shortcode\s*-->/', '', $content );

                // Remove wrapping <p> tags
                $content = preg_replace( '/<p>\s*(\[ays_quiz[^\]]+\])\s*<\/p>/i', '$1', $content );

                // Remove empty paragraphs like <p>&nbsp;</p>
                $content = preg_replace( '/<p>(&nbsp;|\s)*<\/p>/i', '', $content );

                // Save cleaned editor content back
                $widget->set_settings( 'editor', $content );
            }
        }
    }

    public function ays_quiz_send_question_report() {

        if( empty($_REQUEST['_ajax_nonce']) ){
            ob_end_clean();
            $ob_get_clean = ob_get_clean();
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        // Run a security check.
        check_ajax_referer( 'ays_quiz_question_report_nonce', sanitize_key( wp_unslash($_REQUEST['_ajax_nonce']) ) );

        global $wpdb;

        $question_id = isset( $_REQUEST['question_id'] ) && $_REQUEST['question_id'] != '' ? absint( $_REQUEST['question_id'] ) : 0;
        $quiz_id = isset( $_REQUEST['quiz_id'] ) && $_REQUEST['quiz_id'] != '' ? absint( $_REQUEST['quiz_id'] ) : 0;
        $report_text = isset( $_REQUEST['report_text'] ) && $_REQUEST['report_text'] != '' ? stripslashes( sanitize_textarea_field( $_REQUEST['report_text'] ) ) : '';
        // $create_date = isset($_REQUEST['create_date']) && $_REQUEST['create_date'] != '' ? stripslashes( sanitize_text_field($_REQUEST['create_date']) ) : current_time('mysql');
        $create_date = current_time('mysql'); // For Security 

        if(empty($quiz_id) || empty($question_id)){
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        if ( empty( $report_text ) || strlen($report_text) < 2 || strlen($report_text) > 1000 ) {
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        $quiz_question_ids = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT question_ids 
                 FROM {$wpdb->prefix}aysquiz_quizes
                 WHERE id = %d",
                $quiz_id
            )
        );

        if ( empty($quiz_question_ids) ) {
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        $question_ids_array = array_map('absint', explode(',', $quiz_question_ids));

        if ( is_array($question_ids_array) && ! in_array( $question_id, $question_ids_array, true ) ) {
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        $user_ip = $this->get_user_ip();

        $one_minute_ago = date('Y-m-d H:i:s', current_time('timestamp') - 60);

        $recent_reports = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(id)
                 FROM {$wpdb->prefix}aysquiz_question_reports
                 WHERE user_ip = %s
                 AND create_date > %s",
                $user_ip,
                $one_minute_ago
            )
        );

        if ( $recent_reports > 3 ) {
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        $current_user_id = 0;
        $current_user_display_name = "";
        $current_user_email = "";
        if(is_user_logged_in()){
            $current_users = wp_get_current_user();

            if( $current_users ){
                $current_user_id = ( isset( $current_users->data->ID ) && $current_users->data->ID != '' ) ? absint( $current_users->data->ID ) : 0;
                $current_user_display_name = ( isset( $current_users->data->display_name ) && $current_users->data->display_name != '' ) ? sanitize_text_field( $current_users->data->display_name ) : "";
                $current_user_email = ( isset( $current_users->data->user_email ) && $current_users->data->user_email != '' ) ? sanitize_text_field( $current_users->data->user_email ) : "";
            }
        }

        if( $question_id !== 0 && $report_text !== ''){
            $question_reports_table = $wpdb->prefix . 'aysquiz_question_reports';
            $data = array(
                'question_id'   => $question_id,
                'report_text'   => nl2br( stripslashes($report_text) ),
                'resolved'      => 0,
                'create_date'   => $create_date,
                'resolve_date'  => NULL,
                'user_id'       => $current_user_id,
                'user_ip'       => $user_ip,
                'user_name'     => $current_user_display_name,
                'user_email'    => $current_user_email,
            );
            $format = array( 
                '%d', // question_id
                '%s', // report_text
                '%d', // resolved
                '%s', // create_date
                '%s', // resolve_date
                '%d', // user_id
                '%s', // user_ip
                '%s', // user_name
                '%s', // user_email
            );

            $result = $wpdb->insert( $question_reports_table, $data, $format );

            if($result) {
                echo json_encode(array(
                    'status' => true,
                ));
                wp_die();
            } else {
                echo json_encode(array(
                    'status' => false,
                ));
                wp_die();
            }
        } else{
            echo json_encode(array(
                'status' => false,
            ));
            wp_die();
        }

        wp_die();
    }

    public function ays_quiz_check_answer() {

        if(empty( $_REQUEST['_ajax_nonce'] )){
            wp_send_json_error( array( 'message' => 'Invalid request' ) );
        }

        if ( ! check_ajax_referer( 'ays_quiz_nonce', sanitize_key( $_REQUEST['_ajax_nonce'] ) ) ) {
            wp_send_json_error( array( 'message' => 'Invalid request' ) );
        }

        $quiz_id     = isset($_POST['quiz_id']) && !empty($_POST['quiz_id']) ? absint($_POST['quiz_id']) : 0;

        if(empty($quiz_id) || $quiz_id == 0){
            wp_send_json_error( array( 'message' => 'Invalid request' ) );
        }

        $quiz = $this->get_quiz_by_id($quiz_id);
        $options = ( isset($quiz['options']) && ( json_decode($quiz['options'], true) != null ) ) ? json_decode($quiz['options'], true) : array();

        if(empty($options)){
            wp_send_json_error( array( 'message' => 'Invalid request' ) );
        }

        $enable_correction = false;
        if(isset($options['enable_correction']) && $options['enable_correction'] == "on"){
            $enable_correction = true;
        }

        if(!$enable_correction){
            wp_send_json_error( array( 'message' => 'Invalid request' ) );
        }

        $question_id = isset($_POST['question_id']) && !empty($_POST['question_id']) ? absint($_POST['question_id']) : 0;
        $question_type = isset($_POST['question_type']) && !empty($_POST['question_type']) ? sanitize_text_field($_POST['question_type']) : 'radio';
        $user_answer = isset($_POST['answer']) ? sanitize_text_field($_POST['answer']) : '';
        $questions_answer = $user_answer;

        $calculate_score = 'by_correctness';
        $strong_count_checkbox = true;

        $correctness = array();
        $correctness_results = array();

        $quests = array();
        $quiz_questions_ids = array();
        $question_bank_by_categories1 = array();

        if( absint($question_id) > 0 ){
            $quiz_questions_ids[] = strval( absint($question_id) );
        }

        $questions_categories = $this->get_questions_categories( implode( ',', $quiz_questions_ids ) );
        $quest_s = $this->get_quiz_questions_by_ids($quiz_questions_ids);
        foreach($quest_s as $quest){
            $quests[$quest['id']] = $quest;
        }

        $continue = false;
        if($this->is_question_not_influence($question_id)){
            $continue = true;

            wp_send_json_success( array(
                'correct' => true,
                'answer'  => '', //$correct_answer,
                'not_influence_question'  => true,
            ) );

        }
        $multiple_correctness = array();
        $has_multiple = $this->has_multiple_correct_answers($question_id);
        if ($has_multiple) {                        
            if (is_array($questions_answer)) {
                foreach ($questions_answer as $answer_id) {
                    $answer_id = intval($answer_id);
                    $multiple_correctness[] = $this->check_answer_correctness($question_id, $answer_id, $calculate_score);
                }
                
                if($calculate_score == 'by_points'){
                    if(!$continue){
                        $correctness[$question_id] = array_sum($multiple_correctness);
                    }
                    $correctness_results["question_id_" . $question_id] = array_sum($multiple_correctness);
                    // continue;
                }
                
                if($strong_count_checkbox === false){
                    if(!$continue){
                        $correctness[$question_id] = $this->isHomogenousStrong($multiple_correctness, $question_id);
                    }
                    $correctness_results["question_id_" . $question_id] = $this->isHomogenousStrong($multiple_correctness, $question_id);
                }else{
                    if ($this->isHomogenous($multiple_correctness, $question_id)) {
                        if(!$continue){
                            $correctness[$question_id] = true;
                        }
                        $correctness_results["question_id_" . $question_id] = true;
                    } else {
                        if(!$continue){
                            $correctness[$question_id] = false;
                        }
                        $correctness_results["question_id_" . $question_id] = false;
                    }
                }
            } else {
                if($calculate_score == 'by_points'){
                    $questions_answer = intval($questions_answer);
                    if(!$continue){
                        $correctness[$question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                    }
                    $correctness_results["question_id_" . $question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                    // continue;
                }
                if($strong_count_checkbox === false){
                    $questions_answer = intval($questions_answer);
                    if($this->check_answer_correctness($question_id, $questions_answer, $calculate_score)){
                        if(!$continue){
                            $correctness[$question_id] = 1 / intval($this->count_multiple_correct_answers($question_id));
                        }
                    }else{
                        if(!$continue){
                            $correctness[$question_id] = false;
                        }
                    }
                    $correctness_results["question_id_" . $question_id] = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);
                }else{
                    if(!$continue){
                        $correctness[$question_id] = false;
                    }
                    $correctness_results["question_id_" . $question_id] = false;
                }
            }
        } elseif($this->has_text_answer($question_id)) {
            $quests_data = ( isset( $quests[$question_id] ) && ! empty( $quests[$question_id] ) ) ? $quests[$question_id] : array();
            $quests_data_options = isset( $quests_data['options'] ) ? json_decode( $quests_data['options'], true ) : array(); 

            $correctness_data = $this->check_text_answer_correctness($question_id, $questions_answer, $calculate_score, $quests_data_options);

            if(!$continue){
                $correctness[$question_id] = $correctness_data;
            }
            $correctness_results["question_id_" . $question_id] = $correctness_data;
        } else {
            $questions_answer = intval($questions_answer);

            $correctness_data = $this->check_answer_correctness($question_id, $questions_answer, $calculate_score);

            if(!$continue){
                $correctness[$question_id] = $correctness_data;
            }
            $correctness_results["question_id_" . $question_id] = $correctness_data;
        }

        $question_answer = $this->get_correct_and_wrong_answers($question_id, $question_type);

        $is_correct = false;
        if ( $user_answer !== '' && !empty($question_answer)) {
            $is_correct = $correctness[$question_id];
        }

        wp_send_json_success( array(
            'correct'           => $is_correct,
            'question_answer'   => $question_answer,
        ) );
    }
}
