-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodel.php
138 lines (111 loc) · 3.19 KB
/
model.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
<?php
date_default_timezone_set(timezone_name_from_abbr('CET'));
include_once 'klout.php';
include_once 'db.inc.php';
/**
* DB wrapper
*/
class Model {
/**
* Insert a new user based on his/her Twitter username.
*
* @param string $twitter_screen_name
* @return array
*/
public static function insert($twitter_screen_name) {
$status = 'error';
$blacklist = explode("\n", file_get_contents('blacklist.txt'));
if (!in_array(strtolower($twitter_screen_name), $blacklist)) {
$klout_id = Klout::getId('twitter', $twitter_screen_name);
if ($klout_id !== false) {
$score = Klout::getScore($klout_id);
if ($score !== false) {
$kscore = $score['score'];
$kchange = $score['scoreDelta']['weekChange'];
$query1 = mysql_query("SELECT * FROM users WHERE twitter_screen_name = '$twitter_screen_name'");
if (mysql_num_rows($query1) == 0) {
$now = date('Y-m-d H:i:s');
$query2 = mysql_query("INSERT INTO users VALUES('', '$twitter_screen_name', '$klout_id', '$kscore', '$kchange', '$now')");
if ($query2) {
$status = 'ok';
$message = $twitter_screen_name.' has been successfully added to the ranking with a Klout score of '.$kscore.'!';
}
else {
$message = 'Error while accessing the database!';
}
}
else {
$message = 'This Twitter screen name is already ranked!';
}
}
else {
$message = 'Could not get score for this user!';
}
}
else {
$message = 'This Twitter screen name doesn\'t exist!';
}
}
else {
$message = 'This Twitter screen name is not allowed!';
}
return array($status, $message);
}
/**
* Retrieve a specific user's details from DB
*
* @param string $id The Klout id
* @return array|false The user's details from DB, or false on failure
*/
public static function getUser($id) {
$query = mysql_query("SELECT * FROM users WHERE kid = '$id'");
while ($user = mysql_fetch_assoc($query)) {
return $user;
}
return false;
}
/**
* Retrieve all users' details from DB
*
* @param int[optional] $offset
* @param int[optional] $limit
* @return resource
*/
public static function getUsers($offset = 0, $limit = 100) {
$offset = (int) $offset;
$limit = (int) $limit;
$users = array();
$query = mysql_query("SELECT * FROM users ORDER BY last_update ASC LIMIT $offset, $limit");
while ($user = mysql_fetch_assoc($query)) {
$users[] = $user;
}
return $users;
}
/**
* Refresh a user's Klout score.
*
* @param array $user
* @return float New score
*/
public static function refreshScore($user) {
// Get fresh Klout score
$score = Klout::getScore($user['kid']);
// Make sure we did retrieve a response score
if($score !== false) {
$kchange = $score['scoreDelta']['weekChange'];
$kscore = $score['score'];
}
else {
// No response = invalid account
$kchange = 0;
$kscore = -1;
}
// Compose the fields that need to be updated
$set_sql = sprintf('kscore = \'%f\', kchange=\'%f\', ', $kscore, $kchange);
// Update the user
$now = date('Y-m-d H:i:s');
$klout_id = $user['kid'];
mysql_query("UPDATE users SET $set_sql last_update = '$now' WHERE kid = '$klout_id'");
return $score;
}
}