2014-08-18 18:23:27 +00:00
< ? php
/**
* 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
*/
class Assigned {
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 */
2014-08-18 18:23:27 +00:00
////////////////////////////////////////////Functions////////////////////////////////////////////////////
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +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 .
*/
public static function assignTicket ( $user_id , $ticket_id ) {
$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 ();
return " SUCCESS_ASSIGNED " ;
} else {
return " ALREADY_ASSIGNED " ;
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +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 .
*/
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
2014-08-18 18:23:27 +00:00
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +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
*/
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
2014-08-18 18:23:27 +00:00
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +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 .
*/
public static function isAssigned ( $ticket_id , $user_id = 0 ) {
$dbl = new DBLayer ( " lib " );
//check if ticket is already assigned
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
if ( $user_id == 0 && $dbl -> select ( " `assigned` " , array ( 'ticket_id' => $ticket_id ), " `Ticket` = :ticket_id " ) -> rowCount () ){
return true ;
} else if ( $dbl -> select ( " `assigned` " , array ( 'ticket_id' => $ticket_id , 'user_id' => $user_id ), " `Ticket` = :ticket_id and `User` = :user_id " ) -> rowCount () ){
return true ;
} else {
return false ;
2014-09-03 02:46:08 +00:00
}
2014-08-18 18:23:27 +00:00
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
////////////////////////////////////////////Methods////////////////////////////////////////////////////
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* A constructor .
* Empty constructor
*/
public function __construct () {
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* sets the object ' s attributes .
* @ param $values should be an array of the form array ( 'User' => user_id , 'Ticket' => ticket_id ) .
*/
public function set ( $values ) {
$this -> setUser ( $values [ 'User' ]);
$this -> setTicket ( $values [ 'Ticket' ]);
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* creates a new 'assigned' entry .
* this method will use the object 's attributes for creating a new ' assigned ' entry in the database .
*/
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 ()));
2014-08-18 18:23:27 +00:00
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* deletes an existing 'assigned' entry .
* this method will use the object 's attributes for deleting an existing ' assigned ' entry in the database .
*/
public function delete () {
$dbl = new DBLayer ( " lib " );
2014-09-03 02:46:08 +00:00
$dbl -> delete ( " `assigned` " , array ( 'user_id' => $this -> getUser () , 'ticket_id' => $this -> getTicket (), " `User` = :user_id and `Ticket` = :ticket_id " ));
2014-08-18 18:23:27 +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 ) {
$dbl = new DBLayer ( " lib " );
$statement = $dbl -> select ( " `assigned` " , Array ( 'ticket_id' => $ticket_id ), " `Ticket` = :ticket_id " );
$row = $statement -> fetch ();
$this -> set ( $row );
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
////////////////////////////////////////////Getters////////////////////////////////////////////////////
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* get user attribute of the object .
*/
public function getUser (){
return $this -> user ;
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* get ticket attribute of the object .
*/
public function getTicket (){
return $this -> ticket ;
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
////////////////////////////////////////////Setters////////////////////////////////////////////////////
/**
* set user attribute of the object .
* @ param $u integer id of the user
*/
public function setUser ( $u ){
$this -> user = $u ;
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
/**
* set ticket attribute of the object .
* @ param $t integer id of the ticket
*/
public function setTicket ( $t ){
$this -> ticket = $t ;
}
2014-09-03 02:46:08 +00:00
2014-08-18 18:23:27 +00:00
}