| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Activity; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* ActivityLogger class for Hooks |
| 10 |
* |
| 11 |
* @package FluentSupport\App\Hooks\Handlers |
| 12 |
* |
| 13 |
* @version 1.0.0 |
| 14 |
*/ |
| 15 |
|
| 16 |
class ActivityLogger |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* init method will register all action hooks related to ticket creation, ticket response, response by agent, note by agent, ticket closed or reopen |
| 21 |
*/ |
| 22 |
public function init() |
| 23 |
{ |
| 24 |
// Check if activity logs are disabled - if so, don't register any hooks |
| 25 |
if (Helper::areLogsDisabled('_activity_settings')) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
// Ticket Related activities |
| 30 |
add_action('fluent_support/ticket_created', function ($ticket, $customer) { |
| 31 |
|
| 32 |
$customer->last_response_at = current_time('mysql'); |
| 33 |
$customer->save(); |
| 34 |
|
| 35 |
$agent = Helper::getAgentByUserId(); |
| 36 |
|
| 37 |
if ($agent) { |
| 38 |
$description = sprintf( |
| 39 |
'%1$s created a %2$s on behalf of %3$s', |
| 40 |
$this->getPersonMarkup($agent), |
| 41 |
$this->getTicketMarkup($ticket), |
| 42 |
$this->getPersonMarkup($customer) |
| 43 |
); |
| 44 |
$personId = $agent->id; |
| 45 |
$personType = $agent->person_type; |
| 46 |
} else { |
| 47 |
$description = sprintf('%1$s created a %2$s via %3$s', $this->getPersonMarkup($customer), $this->getTicketMarkup($ticket), $ticket->source); |
| 48 |
$personId = $customer->id; |
| 49 |
$personType = $customer->person_type; |
| 50 |
} |
| 51 |
|
| 52 |
$log = [ |
| 53 |
'event_type' => 'fluent_support/ticket_created', |
| 54 |
'person_id' => $personId, |
| 55 |
'person_type' => $personType, |
| 56 |
'object_id' => $ticket->id, |
| 57 |
'object_type' => 'ticket', |
| 58 |
'description' => $description |
| 59 |
]; |
| 60 |
|
| 61 |
Activity::create($log); |
| 62 |
}, 20, 2); |
| 63 |
|
| 64 |
//Response added by customer in a ticket |
| 65 |
add_action('fluent_support/response_added_by_customer', function ($response, $ticket, $person) { |
| 66 |
|
| 67 |
$person->last_response_at = current_time('mysql'); |
| 68 |
$person->save(); |
| 69 |
|
| 70 |
$description = sprintf('Customer %1$s added a response on %2$s via %3$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket), $response->source); |
| 71 |
|
| 72 |
$log = [ |
| 73 |
'event_type' => 'fluent_support/response_added_by_customer', |
| 74 |
'person_id' => $person->id, |
| 75 |
'person_type' => $person->person_type, |
| 76 |
'object_id' => $ticket->id, |
| 77 |
'object_type' => 'ticket', |
| 78 |
'description' => $description |
| 79 |
]; |
| 80 |
|
| 81 |
Activity::create($log); |
| 82 |
}, 20, 3); |
| 83 |
|
| 84 |
//Response added by agent in a ticket |
| 85 |
add_action('fluent_support/response_added_by_agent', function ($response, $ticket, $person) { |
| 86 |
$description = sprintf('%1$s added a response on %2$s via %3$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket), $response->source); |
| 87 |
|
| 88 |
$log = [ |
| 89 |
'event_type' => 'fluent_support/response_added_by_agent', |
| 90 |
'person_id' => $person->id, |
| 91 |
'person_type' => $person->person_type, |
| 92 |
'object_id' => $ticket->id, |
| 93 |
'object_type' => 'ticket', |
| 94 |
'description' => $description |
| 95 |
]; |
| 96 |
|
| 97 |
Activity::create($log); |
| 98 |
}, 20, 3); |
| 99 |
|
| 100 |
//Note added by agent to a ticket |
| 101 |
add_action('fluent_support/note_added_by_agent', function ($response, $ticket, $person) { |
| 102 |
$description = sprintf('%1$s added a note on %2$s via %3$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket), $response->source); |
| 103 |
|
| 104 |
$log = [ |
| 105 |
'event_type' => 'fluent_support/note_added_by_agent', |
| 106 |
'person_id' => $person->id, |
| 107 |
'person_type' => $person->person_type, |
| 108 |
'object_id' => $ticket->id, |
| 109 |
'object_type' => 'ticket', |
| 110 |
'description' => $description |
| 111 |
]; |
| 112 |
|
| 113 |
Activity::create($log); |
| 114 |
}, 20, 3); |
| 115 |
|
| 116 |
//Ticket closed by customer or agent |
| 117 |
add_action('fluent_support/ticket_closed', function ($ticket, $person) { |
| 118 |
|
| 119 |
if ($person->person_type == 'customer') { |
| 120 |
$person->last_response_at = current_time('mysql'); |
| 121 |
$person->save(); |
| 122 |
} |
| 123 |
|
| 124 |
$description = sprintf('%1$s closed %2$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket)); |
| 125 |
|
| 126 |
$log = [ |
| 127 |
'event_type' => 'fluent_support/ticket_closed', |
| 128 |
'person_id' => $person->id, |
| 129 |
'person_type' => $person->person_type, |
| 130 |
'object_id' => $ticket->id, |
| 131 |
'object_type' => 'ticket', |
| 132 |
'description' => $description |
| 133 |
]; |
| 134 |
|
| 135 |
Activity::create($log); |
| 136 |
}, 20, 2); |
| 137 |
|
| 138 |
//Ticket reopen by customer or agent. |
| 139 |
add_action('fluent_support/ticket_reopen', function ($ticket, $person) { |
| 140 |
|
| 141 |
if ($person->person_type == 'customer') { |
| 142 |
$person->last_response_at = current_time('mysql'); |
| 143 |
$person->save(); |
| 144 |
} |
| 145 |
|
| 146 |
$description = sprintf('%1$s reopened on %2$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket)); |
| 147 |
|
| 148 |
$log = [ |
| 149 |
'event_type' => 'fluent_support/ticket_reopen', |
| 150 |
'person_id' => $person->id, |
| 151 |
'person_type' => $person->person_type, |
| 152 |
'object_id' => $ticket->id, |
| 153 |
'object_type' => 'ticket', |
| 154 |
'description' => $description |
| 155 |
]; |
| 156 |
|
| 157 |
Activity::create($log); |
| 158 |
}, 20, 2); |
| 159 |
|
| 160 |
// Ticket Assigned by agent |
| 161 |
add_action('fluent_support/agent_assigned_to_ticket', function ($assignedAgent, $ticket, $assigner) { |
| 162 |
|
| 163 |
$description = sprintf('Assign %1$s ticket to %2$s by %3$s', $this->getTicketMarkup($ticket), $this->getPersonMarkup($assignedAgent), $this->getPersonMarkup($assigner)); |
| 164 |
|
| 165 |
if($assigner && isset($assigner->id) && isset($assigner->person_type)){ |
| 166 |
$log = [ |
| 167 |
'event_type' => 'fluent_support/agent_assigned_to_ticket', |
| 168 |
'person_id' => $assigner->id, |
| 169 |
'person_type' => $assigner->person_type, |
| 170 |
'object_id' => $ticket->id, |
| 171 |
'object_type' => 'ticket', |
| 172 |
'description' => $description |
| 173 |
]; |
| 174 |
Activity::create($log); |
| 175 |
} |
| 176 |
}, 20, 3); |
| 177 |
|
| 178 |
add_action('fluent_support/ticket_deleted', function($agent, $ticketData) { |
| 179 |
$description = sprintf('%s deleted %s(#%d) at %s', $this->getPersonMarkup($agent), $ticketData['title'], $ticketData['id'], current_time('mysql')); |
| 180 |
$log = [ |
| 181 |
'event_type' => 'fluent_support/ticket_deleted', |
| 182 |
'person_id' => $agent->id, |
| 183 |
'person_type' => 'agent', |
| 184 |
'object_id' => $ticketData['id'], |
| 185 |
'object_type' => 'ticket', |
| 186 |
'description' => $description |
| 187 |
]; |
| 188 |
Activity::create($log); |
| 189 |
}, 20, 2); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* getTicketMarkup method will generate hyperlink to view the ticket details |
| 194 |
* @param $ticket |
| 195 |
* @param false $ticketText |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function getTicketMarkup($ticket, $ticketText = false) |
| 199 |
{ |
| 200 |
if (!$ticketText) { |
| 201 |
// translators: %s is the ticket title |
| 202 |
$ticketText = sprintf(__('Ticket: %s', 'fluent-support'), $ticket->title); |
| 203 |
} |
| 204 |
|
| 205 |
return '<a class="fs_link_trans fs_tk" href="#view_ticket">' . $ticketText . '</a>'; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* getPersonMarkup method will generate hyperlink to view a customer or agent |
| 210 |
* @param $person |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
public function getPersonMarkup($person) |
| 214 |
{ |
| 215 |
$route = 'view_agent'; |
| 216 |
if (isset($person->person_type) && $person->person_type == 'customer') { |
| 217 |
$route = 'view_customer'; |
| 218 |
} |
| 219 |
return '<a class="fs_link_trans fs_pr" href="#' . $route . '">' . $person->full_name . '</a>'; |
| 220 |
} |
| 221 |
} |
| 222 |
|