Rewrite Thread::take method to simplify it

This commit is contained in:
Dmitriy Simushev 2015-03-27 12:29:54 +00:00
parent 0075e2169c
commit 70f630d125

View File

@ -986,70 +986,71 @@ class Thread
*/ */
public function take($operator) public function take($operator)
{ {
$take_thread = false; // There are states which forbids thread taking. Make sure the current
// state is not one of them.
$forbidden_states = array(
self::STATE_CLOSED,
self::STATE_LEFT,
self::STATE_INVITED,
);
if (in_array($this->state, $forbidden_states)) {
return false;
}
$is_operator_changed = ($operator['operatorid'] != $this->agentId)
// Only these states allow operators changing. In other states
// changing of operator's ID is treated in different ways (join or
// come back).
&& ($this->state == self::STATE_WAITING || $this->state == self::STATE_CHATTING);
// For these states we assume that the thread has no operator yet. The
// check for operator changing is skipped because it will always
// return positive result.
$is_operator_joined = ($this->state == self::STATE_LOADING)
|| ($this->state == self::STATE_QUEUE);
$is_operator_back = ($this->state == self::STATE_WAITING)
&& ($operator['operatorid'] == $this->agentId);
$message = ''; $message = '';
$operator_name = ($this->locale == get_home_locale()) $operator_name = ($this->locale == get_home_locale())
? $operator['vclocalename'] ? $operator['vclocalename']
: $operator['vccommonname']; : $operator['vccommonname'];
$no_operator_in_chat = self::STATE_QUEUE if ($is_operator_changed) {
|| self::STATE_WAITING $message = getlocal(
|| self::STATE_LOADING; "Operator <strong>{0}</strong> changed operator <strong>{1}</strong>",
array($operator_name, $this->agentName),
if ($no_operator_in_chat) { $this->locale,
// User waiting true
$take_thread = true; );
if ($this->state == self::STATE_WAITING) { } elseif ($is_operator_joined) {
if ($operator['operatorid'] != $this->agentId) { $message = getlocal(
$message = getlocal( "Operator {0} joined the chat",
"Operator <strong>{0}</strong> changed operator <strong>{1}</strong>", array($operator_name),
array($operator_name, $this->agentName), $this->locale,
$this->locale, true
true );
); } elseif ($is_operator_back) {
} else { $message = getlocal(
$message = getlocal( "Operator {0} is back",
"Operator {0} is back", array($operator_name),
array($operator_name), $this->locale,
$this->locale, true
true );
);
}
} else {
$message = getlocal(
"Operator {0} joined the chat",
array($operator_name),
$this->locale,
true
);
}
} elseif ($this->state == self::STATE_CHATTING) {
// User chatting
if ($operator['operatorid'] != $this->agentId) {
$take_thread = true;
$message = getlocal(
"Operator <strong>{0}</strong> changed operator <strong>{1}</strong>",
array($operator_name, $this->agentName),
$this->locale,
true
);
}
} else {
// Thread closed
return false;
} }
// Change operator and update chat info // Make sure the thread have correct operator and state.
if ($take_thread) { $this->state = self::STATE_CHATTING;
$this->state = self::STATE_CHATTING; $this->nextAgent = 0;
$this->nextAgent = 0; $this->agentId = $operator['operatorid'];
$this->agentId = $operator['operatorid']; $this->agentName = $operator_name;
$this->agentName = $operator_name; if (empty($this->chatStarted)) {
if (empty($this->chatStarted)) { // This is needed only if the chat was not started yet. Such threads
$this->chatStarted = time(); // should originally belong to STATE_QUEUE or STATE_LOADING.
} $this->chatStarted = time();
$this->save();
} }
$this->save();
// Send message // Send message
if ($message) { if ($message) {