You are not logged in.
Hello guys,
I need to create a hook for new users assigned to a ticket for my plugin
I was using this code :
$PLUGIN_HOOKS['item_update']['myplug'] = array('Ticket'=> 'plugin_ticket_update_myplug');
But this catches every type of update (solution,task,...) on a ticket, but I only need to catch the update of assigning a new user is to a ticket.
Hope you got the idea guys, and can help me, I really need this asap.
Offline
Try the item_add hook for the Ticket_User itemtype instead. You can then use the data passed as a parameter to that function to get the type of actor and see if it is equal to "CommonITILActor::ASSIGNED" before doing other stuff.
GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.
Offline
do you mean smthg like this : setup.php
$PLUGIN_HOOKS['item_update']['myplug'] = array('Ticket_User'=> 'plugin_ticket_update_myplug');
hook.php:
function plugin_ticket_update_myplug($data)
{
if($data=="CommonITILActor::ASSIGNED"){
// My PHP script to exec
}
}
if yes it actually works but the data variable ($data) is always empty
Last edited by aziz (2022-10-06 19:45:02)
Offline
The hook is "item_add" not "item_update", so it should be registered like:
$PLUGIN_HOOKS['item_add']['myplug'] = ['Ticket_User' => 'plugin_actor_add_myplug'];
I also changed the "array()" to square brackets which is the preferred way to declare an array in modern PHP, but either should still work.
The parameter (that you named data) should be an object whose class extends CommonITILActor.
This means that the actor type should be stored in $data->fields['type'].
Also, "CommonITILActor::ASSIGNED" should not be in quotes in the code. It is a reference to a constant in a class, not a literal string. I also made a typo where it should be "ASSIGN" not "ASSIGNED".
function plugin_actor_add_myplug($actor)
{
if($actor->fields['type'] == CommonITILActor::ASSIGN){
// My PHP script to exec
}
}
GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.
Offline
Thanks a lot now it works fine, but is there a way to know if that ticket which got assigned to a user is it an old ticket, or it just has been created because I want to catch only the users who got assigned to an old ticket not to a ticket that is just created with users assigned to it already.
This will help a lot because I'm dealing with users that are assigned to a new ticket with another hook :
$PLUGIN_HOOKS['item_add']['myplug'] = ["Ticket" => 'plugin_ticket_add_myplug']
So when a new ticket is created, I go find that ticket and then find those users with a PHP script.
Hope you got the idea, and you can help me again.
I really appreciate it.
Offline
There is no way to know for sure if you are adding users to a new ticket or existing one.
You could get the "tickets_id" field value, get the Ticket from the DB, and checking the creation date is not the current request date and time using something like:
$ticket = new Ticket();
$ticket->getFromDB($actor->fields['tickets_id']);
if ($ticket->fields['date_creation'] != $_SESSION['glpi_currenttime']) {
// Adding user to an existing ticket
}
I don't know how accurate it would be though.
GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.
Offline