Completed
Push — master ( e4a341...7497e8 )
by Michael
14s
created

NewbbOnlineHandler   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 351
Duplicated Lines 17.38 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 61
loc 351
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 15 20 4
B update() 0 28 6
C render() 23 55 10
C showOnline() 23 57 10
B write() 0 49 6
A gc() 0 9 1
B getAll() 0 25 6
C checkStatus() 0 28 7
A getCount() 0 13 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like NewbbOnlineHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use NewbbOnlineHandler, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * NewBB 5.0x,  the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
8
 * @since          4.00
9
 * @package        module::newbb
10
 */
11
12
use Xmf\IPAddress;
13
14
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
16
include_once __DIR__ . '/../include/functions.config.php';
17
18
/**
19
 * Class NewbbOnlineHandler
20
 */
21
class NewbbOnlineHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
22
{
23
    public $db;
24
    public $forum_id;
25
    public $forumObject;
26
    public $topic_id;
27
    public $user_ids = [];
28
29
    public function __construct(XoopsDatabase $db)
30
    {
31
        $this->db = $db;
32
    }
33
34
    /**
35
     * @param null|\NewbbForum $forum
36
     * @param null|Topic       $forumtopic
37
     */
38
    public function init($forum = null, $forumtopic = null)
39
    {
40 View Code Duplication
        if (is_object($forum)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
            $this->forum_id     = $forum->getVar('forum_id');
42
            $this->forumObject = $forum;
43
        } else {
44
            $this->forum_id     = (int)$forum;
45
            $this->forumObject = $forum;
46
        }
47 View Code Duplication
        if (is_object($forumtopic)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            $this->topic_id = $forumtopic->getVar('topic_id');
49
            if (empty($this->forum_id)) {
50
                $this->forum_id = $forumtopic->getVar('forum_id');
51
            }
52
        } else {
53
            $this->topic_id = (int)$forumtopic;
54
        }
55
56
        $this->update();
57
    }
58
59
    public function update()
0 ignored issues
show
Coding Style introduced by
update uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
60
    {
61
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
62
63
        mt_srand((double)microtime() * 1000000);
64
        // set gc probabillity to 10% for now..
65
        if (mt_rand(1, 100) < 60) {
66
            $this->gc(150);
67
        }
68
        if (is_object($GLOBALS['xoopsUser'])) {
69
            $uid   = $GLOBALS['xoopsUser']->getVar('uid');
70
            $uname = $GLOBALS['xoopsUser']->getVar('uname');
71
            $name  = $GLOBALS['xoopsUser']->getVar('name');
72
        } else {
73
            $uid   = 0;
74
            $uname = '';
75
            $name  = '';
76
        }
77
78
        $xoops_onlineHandler = xoops_getHandler('online');
79
        $xoopsupdate         = $xoops_onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), Xmf\IPAddress::fromRequest()->asReadable());
80
        if (!$xoopsupdate) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
81
            //xoops_error("newbb online upate error");
82
        }
83
84
        $uname = (empty($GLOBALS['xoopsModuleConfig']['show_realname']) || empty($name)) ? $uname : $name;
85
        $this->write($uid, $uname, time(), $this->forum_id, IPAddress::fromRequest()->asReadable(), $this->topic_id);
86
    }
87
88
    /**
89
     * @param $xoopsTpl
90
     */
91
    public function render(Smarty $xoopsTpl)
92
    {
93
        include_once __DIR__ . '/../include/functions.render.php';
94
        include_once __DIR__ . '/../include/functions.user.php';
95
        $criteria = null;
96 View Code Duplication
        if ($this->topic_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $criteria = new Criteria('online_topic', $this->topic_id);
98
        } elseif ($this->forum_id) {
99
            $criteria = new Criteria('online_forum', $this->forum_id);
100
        }
101
        $users     = $this->getAll($criteria);
102
        $num_total = count($users);
103
104
        $num_user     = 0;
105
        $users_id     = [];
106
        $users_online = [];
107 View Code Duplication
        for ($i = 0; $i < $num_total; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            if (empty($users[$i]['online_uid'])) {
109
                continue;
110
            }
111
            $users_id[]                             = $users[$i]['online_uid'];
112
            $users_online[$users[$i]['online_uid']] = [
113
                'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
114
                'uname' => $users[$i]['online_uname']
115
            ];
116
            ++$num_user;
117
        }
118
        $num_anonymous           = $num_total - $num_user;
119
        $online                  = [];
120
        $online['image']         = newbbDisplayImage('whosonline');
121
        $online['num_total']     = $num_total;
122
        $online['num_user']      = $num_user;
123
        $online['num_anonymous'] = $num_anonymous;
124
        $administrator_list      = newbbIsModuleAdministrators($users_id);
125
        $moderator_list          = [];
126 View Code Duplication
        if ($member_list = array_diff(array_keys($administrator_list), $users_id)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
            if (is_object($this->forumObject)) {
128
                $moderator_list = $this->forumObject->getVar('forum_moderator');
129
            } else {
130
                $moderator_list = newbbIsForumModerators($member_list);
131
            }
132
        }
133
        foreach ($users_online as $uid => $user) {
134
            if (!empty($administrator_list[$uid])) {
135
                $user['level'] = 2;
136
            } elseif (!empty($moderator_list[$uid])) {
137
                $user['level'] = 1;
138
            } else {
139
                $user['level'] = 0;
140
            }
141
            $online['users'][] = $user;
142
        }
143
144
        $xoopsTpl->assign_by_ref('online', $online);
145
    }
146
147
    /**
148
     * Deprecated
149
     */
150
    public function showOnline()
151
    {
152
        include_once __DIR__ . '/../include/functions.render.php';
153
        include_once __DIR__ . '/../include/functions.user.php';
154
        $criteria = null;
155 View Code Duplication
        if ($this->topic_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
            $criteria = new Criteria('online_topic', $this->topic_id);
157
        } elseif ($this->forum_id) {
158
            $criteria = new Criteria('online_forum', $this->forum_id);
159
        }
160
        $users     = $this->getAll($criteria);
161
        $num_total = count($users);
162
163
        $num_user     = 0;
164
        $users_id     = [];
165
        $users_online = [];
166 View Code Duplication
        for ($i = 0; $i < $num_total; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
            if (empty($users[$i]['online_uid'])) {
168
                continue;
169
            }
170
            $users_id[]                             = $users[$i]['online_uid'];
171
            $users_online[$users[$i]['online_uid']] = [
172
                'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
173
                'uname' => $users[$i]['online_uname']
174
            ];
175
            ++$num_user;
176
        }
177
        $num_anonymous           = $num_total - $num_user;
178
        $online                  = [];
179
        $online['image']         = newbbDisplayImage('whosonline');
180
        $online['statistik']     = newbbDisplayImage('statistik');
181
        $online['num_total']     = $num_total;
182
        $online['num_user']      = $num_user;
183
        $online['num_anonymous'] = $num_anonymous;
184
        $administrator_list      = newbbIsModuleAdministrators($users_id);
185
        $moderator_list          = [];
186 View Code Duplication
        if ($member_list = array_diff($users_id, array_keys($administrator_list))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
            if (is_object($this->forumObject)) {
188
                $moderator_list = $this->forumObject->getVar('forum_moderator');
189
            } else {
190
                $moderator_list = newbbIsForumModerators($member_list);
191
            }
192
        }
193
194
        foreach ($users_online as $uid => $user) {
195
            if (in_array($uid, $administrator_list)) {
196
                $user['level'] = 2;
197
            } elseif (in_array($uid, $moderator_list)) {
198
                $user['level'] = 1;
199
            } else {
200
                $user['level'] = 0;
201
            }
202
            $online['users'][] = $user;
203
        }
204
205
        return $online;
206
    }
207
208
    /**
209
     * Write online information to the database
210
     *
211
     * @param  int    $uid      UID of the active user
212
     * @param  string $uname    Username
213
     * @param         $time
214
     * @param  string $forum_id Current forum_id
215
     * @param  string $ip       User's IP adress
216
     * @param         $topic_id
217
     * @return bool   TRUE on success
218
     * @internal param string $timestamp
219
     */
220
    public function write($uid, $uname, $time, $forum_id, $ip, $topic_id)
221
    {
222
        global $xoopsModule, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
223
224
        $uid = (int)$uid;
225
        if ($uid > 0) {
226
            $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid;
227
        } else {
228
            $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid . " AND online_ip='" . $ip . "'";
229
        }
230
        list($count) = $this->db->fetchRow($this->db->queryF($sql));
231
        if ($count > 0) {
232
            $sql = 'UPDATE ' . $this->db->prefix('newbb_online') . " SET online_updated= '" . $time . "', online_forum = '" . $forum_id . "', online_topic = '" . $topic_id . "' WHERE online_uid = " . $uid;
233
            if (0 == $uid) {
234
                $sql .= " AND online_ip='" . $ip . "'";
235
            }
236
        } else {
237
            $sql = sprintf('INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_forum, online_topic) VALUES (%u, %s, %u, %s, %u, %u)', $this->db->prefix('newbb_online'), $uid, $this->db->quote($uname), $time, $this->db->quote($ip), $forum_id, $topic_id);
238
        }
239
        if (!$this->db->queryF($sql)) {
240
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
241
            return false;
242
        }
243
244
        /** @var XoopsOnlineHandler $xoops_onlineHandler */
245
        $xoops_onlineHandler = xoops_getHandler('online');
246
        $xoopsOnlineTable    = $xoops_onlineHandler->table;
247
248
        $sql = 'DELETE FROM '
249
               . $this->db->prefix('newbb_online')
250
               . ' WHERE'
251
               . ' ( online_uid > 0 AND online_uid NOT IN ( SELECT online_uid FROM '
252
               . $xoopsOnlineTable
253
               . ' WHERE online_module ='
254
               . $xoopsModule->getVar('mid')
255
               . ' ) )'
256
               . ' OR ( online_uid = 0 AND online_ip NOT IN ( SELECT online_ip FROM '
257
               . $xoopsOnlineTable
258
               . ' WHERE online_module ='
259
               . $xoopsModule->getVar('mid')
260
               . ' AND online_uid = 0 ) )';
261
262
        if ($result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
263
            return true;
264
        } else {
265
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
266
            return false;
267
        }
268
    }
269
270
    /**
271
     * Garbage Collection
272
     *
273
     * Delete all online information that has not been updated for a certain time
274
     *
275
     * @param int $expire Expiration time in seconds
276
     */
277
    public function gc($expire)
278
    {
279
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
280
        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_updated < ' . (time() - (int)$expire);
281
        $this->db->queryF($sql);
282
283
        $xoops_onlineHandler = xoops_getHandler('online');
284
        $xoops_onlineHandler->gc($expire);
285
    }
286
287
    /**
288
     * Get an array of online information
289
     *
290
     * @param  CriteriaElement $criteria {@link CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
291
     * @return array           Array of associative arrays of online information
292
     */
293
    public function getAll(CriteriaElement $criteria = null)
294
    {
295
        $ret   = [];
296
        $limit = $start = 0;
297
        $sql   = 'SELECT * FROM ' . $this->db->prefix('newbb_online');
298
        if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
299
            $sql   .= ' ' . $criteria->renderWhere();
300
            $limit = $criteria->getLimit();
301
            $start = $criteria->getStart();
302
        }
303
        $result = $this->db->query($sql, $limit, $start);
304
        if (!$result) {
305
            return $ret;
306
        }
307
        while ($myrow = $this->db->fetchArray($result)) {
308
            $ret[] = $myrow;
309
            if ($myrow['online_uid'] > 0) {
310
                $this->user_ids[] = $myrow['online_uid'];
311
            }
312
            unset($myrow);
313
        }
314
        $this->user_ids = array_unique($this->user_ids);
315
316
        return $ret;
317
    }
318
319
    /**
320
     * @param $uids
321
     * @return array
322
     */
323
    public function checkStatus($uids)
324
    {
325
        $online_users = [];
326
        $ret          = [];
327
        if (!empty($this->user_ids)) {
328
            $online_users = $this->user_ids;
329
        } else {
330
            $sql = 'SELECT online_uid FROM ' . $this->db->prefix('newbb_online');
331
            if (!empty($uids)) {
332
                $sql .= ' WHERE online_uid IN (' . implode(', ', array_map('intval', $uids)) . ')';
333
            }
334
335
            $result = $this->db->query($sql);
336
            if (!$result) {
337
                return $ret;
338
            }
339
            while (list($uid) = $this->db->fetchRow($result)) {
340
                $online_users[] = $uid;
341
            }
342
        }
343
        foreach ($uids as $uid) {
344
            if (in_array($uid, $online_users)) {
345
                $ret[$uid] = 1;
346
            }
347
        }
348
349
        return $ret;
350
    }
351
352
    /**
353
     * Count the number of online users
354
     *
355
     * @param  CriteriaElement $criteria {@link CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
356
     * @return bool
357
     */
358
    public function getCount(CriteriaElement $criteria = null)
359
    {
360
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online');
361
        if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
362
            $sql .= ' ' . $criteria->renderWhere();
363
        }
364
        if (!$result = $this->db->query($sql)) {
365
            return false;
366
        }
367
        list($ret) = $this->db->fetchRow($result);
368
369
        return $ret;
370
    }
371
}
372