Location: PHPKode > scripts > Support Tickets Center > zingiri-tickets/osticket/upload/include/class.lock.php

    Ticket lock handle.

    Peter Rotich <hide@address.com>
    Copyright (c)  2006-2010 osTicket

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
    $Id: $

 * Mainly used as a helper...

class TicketLock {
    var $id;
    var $staff_id;
    var $created;
    var $expire;
    var $expiretime;
    function TicketLock($id,$load=true){
        if($load) $this->load();

    function load() {

            return false;
        $sql='SELECT *,TIME_TO_SEC(TIMEDIFF(expire,NOW())) as timeleft FROM '.TICKET_LOCK_TABLE.' WHERE lock_id='.db_input($this->id);
        if(($res=db_query($sql)) && db_num_rows($res)) {
            return true;
        return false;
    function reload() {
        return $this->load();

    //Create a ticket lock...this function assumes the caller check for access & validity of ticket & staff x-ship.    
    function acquire($ticketId,$staffId) {
        global $cfg;

        if(!$ticketId or !$staffId or !$cfg->getLockTime())
            return 0;

        //Cleanup any expired locks on the ticket.
        db_query('DELETE FROM '.TICKET_LOCK_TABLE.' WHERE ticket_id='.db_input($ticketId).' AND expire<NOW()');
        //TODO: cleanup any other locks owned by the user? (NOT a good idea.. could be working on 2 tickets at once??)
        $sql='INSERT IGNORE INTO '.TICKET_LOCK_TABLE.' SET created=NOW() '.
            ',expire=DATE_ADD(NOW(),INTERVAL '.$cfg->getLockTime().' MINUTE) ';
        return db_query($sql)?db_insert_id():0;
    //Renew existing lock.
    function renew() {
        global $cfg;

        $sql='UPDATE '.TICKET_LOCK_TABLE.' SET expire=DATE_ADD(NOW(),INTERVAL '.$cfg->getLockTime().' MINUTE) '.
            ' WHERE lock_id='.db_input($this->getId());
        //echo $sql;
        if(db_query($sql) && db_affected_rows()) {
            return true;
        return false;

    //release aka delete a lock.
    function release(){
        //FORCED release - we don't give a ....
        $sql='DELETE FROM '.TICKET_LOCK_TABLE.' WHERE lock_id='.db_input($this->getId());
        return (db_query($sql) && db_affected_rows())?true:false;

    function getId(){
        return $this->id;
    function getStaffId(){
        return $this->staff_id;

    function getCreateTime() {
        return $this->created;

    function getExpireTime() {
        return $this->expire;
    //Get remaiming time before the lock expires
    function getTime() {
        return $this->isExpired()?0:($this->expiretime-time());
    //Should we be doing realtime check here? (Ans: not really....expiretime is local & based on loadtime)
    function isExpired(){
        return (time()>$this->expiretime)?true:false;

    //Simply remove ALL locks a user (staff) holds on a ticket(s).
    function removeStaffLocks($staffId,$ticketId=0) {
        $sql='DELETE FROM '.TICKET_LOCK_TABLE.' WHERE staff_id='.db_input($staffId);
            $sql.=' AND ticket_id='.db_input($ticketId);

        return db_query($sql)?true:false;

    //Called  via cron 
    function cleanup() {
        //Cleanup any expired locks.
        db_query('DELETE FROM '.TICKET_LOCK_TABLE.' WHERE expire<NOW()');
Return current item: Support Tickets Center