refactor queue's part 1

--HG--
branch : quitta-gsoc-2013
This commit is contained in:
Quitta 2013-08-05 22:35:22 +02:00
parent 91e758c1ee
commit ef05fdda7c
5 changed files with 98 additions and 123 deletions

View file

@ -7,7 +7,7 @@ class Pagination{
private $current; private $current;
private $amountOfRows; private $amountOfRows;
function __construct($query,$db,$nrDisplayed,$resultClass) { function __construct($query, $db, $nrDisplayed, $resultClass, $params = array()) {
if (!(isset($_GET['pagenum']))){ if (!(isset($_GET['pagenum']))){
$this->current= 1; $this->current= 1;
}else{ }else{
@ -16,7 +16,7 @@ class Pagination{
//Here we count the number of results //Here we count the number of results
$db = new DBLayer($db); $db = new DBLayer($db);
$rows = $db->executeWithoutParams($query)->rowCount(); $rows = $db->execute($query, $params)->rowCount();
$this->amountOfRows = $rows; $this->amountOfRows = $rows;
//the array hat will contain all users //the array hat will contain all users
@ -39,7 +39,7 @@ class Pagination{
$max = 'limit ' .($this->current- 1) * $page_rows .',' .$page_rows; $max = 'limit ' .($this->current- 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it //This is your query again, the same one... the only difference is we add $max into it
$data = $db->executeWithoutParams($query . " " . $max); $data = $db->execute($query . " " . $max, $params);
$this->element_array = Array(); $this->element_array = Array();
//This is where we put the results in a resultArray to be sent to smarty //This is where we put the results in a resultArray to be sent to smarty

View file

@ -213,13 +213,14 @@ class Ticket{
//Set ticket object //Set ticket object
public function set($t,$s,$q,$t_c,$a,$p){ public function set($values){
$this->title = $t; $this->tId = $values['TId'];
$this->status = $s; $this->title = $values['Title'];
$this->queue = $q; $this->status = $values['Status'];
$this->ticket_category = $t_c; $this->queue = $values['Queue'];
$this->author = $a; $this->ticket_category = $values['Ticket_Category'];
$this->priority = $p; $this->author = $values['Author'];
$this->priority = $values['Priority'];
} }
//create ticket by writing private data to DB. //create ticket by writing private data to DB.

View file

@ -1,34 +1,42 @@
<?php <?php
class Ticket_Queue{ class Ticket_Queue{
protected $queueElements; private $query;
private $params;
public function loadAllNotAssignedTickets(){ public function loadAllNotAssignedTickets(){
$dbl = new DBLayer("lib"); $this->query = "SELECT ticket . * FROM ticket LEFT JOIN assigned ON ticket.TId = assigned.Ticket WHERE assigned.Ticket IS NULL";
$statement = $dbl->executeWithoutParams("SELECT ticket . * FROM ticket LEFT JOIN assigned ON ticket.TId = assigned.Ticket WHERE assigned.Ticket IS NULL"); $this->params = array();
$rows = $statement->fetchAll();
$this->setQueue($rows);
} }
public function loadAllTickets(){ public function loadAllTickets(){
$dbl = new DBLayer("lib"); $this->query = "SELECT * FROM `ticket`";
$statement = $dbl->executeWithoutParams("SELECT * FROM `ticket`"); $this->params = array();
$rows = $statement->fetchAll();
$this->setQueue($rows);
} }
public function loadAllOpenTickets(){ public function loadAllOpenTickets(){
$dbl = new DBLayer("lib"); $this->query = "SELECT * FROM ticket INNER JOIN ticket_user ON ticket.Author = ticket_user.TUserId and ticket.Status!=3";
$statement = $dbl->executeWithoutParams("SELECT * FROM ticket INNER JOIN ticket_user ON ticket.Author = ticket_user.TUserId and ticket.Status!=3"); $this->params = array();
$rows = $statement->fetchAll();
$this->setQueue($rows);
} }
public function loadAllClosedTickets(){ public function loadAllClosedTickets(){
$dbl = new DBLayer("lib"); $this->query = "SELECT * FROM ticket INNER JOIN ticket_user ON ticket.Author = ticket_user.TUserId and ticket.Status=3";
$statement = $dbl->executeWithoutParams("SELECT * FROM ticket INNER JOIN ticket_user ON ticket.Author = ticket_user.TUserId and ticket.Status=3"); $this->params = array();
$rows = $statement->fetchAll(); }
$this->setQueue($rows);
public function loadToDoTickets($user_id){
//first: find the tickets assigned to the user with status = waiting on support
//second find all not assigned tickets that aren't forwarded yet.
//find all tickets assigned to someone else witht status waiting on support, with timestamp of last reply > 1 day
//find all non-assigned tickets forwarded to the support groups to which that user belongs
$this->query = "SELECT * FROM `ticket` t LEFT JOIN `assigned` a ON t.TId = a.Ticket LEFT JOIN `ticket_user` tu ON tu.TUserId = a.User LEFT JOIN `forwarded` f ON t.TId = f.Ticket
WHERE (tu.ExternId = :user_id AND t.Status = 1)
OR (a.Ticket IS NULL AND f.Group IS NULL)
OR (tu.ExternId != :user_id AND t.Status = 1 AND (SELECT ticket_reply.Timestamp FROM `ticket_reply` WHERE Ticket =t.TId ORDER BY TReplyId DESC LIMIT 1) < NOW() - INTERVAL 1 DAY )
OR (a.Ticket IS NULL AND EXISTS (SELECT * FROM `in_support_group` isg JOIN `ticket_user` tu2 ON isg.User = tu2.TUserId WHERE isg.Group = f.Group))
";
$this->params = array('user_id' => $user_id);
} }
public function createQueue($userid, $groupid, $what, $how, $who){ public function createQueue($userid, $groupid, $what, $how, $who){
@ -71,66 +79,13 @@ class Ticket_Queue{
$statement = $dbl->execute($query, $params); $statement = $dbl->execute($query, $params);
$rows = $statement->fetchAll(); $rows = $statement->fetchAll();
$this->setQueue($rows); $this->setQueue($rows);
}
public function getQuery(){
return $this->query;
} }
public function loadToDoTickets($user_id){ public function getParams(){
return $this->params;
$dbl = new DBLayer("lib");
//first: find the tickets assigned to the user with status = waiting on support
//second find all not assigned tickets that aren't forwarded yet.
//find all tickets assigned to someone else witht status waiting on support, with timestamp of last reply > 1 day
//find all non-assigned tickets forwarded to the support groups to which that user belongs
$query = "SELECT * FROM `ticket` t LEFT JOIN `assigned` a ON t.TId = a.Ticket LEFT JOIN `ticket_user` tu ON tu.TUserId = a.User LEFT JOIN `forwarded` f ON t.TId = f.Ticket
WHERE (tu.ExternId = :user_id AND t.Status = 1)
OR (a.Ticket IS NULL AND f.Group IS NULL)
OR (tu.ExternId != :user_id AND t.Status = 1 AND (SELECT ticket_reply.Timestamp FROM `ticket_reply` WHERE Ticket =t.TId ORDER BY TReplyId DESC LIMIT 1) < NOW() - INTERVAL 1 DAY )
OR (a.Ticket IS NULL AND EXISTS (SELECT * FROM `in_support_group` isg JOIN `ticket_user` tu2 ON isg.User = tu2.TUserId WHERE isg.Group = f.Group))
";
$values = array('user_id' => $user_id);
$statement = $dbl->execute($query,$values);
$rows = $statement->fetchAll();
$this->setQueue($rows);
} }
public function getTickets(){
return $this->queueElements;
}
protected function setQueue($rows){
$result = Array();
foreach($rows as $ticket){
$instance = new Ticket();
$instance->setTId($ticket['TId']);
$instance->setTimestamp($ticket['Timestamp']);
$instance->setTitle($ticket['Title']);
$instance->setStatus($ticket['Status']);
$instance->setQueue($ticket['Queue']);
$catInstance = new Ticket_Category();
$catInstance->load_With_TCategoryId($ticket['Ticket_Category']);
$instance->setTicket_Category($catInstance);
$userInstance = new Ticket_User();
$userInstance->load_With_TUserId($ticket['Author']);
$instance->setAuthor($userInstance);
$result[] = $instance;
}
$this->queueElements = $result;
}
} }

View file

@ -2,11 +2,12 @@
class Ticket_Queue_Handler{ class Ticket_Queue_Handler{
public static function getTickets($input, $user_id){ private $pagination;
public function getTickets($input, $user_id){
$queue = new Ticket_Queue(); $queue = new Ticket_Queue();
switch ($input){ switch ($input){
case "all": case "all":
$queue->loadAllTickets(); $queue->loadAllTickets();
@ -26,8 +27,20 @@ class Ticket_Queue_Handler{
default: default:
return "ERROR"; return "ERROR";
} }
$this->pagination = new Pagination($queue->getQuery(),"lib",10,"Ticket",$queue->getParams());
return $queue->getTickets(); foreach( $this->pagination->getElements() as $element ){
$catInstance = new Ticket_Category();
$catInstance->load_With_TCategoryId($element->getTicket_Category());
$element->setTicket_Category($catInstance);
$userInstance = new Ticket_User();
$userInstance->load_With_TUserId($element->getAuthor());
$element->setAuthor($userInstance);
}
return $this->pagination->getElements();
} }
public static function CreateQueue($userid, $groupid, $what, $how, $who){ public static function CreateQueue($userid, $groupid, $what, $how, $who){

View file

@ -6,43 +6,49 @@ function show_queue(){
if(WebUsers::isLoggedIn() && isset($_GET['get'])){ if(WebUsers::isLoggedIn() && isset($_GET['get'])){
if( Ticket_User::isMod($_SESSION['ticket_user'])){ if( Ticket_User::isMod($_SESSION['ticket_user'])){
//the default queue you want to see.
$result['queue_view'] = filter_var($_GET['get'], FILTER_SANITIZE_STRING); $result['queue_view'] = filter_var($_GET['get'], FILTER_SANITIZE_STRING);
$user_id = $_SESSION['ticket_user']->getTUserId(); $user_id = $_SESSION['ticket_user']->getTUserId();
$queueArray = Ticket_Queue_Handler::getTickets($result['queue_view'], $user_id); $queueArray = array();
$queue_handler = new Ticket_Queue_handler();
//if an action is set
if(isset($_POST['action'])){
switch($_POST['action']){
case "assignTicket":
$ticket_id = filter_var($_POST['ticket_id'], FILTER_SANITIZE_NUMBER_INT);
$result['ACTION_RESULT'] = Ticket::assignTicket($user_id, $ticket_id);
break;
case "unAssignTicket":
$ticket_id = filter_var($_POST['ticket_id'], FILTER_SANITIZE_NUMBER_INT);
$result['ACTION_RESULT'] = Ticket::unAssignTicket($user_id, $ticket_id);
break;
case "create_queue":
$userid = filter_var($_POST['userid'], FILTER_SANITIZE_NUMBER_INT);
$groupid = filter_var($_POST['groupid'], FILTER_SANITIZE_NUMBER_INT);
$what = filter_var($_POST['what'], FILTER_SANITIZE_STRING);
$how = filter_var($_POST['how'], FILTER_SANITIZE_STRING);
$who = filter_var($_POST['who'], FILTER_SANITIZE_STRING);
$result['ACTION_RESULT'] = $queue_handler->CreateQueue($userid, $groupid, $what, $how, $who);
if ($result['ACTION_RESULT'] != "ERROR"){
$queueArray = $result['ACTION_RESULT'];
}
break;
}
}
//if we didn't make a queue ourselves, then use the one specified by the get param
if( ! (isset($_POST['action']) && $_POST['action'] == "create_queue") ){
$queueArray = $queue_handler->getTickets($result['queue_view'], $user_id);
}
//if queue_view is a valid parameter value //if queue_view is a valid parameter value
if ($queueArray != "ERROR"){ if ($queueArray != "ERROR"){
if(isset($_POST['action'])){
switch($_POST['action']){
case "assignTicket":
$ticket_id = filter_var($_POST['ticket_id'], FILTER_SANITIZE_NUMBER_INT);
$result['ACTION_RESULT'] = Ticket::assignTicket($user_id, $ticket_id);
break;
case "unAssignTicket":
$ticket_id = filter_var($_POST['ticket_id'], FILTER_SANITIZE_NUMBER_INT);
$result['ACTION_RESULT'] = Ticket::unAssignTicket($user_id, $ticket_id);
break;
case "create_queue":
$userid = filter_var($_POST['userid'], FILTER_SANITIZE_NUMBER_INT);
$groupid = filter_var($_POST['groupid'], FILTER_SANITIZE_NUMBER_INT);
$what = filter_var($_POST['what'], FILTER_SANITIZE_STRING);
$how = filter_var($_POST['how'], FILTER_SANITIZE_STRING);
$who = filter_var($_POST['who'], FILTER_SANITIZE_STRING);
$result['ACTION_RESULT'] = Ticket_Queue_Handler::CreateQueue($userid, $groupid, $what, $how, $who);
if ($result['ACTION_RESULT'] != "ERROR"){
$queueArray = $result['ACTION_RESULT'];
}
break;
}
}
$result['tickets'] = Gui_Elements::make_table($queueArray, Array("getTId","getTitle","getTimestamp","getAuthor()->getExternId","getTicket_Category()->getName","getStatus","getStatusText","getAssigned","getForwardedGroupName","getForwardedGroupId"), Array("tId","title","timestamp","authorExtern","category","status","statusText","assigned","forwardedGroupName","forwardedGroupId")); $result['tickets'] = Gui_Elements::make_table($queueArray, Array("getTId","getTitle","getTimestamp","getAuthor()->getExternId","getTicket_Category()->getName","getStatus","getStatusText","getAssigned","getForwardedGroupName","getForwardedGroupId"), Array("tId","title","timestamp","authorExtern","category","status","statusText","assigned","forwardedGroupName","forwardedGroupId"));
$i = 0; $i = 0;
foreach( $result['tickets'] as $ticket){ foreach( $result['tickets'] as $ticket){