Commit c51f8d51 authored by Marco Boretto's avatar Marco Boretto

Merge pull request #2 from noplanman/conversations_typos

Correct typos and minor code styling.
parents 4df1b58d e02f1040
......@@ -49,7 +49,7 @@ class Conversation
protected $user_id;
/**
* Telegram chat_id
* Telegram chat id
*
* @var int
*/
......@@ -59,23 +59,23 @@ class Conversation
* Group name let you share the session among commands
* Call this as the same name of the command if you don't need to share the conversation
*
* @var strint
* @var string
*/
protected $group_name;
/**
* Command to be execute if the conversation is active
* Command to be executed if the conversation is active
*
* @var string
*/
protected $command;
/**
* Conversation contructor initialize a new conversation
* Conversation contructor to initialize a new conversation
*
* @param int $user_id
* @param int $chat_id
* @param string $name
* @param string $group_name
* @param string $command
*/
public function __construct($user_id, $chat_id, $group_name = null, $command = null)
......@@ -91,7 +91,7 @@ class Conversation
}
/**
* Check if the conversation already exist
* Check if the conversation already exists
*
* @return bool
*/
......@@ -101,7 +101,7 @@ class Conversation
if ($this->is_fetched) {
return true;
}
//select an active conversation
//Select an active conversation
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
$this->is_fetched = true;
......@@ -114,13 +114,13 @@ class Conversation
return true;
}
//a track with the same name was already opened, store the data inside the class
//A conversation with the same name was already opened, store the data inside the class
if ($this->conversation['conversation_name'] == $this->group_name) {
$this->data = json_decode($this->conversation['data'], true);
return true;
}
//a conversation with a differet name has been opened unsetting the DB one and reacreatea a new one
//A conversation with a different name has been opened, unset the DB one and recreate a new one
ConversationDB::updateConversation(['status' => 'cancelled'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
return false;
}
......@@ -130,9 +130,7 @@ class Conversation
}
/**
* Check if the Conversation already exist
*
* Check if a conversation has already been created in the database. If the conversation is not found, a new conversation is created. start fetch the data stored in the database
* Check if a conversation has already been created in the database. If the conversation is not found, a new conversation is created. Start fetches the data stored in the database.
*
* @return bool
*/
......@@ -148,11 +146,13 @@ class Conversation
/**
* Store the array/variable in the database with json_encode() function
*
* @todo Verify the query before assigning the $data member variable
*
* @param array $data
*/
public function update($data)
{
//conversation must exist!
//Conversation must exist!
if ($this->conversationExist()) {
$fields['data'] = json_encode($data);
......@@ -165,11 +165,9 @@ class Conversation
/**
* Delete the conversation from the database
*
* Currently the Convevrsation is not deleted but just unsetted
* Currently the Conversation is not deleted but just set to 'stopped'
*
* @TODO should return something
*
* @param array $data
* @todo should return something
*/
public function stop()
{
......@@ -181,7 +179,7 @@ class Conversation
/**
* Retrieve the command to execute from the conversation
*
* @param string
* @return string|null
*/
public function getConversationCommand()
{
......@@ -192,9 +190,9 @@ class Conversation
}
/**
* Retrive the data store in the conversation
* Retrieve the data stored in the conversation
*
* @param array $data
* @return array
*/
public function getData()
{
......
......@@ -26,31 +26,31 @@ class ConversationDB extends DB
define('TB_CONVERSATION', self::$table_prefix . 'conversation');
}
}
/**
* Conversation contructor initialize a new conversation
* Select a conversation from the DB
*
* @param int $user_id
* @param int $chat_id
* @param bool $limit
*
* @return array
* @return array|bool
*/
public static function selectConversation($user_id, $chat_id, $limit = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
$query .= 'WHERE `status` = :status ';
$query .= 'AND `chat_id` = :chat_id ';
$query .= 'AND `user_id` = :user_id ';
$tokens = [':chat_id' => $chat_id, ':user_id' => $user_id];
if (!is_null($limit)) {
$query .=' LIMIT :limit';
$query .= ' LIMIT :limit';
}
$sth = self::$pdo->prepare($query);
......@@ -73,7 +73,7 @@ class ConversationDB extends DB
* Insert the conversation in the database
*
* @param string $conversation_command
* @param string $conversation_name
* @param string $conversation_group_name
* @param int $user_id
* @param int $chat_id
*
......@@ -127,13 +127,13 @@ class ConversationDB extends DB
}
/**
* Insert the conversation in the database
* Update the conversation in the database
*
* @param string $table
* @param array $fields_values
* @param array $where_fields_values
* @param string $table
* @param array $fields_values
* @param array $where_fields_values
*
* @todo this function is generic should be moved in DB.php
* @todo This function is generic should be moved in DB.php
*
* @return bool
*/
......@@ -156,8 +156,8 @@ class ConversationDB extends DB
}
++$a;
++$tokens_counter;
$update .= '`'.$field.'` = :'.$tokens_counter;
$tokens[':'.$tokens_counter] = $value;
$update .= '`' . $field . '` = :' . $tokens_counter;
$tokens[':' . $tokens_counter] = $value;
}
//Where
......@@ -165,17 +165,17 @@ class ConversationDB extends DB
$where = '';
foreach ($where_fields_values as $field => $value) {
if ($a) {
$where .= ' AND ';
$where .= ' AND ';
} else {
++$a;
$where .= 'WHERE ';
$where .= 'WHERE ';
}
++$tokens_counter;
$where .= '`'.$field .'`= :'.$tokens_counter ;
$tokens[':'.$tokens_counter] = $value;
$where .= '`' . $field .'`= :' . $tokens_counter ;
$tokens[':' . $tokens_counter] = $value;
}
$query = 'UPDATE `'.$table.'` SET '.$update.' '.$where;
$query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
try {
$sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment