From d3685a9c2a3e94714fd478dcd62c6c6945377b78 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Fri, 14 Aug 2015 10:42:44 -0500 Subject: [PATCH] ticket: Fix new message alerts to random agent Previously, the Ticket::getLastRespondent() method would find an agent, seemingly at random. This was aggravated by a logic mismatch in the Ticket::onMessage() method which would send a message to the last respondent if the "Assigned Agent" were select in the configuration for the new message alert. --- include/class.orm.php | 2 +- include/class.ticket.php | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/class.orm.php b/include/class.orm.php index 36f666b5c..f961d3944 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -2324,7 +2324,7 @@ class MySqlCompiler extends SqlCompiler { */ function input($what, $slot=false, $model=false) { if ($what instanceof QuerySet) { - $q = $what->getQuery(array('nosort'=>true)); + $q = $what->getQuery(array('nosort'=>!($what->limit || $what->offset))); // Rewrite the parameter numbers so they fit the parameter numbers // of the current parameters of the $compiler $self = $this; diff --git a/include/class.ticket.php b/include/class.ticket.php index c97607c95..d2a85ae8c 100644 --- a/include/class.ticket.php +++ b/include/class.ticket.php @@ -708,15 +708,17 @@ implements RestrictedAccess, Threadable { function getLastRespondent() { if (!isset($this->lastrespondent)) { + if (!$this->thread || !$this->thread->entries) + return $this->lastrespondent = false; $this->lastrespondent = Staff::objects() ->filter(array( - 'staff_id' => static::objects() + 'staff_id' => $this->thread->entries ->filter(array( - 'thread__entries__type' => 'R', - 'thread__entries__staff_id__gt' => 0 + 'type' => 'R', + 'staff_id__gt' => 0, )) - ->values_flat('thread__entries__staff_id') - ->order_by('-thread__entries__id') + ->values_flat('staff_id') + ->order_by('-id') ->limit(1) )) ->first() @@ -2245,8 +2247,8 @@ implements RestrictedAccess, Threadable { // Build list of recipients and fire the alerts. $recipients = array(); //Last respondent. - if ($cfg->alertLastRespondentONNewMessage() || $cfg->alertAssignedONNewMessage()) - $recipients[] = $this->getLastRespondent(); + if ($cfg->alertLastRespondentONNewMessage() && ($lr = $this->getLastRespondent())) + $recipients[] = $lr; //Assigned staff if any...could be the last respondent if ($cfg->alertAssignedONNewMessage() && $this->isAssigned()) { -- GitLab