Passed
Branch master (410c7b)
by Michael
03:30
created

RelgroupuserHandler::getUsersFromGroup()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 45
rs 9.568
c 0
b 0
f 0
cc 4
nc 6
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         suico
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use CriteriaElement;
26
use XoopsDatabase;
27
use XoopsObject;
28
use XoopsPersistableObjectHandler;
29
30
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
31
32
// -------------------------------------------------------------------------
33
// ------------------Relgroupuser user handler class -------------------
34
// -------------------------------------------------------------------------
35
36
/**
37
 * suico_relgroupuserhandler class.
38
 * This class provides simple mecanisme for Relgroupuser object
39
 */
40
class RelgroupuserHandler extends XoopsPersistableObjectHandler
41
{
42
    public $helper;
43
44
    public $isAdmin;
45
46
    /**
47
     * Constructor
48
     * @param \XoopsDatabase|null              $xoopsDatabase
49
     * @param \XoopsModules\Suico\Helper|null $helper
50
     */
51
52
    public function __construct(
53
        ?XoopsDatabase $xoopsDatabase = null,
54
        $helper = null
55
    ) {
56
        /** @var \XoopsModules\Suico\Helper $this ->helper */
57
58
        if (null === $helper) {
59
            $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Suico\Helper.
Loading history...
60
        } else {
61
            $this->helper = $helper;
62
        }
63
64
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
65
66
        parent::__construct($xoopsDatabase, 'suico_relgroupuser', Relgroupuser::class, 'rel_id', 'rel_id');
67
    }
68
69
    /**
70
     * create a new Groups
71
     *
72
     * @param bool $isNew flag the new objects as "new"?
73
     * @return \XoopsObject Groups
74
     */
75
76
    public function create(
77
        $isNew = true
78
    ) {
79
        $obj = parent::create($isNew);
80
81
        if ($isNew) {
82
            $obj->setNew();
83
        } else {
84
            $obj->unsetNew();
85
        }
86
87
        $obj->helper = $this->helper;
88
89
        return $obj;
90
    }
91
92
    /**
93
     * retrieve a Relgroupuser
94
     *
95
     * @param int  $id of the Relgroupuser
96
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
97
     * @return mixed reference to the {@link Relgroupuser} object, FALSE if failed
98
     */
99
100
    public function get2(
101
        $id = null,
102
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields 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

102
        /** @scrutinizer ignore-unused */ $fields = null

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...
103
    ) {
104
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser') . ' WHERE rel_id=' . $id;
105
106
        if (!$result = $this->db->query($sql)) {
107
            return false;
108
        }
109
110
        $numrows = $this->db->getRowsNum($result);
111
112
        if (1 === $numrows) {
113
            $suico_relgroupuser = new Relgroupuser();
114
115
            $suico_relgroupuser->assignVars($this->db->fetchArray($result));
116
117
            return $suico_relgroupuser;
118
        }
119
120
        return false;
121
    }
122
123
    /**
124
     * insert a new Relgroupuser in the database
125
     *
126
     * @param \XoopsObject $xoopsObject         reference to the {@link Relgroupuser}
127
     *                                          object
128
     * @param bool         $force
129
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
130
     */
131
132
    public function insert2(
133
        XoopsObject $xoopsObject,
134
        $force = false
135
    ) {
136
        global $xoopsConfig;
137
138
        if (!$xoopsObject instanceof Relgroupuser) {
139
            return false;
140
        }
141
142
        if (!$xoopsObject->isDirty()) {
143
            return true;
144
        }
145
146
        if (!$xoopsObject->cleanVars()) {
147
            return false;
148
        }
149
150
        foreach ($xoopsObject->cleanVars as $k => $v) {
151
            ${$k} = $v;
152
        }
153
154
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
0 ignored issues
show
Unused Code introduced by
The assignment to $now is dead and can be removed.
Loading history...
155
156
        if ($xoopsObject->isNew()) {
157
            // ajout/modification d'un Relgroupuser
158
159
            $xoopsObject = new Relgroupuser();
160
161
            $format = 'INSERT INTO %s (rel_id, rel_group_id, rel_user_uid)';
162
163
            $format .= 'VALUES (%u, %u, %u)';
164
165
            $sql = \sprintf($format, $this->db->prefix('suico_relgroupuser'), $rel_id, $rel_group_id, $rel_user_uid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rel_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $rel_group_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $rel_user_uid seems to be never defined.
Loading history...
166
167
            $force = true;
168
        } else {
169
            $format = 'UPDATE %s SET ';
170
171
            $format .= 'rel_id=%u, rel_group_id=%u, rel_user_uid=%u';
172
173
            $format .= ' WHERE rel_id = %u';
174
175
            $sql = \sprintf(
176
                $format,
177
                $this->db->prefix('suico_relgroupuser'),
178
                $rel_id,
179
                $rel_group_id,
180
                $rel_user_uid,
181
                $rel_id
182
            );
183
        }
184
185
        if ($force) {
186
            $result = $this->db->queryF($sql);
187
        } else {
188
            $result = $this->db->query($sql);
189
        }
190
191
        if (!$result) {
192
            return false;
193
        }
194
195
        if (empty($rel_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rel_id seems to never exist and therefore empty should always be true.
Loading history...
196
            $rel_id = $this->db->getInsertId();
197
        }
198
199
        $xoopsObject->assignVar('rel_id', $rel_id);
200
201
        return true;
202
    }
203
204
    /**
205
     * delete a Relgroupuser from the database
206
     *
207
     * @param \XoopsObject $xoopsObject reference to the Relgroupuser to delete
208
     * @param bool         $force
209
     * @return bool FALSE if failed.
210
     */
211
212
    public function delete(
213
        XoopsObject $xoopsObject,
214
        $force = false
215
    ) {
216
        if (!$xoopsObject instanceof Relgroupuser) {
217
            return false;
218
        }
219
220
        $sql = \sprintf(
221
            'DELETE FROM %s WHERE rel_id = %u',
222
            $this->db->prefix('suico_relgroupuser'),
223
            $xoopsObject->getVar('rel_id')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('rel_id') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept 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

223
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('rel_id')
Loading history...
224
        );
225
226
        if ($force) {
227
            $result = $this->db->queryF($sql);
228
        } else {
229
            $result = $this->db->query($sql);
230
        }
231
232
        if (!$result) {
233
            return false;
234
        }
235
236
        return true;
237
    }
238
239
    /**
240
     * retrieve suico_relgroupusers from the database
241
     *
242
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
243
     * @param bool                                 $id_as_key       use the UID as key for the array?
244
     * @param bool                                 $as_object
245
     * @return array array of {@link Relgroupuser} objects
246
     */
247
248
    public function &getObjects(
249
        ?CriteriaElement $criteriaElement = null,
250
        $id_as_key = false,
251
        $as_object = true
252
    ) {
253
        $ret = [];
254
255
        $limit = $start = 0;
256
257
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser');
258
259
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
260
            $sql .= ' ' . $criteriaElement->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

260
            $sql .= ' ' . $criteriaElement->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
261
262
            if ('' !== $criteriaElement->getSort()) {
263
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
264
            }
265
266
            $limit = $criteriaElement->getLimit();
267
268
            $start = $criteriaElement->getStart();
269
        }
270
271
        $result = $this->db->query($sql, $limit, $start);
272
273
        if (!$result) {
274
            return $ret;
275
        }
276
277
        while (false !== ($myrow = $this->db->fetchArray($result))) {
278
            $suico_relgroupuser = new Relgroupuser();
279
280
            $suico_relgroupuser->assignVars($myrow);
281
282
            if (!$id_as_key) {
283
                $ret[] = &$suico_relgroupuser;
284
            } else {
285
                $ret[$myrow['rel_id']] = &$suico_relgroupuser;
286
            }
287
288
            unset($suico_relgroupuser);
289
        }
290
291
        return $ret;
292
    }
293
294
    /**
295
     * count suico_relgroupusers matching a condition
296
     *
297
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
298
     * @return int count of suico_relgroupusers
299
     */
300
301
    public function getCount(
302
        ?CriteriaElement $criteriaElement = null
303
    ) {
304
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_relgroupuser');
305
306
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
307
            $sql .= ' ' . $criteriaElement->renderWhere();
308
        }
309
310
        $result = $this->db->query($sql);
311
312
        if (!$result) {
313
            return 0;
314
        }
315
316
        [$count] = $this->db->fetchRow($result);
317
318
        return (int)$count;
319
    }
320
321
    /**
322
     * delete suico_relgroupusers matching a set of conditions
323
     *
324
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
325
     * @param bool                                 $force
326
     * @param bool                                 $asObject
327
     * @return bool FALSE if deletion failed
328
     */
329
330
    public function deleteAll(
331
        ?CriteriaElement $criteriaElement = null,
332
        $force = true,
333
        $asObject = false
334
    ) {
335
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_relgroupuser');
336
337
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
338
            $sql .= ' ' . $criteriaElement->renderWhere();
339
        }
340
341
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
342
            return false;
343
        }
344
345
        return true;
346
    }
347
348
    /**
349
     * @param      $countGroups
350
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
351
     * @param int  $shuffle
352
     * @return array
353
     */
354
355
    public function getGroups(
356
        $countGroups,
357
        $criteria = null,
358
        $shuffle = 1
359
    ) {
360
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
361
362
        $sql = 'SELECT rel_id, rel_group_id, rel_user_uid, group_title, group_desc, group_img, owner_uid FROM ' . $this->db->prefix(
363
                'suico_groups'
364
            ) . ', ' . $this->db->prefix(
365
                'suico_relgroupuser'
366
            );
367
368
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
369
            $sql .= ' ' . $criteria->renderWhere();
370
371
            //attention here this is kind of a hack
372
373
            $sql .= ' AND group_id = rel_group_id ';
374
375
            if ('' !== $criteria->getSort()) {
376
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
377
            }
378
379
            $limit = $criteria->getLimit();
380
381
            $start = $criteria->getStart();
382
383
            $result = $this->db->query($sql, $limit, $start);
384
385
            $vetor = [];
386
387
            $i = 0;
388
389
            while (false !== ($myrow = $this->db->fetchArray($result))) {
390
                $vetor[$i]['title'] = $myrow['group_title'];
391
392
                $vetor[$i]['desc'] = $myrow['group_desc'];
393
394
                $vetor[$i]['img'] = $myrow['group_img'];
395
396
                $vetor[$i]['id'] = $myrow['rel_id'];
397
398
                $vetor[$i]['uid'] = $myrow['owner_uid'];
399
400
                $vetor[$i]['group_id'] = $myrow['rel_group_id'];
401
402
                $i++;
403
            }
404
405
            if (1 === $shuffle) {
406
                \shuffle($vetor);
407
408
                $vetor = \array_slice($vetor, 0, $countGroups);
409
            }
410
411
            return $vetor;
412
        }
413
    }
414
415
    /**
416
     * @param     $groupId
417
     * @param     $start
418
     * @param     $nbUsers
419
     * @param int $isShuffle
420
     * @return array
421
     */
422
423
    public function getUsersFromGroup(
424
        $groupId,
425
        $start,
426
        $nbUsers,
427
        $isShuffle = 0
428
    ) {
429
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
430
431
        $sql = 'SELECT rel_group_id, rel_user_uid, owner_uid, uname, user_avatar, uid FROM ' . $this->db->prefix(
432
                'users'
433
            ) . ', ' . $this->db->prefix(
434
                'suico_groups'
435
            ) . ', ' . $this->db->prefix(
436
                'suico_relgroupuser'
437
            );
438
439
        $sql .= ' WHERE rel_user_uid = uid AND rel_group_id = group_id AND group_id =' . $groupId . ' GROUP BY rel_user_uid ';
440
441
        $result = $this->db->query($sql, $nbUsers, $start);
442
443
        $ret = [];
444
445
        $i = 0;
446
447
        while (false !== ($myrow = $this->db->fetchArray($result))) {
448
            $ret[$i]['uid'] = $myrow['uid'];
449
450
            $ret[$i]['uname'] = $myrow['uname'];
451
452
            $ret[$i]['avatar'] = $myrow['user_avatar'];
453
454
            $isOwner = $myrow['rel_user_uid'] === $myrow['owner_uid'] ? 1 : 0;
455
456
            $ret[$i]['isOwner'] = $isOwner;
457
458
            $i++;
459
        }
460
461
        if (1 === $isShuffle) {
462
            \shuffle($ret);
463
464
            $ret = \array_slice($ret, 0, $nbUsers);
465
        }
466
467
        return $ret;
468
    }
469
}
470