Completed
Push — master ( 9a4ef0...878baf )
by
unknown
01:47
created

SmallWorldUser::getRequests()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 24

Duplication

Lines 11
Ratio 45.83 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 11
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php 
2
namespace XoopsModules\Smallworld;
3
/**
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * SmallWorld
15
 *
16
 * @copyright    The XOOPS Project (https://xoops.org)
17
 * @copyright    2011 Culex
18
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
19
 * @package      SmallWorld
20
 * @since        1.0
21
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
22
 */
23
24
//include_once $GLOBALS['xoops']->path('include/common.php');
25
26
27
class SmallWorldUser
28
{
29
30
    /**
31
     * @Check if user has profile
32
     * @param int $userID
33
     * @return int
34
     */
35
    public function checkIfProfile($userID)
36
    {
37
        global $xoopsUser, $xoopsDB;
38
        $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...
39
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $userID . "'";
40
        $result = $xoopsDB->queryF($sql);
41
        $i      = $xoopsDB->getRowsNum($result);
42
        if ($xoopsUser) {
43
            // If xoopsuser but no smallworld profile
44
            if (0 == $i) {
45
                $i = 1;
46
            } else {
47
                // if xoopsuser and has profile
48
                $i = 2;
49
            }
50
        } else {
51
            // if not xoopsUser ie anonymous user
52
            $i = 0;
53
        }
54
        return $i;
55
    }
56
57
    /**
58
     * @Create user
59
     * @param int $userid
60
     * @return void
61
     */
62 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...
63
    {
64
        global $xoopsUser, $xoopsDB;
65
        $a      = new $xoopsUser($userid);
66
        $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...
67
        $sql    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_user') . ' (userid) VALUES (' . (int)$userid . ')';
68
        $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...
69
    }
70
71
    /**
72
     * @Check is user is smallworld user
73
     * @return void
74
     */
75
    public function chkUser()
76
    {
77
        global $xoopsUser, $xoopsTpl;
78
        $greeting = '<br>';
79
        $greeting .= _SMALLWORLD_NOTYETUSER_GREETING . ' ' . $xoopsUser->uname() . '.<br><br>';
80
        $greeting .= _SMALLWORLD_NOTYETUSER_BOXTEXT;
81
82
        $xoopsTpl->assign('notyetusercontent', $greeting);
83
        $xoopsTpl->assign('check', 0);
84
    }
85
86
    /**
87
     * @Check is user is friend
88
     * @param int $user
89
     * @param int $userID
90
     * @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...
91
     */
92
    public function friendcheck($user, $userID)
93
    {
94
        global $xoopsUser, $xoopsDB;
95
        $respons = [];
96
        if ($user == $userID) {
97
            $respons[0] = 2;
98
            return $respons;
99
        }
100
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$user . "' AND you = '" . (int)$userID . "'";
101
        $result = $xoopsDB->query($sql);
102
        $i      = $xoopsDB->getRowsNum($result);
103
        if (0 == $i) {
104
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$user . "' AND me = '" . (int)$userID . "'";
105
            $result = $xoopsDB->query($sql);
106
            $i      = $xoopsDB->getRowsNum($result);
107
        }
108 View Code Duplication
        while ($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...
109
            if (0 == $i and '' == $i) {
110
                $respons[0] = 0;
111
            }
112
113
            if (1 == $i and 1 == $row['status']) {
114
                $respons[0] = 1;
115
            }
116
            if (1 == $i and 2 == $row['status']) {
117
                $respons[0] = 2;
118
            }
119
            return $respons;
120
        }
121
    }
122
123
    /**
124
     * @Get name from userid
125
     * @param int $userID
126
     * @return string
127
     */
128
    public function getName($userID)
129
    {
130
        global $xoopsUser, $xoopsDB;
131
        $name = '';
132
        $sql    = 'SELECT username FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . (int)$userID . "'";
133
        $result = $xoopsDB->queryF($sql);
134
        while ($row = $xoopsDB->fetchArray($result)) {
135
            $name = $row['username'];
136
        }
137
        return $name;
138
    }
139
140
    /**
141
     * @Check if user is follower
142
     * @param int $userid
143
     * @param int $friendid
144
     * @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...
145
     */
146
    public function following_or($userid, $friendid)
147
    {
148
        global $xoopsDB, $xoopsUser;
149
        $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...
150
        if ($userid != $friendid) {
151
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . (int)$userid . "' AND you = '" . (int)$friendid . "'";
152
            $result = $xoopsDB->query($sql);
153
            $i      = $xoopsDB->getRowsNum($result);
154 View Code Duplication
            while ($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...
155
                if (0 == $i) {
156
                    $respons[0] = 0;
157
                }
158
159
                if (1 == $i and 1 == $row['status']) {
160
                    $respons[0] = 1;
161
                }
162
                if (1 == $i and 2 == $row['status']) {
163
                    $respons[0] = 2;
164
                }
165
            }
166
        } else {
167
        }
168
        return $respons;
169
    }
170
171
    /**
172
     * @Get requests
173
     * @param int $userid
174
     * @return array
175
     */
176
    public function getRequests($userid)
177
    {
178
        global $xoopsDB, $xoopsUser;
179
        $msg      = [];
180
        $sql      = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$userid . "' AND status = '1'";
181
        $result   = $xoopsDB->queryF($sql);
182
        $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...
183
        $db       = new SmallWorldDB;
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...
184
        $Wall     = new WallUpdates();
185
        $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...
Deprecated Code introduced by
The method XoopsModules\Smallworld\WallUpdates::Gravatar() has been deprecated.

This method has been deprecated.

Loading history...
186
        $start    = 0;
187 View Code Duplication
        while ($row = $xoopsDB->fetchArray($result) and $start <= count($row)) {
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...
188
            $msg[$start]['friendname']  = $this->getName($row['me']);
189
            $msg[$start]['img']         = $Wall->Gravatar($row['me']);
0 ignored issues
show
Deprecated Code introduced by
The method XoopsModules\Smallworld\WallUpdates::Gravatar() has been deprecated.

This method has been deprecated.

Loading history...
190
            $msg[$start]['friendimage'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $msg[$start]['img'] . "' height='40px'>";
191
            $msg[$start]['frienddate']  = date('d-m-Y', $row['date']);
192
            $msg[$start]['accept']      = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_ACCEPT . '</a>';
193
            $msg[$start]['deny']        = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(-1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_DENY . '</a>';
194
            $msg[$start]['later']       = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(0,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_LATER . '</a>';
195
            $msg[$start]['cnt']         = $start;
196
            ++$start;
197
        }
198
        return $msg;
199
    }
200
201
    /**
202
     * @Get partner
203
     * @param string $name
204
     * @return int
205
     */
206
    public function spousexist($name)
207
    {
208
        global $xoopsUser, $xoopsDB;
209
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE username = '" . $name . "'";
210
        $result = $xoopsDB->queryF($sql);
211
        $i      = $xoopsDB->getRowsNum($result);
212
        return $i;
213
    }
214
215
    /**
216
     * Get all users
217
     * @return array
218
     */
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...
219
220
    public function allUsers()
221
    {
222
        global $xoopsDB;
223
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_user') . ' ORDER BY userid';
224
        $result = $xoopsDB->queryF($sql);
225
        $i      = $xoopsDB->getRowsNum($result);
226
        if (0 != $i) {
227
            while ($r = $xoopsDB->fetchArray($result)) {
228
                $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...
229
            }
230
            if (!empty($data)) {
231
                return Smallworld_array_flatten($data, 0);
232
            }
233
        } else {
234
            //redirect_header(XOOPS_URL . "/modules/smallworld/register.php");
235
        }
236
    }
237
}
238