<?php
/**
 * template: ProQyz Quiz template 
 */
defined('ABSPATH') || exit;
require_once ST_PROQYZ_LIBS_DIR . '/lms/utill.php';
require_once 'functions.php';

$home_url                           = home_url();
$proqyz_action                      = get_query_var('action');
$proqyz_quiz_category               = get_query_var('quiz_category');
$proqyz_quiz_slug                   = get_query_var('quiz_slug');
$proqyz_quiz_id                     = get_query_var('quiz_id');

$ilms_status                        = false;
//$ilms_classes                     = '';
$proqyz_response                    = '';
$proqyz_attempt_url                 = '';


// Get the current page URL
$current_url = home_url($_SERVER['REQUEST_URI']);

# handle quiz_id check
if (!isset($proqyz_quiz_id) || empty($proqyz_quiz_id)) {
    $proqyz_response = 'It looks like the quiz is not associated with any backend';
    include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    exit;
}

if (isset($proqyz_quiz_category) && !empty($proqyz_quiz_category)) {
    if (in_array($proqyz_quiz_category, [
        "ielts-reading", 
        "ielts-listening", 
        "ielts-writing", 
        "ielts-speaking",
        "gov-non-sectional-timing",
        "gov-sectional-timing",
        'proqyz-pdf-quiz'
    ])) {
        //$ilms_classes = $proqyz_quiz_category . '-test-page ' . $proqyz_quiz_category . '-test show-palette take-test-page -practice-mode user-logged-in path-node page-node-type-quiz has-glyphicons';
        // $proqyz_attempt_url .= '/proqyz/quiz/' . $proqyz_quiz_category . '/' . $proqyz_quiz_slug . '/' . $proqyz_quiz_id . '/start';
        # update - slug
        $proqyz_attempt_url = st_proqyz_get_quiz_url( $proqyz_quiz_category, $proqyz_quiz_slug, $proqyz_quiz_id, 'start' );
    } else {
        $proqyz_response = 'It looks like the quiz category dosnt exists';
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
        exit;
    } 
}


// Check if it's a quiz page
if (strpos($current_url, '/quiz/') !== false) {
    handle_quiz_page($proqyz_attempt_url, $proqyz_quiz_category);
}

// Check if it's a quiz page
if (strpos($current_url, '/result/') !== false) {
    if(is_user_logged_in()){
        handle_result_page($proqyz_attempt_url);
    } else {
        $redirect_url = $current_url;
        $settings_st_pro_quiz_auth_redirect_enable_key  = 'st-pro-quiz__auth-redirect-enable';
        $settings_st_pro_quiz__auth_signup_url_key      = 'st-pro-quiz__auth-signup-url';
        $settings_st_pro_quiz__auth_signin_url_key      = 'st-pro-quiz__auth-signin-url';

        $enable_auth_redirect   = get_option($settings_st_pro_quiz_auth_redirect_enable_key,0);
        $auth_signin_url        = get_option($settings_st_pro_quiz__auth_signin_url_key,"/wp-login.php");
        $auth_signup_url        = get_option($settings_st_pro_quiz__auth_signup_url_key,"/wp-login.php");
        $url    = $auth_signin_url;
        $url    .= $enable_auth_redirect == 1? '?redirect_to='.$redirect_url : '';
        wp_safe_redirect($url);
        exit;
    }
}

if (strpos($current_url, '/result-analyize/') !== false) {
    if(is_user_logged_in()){
        handle_result_analyze_page($proqyz_attempt_url);
    } else {
        $redirect_url = $current_url;
        $settings_st_pro_quiz_auth_redirect_enable_key  = 'st-pro-quiz__auth-redirect-enable';
        $settings_st_pro_quiz__auth_signup_url_key      = 'st-pro-quiz__auth-signup-url';
        $settings_st_pro_quiz__auth_signin_url_key      = 'st-pro-quiz__auth-signin-url';

        $enable_auth_redirect   = get_option($settings_st_pro_quiz_auth_redirect_enable_key,0);
        $auth_signin_url        = get_option($settings_st_pro_quiz__auth_signin_url_key,"/wp-login.php");
        $auth_signup_url        = get_option($settings_st_pro_quiz__auth_signup_url_key,"/wp-login.php");
        $url    = $auth_signin_url;
        $url    .= $enable_auth_redirect == 1? '?redirect_to='.$redirect_url : '';
        wp_safe_redirect($url);
        exit;
    }
}

// Check if it's a review page
if (strpos($current_url, '/review/') !== false) {
    if(is_user_logged_in()){
            $user = get_userdata(get_current_user_id());
            if (!$user) {
                return null;
            }

            $user_roles = $user->roles;
            $current_role = "student";

            if (in_array("administrator", $user_roles)) {
                handle_review_page();
            } elseif (in_array("teacher", $user_roles)) {
                handle_review_page($proqyz_attempt_url);
            } else {
                $redirect_url = str_replace('review','result', $current_url);
                wp_safe_redirect($redirect_url);
                exit;
            }
    } else {
            $redirect_url = $current_url;
            $settings_st_pro_quiz_auth_redirect_enable_key  = 'st-pro-quiz__auth-redirect-enable';
            $settings_st_pro_quiz__auth_signup_url_key      = 'st-pro-quiz__auth-signup-url';
            $settings_st_pro_quiz__auth_signin_url_key      = 'st-pro-quiz__auth-signin-url';

            $enable_auth_redirect   = get_option($settings_st_pro_quiz_auth_redirect_enable_key,0);
            $auth_signin_url        = get_option($settings_st_pro_quiz__auth_signin_url_key,"/wp-login.php");
            $auth_signup_url        = get_option($settings_st_pro_quiz__auth_signup_url_key,"/wp-login.php");
            $url    = $auth_signin_url;
            $url    .= $enable_auth_redirect == 1? '?redirect_to='.$redirect_url : '';
            wp_safe_redirect($url);
            exit;
    }
}

