1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Lslib extends TikiLib 9{ 10 function set_operator_id($reqId, $senderId) 11 { 12 $query = 'update `tiki_live_support_requests` set `operator_id` = ? where `reqId`=?'; 13 14 $this->query($query, [$senderId, $reqId]); 15 } 16 17 function set_user_id($reqId, $senderId) 18 { 19 $query = 'update `tiki_live_support_requests` set `user_id` = ? where `reqId`=?'; 20 21 $this->query($query, [$senderId, $reqId]); 22 } 23 24 function new_user_request($user, $tiki_user, $email, $reason) 25 { 26 $reqId = md5(uniqid('.')); 27 $query = 'insert into `tiki_live_support_requests`' . 28 ' (`reqId`, `user`, `tiki_user`, `email`, `reason`, `req_timestamp`,' . 29 ' `status`, `timestamp`, `operator`, `chat_started`, `chat_ended`, `operator_id`, `user_id`)' . 30 ' values(?,?,?,?,?,?,?,?,?,?,?,?,?)'; 31 32 $this->query( 33 $query, 34 [$reqId, $user, $tiki_user, $email, $reason, $this->now, 'active', $this->now, '', 0, 0, '', ''] 35 ); 36 return $reqId; 37 } 38 39 function get_last_request() 40 { 41 $x = $this->getOne('select max(`timestamp`) from `tiki_live_support_requests`', []); 42 43 if ($x) { 44 return $x; 45 } else { 46 return 0; 47 } 48 } 49 50 function get_max_active_request() 51 { 52 return $this->getOne('select max(`reqId`) from `tiki_live_support_requests` where `status`=?', ['active']); 53 } 54 55 // Remove active requests 56 function purge_requests() 57 { 58 $min = $this->now - 60 * 2; // 1 minute = timeout. 59 $query = 'update `tiki_live_support_requests` set `status`=? where `timestamp` < ?'; 60 $this->query($query, ['timeout', $min]); 61 } 62 63 // Get status for request 64 function get_request_status($reqId) 65 { 66 return $this->getOne('select `status` from `tiki_live_support_requests` where `reqId`=?', [$reqId]); 67 } 68 69 function set_request_status($reqId, $status) 70 { 71 $query = 'update `tiki_live_support_requests` set `status`=? where `reqId`=?'; 72 73 $this->query($query, [$status, $reqId]); 74 } 75 76 // Get request information 77 function get_request($reqId) 78 { 79 $query = 'select * from `tiki_live_support_requests` where `reqId`=?'; 80 81 $result = $this->query($query, [$reqId]); 82 $res = $result->fetchRow(); 83 return $res; 84 } 85 86 function set_operator_status($user, $status) 87 { 88 // If switching to offline then sum online time for this operator 89 if ($status == 'offline') { 90 $query = 'update `tiki_live_support_operators` set `time_online` = ? - `status_since` where `user`=? and `status`=?'; 91 92 $this->query($query, [$this->now, $user,'online']); 93 } 94 95 $query = 'update `tiki_live_support_operators` set `status`=?, `status_since`=? where `user`=?'; 96 $this->query($query, [$status, $this->now,$user]); 97 } 98 99 function get_operator_status($user) 100 { 101 $status = $this->getOne('select `status` from `tiki_live_support_operators` where `user`=?', [$user]); 102 103 if (! $status) { 104 $status = 'offline'; 105 } 106 107 return $status; 108 } 109 110 // Accepts a request, change status to op_accepted 111 function operator_accept($reqId, $user, $operator_id) 112 { 113 $query = 'update `tiki_live_support_requests` set `operator_id`=?,operator=?,status=?,timestamp=?,chat_started=? where `reqId`=?'; 114 $this->query($query, [$operator_id, $user, 'op_accepted', $this->now, $this->now, $reqId]); 115 $query = 'update `tiki_live_support_operators` set `accepted_requests` = `accepted_requests` + 1 where `user`=?'; 116 $this->query($query, [$user]); 117 } 118 119 function user_close_request($reqId) 120 { 121 if (! $reqId) { 122 return; 123 } 124 125 $query = 'update `tiki_live_support_requests` set `status`=?,timestamp=?,chat_ended=? where `reqId`=?'; 126 $this->query($query, ['user closed', $this->now, $this->now, $reqId]); 127 } 128 129 function operator_close_request($reqId) 130 { 131 if (! $reqId) { 132 return; 133 } 134 135 $query = 'update `tiki_live_support_requests` set `status`=?,timestamp=?,chat_ended=? where `reqId`=?'; 136 $this->query($query, ['operator closed', $this->now, $this->now, $reqId]); 137 } 138 139 function get_requests($status) 140 { 141 $this->purge_requests(); 142 143 $query = 'select * from `tiki_live_support_requests` where `status`=?'; 144 $result = $this->query($query, [$status]); 145 $ret = []; 146 147 while ($res = $result->fetchRow()) { 148 $ret[] = $res; 149 } 150 151 return $ret; 152 } 153 154 //EVENT HANDLING 155 function get_new_events($reqId, $senderId, $last) 156 { 157 $query = 'select * from `tiki_live_support_events` where `senderId`=? and reqId=? and `eventId`>?'; 158 159 $result = $this->query($query, [$senderId, $reqId, $last]); 160 $ret = ''; 161 $ret = '<?xml version="1.0" ?>'; 162 $ret .= '<events>'; 163 164 while ($res = $result->fetchRow()) { 165 $ret .= '<event>' . '<data>' . $res['data'] . '</data></event>'; 166 } 167 168 $ret .= '</events>'; 169 return $ret; 170 } 171 172 function get_last_event($reqId, $senderId) 173 { 174 return $this->getOne( 175 'select max(`seqId`) from `tiki_live_support_events` where `senderId`<>? and reqId=?', 176 [$senderId, $reqId] 177 ); 178 } 179 180 function get_support_event($reqId, $event, $senderId) 181 { 182 return $this->getOne( 183 'select `data` from `tiki_live_support_events` where `senderId`<>? and `reqId`=? and `seqId`=?', 184 [$senderId, $reqId, $event] 185 ); 186 } 187 188 function put_message($reqId, $msg, $senderId) 189 { 190 $seq = $this->getOne('select max(`seqId`) from `tiki_live_support_events` where `reqId`=?', [$reqId]); 191 192 if (! $seq) { 193 $seq = 0; 194 } 195 196 $seq++; 197 $query = 'insert into `tiki_live_support_events`(`seqId`, `reqId`, `type`, `senderId`, `data`, `timestamp`)' . 198 ' values(?,?,?,?,?,?)'; 199 200 $this->query($query, [$seq, $reqId, 'msg', $senderId, $msg, $this->now]); 201 } 202 203 function operators_online() 204 { 205 return $this->getOne('select count(*) from `tiki_live_support_operators` where `status`=?', ['online']); 206 } 207} 208$lslib = new Lslib; 209