<?php
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT.'_config/dbConfig.php');


if ($_GET['returnSID']) {
	
	$expCode = explode("_",$_GET['returnSID']);
	if (!is_numeric($expCode[1])) { echo "Wrong ID!"; exit; } 
	
	
	$db->ChangeDatabase(DB_WARPIT_USERSPOOL);

	$sql = "SELECT endLinkId,returnSID,id_project,id_user,int_finish FROM _RedirectEndLink WHERE endLinkId = ".$expCode[1];
	//echo $sql;
	$result = $db->SQLexecute($sql);
	$zad = $db->fetchAssoc($result);
	
	$id_project = $zad['id_project'];
	$user_id = $zad['id_user'];
	
	if ($zad['returnSID'] != $_GET['returnSID']) { echo "Wrong ID!"; exit; } 
	
	if($zad['int_finish']) { echo "Survey already finished!"; exit; }
	
	$sql = "UPDATE _RedirectEndLink  SET int_finish = ".$_GET['qstatus'].",endTime=NOW() 
				WHERE endLinkId = ".$expCode[1];
	
	$db->SQLexecute($sql);
	
	
	/*we don't need to update our tables anymore!
	mysql_select_db("online1_Rm_cati_base");
	
	$sql = "SELECT proj_name FROM projects WHERE id = ".$id_project;
	$result = mysql_query($sql,$SQLconnect);
	$proj = mysql_fetch_assoc($result);
	
	
	$sql = "INSERT INTO ".$proj['proj_name']."_structure SET user_id = ".$user_id.",
				flag_bad=1,t_start=1,t_stop=2,t_day=1,t_month=1,t_year=1,t_hour=1,t_min=1,qstatus=".$_GET['qstatus'];
	mysql_query($sql,$SQLconnect);
	// echo $sql;
	$sql = "UPDATE www_interview_statistic SET 
					end_date 	= NOW(),
					end_tstamp 	= '".time()."',
					status		= 'F' 
			WHERE  	user_id = ".$user_id." AND questionaire_id = ".$id_project;
	
	mysql_query($sql,$SQLconnect);
	
	mysql_select_db("online1_WEBuserspool");
	*/
	
	
	$sql = "SELECT link,webPoints FROM _RedirectStatus 
				WHERE id_status = ".$_GET['qstatus']." AND id_project = ".$id_project;
	$result = $db->SQLexecute($sql);
	$zadetek = $db->fetchAssoc($result);
	
	if($points = $zadetek['webPoints'])
	{
		$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
		$sql = "INSERT INTO _wwwPoints (id_project,id_user,pointType,webPoints,dateUsed) VALUES ($id_project,$user_id,2,$points,NOW() )";
		$db->SQLexecute($sql);
	}
	header("Location: ".$zadetek['link']);
	
} else echo "No ID!!!"



?>
