diff --git a/api/polling/universitysync.php b/api/polling/universitysync.php index 18b1655a5c1c4bd723c0d6c42c0bb309a8a43c84..d5ccb9a4016f5331f9c287d31a21ed25430e152a 100644 --- a/api/polling/universitysync.php +++ b/api/polling/universitysync.php @@ -10,30 +10,61 @@ $response = $soap->doRequest("getAllUniversities", null); $db = new Database(); -if(is_array($response->return)){ - for($i = 0; $i < count($response->return) ; $i++){ - $uni_name = $response->return[$i]->name; - $phpUniId = $response->return[$i]->phpUniId; - $restUniId = $response->return[$i]->restUniId; +if ($response->return != null) { + if (is_array($response->return)) { + for ($i = 0; $i < count($response->return); $i++) { + $uni_name = $response->return[$i]->name; + $phpUniId = $response->return[$i]->phpUniId; + $restUniId = $response->return[$i]->restUniId; - if($phpUniId == -1){ + if ($phpUniId == -1) { + $query = "SELECT * FROM university WHERE name = ?"; + $stmt = $db->setSTMT($query); + mysqli_stmt_bind_param($stmt, "s", $uni_name); + $exec = mysqli_stmt_execute($stmt); + /* If there is this instance do a soap request*/ + if ($exec) { + $res = mysqli_stmt_get_result($stmt); + $row = mysqli_fetch_array($res); + $params = array("php_uni_id" => $row['university_id'], "rest_uni_id" => $restUniId); + $response = $soap->doRequest("setPHPId", $params); + } else { + /* Create the instance in PHP*/ + $query = "INSERT INTO university (name) VALUES ?"; + $stmt = $db->setSTMT($query); + mysqli_stmt_bind_param($stmt, "s", $uni_name); + $exec = mysqli_stmt_execute($stmt); + if ($exec) { + $id = mysqli_insert_id($db->getDatabase()); + $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); + $response = $soap->doRequest("setPHPId", $params); + } + } + } + } + } else { + $uni_name = $response->return->name; + $phpUniId = $response->return->phpUniId; + $restUniId = $response->return->restUniId; + + if ($phpUniId == -1) { $query = "SELECT * FROM university WHERE name = ?"; $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt,"s", $uni_name); + mysqli_stmt_bind_param($stmt, "s", $uni_name); $exec = mysqli_stmt_execute($stmt); /* If there is this instance do a soap request*/ - if($exec){ + if ($exec) { $res = mysqli_stmt_get_result($stmt); $row = mysqli_fetch_array($res); - $params = array("php_uni_id" => $row['university_id'], "rest_uni_id" => $restUniId); + $params = array("php_uni_id" => $row["university_id"], "rest_uni_id" => $restUniId); $response = $soap->doRequest("setPHPId", $params); - }else{ + } else { /* Create the instance in PHP*/ $query = "INSERT INTO university (name) VALUES ?"; $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt,"s", $uni_name); + mysqli_stmt_bind_param($stmt, "s", $uni_name); $exec = mysqli_stmt_execute($stmt); - if($exec){ + if ($exec) { $id = mysqli_insert_id($db->getDatabase()); $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); $response = $soap->doRequest("setPHPId", $params); @@ -41,35 +72,4 @@ if(is_array($response->return)){ } } } -}else{ - $uni_name = $response->return->name; - $phpUniId = $response->return->phpUniId; - $restUniId = $response->return->restUniId; - - if($phpUniId == -1){ - $query = "SELECT * FROM university WHERE name = ?"; - $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt,"s", $uni_name); - $exec = mysqli_stmt_execute($stmt); - /* If there is this instance do a soap request*/ - if($exec){ - $res = mysqli_stmt_get_result($stmt); - $row = mysqli_fetch_array($res); - $params = array("php_uni_id" => $row["university_id"], "rest_uni_id" => $restUniId); - $response = $soap->doRequest("setPHPId", $params); - }else{ - /* Create the instance in PHP*/ - $query = "INSERT INTO university (name) VALUES ?"; - $stmt = $db->setSTMT($query); - mysqli_stmt_bind_param($stmt,"s", $uni_name); - $exec = mysqli_stmt_execute($stmt); - if($exec){ - $id = mysqli_insert_id($db->getDatabase()); - $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); - $response = $soap->doRequest("setPHPId", $params); - } - } - } } - -?> \ No newline at end of file