/**
 * Assign an agent to a ticket.
 *
 * Assign the given agent to a ticket or find an available
 * agent if no agent ID is given.
 *
 * @since  3.0.2
 *
 * @param  integer $ticket_id ID of the post in need of a new agent
 * @param  integer $agent_id  ID of the agent to assign the ticket to
 * @param  boolean $log       Shall the assignment be logged or not
 *
 * @return object|boolean|integer WP_Error in case of problem, true if no change is required or the post meta ID if the
 *                                agent was changed
 */
function wpas_assign_ticket($ticket_id, $agent_id = null, $log = true)
{
    if ('ticket' !== get_post_type($ticket_id)) {
        return new WP_Error('incorrect_post_type', __('The given post ID is not a ticket', 'awesome-support'));
    }
    if (is_null($agent_id)) {
        $agent_id = wpas_find_agent($ticket_id);
    }
    if (!user_can($agent_id, 'edit_ticket')) {
        return new WP_Error('incorrect_agent', __('The chosen agent does not have the sufficient capabilities to be assigned a ticket', 'awesome-support'));
    }
    /* Get the current agent if any */
    $current = get_post_meta($ticket_id, '_wpas_assignee', true);
    if ($current === $agent_id) {
        return true;
    }
    $update = update_post_meta($ticket_id, '_wpas_assignee', $agent_id, $current);
    /* Increment the number of tickets open for this agent */
    $agent = new WPAS_Member_Agent($agent_id);
    $agent->ticket_plus();
    /* Log the action */
    if (true === $log) {
        $log = array();
        $log[] = array('action' => 'updated', 'label' => __('Support Staff', 'awesome-support'), 'value' => $agent_id, 'field_id' => 'assignee');
    }
    wpas_log($ticket_id, $log);
    /**
     * wpas_ticket_assigned hook
     *
     * since 3.0.2
     */
    do_action('wpas_ticket_assigned', $ticket_id, $agent_id);
    // In case this is a ticket transfer from one agent to another, we fire a dedicated action
    if (!empty($current) && user_can((int) $current, 'edit_ticket')) {
        /**
         * Fired only if the current assignment is a ticket transfer
         *
         * @since 3.2.8
         *
         * @param int $agent_id ID of the new assignee
         * @param int $current  ID of the previous assignee
         */
        do_action('wpas_ticket_assignee_changed', $agent_id, (int) $current);
    }
    return $update;
}
Example #2
0
 function test_wpas_find_agent()
 {
     $agent = wpas_find_agent();
     $this->assertInternalType('int', $agent);
 }
Example #3
0
/**
 * Assign an agent to a ticket.
 *
 * Assign the given agent to a ticket or find an available
 * agent if no agent ID is given.
 *
 * @since  3.0.2
 * @param  integer  $ticket_id    ID of the post in need of a new agent
 * @param  integer  $agent_id     ID of the agent to assign the ticket to
 * @param  boolean  $log          Shall the assignment be logged or not
 * @return object|boolean|integer WP_Error in case of problem, true if no change is required or the post meta ID if the agent was changed
 */
function wpas_assign_ticket($ticket_id, $agent_id = null, $log = true)
{
    if ('ticket' !== get_post_type($ticket_id)) {
        return new WP_Error('incorrect_post_type', __('The given post ID is not a ticket', 'wpas'));
    }
    if (is_null($agent_id)) {
        $agent_id = wpas_find_agent($ticket_id);
    }
    if (!user_can($agent_id, 'edit_ticket')) {
        return new WP_Error('incorrect_agent', __('The chosen agent does not have the sufficient capabilities to be assigned a ticket', 'wpas'));
    }
    /* Get the current agent if any */
    $current = get_post_meta($ticket_id, '_wpas_assignee', true);
    if ($current === $agent_id) {
        return true;
    }
    $update = update_post_meta($ticket_id, '_wpas_assignee', $agent_id, $current);
    /* Log the action */
    if (true === $log) {
        $log = array();
        $log[] = array('action' => 'updated', 'label' => __('Support staff', 'wpas'), 'value' => $agent_id, 'field_id' => 'assignee');
    }
    wpas_log($ticket_id, $log);
    /**
     * wpas_ticket_assigned hook
     *
     * since 3.0.2
     */
    do_action('wpas_ticket_assigned', $ticket_id, $agent_id);
    return $update;
}