-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
232 lines (194 loc) · 4.69 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
require_once "./Database/functions.php";
require_once "./Constants/db_queries.php";
function MeanRate($rid)
{
$rates = select(QUERY_GET_RESOURCE_RATES, "i", [$rid]);
$sumRate = 0;
if ($rates == null) {
return [0, 0];
}
foreach ($rates as $r) {
$sumRate += intval($r[0][0]);
}
return [intval($sumRate / sizeof($rates)), sizeof($rates)];
}
function convertToHoursMins($time)
{
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
if ($hours < 1) {
return sprintf("%dmins", $minutes);
}
if ($hours == 1) {
if ($minutes == 0) {
return sprintf("%dhr", $hours);
} else {
return sprintf("%dhr %dmins", $hours, $minutes);
}
}
if ($minutes == 0) {
return sprintf("%dhrs", $hours);
} else {
return sprintf("%dhrs %dmins", $hours, $minutes);
}
}
function getTopics($rid)
{
return select(QUERY_GET_RESOURCE_TOPICS, "i", [$rid]);
}
function getFields($rid)
{
return select(QUERY_GET_RESOURCE_FIELDS, "i", [$rid]);
}
function fetch_all_users()
{
return select(QUERY_ALL_USERS, "", []);
}
function dateTostr($date)
{
$time = strtotime($date);
$newformat = date('d M, Y', $time);
return $newformat;
}
function dateTimeTostr($date)
{
$time = strtotime($date);
$newformat = date('d M, Y H:i', $time);
return $newformat;
}
function isRated($username, $rid)
{
$arr = select("SELECT * FROM " . DB_TABLE_RATE . " WHERE resourceID='" . $rid . "' AND username='" . $username . "' ", "", []);
return $arr != null;
}
function fetch_topics($field_id)
{
return select(QUERY_GET_TOPICS, "i", [$field_id]);
}
function findCreator($rid)
{
$creator = select(QUERY_GET_RESOURCE_CREAETOR, "i", [$rid]);
if (is_array($creator)) {
return $creator[0];
} else {
return null;
}
}
function getStdID($username)
{
$res = select(QUERY_GET_STD_ID, "s", [$username]);
if ($res != null) {
return $res[0][0];
} else {
return null;
}
}
function getCCID($username)
{
$res = select(QUERY_GET_CC_ID, "s", [$username]);
if ($res != null) {
return $res[0][0];
} else {
return null;
}
}
function getSTDUsername($sid)
{
$std = select(QUERY_GET_STD_USERNAME, "i", [$sid]);
if ($std != null) {
return $std[0][0];
} else {
return null;
}
}
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function custom_number_format($n, $precision = 3)
{
if ($n < 1000) {
$n_format = number_format($n);
} else if ($n < 1000000) {
// Anything less than a million
$n_format = number_format($n / 1000, $precision) . 'K';
} else if ($n < 1000000000) {
// Anything less than a billion
$n_format = number_format($n / 1000000, $precision) . 'M';
} else {
// At least a billion
$n_format = number_format($n / 1000000000, $precision) . 'B';
}
return $n_format;
}
function normalizeURL($text)
{
$divider = '-';
// replace non letter or digits by divider
$text = preg_replace('~[^\pL\d]+~u', $divider, $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, $divider);
// remove duplicate divider
$text = preg_replace('~-+~', $divider, $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
function getRole($username)
{
$res = select(QUERY_GET_USER_ROLE, "s", [$username]);
if ($res == null) {
return null;
} else {
return $res[0][0];
}
}
function getSuggestCount($sid)
{
$res = select(QUERY_GET_RESOURCE_SUGGEST_COUNT, "i", [$sid]);
if ($res == null) {
return null;
} else {
return $res[0][0];
}
}
function getCommentCount($username)
{
$res = select(QUERY_GET_COMMENT_COUNT, "s", [$username]);
if ($res == null) {
return null;
} else {
return $res[0][0];
}
}
function getResourceCount($username)
{
$res = select(QUERY_GET_USER_RESOURCE_COUNT, "s", [$username]);
if ($res == null) {
return null;
} else {
return $res[0][0];
}
}
function getTags($inp)
{
preg_match_all("~\#(\w+)~i", $inp, $matches);
return $matches[1];
}