<?php
    include("config.php");
	$my_campus = $_SESSION['campus'];

    // SELECT FROM usertype table
    $sql_usertype = "SELECT * FROM usertype WHERE Status='Active' AND `Campus`='$my_campus'";
        if($result_usertype = $db->query($sql_usertype)){
            if($result_usertype->num_rows > 0){
                while($row_usertype = $result_usertype->fetch_array()){
                    $dept_from_usertype = $row_usertype['Department/Office'];
                    
                    // 	SELECT FROM ass_monitoring table
                	$sql_monitoring = "SELECT * FROM ass_monitoring WHERE `Department/Office`='$dept_from_usertype' AND Campus='$my_campus'";
                    if($result_monitoring = $db->query($sql_monitoring)){
                        if($result_monitoring->num_rows > 0){
                            // UPDATE TO ZERO
                            $sql_update_zero = "UPDATE ass_monitoring Set `Total_Accomplished` = '0' WHERE Campus='$my_campus'"; 
					        $result_update_zero = mysqli_query($db,$sql_update_zero);
                        }
                        else{
                            $sql_insert = "INSERT INTO ass_monitoring (`Department/Office`, `Campus`, `Total_Accomplished`) VALUES ('$dept_from_usertype', '$my_campus', '0')";
                            $result_insert = $db->query($sql_insert);
                        }
                    }
                }
            }
        }
    
    // SELECT tasks table
	$sql_tasks = "SELECT * FROM tasks WHERE `File_Status`='approved' AND `Campus`='$my_campus'";
        if($result_tasks = $db->query($sql_tasks)){
            if($result_tasks->num_rows > 0){
                while($row_tasks = $result_tasks->fetch_array()){
                    $dept_from_tasks = $row_tasks['Department/Office'];
                    
                    // 	SELECT AGAIN FROM ass_monitoring table
                	$sql_monitoring2 = "SELECT * FROM ass_monitoring WHERE `Department/Office`='$dept_from_tasks' AND Campus='$my_campus'";
                    if($result_monitoring2 = $db->query($sql_monitoring2)){
                        while($row_monitoring2 = $result_monitoring2->fetch_array()){
                            $current_ass = $row_monitoring2['Total_Accomplished'];
                            
                        }
                    }
                        
                    // UPDATE ass_monitoring table
                    $ttl_ass = $current_ass + 1; 
                    $sql_update_monitoring = "UPDATE ass_monitoring Set `Total_Accomplished` = '$ttl_ass' WHERE `Department/Office`='$dept_from_tasks' AND Campus='$my_campus'"; 
					$result_update_monitoring = mysqli_query($db,$sql_update_monitoring);
                    
                }
            }
        }
?>