2013-07-20 11:19:14 +00:00
< ? php
2013-09-10 19:26:03 +00:00
/**
* Handles the assigning of a ticket to a user . This is being used to make someone responsible for the handling and solving of a ticket .
* The idea is that someone can easily assign a ticket to himself and by doing that , he makes aware to the other moderators that he will deal with the ticket in the future .
* @ author Daan Janssens , mentored by Matthew Lagoe
*/
2013-07-20 11:19:14 +00:00
class Assigned {
2013-09-10 19:26:03 +00:00
2014-09-03 02:46:08 +00:00
private $user ; /**< The id of the user being assigned */
private $ticket ; /**< The id of the ticket being assigned */
2013-07-20 11:19:14 +00:00
////////////////////////////////////////////Functions////////////////////////////////////////////////////
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* Assigns a ticket to a user or returns an error message .
* It will first check if the ticket isn 't already assigned, if not, it will create a new ' assigned ' entry .
* @ param $user_id the id of the user we want to assign to the ticket
* @ param $ticket_id the id of the ticket .
* @ return A string , if assigning succeedded " SUCCESS_ASSIGNED " will be returned , else " ALREADY_ASSIGNED " will be returned .
*/
2013-07-21 01:49:31 +00:00
public static function assignTicket ( $user_id , $ticket_id ) {
2013-07-20 11:19:14 +00:00
$dbl = new DBLayer ( " lib " );
//check if ticket is already assigned, if so return "ALREADY ASSIGNED"
if ( ! Assigned :: isAssigned ( $ticket_id )){
$assignation = new Assigned ();
$assignation -> set ( array ( 'User' => $user_id , 'Ticket' => $ticket_id ));
$assignation -> create ();
2013-07-21 01:49:31 +00:00
return " SUCCESS_ASSIGNED " ;
2013-07-20 11:19:14 +00:00
} else {
return " ALREADY_ASSIGNED " ;
}
2014-09-03 02:46:08 +00:00
2013-07-20 12:45:05 +00:00
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* Unassign a ticket being coupled to a user or return an error message .
* It will first check if the ticket is assigned , if this is indeed the case it will delete the 'assigned' entry .
* @ param $user_id the id of the user we want to unassign from the ticket
* @ param $ticket_id the id of the ticket .
* @ return A string , if unassigning succeedded " SUCCESS_UNASSIGNED " will be returned , else " NOT_ASSIGNED " will be returned .
*/
2013-07-21 01:49:31 +00:00
public static function unAssignTicket ( $user_id , $ticket_id ) {
$dbl = new DBLayer ( " lib " );
//check if ticket is really assigned to that user
if ( Assigned :: isAssigned ( $ticket_id , $user_id )){
$assignation = new Assigned ();
$assignation -> set ( array ( 'User' => $user_id , 'Ticket' => $ticket_id ));
$assignation -> delete ();
return " SUCCESS_UNASSIGNED " ;
} else {
return " NOT_ASSIGNED " ;
}
2014-09-03 02:46:08 +00:00
2013-07-21 01:49:31 +00:00
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* Get the ( external ) id of the user assigned to a ticket
* @ param $ticket_id the Id of the ticket that ' s being queried
* @ return The ( external ) id of the user being assigned to the ticket
*/
2013-07-20 12:45:05 +00:00
public static function getUserAssignedToTicket ( $ticket_id ) {
$dbl = new DBLayer ( " lib " );
$statement = $dbl -> execute ( " SELECT ticket_user.ExternId FROM `assigned` JOIN `ticket_user` ON assigned.User = ticket_user.TUserId WHERE `Ticket` = :ticket_id " , Array ( 'ticket_id' => $ticket_id ));
$user_id = $statement -> fetch ();
return $user_id [ 'ExternId' ];
2014-09-03 02:46:08 +00:00
2013-07-20 11:19:14 +00:00
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* Check if a ticket is already assigned ( in case the user_id param is used , it will check if it ' s assigned to that user )
* @ param $ticket_id the Id of the ticket that ' s being queried
* @ param $user_id the id of the user , default parameter = 0 , by using a user_id , it will check if that user is assigned to the ticket .
* @ return true in case it 's assigned, false in case it isn' t .
*/
2013-07-21 01:49:31 +00:00
public static function isAssigned ( $ticket_id , $user_id = 0 ) {
2013-07-20 11:19:14 +00:00
$dbl = new DBLayer ( " lib " );
//check if ticket is already assigned
2014-09-03 02:46:08 +00:00
2014-05-27 22:18:44 +00:00
if ( $user_id == 0 && $dbl -> select ( " `assigned` " , array ( 'ticket_id' => $ticket_id ), " `Ticket` = :ticket_id " ) -> rowCount () ){
2013-07-21 01:49:31 +00:00
return true ;
2014-05-27 22:18:44 +00:00
} else if ( $dbl -> select ( " `assigned` " , array ( 'ticket_id' => $ticket_id , 'user_id' => $user_id ), " `Ticket` = :ticket_id and `User` = :user_id " ) -> rowCount () ){
2013-07-20 11:19:14 +00:00
return true ;
} else {
return false ;
2014-09-03 02:46:08 +00:00
}
2013-07-20 11:19:14 +00:00
}
2014-09-03 02:46:08 +00:00
2013-07-20 11:19:14 +00:00
////////////////////////////////////////////Methods////////////////////////////////////////////////////
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* A constructor .
* Empty constructor
*/
2013-07-20 11:19:14 +00:00
public function __construct () {
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* sets the object ' s attributes .
* @ param $values should be an array of the form array ( 'User' => user_id , 'Ticket' => ticket_id ) .
*/
2013-07-20 11:19:14 +00:00
public function set ( $values ) {
$this -> setUser ( $values [ 'User' ]);
2013-07-21 01:49:31 +00:00
$this -> setTicket ( $values [ 'Ticket' ]);
2013-07-20 11:19:14 +00:00
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* creates a new 'assigned' entry .
* this method will use the object 's attributes for creating a new ' assigned ' entry in the database .
*/
2013-07-20 11:19:14 +00:00
public function create () {
$dbl = new DBLayer ( " lib " );
2014-09-03 02:46:08 +00:00
$dbl -> insert ( " `assigned` " , Array ( 'User' => $this -> getUser (), 'Ticket' => $this -> getTicket ()));
2013-07-20 11:19:14 +00:00
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* deletes an existing 'assigned' entry .
* this method will use the object 's attributes for deleting an existing ' assigned ' entry in the database .
*/
2013-07-20 11:19:14 +00:00
public function delete () {
$dbl = new DBLayer ( " lib " );
2014-09-03 11:13:44 +00:00
$dbl -> delete ( " `assigned` " , array ( 'user_id' => $this -> getUser () , 'ticket_id' => $this -> getTicket ()), " `User` = :user_id and `Ticket` = :ticket_id " );
2013-07-20 11:19:14 +00:00
}
2013-09-10 19:26:03 +00:00
/**
* loads the object ' s attributes .
* loads the object ' s attributes by giving a ticket_id , it will put the matching user_id and the ticket_id into the attributes .
* @ param $ticket_id the id of the ticket that should be loaded
*/
public function load ( $ticket_id ) {
2013-07-20 11:19:14 +00:00
$dbl = new DBLayer ( " lib " );
2014-05-27 22:18:44 +00:00
$statement = $dbl -> select ( " `assigned` " , Array ( 'ticket_id' => $ticket_id ), " `Ticket` = :ticket_id " );
2013-07-20 11:19:14 +00:00
$row = $statement -> fetch ();
$this -> set ( $row );
}
2014-09-03 02:46:08 +00:00
2013-07-20 11:19:14 +00:00
////////////////////////////////////////////Getters////////////////////////////////////////////////////
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* get user attribute of the object .
*/
2013-07-20 11:19:14 +00:00
public function getUser (){
return $this -> user ;
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* get ticket attribute of the object .
*/
2013-07-20 11:19:14 +00:00
public function getTicket (){
return $this -> ticket ;
}
2014-09-03 02:46:08 +00:00
2013-07-20 11:19:14 +00:00
////////////////////////////////////////////Setters////////////////////////////////////////////////////
2013-09-10 19:26:03 +00:00
/**
* set user attribute of the object .
* @ param $u integer id of the user
*/
2013-07-20 11:19:14 +00:00
public function setUser ( $u ){
$this -> user = $u ;
}
2014-09-03 02:46:08 +00:00
2013-09-10 19:26:03 +00:00
/**
* set ticket attribute of the object .
* @ param $t integer id of the ticket
*/
public function setTicket ( $t ){
$this -> ticket = $t ;
2013-07-20 11:19:14 +00:00
}
2014-09-03 02:46:08 +00:00
2014-05-27 22:18:44 +00:00
}