<? include ("../settings.php"); ?>
<? include ("session.php"); ?>
<?

    if (isset($_REQUEST)){
        if ($_SESSION['user_admin']=="1"){
            
            $update = array(
				"registrant_salutation" => $_REQUEST['registrant_salutation'],
                "registrant_lastname" => $_REQUEST['registrant_lastname'],
                "registrant_firstname" => $_REQUEST['registrant_firstname'],
                "registrant_organisation" => $_REQUEST['registrant_organisation'],
				"registrant_designation" => $_REQUEST['registrant_designation'],
                "registrant_email" => $_REQUEST['registrant_email'],
                "registrant_mailing1" => $_REQUEST['registrant_mailing1'],
				"registrant_mailing2" => $_REQUEST['registrant_mailing2'],
                "registrant_postal" => $_REQUEST['registrant_postal'],
                "registrant_city" => $_REQUEST['registrant_city'],
				"registrant_state" => $_REQUEST['registrant_state'],
                "registrant_country" => $_REQUEST['registrant_country'],
                "registrant_officecc" => $_REQUEST['registrant_officecc'],
				"registrant_office" => $_REQUEST['registrant_office'],
				"registrant_mobilecc" => $_REQUEST['registrant_mobilecc'],
				"registrant_mobile" => $_REQUEST['registrant_mobile'],
                "abstract_title" => $_REQUEST['abstract_title'],
                "abstract_op" => $_REQUEST['abstract_op'],
                "topic_id"=> $_REQUEST['topic_id'],
				"abstract_status"=> $_REQUEST['abstract_status_id']
            );
            $bind = array(
                ":search1" => $_REQUEST['abstract_id']
            );
            $db->update("tbl_abstract", $update, "abstract_id = :search1", $bind);
            
            // Update Abstract Number
			if ($_REQUEST['abstract_op']=="P"){
				$paper_id = "P-".str_pad($_REQUEST['topic_id'], 2, "0", STR_PAD_LEFT)."-".str_pad($_REQUEST['abstract_id'], 4, "0", STR_PAD_LEFT);
			}else{
				$paper_id = "O-".str_pad($_REQUEST['topic_id'], 2, "0", STR_PAD_LEFT)."-".str_pad($_REQUEST['abstract_id'], 4, "0", STR_PAD_LEFT);
			}
            $update = array(
				"abstract_number" => $paper_id
			);
			$db->update("tbl_abstract", $update, "abstract_id = :search1", $bind);
			
			// Update Poster/Oral and Topic
            $update = array(
                "abstract_op" => $_REQUEST['abstract_op'],
                "topic_id" => $_REQUEST['topic_id']
            );
            $bind = array(
                ":search1" => $_REQUEST['abstract_id']
            );
            $db->update("tbl_abstract", $update, "abstract_id = :search1", $bind);
            // Update Log
            $insert = array(
                "abstract_id" => $_REQUEST['abstract_id'],
                "action_by" => $_SESSION['user_id'],
                "log_action" => "abstract (".serialize($update).") and topic_id(".$_REQUEST['topic_id'].")"
            );
            $db->insert("tbl_abstract_log", $insert);
            $topic_id = str_pad($_REQUEST['topic_id'],2,"0",STR_PAD_LEFT);
        }else{
			// Search if reviewer has inserted
			$bind = array(":search1"=>$_SESSION['user_id'],":search2"=>$_REQUEST['abstract_id']);
			$exists = $db->select("tbl_abstract_remarks","user_id = :search1 AND abstract_id = :search2",$bind);
			if (count($exists)>0){
				$update = array(
					"score1"=>$_REQUEST['score1'],
					"score2"=>$_REQUEST['score2'],
					"score3"=>$_REQUEST['score3'],
					"score4"=>$_REQUEST['score4'],
					"score5"=>$_REQUEST['score5'],
					"score6"=>$_REQUEST['score6'],
					"recommendation"=>$_REQUEST['recommendation'],
					"remarks"=>$_REQUEST['remarks']
				);
				$db->update("tbl_abstract_remarks", $update, "user_id = :search1 AND abstract_id = :search2", $bind);
			}else{
				$insert = array(
					"abstract_id"=>$_REQUEST['abstract_id'],
					"user_id"=>$_SESSION['user_id'],
					"score1"=>$_REQUEST['score1'],
					"score2"=>$_REQUEST['score2'],
					"score3"=>$_REQUEST['score3'],
					"score4"=>$_REQUEST['score4'],
					"score5"=>$_REQUEST['score5'],
					"score6"=>$_REQUEST['score6'],
					"recommendation"=>$_REQUEST['recommendation'],
					"remarks"=>$_REQUEST['remarks']
				);
				$db->insert("tbl_abstract_remarks",$insert);
			}
			
			// Update Log
			$insert = array(
				"abstract_id" => $_REQUEST['abstract_id'],
				"action_by" => $_SESSION['user_id'],
				"log_action" => "Reviewer => ".serialize($update).serialize($insert)
			);
			$db->insert("tbl_abstract_log", $insert);     
		}
        
        echo "1";
    }

?>