<?php
include '../includes/connection.php';

$progress = intval($_POST['progress']);
$user_id = intval($_POST['user_id']);
$course_id = intval($_POST['course_id']);
$sub_course_id = intval($_POST['sub_course_id']);



$exists = mysqli_query($conn, "SELECT * FROM user_progress WHERE student_id=$user_id AND subject_id=$course_id AND module_id=$sub_course_id");

if (mysqli_num_rows($exists) > 0) {
    $quiz = mysqli_query($conn,"SELECT SUM(marks) AS T_MARKS FROM `quiz_subject_question_answer` WHERE student_id=$user_id AND subject_id=$course_id AND module_id=$sub_course_id");
    $quiz_r = mysqli_fetch_array($quiz); 
    $q_marks=$quiz_r['T_MARKS'];
    mysqli_query($conn, "UPDATE user_progress SET progress_percent=$progress,q_marks=$q_marks WHERE student_id=$user_id AND subject_id=$course_id AND module_id=$sub_course_id");
} else {
    mysqli_query($conn, "INSERT INTO `user_progress`(`student_id`, `subject_id`, `module_id`, `progress_percent`) VALUES($user_id, $course_id, $sub_course_id, $progress)");
}
?>