1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xhelp; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* You may not change or alter any portion of this comment or credits |
7
|
|
|
* of supporting developers from this source code or any supporting source code |
8
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
17
|
|
|
* @license {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
18
|
|
|
* @author Brian Wahoff <[email protected]> |
19
|
|
|
* @author XOOPS Development Team |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
if (!\defined('XHELP_CLASS_PATH')) { |
23
|
|
|
exit(); |
24
|
|
|
} |
25
|
|
|
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php'; |
26
|
|
|
// require_once XHELP_CLASS_PATH . '/NaiveBayesian.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* class TicketSolutionHandler |
30
|
|
|
*/ |
31
|
|
|
class TicketSolutionHandler extends BaseObjectHandler |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Name of child class |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $classname = TicketSolution::class; |
39
|
|
|
/** |
40
|
|
|
* DB Table Name |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $dbtable = 'xhelp_ticket_solutions'; |
45
|
|
|
|
46
|
|
|
private const TABLE = 'xhelp_ticket_solutions'; |
47
|
|
|
private const ENTITY = TicketSolution::class; |
48
|
|
|
private const ENTITYNAME = 'TicketSolution'; |
49
|
|
|
private const KEYNAME = 'id'; |
50
|
|
|
private const IDENTIFIER = 'ticketid'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param \XoopsMySQLDatabase|null $db reference to a xoopsDB object |
56
|
|
|
*/ |
57
|
|
|
public function __construct(\XoopsMySQLDatabase $db = null) |
58
|
|
|
{ |
59
|
|
|
$this->init($db); |
60
|
|
|
$this->helper = Helper::getInstance(); |
61
|
|
|
parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \XoopsObject $object |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function insertQuery(\XoopsObject $object): string |
69
|
|
|
{ |
70
|
|
|
//TODO mb replace with individual variables |
71
|
|
|
// Copy all object vars into local variables |
72
|
|
|
foreach ($object->cleanVars as $k => $v) { |
73
|
|
|
${$k} = $v; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$sql = \sprintf( |
77
|
|
|
'INSERT INTO `%s` (id, ticketid, url, title, description, uid, posted) VALUES (%u, %u, %s, %s, %s, %u, %u)', |
78
|
|
|
$this->db->prefix($this->dbtable), |
79
|
|
|
$id, |
|
|
|
|
80
|
|
|
$ticketid, |
|
|
|
|
81
|
|
|
$this->db->quoteString($url), |
|
|
|
|
82
|
|
|
$this->db->quoteString($title), |
|
|
|
|
83
|
|
|
$this->db->quoteString($description), |
|
|
|
|
84
|
|
|
$uid, |
|
|
|
|
85
|
|
|
\time() |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return $sql; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \XoopsObject $object |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function updateQuery(\XoopsObject $object): string |
96
|
|
|
{ |
97
|
|
|
//TODO mb replace with individual variables |
98
|
|
|
// Copy all object vars into local variables |
99
|
|
|
foreach ($object->cleanVars as $k => $v) { |
100
|
|
|
${$k} = $v; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$sql = \sprintf( |
104
|
|
|
'UPDATE `%s` SET ticketid = %u, url = %s, title = %s, description = %s, uid = %u, posted = %u WHERE id = %u', |
105
|
|
|
$this->db->prefix($this->dbtable), |
106
|
|
|
$ticketid, |
|
|
|
|
107
|
|
|
$this->db->quoteString($url), |
|
|
|
|
108
|
|
|
$this->db->quoteString($title), |
|
|
|
|
109
|
|
|
$this->db->quoteString($description), |
|
|
|
|
110
|
|
|
$uid, |
|
|
|
|
111
|
|
|
$posted, |
|
|
|
|
112
|
|
|
$id |
|
|
|
|
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return $sql; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \XoopsObject $object |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function deleteQuery(\XoopsObject $object): string |
123
|
|
|
{ |
124
|
|
|
$sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), $object->getVar('id')); |
|
|
|
|
125
|
|
|
|
126
|
|
|
return $sql; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Recommend solutions to a ticket based on similarity |
131
|
|
|
* to previous tickets and their solutions |
132
|
|
|
* @param Ticket $ticket ticket to search for solutions |
133
|
|
|
* @return array Value 1 = bayesian likeness probability, Value 2 = TicketSolution object |
134
|
|
|
*/ |
135
|
|
|
public function &recommendSolutions(Ticket $ticket): array |
136
|
|
|
{ |
137
|
|
|
$ret = []; |
138
|
|
|
|
139
|
|
|
//1. Get list of bayesian categories(tickets) similar to current ticket |
140
|
|
|
$bayes = new NaiveBayesian(new NaiveBayesianStorage()); |
141
|
|
|
$document = $ticket->getVar('subject') . "\r\n" . $ticket->getVar('description'); |
142
|
|
|
$cats = $bayes->categorize($document); |
143
|
|
|
|
144
|
|
|
//2. Get solutions to those tickets |
145
|
|
|
$criteria = new \Criteria('ticketid', '(' . \implode(',', \array_keys($cats)) . ')', 'IN'); |
146
|
|
|
$solutions = $this->getObjects($criteria); |
147
|
|
|
|
148
|
|
|
//3. Sort solutions based on likeness probability |
149
|
|
|
foreach ($solutions as $solution) { |
150
|
|
|
$ret[] = [ |
151
|
|
|
'probability' => $cats[$solution->getVar('ticketid')], |
152
|
|
|
'solution' => $solution, |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
unset($solutions); |
156
|
|
|
|
157
|
|
|
return $this->multi_sort($ret, 'probability'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Ticket $ticket |
162
|
|
|
* @param \XoopsModules\Xhelp\TicketSolution $solution |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function addSolution(Ticket $ticket, TicketSolution $solution): bool |
166
|
|
|
{ |
167
|
|
|
//1. Store solution in db for current ticket |
168
|
|
|
if ($this->insert($solution)) { |
169
|
|
|
//2. Train Bayesian DB |
170
|
|
|
$bayes = new NaiveBayesian(new NaiveBayesianStorage()); |
171
|
|
|
$documentid = (string)$ticket->getVar('id'); |
172
|
|
|
$categoryid = (string)$ticket->getVar('id'); |
173
|
|
|
$document = $ticket->getVar('subject') . "\r\n" . $ticket->getVar('description'); |
174
|
|
|
$bayes->train($documentid, $categoryid, $document); |
175
|
|
|
|
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param array $array |
184
|
|
|
* @param string $akey |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
public function &multi_sort(array $array, string $akey): array |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
/** |
190
|
|
|
* @param array $a |
191
|
|
|
* @param array $b |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
function _compare(array $a, array $b): string |
195
|
|
|
{ |
196
|
|
|
global $key; |
197
|
|
|
if ($a[$key] > $b[$key]) { |
198
|
|
|
$varcmp = '1'; |
199
|
|
|
} elseif ($a[$key] < $b[$key]) { |
200
|
|
|
$varcmp = '-1'; |
201
|
|
|
} elseif ($a[$key] == $b[$key]) { |
202
|
|
|
$varcmp = '0'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $varcmp; |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
\usort($array, '_compare'); |
209
|
|
|
|
210
|
|
|
return $array; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|