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