<?php 
/**
 * v3.1.0 
 * varible above: $get_session
 * hint: above we are expecting full session with meta ('score', 'quiz_title', 'json')
 * status: pending
 * note: quiz_title, quiz_category,  are not part of $get_session object, so no worry about it 
 * note: `score` is part of $get_session object, so we need to get score from $get_session object
 */



$display_attempt_btn = 0;
$fresh_session  = null;
$retake_session = null;
$recent_session = null;
$result_id      = null;
$show_result    = 0;
$btn_type       = "take-test";
$course_subcategory = 'academic';

if( $course_access == "access" ){
    $display_attempt_btn = 1;
}


if( $lesson_preview == 1){
    $display_attempt_btn = 1;
}

if( $display_attempt_btn == 1 ) {
    if( $get_session->status == 1 ) {
        if($get_session->fresh_session) { $fresh_session = (object) $get_session->fresh_session; }
        if($get_session->retake_session) { $retake_session = (object) $get_session->retake_session; }
        if($get_session->recent_session) { $recent_session = (object) $get_session->recent_session; }
        if($get_session->btn_type){ 
            $btn_type = $get_session->btn_type; 
        }

    }
}




if($quiz_category  == "ielts-listening") {
    $color_class = "-blue";
    $bar_color = "#33B2C7";
    $category_txt = "Listening";
    $category_class = "-listening";
    $tabindex = 1;
} else if($quiz_category == "ielts-reading") {
    $color_class = "-green";
    $bar_color = "#327846";
    $category_class = "-reading";
    $category_txt = "Reading";
    $tabindex = 2;
} else if($quiz_category == "ielts-writing"){
    $color_class = "-orange";
    $bar_color = "#faaa5a";
    $category_class = "-writing";
    $category_txt = "Writing";
    $tabindex = 3;
} else if($quiz_category == "ielts-speaking"){
    $color_class = "-pink";
    $bar_color = "#c86478";
    $category_class = "-speaking";
    $category_txt = "Speaking";
    $tabindex = 4;
} else if($quiz_category == "gov-sectional-timing" || $quiz_category == "gov-non-sectional-timing"){
    $color_class = "-gov";
    $bar_color = "#6f51f8";
    $category_txt = "-".$quiz_category;
    $category_class = "-".$quiz_category;
    $tabindex = 1;
} else if($quiz_category == "proqyz-pdf-quiz"){
    $color_class = "-pdf-quiz";
    $bar_color = "#1B72E8";
    $category_txt = "-".$quiz_category;
    $category_class = "-".$quiz_category;
    $tabindex = 1;
}

$btn_class_starter = "";
$data_pie = json_encode((object) [
    "percent"       => 0,
    "colorCircle"   => "#ECF8FA",
    "colorSlice"    => $bar_color
]);


if($btn_type == "take-test"){ 

    $btn_class_starter  = "-not-start";
    $percent = "0";  

}else if($btn_type == "resume"){ 
    if( $recent_session !== null ){

        if($quiz_category == "ielts-writing" || $quiz_category == "ielts-speaking"){
            $result_id      = isset($recent_session->ID)? $recent_session->ID : null;
            $reviewed       = isset($recent_session->review)? (int) $recent_session->review : 0;
            $scores         = isset($recent_session->score)? (array) $recent_session->score : [];
            $overall_band   = 2; 
            if($reviewed == 1){

                if(count($scores) > 0 ){ 

                    if($quiz_category == "ielts-writing"){
                        $task1_scores = isset($scores['task-1'])? (array) $scores['task-1'] : [0,0,0,0];
                        $task2_scores = isset($scores['task-2'])? (array) $scores['task-2'] : [0,0,0,0];

                        $total_score1 = array_sum($task1_scores)/4;
                        $total_score2 = array_sum($task2_scores)/4;

                        $total_score1 = round($total_score1*2);
                        $total_score1 = $total_score1/2;

                        $total_score2 = round($total_score2*2);
                        $total_score2 = $total_score2/2;


                        $band = ($total_score1 + ($total_score2*2));
                        $band = $band/3;
                        $band = round($band*2);
                        $band = $band/2;

                    } else if($quiz_category == "ielts-speaking"){

                        $task_scores = isset($scores['tasks'])? (array) $scores['tasks'] : [0,0,0,0];
                        $total_scores = array_sum($task_scores)/4;
                        $total_scores = round($total_scores*2);
                        $band = $total_scores/2;

                    }

                    $overall_band = $band; 
                }    

                $btn_class_starter  = "-finished -has-score hide-percent  $overall_band visible"; 
                $data_pie = json_encode((object) [
                    "percent"       => 100,
                    "unit"          => $overall_band,
                    "colorCircle"   => "#ECF8FA",
                    "colorSlice"    => $bar_color
                ]);

            } else {
                $btn_class_starter  = "-finished -persent -pep"; 
                $data_pie = json_encode((object) [
                    "percent"       => 100,
                    "unit"          => "%",
                    "colorCircle"   => "#ECF8FA",
                    "colorSlice"    => $bar_color
                ]);
            }
        } else {
            $result_id      = isset($recent_session->ID)? $recent_session->ID : null;
            $score          = isset($recent_session->score)? (object) $recent_session->score : null;
            $overall_band   = 2; 

            # conver the overall score to band and percentage
            if($quiz_category == "ielts-reading" || $quiz_category == "ielts-listening"){

                if($score !== null){
                    $course_subcategory = isset($recent_session->course_subcategory)? $recent_session->course_subcategory : 'academic';
                    $score_s      = isset($score->scalar)? $score->scalar : null;
                    if( $score_s !== null ) {
                        $score_obj  = json_decode($score_s);
                        $total      = $score_obj->total;
                        $correct    = $score_obj->correct;
                        $wrong      = $score_obj->wrong;
                        $attempt    = $score_obj->attempt;
                        $overall_band = ProQyz_Ielts_scoreToBand($correct, $course_subcategory); 


                    }
                }

            } else if($quiz_category == "gov-sectional-timing" || $quiz_category == "gov-non-sectional-timing"){
                
                $overall_band = "100"; 
            } else if($quiz_category === "proqyz-pdf-quiz") {
                $overall_band = "0";
                if($score !== null){
                    $score_s      = isset($score->scalar)? $score->scalar : null;
                    if( $score_s !== null ) {
                        $score_obj  = json_decode($score_s);
                        $score_report = isset($score_obj->report)? (object) $score_obj->report : null;
                        $total_questions = isset($score_report->total_set_questions)? (int) $score_report->total_set_questions : 0;
                        $total_correct   = isset($score_report->total_correct)? (int) $score_report->total_correct : 0;
                        if($total_questions > 0) {
                            $overall_band = $total_correct/$total_questions;
                            $overall_band = $overall_band*100;
                            $overall_band = round($overall_band / 5) * 5;
                            $overall_band = $overall_band;
                        }


                    }
                }
            }

            $btn_class_starter  = "-finished -has-score hide-percent  $overall_band visible"; 
            $data_pie = json_encode((object) [
                "percent"       => 100,
                "unit"          => $overall_band,
                "colorCircle"   => "#ECF8FA",
                "colorSlice"    => $bar_color
            ]);
        }


        $show_result = 1;
    } else {
        $btn_class_starter  = "-resume"; 
        $percent = "0"; 
    }

} else if($btn_type == "retake"){
    if( $recent_session !== null ){
        if($quiz_category == "ielts-writing" || $quiz_category == "ielts-speaking"){
            $result_id      = isset($recent_session->ID)? $recent_session->ID : null;
            $reviewed       = isset($recent_session->review)? (int) $recent_session->review : 0;
            $scores         = isset($recent_session->score)? (array) $recent_session->score : [];
            $overall_band   = 2; 
            if($reviewed == 1){

                if(count($scores) > 0 ){ 

                    if($quiz_category == "ielts-writing"){
                        $task1_scores = isset($scores['task-1'])? (array) $scores['task-1'] : [0,0,0,0];
                        $task2_scores = isset($scores['task-2'])? (array) $scores['task-2'] : [0,0,0,0];

                        $total_score1 = array_sum($task1_scores)/4;
                        $total_score2 = array_sum($task2_scores)/4;

                        $total_score1 = round($total_score1*2);
                        $total_score1 = $total_score1/2;

                        $total_score2 = round($total_score2*2);
                        $total_score2 = $total_score2/2;


                        $band = ($total_score1 + ($total_score2*2));
                        $band = $band/3;
                        $band = round($band*2);
                        $band = $band/2;

                    } else if($quiz_category == "ielts-speaking"){

                        $task_scores = isset($scores['tasks'])? (array) $scores['tasks'] : [0,0,0,0];
                        $total_scores = array_sum($task_scores)/4;
                        $total_scores = round($total_scores*2);
                        $band = $total_scores/2;

                        

                    }

                    $overall_band = $band; 
                }    

                $btn_class_starter  = "-finished -has-score hide-percent  $overall_band visible"; 
                $data_pie = json_encode((object) [
                    "percent"       => 100,
                    "unit"          => $overall_band,
                    "colorCircle"   => "#ECF8FA",
                    "colorSlice"    => $bar_color
                ]);

            } else {
                $btn_class_starter  = "-finished -persent -pep"; 
                $data_pie = json_encode((object) [
                    "percent"       => 100,
                    "unit"          => "%",
                    "colorCircle"   => "#ECF8FA",
                    "colorSlice"    => $bar_color
                ]);
            }
        } else {
            $result_id      = isset($recent_session->ID)? $recent_session->ID : null;
            $score          = isset($recent_session->score)? (object) $recent_session->score : null;
            $overall_band   = 2; 

            
            # conver the overall score to band and percentage
            # conver the overall score to band and percentage
            if($quiz_category == "ielts-reading" || $quiz_category == "ielts-listening"){

                if($score !== null){
                    $course_subcategory = isset($recent_session->course_subcategory)? $recent_session->course_subcategory : 'academic';
                    $score_s      = isset($score->scalar)? $score->scalar : null;
                    if( $score_s !== null ) {
                        $score_obj  = json_decode($score_s);
                        $total      = $score_obj->total;
                        $correct    = $score_obj->correct;
                        $wrong      = $score_obj->wrong;
                        $attempt    = $score_obj->attempt;
                        $overall_band = ProQyz_Ielts_scoreToBand($correct, $course_subcategory); 


                    }
                }

            } else if($quiz_category == "gov-sectional-timing" || $quiz_category == "gov-non-sectional-timing"){
                
                $overall_band = "100"; 
            } else if($quiz_category === "proqyz-pdf-quiz"){
                
                $overall_band = "0";
                if($score !== null){
                    $score_s      = isset($score->scalar)? $score->scalar : null;
                    if( $score_s !== null ) {
                        $score_obj  = json_decode($score_s);
                        $score_report = isset($score_obj->report)? (object) $score_obj->report : null;
                        $total_questions = isset($score_report->total_set_questions)? (int) $score_report->total_set_questions : 0;
                        $total_correct   = isset($score_report->total_correct)? (int) $score_report->total_correct : 0;
                        if($total_questions > 0) {
                            $overall_band = $total_correct/$total_questions;
                            $overall_band = $overall_band*100;
                            $overall_band = round($overall_band / 5) * 5;
                            $overall_band = $overall_band;
                        }


                    }
                }
                
            } 

            $btn_class_starter  = "-finished -has-score hide-percent  $overall_band visible"; 
            $data_pie = json_encode((object) [
                "percent"       => 100,
                "unit"          => $overall_band,
                "colorCircle"   => "#ECF8FA",
                "colorSlice"    => $bar_color
            ]);
        }
        $show_result = 1;
    } else {
        $btn_class_starter  = "-resume"; 
        $percent = "0"; 
    }      
} 


// --- CHANGED: ALL URLS BELOW ARE NOW DYNAMIC ---
$pie_key = '';
if (isset($session_category)) {
    if ($session_category == "course") {
        $pie_key = "-".$course_id;
    }
}

if ($display_attempt_btn == 1) {
    if (isset($session_category)) {
        if ($session_category == "course") {
            // This is a quiz within a proqyz-course
            $url_args = [
                'course_type'   => 'proqyz-course',
                'course_id'     => $course_id,
                'lesson_id'     => $lesson_id,
                'quiz_category' => $quiz_category,
                'quiz_slug'     => $quiz_slug,
                'quiz_id'       => $quiz_id,
                'action'        => 'start'
            ];
            
            # update - slug
            $quiz_attempt_url = st_proqyz_get_course_quiz_url($url_args);
        } else if ($session_category == "single") {
            // This is a standalone quiz
            # update - slug
            $quiz_attempt_url = st_proqyz_get_quiz_url($quiz_category, $quiz_slug, $quiz_id, 'start');
        }
    }
} else {
    $quiz_attempt_url = 'javascript:void(0);';
}

if ($show_result == 1 && isset($result_id)) {
    # update - slug
    $overall_url = st_proqyz_get_result_url($quiz_category, $quiz_slug, $quiz_id, $result_id);
} else {
    // Set a default empty value if there is no result to show
    $overall_url = 'javascript:void(0);'; 
}

// --- END OF CHANGES ---


ob_start(); ?>


<div data-p-preview="<?php echo $lesson_preview; ?>" data-p-access="<?php echo $course_access; ?>" class="practice-item <?php echo $color_class; ?> <?php echo $btn_class_starter; ?>">
    <span class="practice-item__icon <?php echo $category_class; ?>"></span>
    <h6 class="practice-item__title"><?php echo $quiz_title; ?></h6>
    <div data-target="<?php echo $pie_key; ?>" class="practice-item__progress circular-progress cc-circular-progress<?php echo $pie_key; ?> <?php echo $btn_class_starter; ?> " data-pie='<?php echo $data_pie; ?>' data-pie-index="<?php echo $tabindex; ?>" style="width:64px;height:64px;"></div>
    <?php if($btn_type == "take-test"){ ?>
        <a <?php echo ($display_attempt_btn == 1)? 'target="_blank"' : ''; ?> href="<?php echo esc_url($quiz_attempt_url); ?>" class="practice-item__btn iot-gribt <?php echo $color_class; ?> <?php echo $category_class; ?> <?php echo ($display_attempt_btn == 1)? '' : 'btn-locked'; ?>">
            <?php echo ($display_attempt_btn == 1)? 'Take Test' : 'Locked'; ?>
        </a>
    <?php } ?>
    <?php if($btn_type == "resume"){ ?>
        <a target="_blank" href="<?php echo esc_url($quiz_attempt_url); ?>" data-target="#modal-choose-mode" class="practice-item__btn iot-gribt <?php echo $color_class; ?> <?php echo $category_class; ?> -resume">
            Resume
        </a>
    <?php } ?>
    <?php if($btn_type == "retake"){ ?>
        <a target="_blank" href="<?php echo esc_url($quiz_attempt_url); ?>" class="practice-item__btn iot-gribt <?php echo $color_class; ?> <?php echo $category_class; ?> -restart">
            Restart
        </a>
    <?php } ?>
    <div class="practice-item__features">  
        <?php if($show_result == 1) { ?>
            <a href="<?php echo esc_url($overall_url); ?>" target="_blank" class="practice-item__feature-item -video-answer" title=""></a>
            <a href="<?php echo esc_url($overall_url); ?>" target="_blank" class="practice-item__feature-item -previous -active" title="Recent Result"></a>
        <?php } else { ?>
            <a href="javascript:void(0);" class="practice-item__feature-item -video-answer -inactive" title=""></a>
            
            <a href="javascript:void(0);" class="practice-item__feature-item -previous -inactive" title="0 Recent Result"></a>
        <?php } ?>
    </div>
</div>



<?php 
$quiz_attempt_url = "";
$quiz_html .= ob_get_clean();