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

FriendshipHandler   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 687
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 56
eloc 289
dl 0
loc 687
rs 5.5199
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B getFans() 0 53 6
A getCount() 0 18 4
B renderFormSubmit() 0 150 4
A delete() 0 25 4
B getFriends() 0 53 6
B insert2() 0 88 9
A __construct() 0 15 2
A get2() 0 21 3
A create() 0 14 2
A getMoyennes() 0 72 5
B getObjects() 0 44 7
A deleteAll() 0 16 4

How to fix   Complexity   

Complex Class

Complex classes like FriendshipHandler 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.

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 FriendshipHandler, and based on these observations, apply Extract Interface, too.

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 Criteria;
26
use CriteriaCompo;
27
use CriteriaElement;
28
use XoopsDatabase;
29
use XoopsFormButton;
30
use XoopsFormHidden;
31
use XoopsFormLabel;
32
use XoopsFormRadio;
33
use XoopsFormRadioYN;
34
use XoopsObject;
35
use XoopsPersistableObjectHandler;
36
use XoopsThemeForm;
37
38
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
39
/**
40
 * Includes of form objects and uploader
41
 */
42
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
43
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
44
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
45
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
46
47
/**
48
 * suico_friendshiphandler class.
49
 * This class provides simple mecanisme for Friendship object
50
 */
51
class FriendshipHandler extends XoopsPersistableObjectHandler
52
{
53
    public $helper;
54
55
    public $isAdmin;
56
57
    /**
58
     * Constructor
59
     * @param \XoopsDatabase|null              $xoopsDatabase
60
     * @param \XoopsModules\Suico\Helper|null $helper
61
     */
62
63
    public function __construct(
64
        ?XoopsDatabase $xoopsDatabase = null,
65
        $helper = null
66
    ) {
67
        /** @var \XoopsModules\Suico\Helper $this ->helper */
68
69
        if (null === $helper) {
70
            $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...
71
        } else {
72
            $this->helper = $helper;
73
        }
74
75
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
76
77
        parent::__construct($xoopsDatabase, 'suico_friendships', Friendship::class, 'friendship_id', 'friendship_id');
78
    }
79
80
    /**
81
     * create a new Groups
82
     *
83
     * @param bool $isNew flag the new objects as "new"?
84
     * @return \XoopsObject Groups
85
     */
86
87
    public function create(
88
        $isNew = true
89
    ) {
90
        $obj = parent::create($isNew);
91
92
        if ($isNew) {
93
            $obj->setNew();
94
        } else {
95
            $obj->unsetNew();
96
        }
97
98
        $obj->helper = $this->helper;
99
100
        return $obj;
101
    }
102
103
    /**
104
     * retrieve a Friendship
105
     *
106
     * @param int  $id of the Friendship
107
     * @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...
108
     * @return mixed reference to the {@link Friendship} object, FALSE if failed
109
     */
110
111
    public function get2(
112
        $id = null,
113
        $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

113
        /** @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...
114
    ) {
115
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_friendships') . ' WHERE friendship_id=' . $id;
116
117
        if (!$result = $this->db->query($sql)) {
118
            return false;
119
        }
120
121
        $numrows = $this->db->getRowsNum($result);
122
123
        if (1 === $numrows) {
124
            $suico_friendship = new Friendship();
125
126
            $suico_friendship->assignVars($this->db->fetchArray($result));
127
128
            return $suico_friendship;
129
        }
130
131
        return false;
132
    }
133
134
    /**
135
     * insert a new Friendship in the database
136
     *
137
     * @param \XoopsObject $xoopsObject       reference to the {@link Friendship}
138
     *                                        object
139
     * @param bool         $force
140
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
141
     */
142
143
    public function insert2(
144
        XoopsObject $xoopsObject,
145
        $force = false
146
    ) {
147
        global $xoopsConfig;
148
149
        if (!$xoopsObject instanceof Friendship) {
150
            return false;
151
        }
152
153
        if (!$xoopsObject->isDirty()) {
154
            return true;
155
        }
156
157
        if (!$xoopsObject->cleanVars()) {
158
            return false;
159
        }
160
161
        $friendship_id = $friend1_uid = $friend2_uid = $level = $hot = $trust = $cool = $fan = '';
162
163
        foreach ($xoopsObject->cleanVars as $k => $v) {
164
            ${$k} = $v;
165
        }
166
167
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
168
169
        if ($xoopsObject->isNew()) {
170
            // ajout/modification d'un Friendship
171
172
            $xoopsObject = new Friendship();
173
174
            $format = 'INSERT INTO %s (friendship_id, friend1_uid, friend2_uid, LEVEL, hot, trust, cool, fan)';
175
176
            $format .= 'VALUES (%u, %u, %u, %u, %u, %u, %u, %u)';
177
178
            $sql = \sprintf(
179
                $format,
180
                $this->db->prefix('suico_friendships'),
181
                $friendship_id,
182
                $friend1_uid,
183
                $friend2_uid,
184
                $level,
185
                $hot,
186
                $trust,
187
                $cool,
188
                $fan
189
            );
190
191
            $force = true;
192
        } else {
193
            $format = 'UPDATE %s SET ';
194
195
            $format .= 'friendship_id=%u, friend1_uid=%u, friend2_uid=%u, level=%u, hot=%u, trust=%u, cool=%u, fan=%u';
196
197
            $format .= ' WHERE friendship_id = %u';
198
199
            $sql = \sprintf(
200
                $format,
201
                $this->db->prefix('suico_friendships'),
202
                $friendship_id,
203
                $friend1_uid,
204
                $friend2_uid,
205
                $level,
206
                $hot,
207
                $trust,
208
                $cool,
209
                $fan,
210
                $friendship_id
211
            );
212
        }
213
214
        if ($force) {
215
            $result = $this->db->queryF($sql);
216
        } else {
217
            $result = $this->db->query($sql);
218
        }
219
220
        if (!$result) {
221
            return false;
222
        }
223
224
        if (empty($friendship_id)) {
0 ignored issues
show
introduced by
The condition empty($friendship_id) is always true.
Loading history...
225
            $friendship_id = $this->db->getInsertId();
226
        }
227
228
        $xoopsObject->assignVar('friendship_id', $friendship_id);
229
230
        return true;
231
    }
232
233
    /**
234
     * delete a Friendship from the database
235
     *
236
     * @param \XoopsObject $xoopsObject reference to the Friendship to delete
237
     * @param bool         $force
238
     * @return bool FALSE if failed.
239
     */
240
241
    public function delete(
242
        XoopsObject $xoopsObject,
243
        $force = false
244
    ) {
245
        if (!$xoopsObject instanceof Friendship) {
246
            return false;
247
        }
248
249
        $sql = \sprintf(
250
            'DELETE FROM %s WHERE friendship_id = %u',
251
            $this->db->prefix('suico_friendships'),
252
            $xoopsObject->getVar('friendship_id')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('friendship_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

252
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('friendship_id')
Loading history...
253
        );
254
255
        if ($force) {
256
            $result = $this->db->queryF($sql);
257
        } else {
258
            $result = $this->db->query($sql);
259
        }
260
261
        if (!$result) {
262
            return false;
263
        }
264
265
        return true;
266
    }
267
268
    /**
269
     * retrieve suico_friendships from the database
270
     *
271
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
272
     * @param bool                                 $id_as_key       use the UID as key for the array?
273
     * @param bool                                 $as_object
274
     * @return array array of {@link Friendship} objects
275
     */
276
277
    public function &getObjects(
278
        ?CriteriaElement $criteriaElement = null,
279
        $id_as_key = false,
280
        $as_object = true
281
    ) {
282
        $ret = [];
283
284
        $limit = $start = 0;
285
286
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_friendships');
287
288
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
289
            $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

289
            $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...
290
291
            if ('' !== $criteriaElement->getSort()) {
292
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
293
            }
294
295
            $limit = $criteriaElement->getLimit();
296
297
            $start = $criteriaElement->getStart();
298
        }
299
300
        $result = $this->db->query($sql, $limit, $start);
301
302
        if (!$result) {
303
            return $ret;
304
        }
305
306
        while (false !== ($myrow = $this->db->fetchArray($result))) {
307
            $suico_friendship = new Friendship();
308
309
            $suico_friendship->assignVars($myrow);
310
311
            if (!$id_as_key) {
312
                $ret[] = &$suico_friendship;
313
            } else {
314
                $ret[$myrow['friendship_id']] = &$suico_friendship;
315
            }
316
317
            unset($suico_friendship);
318
        }
319
320
        return $ret;
321
    }
322
323
    /**
324
     * count suico_friendships matching a condition
325
     *
326
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
327
     * @return int count of suico_friendships
328
     */
329
330
    public function getCount(
331
        ?CriteriaElement $criteriaElement = null
332
    ) {
333
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_friendships');
334
335
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
336
            $sql .= ' ' . $criteriaElement->renderWhere();
337
        }
338
339
        $result = $this->db->query($sql);
340
341
        if (!$result) {
342
            return 0;
343
        }
344
345
        [$count] = $this->db->fetchRow($result);
346
347
        return (int)$count;
348
    }
349
350
    /**
351
     * delete suico_friendships matching a set of conditions
352
     *
353
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
354
     * @param bool                                 $force
355
     * @param bool                                 $asObject
356
     * @return bool FALSE if deletion failed
357
     */
358
359
    public function deleteAll(
360
        ?CriteriaElement $criteriaElement = null,
361
        $force = true,
362
        $asObject = false
363
    ) {
364
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_friendships');
365
366
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
367
            $sql .= ' ' . $criteriaElement->renderWhere();
368
        }
369
370
        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...
371
            return false;
372
        }
373
374
        return true;
375
    }
376
377
    /**
378
     * @param      $countFriends
379
     * @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...
380
     * @param int  $shuffle
381
     * @return array
382
     */
383
384
    public function getFriends(
385
        $countFriends,
386
        $criteria = null,
387
        $shuffle = 1
388
    ) {
389
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
390
391
        $limit = $start = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $start is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $limit is dead and can be removed.
Loading history...
392
393
        $sql = 'SELECT uname, user_avatar, friend2_uid FROM ' . $this->db->prefix(
394
                'suico_friendships'
395
            ) . ', ' . $this->db->prefix(
396
                'users'
397
            );
398
399
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
400
            $sql .= ' ' . $criteria->renderWhere();
401
402
            //attention here this is kind of a hack
403
404
            $sql .= ' AND uid = friend2_uid ';
405
406
            if ('' !== $criteria->getSort()) {
407
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
408
            }
409
410
            $limit = $criteria->getLimit();
411
412
            $start = $criteria->getStart();
413
414
            $result = $this->db->query($sql, $limit, $start);
415
416
            $vetor = [];
417
418
            $i = 0;
419
420
            while (false !== ($myrow = $this->db->fetchArray($result))) {
421
                $vetor[$i]['uid'] = $myrow['friend2_uid'];
422
423
                $vetor[$i]['uname'] = $myrow['uname'];
424
425
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
426
427
                $i++;
428
            }
429
430
            if (1 === $shuffle) {
431
                \shuffle($vetor);
432
433
                $vetor = \array_slice($vetor, 0, $countFriends);
434
            }
435
436
            return $vetor;
437
        }
438
    }
439
440
    /**
441
     * @param      $countFriends
442
     * @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...
443
     * @param int  $shuffle
444
     * @return array
445
     */
446
447
    public function getFans(
448
        $countFriends,
449
        $criteria = null,
450
        $shuffle = 1
451
    ) {
452
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
453
454
        $limit = $start = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $start is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $limit is dead and can be removed.
Loading history...
455
456
        $sql = 'SELECT uname, user_avatar, friend1_uid FROM ' . $this->db->prefix(
457
                'suico_friendships'
458
            ) . ', ' . $this->db->prefix(
459
                'users'
460
            );
461
462
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
463
            $sql .= ' ' . $criteria->renderWhere();
464
465
            //attention here this is kind of a hack
466
467
            $sql .= ' AND uid = friend1_uid ';
468
469
            if ('' !== $criteria->getSort()) {
470
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
471
            }
472
473
            $limit = $criteria->getLimit();
474
475
            $start = $criteria->getStart();
476
477
            $result = $this->db->query($sql, $limit, $start);
478
479
            $vetor = [];
480
481
            $i = 0;
482
483
            while (false !== ($myrow = $this->db->fetchArray($result))) {
484
                $vetor[$i]['uid'] = $myrow['friend1_uid'];
485
486
                $vetor[$i]['uname'] = $myrow['uname'];
487
488
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
489
490
                $i++;
491
            }
492
493
            if (1 === $shuffle) {
494
                \shuffle($vetor);
495
496
                $vetor = \array_slice($vetor, 0, $countFriends);
497
            }
498
499
            return $vetor;
500
        }
501
    }
502
503
    /**
504
     * @param $friend
505
     */
506
507
    public function renderFormSubmit($friend)
508
    {
509
        global $xoopsUser;
510
511
        /**
512
         * criteria fetch friendship to be edited
513
         */
514
515
        $criteria_friend1 = new Criteria(
516
            'friend1_uid', $xoopsUser->getVar(
517
            'uid'
518
        )
519
        );
520
521
        $field_friend_fan = $field_friend_friendly = $field_friend_funny = $field_friend_cool = '';
522
523
        $criteria_friend2 = new Criteria('friend2_uid', $friend->getVar('uid'));
524
525
        $criteria_friendship = new CriteriaCompo($criteria_friend1);
526
527
        $criteria_friendship->add($criteria_friend2);
528
529
        $friendships = $this->getObjects($criteria_friendship);
530
531
        $friendship = $friendships[0];
532
533
        $form = new XoopsThemeForm(
534
            \_MD_SUICO_EDIT_FRIENDSHIP, 'form_editfriendship', 'editfriendship.php', 'post', true
535
        );
536
537
        //$field_friend_avatar      = new XoopsFormLabel(_MD_SUICO_PHOTO, "<img src=../../uploads/".$friend->getVar('user_avatar').">");
538
539
        if ('avatars/blank.gif' === $friend->getVar(
540
                'user_avatar'
541
            )) {
542
            $field_friend_avatar = new XoopsFormLabel(\_MD_SUICO_PHOTO, '<img src=assets/images/noavatar.gif>');
543
        } else {
544
            $field_friend_avatar = new XoopsFormLabel(
545
                \_MD_SUICO_PHOTO, '<img src=../../uploads/' . $friend->getVar(
546
                                     'user_avatar'
547
                                 ) . '>'
548
            );
549
        }
550
551
        $field_friend_name = new XoopsFormLabel(\_MD_SUICO_FRIENDNAME, $friend->getVar('uname'));
552
553
        if (1 === $this->helper->getConfig('allow_friendshiplevel')) {
554
            $field_friend_level = new XoopsFormRadio(\_MD_SUICO_LEVEL, 'level', $friendship->getVar('level'), '<br>');
555
556
            $field_friend_level->addOption('1', \_MD_SUICO_UNKNOWN_ACCEPTED);
557
558
            $field_friend_level->addOption('3', \_MD_SUICO_AQUAITANCE);
559
560
            $field_friend_level->addOption('5', \_MD_SUICO_FRIEND);
561
562
            $field_friend_level->addOption('7', \_MD_SUICO_BESTFRIEND);
563
        }
564
565
        if (1 === $this->helper->getConfig('allow_fanssevaluation')) {
566
            $field_friend_fan = new XoopsFormRadioYN(
567
                \_MD_SUICO_FAN, 'fan', $friendship->getVar(
568
                'fan'
569
            ), '<img src="assets/images/fans.gif" alt="' . \_YES . '" title="' . \_YES . '">', '<img src="assets/images/fansbw.gif" alt="' . \_NO . '" title="' . \_NO . '">'
570
            );
571
572
            $field_friend_friendly = new XoopsFormRadio(\_MD_SUICO_FRIENDLY, 'hot', $friendship->getVar('hot'));
573
574
            $field_friend_friendly->addOption(
575
                '1',
576
                '<img src="assets/images/friendlya.gif" alt="' . \_MD_SUICO_FRIENDLYNO . '" title="' . \_MD_SUICO_FRIENDLYNO . '">'
577
            );
578
579
            $field_friend_friendly->addOption(
580
                '2',
581
                '<img src="assets/images/friendlyb.gif" alt="' . \_MD_SUICO_FRIENDLYYES . '" title="' . \_MD_SUICO_FRIENDLYYES . '">'
582
            );
583
584
            $field_friend_friendly->addOption(
585
                '3',
586
                '<img src="assets/images/friendlyc.gif" alt="' . \_MD_SUICO_FRIENDLYALOT . '" title="' . \_MD_SUICO_FRIENDLYALOT . '">'
587
            );
588
589
            $field_friend_funny = new XoopsFormRadio(\_MD_SUICO_FUNNY, 'trust', $friendship->getVar('trust'));
590
591
            $field_friend_funny->addOption(
592
                '1',
593
                '<img src="assets/images/funnya.gif" alt="' . \_MD_SUICO_FUNNYNO . '" title="' . \_MD_SUICO_FUNNYNO . '">'
594
            );
595
596
            $field_friend_funny->addOption(
597
                '2',
598
                '<img src="assets/images/funnyb.gif" alt="' . \_MD_SUICO_FUNNYYES . '" title="' . \_MD_SUICO_FUNNYYES . '">'
599
            );
600
601
            $field_friend_funny->addOption(
602
                '3',
603
                '<img src="assets/images/funnyc.gif" alt="' . \_MD_SUICO_FUNNYALOT . '" title="' . \_MD_SUICO_FUNNYALOT . '">'
604
            );
605
606
            $field_friend_cool = new XoopsFormRadio(\_MD_SUICO_COOL, 'cool', $friendship->getVar('cool'));
607
608
            $field_friend_cool->addOption(
609
                '1',
610
                '<img src="assets/images/coola.gif" alt="' . \_MD_SUICO_COOLNO . '" title="' . \_MD_SUICO_COOLNO . '">'
611
            );
612
613
            $field_friend_cool->addOption(
614
                '2',
615
                '<img src="assets/images/coolb.gif" alt="' . \_MD_SUICO_COOLYES . '" title="' . \_MD_SUICO_COOLYES . '">'
616
            );
617
618
            $field_friend_cool->addOption(
619
                '3',
620
                '<img src="assets/images/coolc.gif" alt="' . \_MD_SUICO_COOLALOT . '" title="' . \_MD_SUICO_COOLALOT . '">'
621
            );
622
        }
623
624
        $form->setExtra('enctype="multipart/form-data"');
625
626
        $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPDATEFRIEND, 'submit');
627
628
        $field_friend_friendid = new XoopsFormHidden('friend_uid', $friend->getVar('uid'));
629
630
        $field_friend_marker = new XoopsFormHidden('marker', '1');
631
632
        $field_friend_friendshio_id = new XoopsFormHidden('friendship_id', $friendship->getVar('friendship_id'));
633
634
        $form->addElement($field_friend_friendid);
635
636
        $form->addElement($field_friend_friendshio_id);
637
638
        $form->addElement($field_friend_marker);
639
640
        $form->addElement($field_friend_avatar);
641
642
        $form->addElement($field_friend_name);
643
644
        $form->addElement($field_friend_level);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $field_friend_level does not seem to be defined for all execution paths leading up to this point.
Loading history...
645
646
        $form->addElement($field_friend_fan);
647
648
        $form->addElement($field_friend_friendly);
649
650
        $form->addElement($field_friend_funny);
651
652
        $form->addElement($field_friend_cool);
653
654
        $form->addElement($buttonSend);
655
656
        $form->display();
657
    }
658
659
    /**
660
     * Get the averages of each evaluation hot funny etc...
661
     *
662
     * @param int $user_uid
663
     * @return array with averages
664
     */
665
666
    public function getMoyennes(
667
        $user_uid
668
    ) {
669
        global $xoopsUser;
670
671
        $vetor = [];
672
673
        $vetor['mediahot'] = 0;
674
675
        $vetor['mediatrust'] = 0;
676
677
        $vetor['mediacool'] = 0;
678
679
        $vetor['sumfan'] = 0;
680
681
        //Calculating avg(hot)
682
683
        $sql = 'SELECT friend2_uid, Avg(hot) AS mediahot FROM ' . $this->db->prefix(
684
                'suico_friendships'
685
            );
686
687
        $sql .= ' WHERE  (hot>0) GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
688
689
        $result = $this->db->query($sql);
690
691
        while (false !== ($myrow = $this->db->fetchArray($result))) {
692
            $vetor['mediahot'] = $myrow['mediahot'] * 16;
693
        }
694
695
        //Calculating avg(trust)
696
697
        $sql = 'SELECT friend2_uid, Avg(trust) AS mediatrust FROM ' . $this->db->prefix(
698
                'suico_friendships'
699
            );
700
701
        $sql .= ' WHERE  (trust>0) GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
702
703
        $result = $this->db->query($sql);
704
705
        while (false !== ($myrow = $this->db->fetchArray($result))) {
706
            $vetor['mediatrust'] = $myrow['mediatrust'] * 16;
707
        }
708
709
        //Calculating avg(cool)
710
711
        $sql = 'SELECT friend2_uid, Avg(cool) AS mediacool FROM ' . $this->db->prefix(
712
                'suico_friendships'
713
            );
714
715
        $sql .= ' WHERE  (cool>0) GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
716
717
        $result = $this->db->query($sql);
718
719
        while (false !== ($myrow = $this->db->fetchArray($result))) {
720
            $vetor['mediacool'] = $myrow['mediacool'] * 16;
721
        }
722
723
        //Calculating sum(fans)
724
725
        $sql = 'SELECT friend2_uid, Sum(fan) AS sumfan FROM ' . $this->db->prefix(
726
                'suico_friendships'
727
            );
728
729
        $sql .= ' GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
730
731
        $result = $this->db->query($sql);
732
733
        while (false !== ($myrow = $this->db->fetchArray($result))) {
734
            $vetor['sumfan'] = $myrow['sumfan'];
735
        }
736
737
        return $vetor;
738
    }
739
}
740