Completed
Pull Request — master (#5)
by Michael
01:37
created

User::getRequests()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Smallworld;
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
 * SmallWorld
17
 *
18
 * @copyright    The XOOPS Project (https://xoops.org)
19
 * @copyright    2011 Culex
20
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
21
 * @package      SmallWorld
22
 * @since        1.0
23
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
24
 */
25
26
//include_once $GLOBALS['xoops']->path('include/common.php');
27
28
use XoopsModules\Smallworld;
29
30
/**
31
 * Class User
32
 * @package XoopsModules\Smallworld
33
 */
34
class User
35
{
36
    /**
37
     * @Check if user has profile
38
     * @param int $userID
39
     * @return int
40
     */
41
    public function checkIfProfile($userID)
42
    {
43
        global $xoopsUser, $xoopsDB;
44
        $i      = 0;
0 ignored issues
show
Unused Code introduced by
$i 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...
45
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $userID . "'";
46
        $result = $xoopsDB->queryF($sql);
47
        $i      = $xoopsDB->getRowsNum($result);
48
        if ($xoopsUser) {
49
            // If xoopsuser but no smallworld profile
50
            if (0 == $i) {
51
                $i = 1;
52
            } else {
53
                // if xoopsuser and has profile
54
                $i = 2;
55
            }
56
        } else {
57
            // if not xoopsUser ie anonymous user
58
            $i = 0;
59
        }
60
61
        return $i;
62
    }
63
64
    /**
65
     * @Create user
66
     * @param int $userid
67
     */
68 View Code Duplication
    public function createUser($userid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        global $xoopsUser, $xoopsDB;
71
        $a      = new $xoopsUser($userid);
72
        $b      = $a->uname();
0 ignored issues
show
Unused Code introduced by
$b 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...
73
        $sql    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_user') . ' (userid) VALUES (' . (int)$userid . ')';
74
        $result = $xoopsDB->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...
75
    }
76
77
    /**
78
     * @Check is user is smallworld user
79
     */
80
    public function chkUser()
81
    {
82
        global $xoopsUser, $xoopsTpl;
83
        $greeting = '<br>';
84
        $greeting .= _SMALLWORLD_NOTYETUSER_GREETING . ' ' . $xoopsUser->uname() . '.<br><br>';
85
        $greeting .= _SMALLWORLD_NOTYETUSER_BOXTEXT;
86
87
        $xoopsTpl->assign('notyetusercontent', $greeting);
88
        $xoopsTpl->assign('check', 0);
89
    }
90
91
    /**
92
     * @Check is user is friend
93
     * @param int $user
94
     * @param int $userID
95
     * @return array|int
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    public function friendcheck($user, $userID)
98
    {
99
        global $xoopsUser, $xoopsDB;
100
        $respons = [];
101
        if ($user == $userID) {
102
            $respons[0] = 2;
103
104
            return $respons;
105
        }
106
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$user . "' AND you = '" . (int)$userID . "'";
107
        $result = $xoopsDB->query($sql);
108
        $i      = $xoopsDB->getRowsNum($result);
109
        if (0 == $i) {
110
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$user . "' AND me = '" . (int)$userID . "'";
111
            $result = $xoopsDB->query($sql);
112
            $i      = $xoopsDB->getRowsNum($result);
113
        }
114 View Code Duplication
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
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...
115
            if (0 == $i && '' == $i) {
116
                $respons[0] = 0;
117
            }
118
119
            if (1 == $i && 1 == $row['status']) {
120
                $respons[0] = 1;
121
            }
122
            if (1 == $i && 2 == $row['status']) {
123
                $respons[0] = 2;
124
            }
125
126
            return $respons;
127
        }
128
    }
129
130
    /**
131
     * @Get name from userid
132
     * @param int $userID
133
     * @return string
134
     */
135
    public function getName($userID)
136
    {
137
        global $xoopsUser, $xoopsDB;
138
        $name   = '';
139
        $sql    = 'SELECT username FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . (int)$userID . "'";
140
        $result = $xoopsDB->queryF($sql);
141
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
142
            $name = $row['username'];
143
        }
144
145
        return $name;
146
    }
147
148
    /**
149
     * @Check if user is follower
150
     * @param int $userid
151
     * @param int $friendid
152
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
153
     */
154
    public function following_or($userid, $friendid)
155
    {
156
        global $xoopsDB, $xoopsUser;
157
        $respons[0] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$respons was never initialized. Although not strictly required by PHP, it is generally a good practice to add $respons = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
158
        if ($userid != $friendid) {
159
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . (int)$userid . "' AND you = '" . (int)$friendid . "'";
160
            $result = $xoopsDB->query($sql);
161
            $i      = $xoopsDB->getRowsNum($result);
162 View Code Duplication
            while (false !== ($row = $xoopsDB->fetchArray($result))) {
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...
163
                if (0 == $i) {
164
                    $respons[0] = 0;
165
                }
166
167
                if (1 == $i && 1 == $row['status']) {
168
                    $respons[0] = 1;
169
                }
170
                if (1 == $i && 2 == $row['status']) {
171
                    $respons[0] = 2;
172
                }
173
            }
174
        }
175
176
        return $respons;
177
    }
178
179
    /**
180
     * @Get requests
181
     * @param int $userid
182
     * @return array
183
     */
184
    public function getRequests($userid)
185
    {
186
        global $xoopsDB, $xoopsUser;
187
        $msg      = [];
188
        $sql      = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$userid . "' AND status = '1'";
189
        $result   = $xoopsDB->queryF($sql);
190
        $i        = $xoopsDB->getRowsNum($result);
0 ignored issues
show
Unused Code introduced by
$i 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...
191
        $db       = new \XoopsModules\Smallworld\SwDatabase();
0 ignored issues
show
Unused Code introduced by
$db 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...
192
        $Wall     = new \XoopsModules\Smallworld\WallUpdates();
193
        $myavatar = $Wall->Gravatar($userid);
0 ignored issues
show
Unused Code introduced by
$myavatar 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...
194
        $start    = 0;
195
        while (false !== ($row = $xoopsDB->fetchArray($result)) && $start <= count($row)) {
196
            $msg[$start]['friendname']  = $this->getName($row['me']);
197
            $msg[$start]['img']         = $Wall->Gravatar($row['me']);
198
            $msg[$start]['friendimage'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $msg[$start]['img'] . "' height='40px'>";
199
            $msg[$start]['frienddate']  = date('d-m-Y', $row['date']);
200
            $msg[$start]['accept']      = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_ACCEPT . '</a>';
201
            $msg[$start]['deny']        = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(-1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_DENY . '</a>';
202
            $msg[$start]['later']       = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(0,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_LATER . '</a>';
203
            $msg[$start]['cnt']         = $start;
204
            ++$start;
205
        }
206
207
        return $msg;
208
    }
209
210
    /**
211
     * @Get partner
212
     * @param string $name
213
     * @return int
214
     */
215
    public function spousexist($name)
216
    {
217
        global $xoopsUser, $xoopsDB;
218
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE username = '" . $name . "'";
219
        $result = $xoopsDB->queryF($sql);
220
        $i      = $xoopsDB->getRowsNum($result);
221
222
        return $i;
223
    }
224
225
    /**
226
     * Get all users
227
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
228
     */
229 View Code Duplication
    public function allUsers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
230
    {
231
        global $xoopsDB;
232
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_user') . ' ORDER BY userid';
233
        $result = $xoopsDB->queryF($sql);
234
        $i      = $xoopsDB->getRowsNum($result);
235
        if (0 != $i) {
236
            while (false !== ($r = $xoopsDB->fetchArray($result))) {
237
                $data[] = $r;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
238
            }
239
            if (!empty($data)) {
240
                return Smallworld_array_flatten($data, 0);
241
            }
242
        }
243
        //redirect_header(XOOPS_URL . "/modules/smallworld/register.php");
244
    }
245
}
246