AgentsPerQueue.pm 3.46 KB
# --
# Kernel/Modules/AgentsPerQueue.pm - frontend module for displaying the agents per queue matrix panel
# 
# Copyright (C) 2014 SeTIC - UFSC - http://setic.ufsc.br/
# Rodrigo Goncalves - rodrigo.g@ufsc.br
#
# Version 2015-01-15 - Adjustments for OTRS 4
# Version 2018-01-25 - Refactoring for OTRS 6
#
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::Modules::AgentsPerQueue;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);


our @ObjectDependencies = (
"Kernel::System::Web::Request",	# OLD ParamObject
"Kernel::System::DB",
"Kernel::Output::HTML::Layout",
"Kernel::System::Log", 
"Kernel::Config"
);

sub new {
	my ( $Type, %Param ) = @_;

	# allocate new hash for object
	my $Self = {%Param};
	bless( $Self, $Type );

	return $Self;
}

sub Run {
	my ( $Self, %Param ) = @_;
	my %Data = ();
	my $layoutObject = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
	my $languageObject = $Kernel::OM->Get("Kernel::Language");

	# load data
	my %matrix = $Self->GetQueuesPerAgent();
	
	# build output
	my $Output =
	  $layoutObject->Header( Title => $languageObject->Translate("Queues per agent") );
	$Output .= $layoutObject->NavigationBar();
		
	my $id = 1;
	
	# include each agent
	foreach my $agent ( sort keys %matrix ) {
		$layoutObject->Block(
			Name => 'QueuesPerAgent',
			Data => {  ID => $id, Agent => $agent, Queues => $matrix{$agent}{"Manage"}, Paths => $matrix{$agent}{"Path"} },
		);
		$id += 1;
	}

	$Output .= $layoutObject->Output(
		Data         => \%Data,
		TemplateFile => 'AgentsPerQueue',
	);
	$Output .= $layoutObject->Footer();
	return $Output;

}

sub GetQueuesPerAgent {
	my ( $Self, %Param ) = @_;
	my %result = ();
	my $dbObject = $Kernel::OM->Get("Kernel::System::DB");

	my $SQL = "SELECT type, user, queue from vw_user_queues order by user,queue, type";
	
	# get queues 
	$dbObject->Prepare(
		SQL => $SQL,
		Bind => [ ]
	);

	my $queues = "";
	my $queuesPath = "";
	
	my $lastAgent = "";
	my $lastQueue = "";
	my $lastType = "";
	my $queue = "";
	my $type = "";
	my $agent = "";
	
	# for each user/queue
	while ( my @row = $dbObject->FetchrowArray() ) {
		
		# current line data
		$type = $row[0];
		$agent = $row[1];
		$queue = $row[2];
		
		# if agent changed from last row (or is first line)
		if ($agent ne $lastAgent) {			
			if ($lastAgent ne "") {
				# if agent changed, save the previous agent data and reset the variables
				$result{$lastAgent}{"Manage"} = $queues;
				$result{$lastAgent}{"Path"} = $queuesPath;
				$queues = "";
				$queuesPath = "";
				$lastQueue = "";
				$lastType = "";				
			}			
		} 
				
		# if the same queue appears in manage and path, ignore the path clause				
		if (($lastQueue ne "") and ($type eq $lastType) and (index($queue, $lastQueue . "::") != -1)) {
			next;
		} elsif (($type eq "Path") and ($lastQueue eq $queue)) {
			
		} elsif ($type eq "Manage") {
			if ($lastQueue ne "") {
				$queues = $queues . "<BR/>";
			}
			$queues = $queues . $queue;
		} elsif ($type eq "Path") {
			if ($lastQueue ne "") {
				$queuesPath = $queuesPath . "<BR/>";
			}
			$queuesPath = $queuesPath . $queue;
		}				
		
		# data for next loop
		$lastType = $type;
		$lastQueue = $queue;
		$lastAgent = $agent;
	}
	
	if (($queues ne "") or ($queuesPath ne "")) {
		$result{$agent}{"Manage"} = $queues;
		$result{$agent}{"Path"} = $queuesPath;
	}
	
	return %result;
}

1;