<?php

include ('includes/includes.php');

if(isset($_GET['token']) && !empty($_GET['token'])){
    
$token = $_GET['token'];
$current_session = get_current_session('1');

$q = "SELECT  `programme_id`,`programme_type_id`, `id`, `department_id`,  `course_id`, `seen_as_elective`, `session_id`,
			   `level`, `status`, `token`
			   FROM programmes_courses_nce 
			   WHERE token = '$token'
			   GROUP BY token
			   ";
			  $r = mysqli_query(dbc(),$q);
					
			  //if(!$r) { die(mysqli_error()); }
			
				  
				  if(mysqli_num_rows($r) > 0)
				  {
					  while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
					  {
					      $r1 = mysqli_query(dbc(),"SELECT id, title,type_id,department_comb  FROM programmes WHERE id = '". $row['programme_id'] ."'");
					      $row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC);
					      //echo [0];
					      $dep = explode(',', $row1['department_comb']);
					      //$dep[] = 64;
					      //$dep[] = 9;
					      for($i = 0; $i < count($dep); $i++){
					        
					        $r2 = mysqli_query(dbc(),"SELECT * FROM `courses` WHERE `id` >= 1583 AND `department_id` = ".$dep[$i]." AND `level` LIKE 'I'");
					        
					        if(mysqli_num_rows($r2) > 0 )
		                    {
					        while($row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC)){
					            
					            $cidd = $row2['id'];
					            
					            $r3 = mysqli_query(dbc(),"SELECT * FROM `programmes_courses_nce` WHERE `course_id` = $cidd AND `token` = '$token' ");
					            
					            if(mysqli_num_rows($r3) == 0 )
		                        {
					            
					            if($row2['course_type'] == 2){
					                
					                $status = $row['status'];
					                $cid = $row2['id'];
					                $school_id = get_school_id_by_programme($row['programme_id']);
					                $search_department = $row['department_id'];
					                $programme_type = $row['programme_type_id'];
					                $programme = $row['programme_id'];
					                $seen_as_elective = '1';
					                
					                $q = "INSERT INTO `programmes_courses_nce` (`id`,`school_id`,`department_id`, `programme_type_id`, `programme_id`, `course_id`, `level`, `seen_as_elective`, `session_id`,`date_added`, `added_by` , `status` , `token` ) 
				VALUES (NULL, '$school_id','$search_department','$programme_type', '$programme', '$cid', 'I', '$seen_as_elective', '$current_session', NOW(), 'ghost' ,'$status' ,'$token')";
							
							
		                        mysqli_query($dbc,$q);
					                
					            } else {
					                
					                $status = $row['status'];
					                $cid = $row2['id'];
					                $school_id = get_school_id_by_programme($row['programme_id']);
					                $search_department = $row['department_id'];
					                $programme_type = $row['programme_type_id'];
					                $programme = $row['programme_id'];
					                $seen_as_elective = '0';
					                
					                $q = "INSERT INTO `programmes_courses_nce` (`id`,`school_id`,`department_id`, `programme_type_id`, `programme_id`, `course_id`, `level`, `seen_as_elective`, `session_id`,`date_added`, `added_by` , `status` , `token` ) 
				VALUES (NULL, '$school_id','$search_department','$programme_type', '$programme', '$cid', 'I', '$seen_as_elective', '$current_session', NOW(), 'ghost' ,'$status' ,'$token')";
							
							
		                        mysqli_query($dbc,$q);
					                
					                
					                
					            }
		                        } else {
		                            echo $row2['title']." Course exist<br>";
		                        }
					            
					            
					        }
		                    } else {
		                        echo "No course found";
		                    }
					      }
					  }
				  } else {
				      echo "incorrect token";
				  }

} else {
    echo "Empty token";
}


?>