<?php 
// error_reporting(E_ALL); ini_set('display_errors', '1');
// require_once('../Connections/dbConn.php'); 
require_once('../Connections/oprmConn.php');

//get users list
mysql_select_db($database_oprmConn, $oprmConn);

header('Content-type: text/csv');
Header("Content-Disposition: attachment; filename=vtek-users-download.csv");

header('Pragma: no-cache');
header('Expires: 0');

echo 'Subject,Resource,Project,Start Date,Status';


if( isset($_POST['created_by']) && $_POST['created_by'] == '' ){
	$query_gettasklist = "SELECT * FROM weby_tasks WHERE status != 'D' order by start_date desc";
} 
else { 
	$ruid = $_POST['created_by'];
	$query_gettasklist = "SELECT * FROM weby_tasks WHERE status != 'D' AND resource_id = '$ruid' order by start_date desc";
}
$gettasklist = mysql_query($query_gettasklist, $oprmConn) or die(mysql_error());
$row_gettasklist = mysql_fetch_assoc($gettasklist);
$totalRows_gettasklist = mysql_num_rows($gettasklist);

if($totalRows_gettasklist > 0){
	do{
		echo PHP_EOL;
		echo str_replace(',', ' ', $row_gettasklist['subject']);
		echo ',';

		$query_getresource = "SELECT weby_profiles.full_name FROM weby_users, weby_profiles WHERE weby_profiles.user_id = weby_users.user_id AND weby_users.status = 'A' AND weby_profiles.status = 'A' AND weby_users.user_role != '1' AND weby_profiles.user_id = '$row_gettasklist[resource_id]'";
		$getresource = mysql_query($query_getresource, $oprmConn) or die(mysql_error());
		$row_getresource = mysql_fetch_assoc($getresource);
		$totalRows_getresource = mysql_num_rows($getresource);
		echo $row_getresource['full_name']; 
		echo ',';

		$query_getProject = "SELECT * FROM weby_projects WHERE weby_projects.projetc_id = '$row_gettasklist[project_id]'";
		$getProject  = mysql_query($query_getProject , $oprmConn) or die(mysql_error());
		$row_getProject  = mysql_fetch_assoc($getProject );
		$totalRows_getProject  = mysql_num_rows($getProject );
		echo $row_getProject['project_name'];
		echo ',';

		echo date("jS M Y", strtotime($row_gettasklist['start_date']));
		echo ',';

		$query_getStatus = "SELECT * FROM weby_tasks_status WHERE weby_tasks_status_key = '$row_gettasklist[status]'";
		$getStatus = mysql_query($query_getStatus, $oprmConn) or die(mysql_error());
		$row_getStatus = mysql_fetch_assoc($getStatus);
		$totalRows_ggetStatus = mysql_num_rows($getStatus);
		echo $row_getStatus['weby_tasks_status'];

	}while($row_gettasklist = mysql_fetch_assoc($gettasklist));
}