_compare()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
dl 0
loc 12
rs 9.8666
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,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
80
            $ticketid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ticketid seems to be never defined.
Loading history...
81
            $this->db->quoteString($url),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
82
            $this->db->quoteString($title),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title seems to be never defined.
Loading history...
83
            $this->db->quoteString($description),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
84
            $uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
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,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ticketid seems to be never defined.
Loading history...
107
            $this->db->quoteString($url),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
108
            $this->db->quoteString($title),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title seems to be never defined.
Loading history...
109
            $this->db->quoteString($description),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
110
            $uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
111
            $posted,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $posted seems to be never defined.
Loading history...
112
            $id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('id') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), /** @scrutinizer ignore-type */ $object->getVar('id'));
Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $akey is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

187
    public function &multi_sort(array $array, /** @scrutinizer ignore-unused */ string $akey): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $varcmp does not seem to be defined for all execution paths leading up to this point.
Loading history...
206
        }
207
208
        \usort($array, '_compare');
209
210
        return $array;
211
    }
212
}
213