Hi. I have a department called "Purchases" and one called "Finance". What I am trying to achieve is:
I need a plugin that will automatically transfer a ticket "Purchasees" to the "Finance" department when an agent from "Purchases" closes the ticket. This is what i currently have, but it's not working. It seems to be properly detecting a ticket closure (ticket.close signal). But nothing happens. Can anyone help me out?

        <?php
        require_once(INCLUDE_DIR . 'class.signal.php');
        require_once(INCLUDE_DIR . 'class.plugin.php');
        require_once(INCLUDE_DIR . 'class.ticket.php');
        require_once(INCLUDE_DIR . 'class.osticket.php');
        require_once(INCLUDE_DIR . 'class.config.php');
        require_once(INCLUDE_DIR . 'class.format.php');

        class PurchaseCloseTransferPlugin extends Plugin
        {

            var $config_class = 'PurchaseCloseTransferPluginConfig';

            function bootstrap()
            {
                Signal::connect('ticket.close.before', array($this, 'onTicketClose'));
                error_log("Detected! a ticket closure");
            }

            function onTicketClose($ticket)
            {
                if ($ticket->dept->name == 'Purchases') {
                    $transfer_dept = $this->getConfig()->get('transfer_department');
                    $dept = Department::lookupByName($transfer_dept);
                    if ($dept) {
                        $ticket->transfer($dept->getId());
                    } else {
                        $log = Ticket::log(
                            'PurchaseCloseTransferPlugin',
                            sprintf('Transfer department "%s" not found', $transfer_dept)
                        );
                    }
                }
            }
        }

        class PurchaseCloseTransferPluginConfig extends PluginConfig
        {

            function getOptions()
            {
                return array(
                    'transfer_department' => new TextboxField(array(
                        'label' => 'Transfer To Department',
                        'default' => 'Finance',
                        'configuration' => array('size' => 20, 'length' => 100),
                    )),
                );
            }
        }

    yurividal

    Put some code above the if() at the beginning of the onTicketClose method to write to a file or something to see if it’s actually hitting that method. If so, then it’s likely your if() is failing.

    While I’m here I would like to suggest doing $ticket->getDeptName() instead (method reference).

    Cheers.

      KevinTheJedi Thanks, ill try that. I have put a "error_log("Enetered the function");" before my if statement, and it never gets printing. It seems like the osTicketClose function is not being called.

        yurividal

        Can you add it to the bootstrap method to see if that’s even being called?

        Did you install/enabled the plugin and add an instance?

        Cheers.

          KevinTheJedi the bootstrap method is indeed being called. As you can see in the code, I added a log message inside the bootstrap part and I see the log in my PHP log file. (Multiple actually. Every time I close a ticket, it prints about 5 or 6 times the "Detected" log line.)

          But, it seems like ot never actually goes to the onTicketClose function.

          And yes, plugin and instance are enabled

            yurividal

            I don't think we have anything that sends a ticket.close or even ticket.close.before signal. I could be wrong though, it's been a minute since I looked at the Singals.

            Cheers.

              KevinTheJedi this is what I am thinking. I tried with ticket.close and that didn't work either. So, any ideas how else this could be implemented?

                yurividal

                You’d need to modify the core codebase and add a Signal::send for ticket close. You can look at the various set status methods and add logic to determine if being closed the send the signal is changed successfully.

                Cheers.

                Write a Reply...