File: /var/www/html/labs/chatbot-v-4/classes/Master copy 3.php
<?php
require_once('../config.php');
Class Master extends DBConnection {
private $settings;
public function __construct(){
global $_settings;
$this->settings = $_settings;
parent::__construct();
}
public function __destruct(){
parent::__destruct();
}
public function save_response(){
extract($_POST);
if(!empty($id)){
$del = $this->conn->query("DELETE FROM `questions` where id= '$id' ");
if(!$del){
return 2;
exit;
}
}
$data = "";
$ins_resp = $this->conn->query("INSERT INTO `responses` set response_message = '{$response_message}' ");
if(!$ins_resp){
return 2;
exit;
}
$resp_id = $this->conn->insert_id;
foreach($question as $k => $v){
$data = " response_id = {$resp_id} ";
$data .= ", `question` = '$question[$k]' ";
$ins[] = $this->conn->query("INSERT INTO `questions` set $data ");
}
if(isset($ins) && count($ins) == count($question)){
$this->settings->set_flashdata("success"," Data successfully saved");
return 1;
}else{
return 2;
exit;
}
}
public function delete_response(){
extract($_POST);
$del = $this->conn->query("DELETE FROM `questions` where id = $id");
if($del){
$this->settings->set_flashdata("success"," Data successfully deleted");
return 1;
}else{
$this->conn->error;
}
}
public function get_response(){
extract($_POST);
$message = str_replace(array("?"), '', $message);
$chatId = str_replace(array("?"), '', $chatId);
$option_value = str_replace(array("?"), '', $option_value);
$preQuestionId = str_replace(array("?"), '', $preQuestionId);
$not_question = array("what", "what is","who","who is", "where");
if(in_array($message,$not_question)){
$resp['status'] = "success";
$resp['message'] = $this->settings->info('no_result');
return json_encode($resp);
exit;
}
$response = array();
if(strtoupper($message) == strtoupper("y0"))
{
$male_status = 'unchecked';
$female_status = 'unchecked';
$htmlQuestion1 = "";
$htmlQuestion1 .= "Do you want to report the issue anonymously? <br/>";
$htmlQuestion1 .= "<a class='btn btn-success cm-radio radio_send_chat' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('No', '".$chatId."')\" > No <a/>";
$htmlQuestion1 .= "<a class='btn btn-danger cm-radio radio_send_chat' data-id='Yes' href='javascript:void(0);' onclick=\"selectAnsRadio('Yes', '".$chatId."')\" > Yes <a/>";
$response['status'] = "success";
$response['message'] = $htmlQuestion1;
return json_encode($response);
}
//Get Answer
$sqlC = "SELECT * from `chat_history` where chatId = '".$chatId."'";
$qryC = $this->conn->query($sqlC);
$qArray = array();
$questionIdC = '';
$qchatCount = $qryC->num_rows;
if($qryC->num_rows > 0)
{
//$resultC = $qryC->fetch_array();
while($rowC = $qryC->fetch_assoc()){
$questionIdC = $rowC['questionId'];
$qArray[] = $questionIdC;
}
}
if(strtoupper($message) != strtoupper("Yes") && !$questionIdC)
{
$response['status'] = "success1";
$response['message'] = $Question;
return json_encode($response);
exit;
}
// $qArray = array("1000", "2000", "3000");
if($qchatCount > 0)
{
$sql = "SELECT * from `tbl_labs_question_answer` where questionId NOT IN (".implode(',',$qArray).")";
}
else
{
$sql = "SELECT * from `tbl_labs_question_answer` where questionId in ('1000') limit 0,1";
}
$qry = $this->conn->query($sql);
$QuestionId = '';
$question="";
if($qry->num_rows > 0)
{
$result = $qry->fetch_array();
$questionId = $result['questionId'];
$question = $result['question'];
$questiontype = $result['questiontype'];
//return json_encode($resp);
$cdate = date('Y-m-d',time());
$response['status'] = "success";
$response['questiontype'] = $questiontype;
$htmlQuestion1 = "";
$htmlQuestion1 = $question;
if(strtoupper($questiontype) == strtoupper('radio'))
{
$titleOption1 = $result['titleOption1'];
$valueOption1 = $result['valueOption1'];
$titleOption2 = $result['titleOption2'];
$valueOption2 = $result['valueOption2'];
$titleOption3 = $result['titleOption3'];
$valueOption3 = $result['valueOption3'];
$htmlQuestion1 .= "<br/>";
if($titleOption1)
{
$htmlQuestion1 .= "<a class='btn btn-success cm-radio radio_send_chat' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption1."', '".$chatId."')\" > ".$titleOption1." <a/>";
}
if($titleOption2)
{
$htmlQuestion1 .= "<a class='btn btn-danger cm-radio radio_send_chat' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption2."', '".$chatId."')\" > ".$titleOption2." <a/>";
}
if($titleOption3)
{
$htmlQuestion1 .= "<a class='btn btn-warning cm-radio radio_send_chat' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption3."', '".$chatId."')\" > ".$titleOption3." <a/>";
}
//$htmlQuestion1 .= "<a class='btn btn-danger cm-radio radio_send_chat' data-id='Yes' href='javascript:void(0);' onclick=\"selectAnsRadio('Yes', '".$chatId."')\" > Yes <a/>";
}
$response['message'] = $htmlQuestion1;
$response['questionId'] = $questionId;
$insertResponse = "";
$sqlC = "SELECT * from `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
$qryC = $this->conn->query($sqlC);
$qArray = array();
$questionIdC = '';
$qchatCount = $qryC->num_rows;
if(strtoupper($questiontype) == strtoupper('radio'))
{
$question = $option_value;
}
if($qchatCount > 0)
{
if($preQuestionId) {
$sqlChatInsert = "update `chat_history` set answer = '".$message."', updated_date = '".$cdate."' where chatId = '".$chatId."' and questionId = '".$questionId."'";
$qryChatInsert = $this->conn->query($sqlChatInsert);
if($qryChatInsert)
{
$insertResponse = "updated";
}
}
}
else
{
$sqlChatInsert = "insert into `chat_history` (chatId, questionId, question, answer, create_date, updated_date) values ('".$chatId."', '".$questionId."', '".$question."', '".$message."', '".$cdate."', '".$cdate."')";
$qryChatInsert = $this->conn->query($sqlChatInsert);
if($qryChatInsert)
{
$insertResponse = "inserted";
}
}
$response['histroy'] = $insertResponse;
return json_encode($response);
}
if(strtoupper($message) == strtoupper("1000"))
{
// $response['status'] = "success";
// $response['message'] = $Question;
// return json_encode($response);
}
// if(strtoupper($message) == strtoupper("Yes"))
// {
// $resp = '{"status":"success","message":"Please share your love name? <a href=\'good\'>Good</a>"}';
// $resp = json_decode($resp);
// return json_encode($resp);
// }
if(strtoupper($message) == strtoupper("Yes"))
{
$male_status = 'unchecked';
$female_status = 'unchecked';
$htmlQuestion1 = "";
$htmlQuestion1 .= "Do you want to report the issue anonymously? <br/>";
$htmlQuestion1 .= "<Input type = 'Radio' class='' Name ='anonymously' value= 'No' $male_status onclick=\"selectAns('No')\" > No <br/>";
$htmlQuestion1 .= "<Input type = 'Radio' Name ='anonymously' value= 'Yes' $male_status onclick=\"selectAns('Yes')\"> Yes <br/>";
$response['status'] = "success";
$response['message'] = $htmlQuestion1;
return json_encode($response);
}
if($message == "live")
{
$htmlQuestion1 = "";
$htmlQuestion1 .= "Do you want to live ? <br/>";
$htmlQuestion1 .= "<Input type = 'Radio' Name ='anonymously' value= 'Noida' $male_status onclick=\"selectAns('Noida')\" > Noida <br/>";
$htmlQuestion1 .= "<Input type = 'Radio' Name ='anonymously' value= 'Delhi' $male_status onclick=\"selectAns('Delhi')\"> Delhi <br/>";
$response['status'] = "success";
$response['message'] = $htmlQuestion1;
return json_encode($response);
}
$sql = "SELECT r.response_message,q.id from `questions` q inner join `responses` r on q.response_id = r.id where q.question Like '%{$message}%' ";
$qry = $this->conn->query($sql);
if($qry->num_rows > 0){
$result = $qry->fetch_array();
// var_dump($result);
$resp['status'] = "success";
$resp['message'] = $result['response_message'];
$resp['sql'] = $sql;
//$this->conn->query("INSERT INTO `frequent_asks` set question_id = '{$result['id']}' ");
$resp = '{"status":"success","message":"Please share your name?"}';
$resp = json_decode($resp);
return json_encode($resp);
}else{
$resp['status'] = "success";
$resp['message'] = $this->settings->info('no_result');
$chk = $this->conn->query("SELECT * FROM `unanswered` where `question` = '{$message}' ");
// if($chk->num_rows > 0){
// $this->conn->query("UPDATE `unanswered` set no_asks = no_asks + 1 ");
// }else{
// $this->conn->query("INSERT INTO `unanswered` set question = '{$message}' ");
// }
$resp = '{"status":"success","message":"Please share your name?"}';
$resp = json_decode($resp);
return json_encode($resp);
}
}
public function delete_unanswer(){
extract($_POST);
$del = $this->conn->query("DELETE FROM `unanswered` where id = $id");
if($del){
$this->settings->set_flashdata("success"," Data successfully deleted");
return 1;
}else{
$this->conn->error;
}
}
}
$Master = new Master();
$action = !isset($_GET['f']) ? 'none' : strtolower($_GET['f']);
$sysset = new SystemSettings();
switch ($action) {
case 'save_response':
echo $Master->save_response();
break;
case 'delete_response':
echo $Master->delete_response();
break;
case 'get_response':
echo $Master->get_response();
break;
case 'delete_unanswer':
echo $Master->delete_unanswer();
break;
default:
// echo $sysset->index();
break;
}