forked from sidooms/Recsys-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.php
136 lines (119 loc) · 4.1 KB
/
actions.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
<?php
/**
* Recsys-frontend, Copyright (c) 2013, Simon Dooms
* http://github.com/sidooms/recsys-frontend
* MIT License
*/
require_once('general.php');
$action = $_REQUEST['action'];
if ($action == 'rate'){
$movieid=$_REQUEST['mid'];
$userid=$_REQUEST['uid'];
$rating=$_REQUEST['r'];
$synced = 0;
//if exists
$sql = "SELECT COUNT(*) from ratings WHERE userid=? and movieid=?";
$stat = prepareStatement($sql);
$stat->bindParam(1, $userid);
$stat->bindParam(2, $movieid);
$stat->execute();
$res = $stat->fetchAll();
if ($res[0][0] > 0){
//update required
$sql = "UPDATE ratings SET rating=? WHERE userid=? and movieid=?";
$stat = prepareStatement($sql);
$stat->bindParam(1, $rating);
$stat->bindParam(2, $userid);
$stat->bindParam(3, $movieid);
$stat->execute();
}else{
//insert required
$sql = "INSERT INTO ratings (userid,movieid,rating,synced) VALUES (?,?,?,?)";
$stat = prepareStatement($sql);
$stat->bindParam(1, $userid);
$stat->bindParam(2, $movieid);
$stat->bindParam(3, $rating);
$stat->bindParam(4, $synced);
$stat->execute();
}
echo 'done.';
}else if ($action == 'cancelrating' ){
$movieid=$_REQUEST['mid'];
$userid=$_REQUEST['uid'];
$sql = "DELETE FROM ratings WHERE userid=? and movieid=?";
$stat = prepareStatement($sql);
$stat->bindParam(1, $userid);
$stat->bindParam(2, $movieid);
$stat->execute();
}else if ($action == 'updatealgoweight'){
$algo= $_REQUEST['algo'];
$weight = $_REQUEST['weight'];
$user = $_REQUEST['user'];
update_algo_weight($user, $algo, $weight);
}else if ($action == 'startreccalc'){
sync_ratings();
$path = str_replace('actions.php', '', __FILE__) . 'calc_recs/';
$command = $path . 'start.sh';
$output = exec($command);
echo $output;
}else if ($action == 'hybridcalc'){
$pythonpath = str_replace('actions.php', '', __FILE__) . 'combine_hybrid_recs/';
exec('python ' . $pythonpath . 'start.py');
}
function update_algo_weight($user, $algo, $weight)
{
#Check if weight exists
$sql = "SELECT COUNT(*) from algorithmweights WHERE userid=? and algorithm=?";
$stat = prepareStatement($sql);
$stat->bindParam(1, $user);
$stat->bindParam(2, $algo);
$stat->execute();
$res = $stat->fetchAll();
if ($res[0][0] == 0){
insert_algo_weight($user, $algo, $weight);
}else{
$sql = "UPDATE algorithmweights SET weight=? WHERE algorithm=? AND userid=?";
$stat = prepareStatement($sql);
$stat->bindParam(1, $weight);
$stat->bindParam(2, $algo);
$stat->bindParam(3, $user);
$stat->execute();
}
}
function insert_algo_weight($user,$algo,$weight)
{
$sql = "INSERT INTO algorithmweights (userid,algorithm,weight) VALUES (?,?,?)";
$stat = prepareStatement($sql);
$stat->bindParam(1, $user);
$stat->bindParam(2, $algo);
$stat->bindParam(3, $weight);
$stat->execute();
}
function sync_ratings()
{
$sql = "SELECT ratings.userid, ratings.movieid, ratings.rating, UNIX_TIMESTAMP(ratings.time) as time, movies.title, movies.year from ratings INNER JOIN movies ON ratings.movieid = movies.movieid";
$stat = prepareStatement($sql);
$stat->execute();
$rows = $stat->fetchAll();
$local_path = str_replace('actions.php', '', __FILE__) . 'calc_recs';
$rating_file = 'ratings.dat';
$fh = fopen("$local_path/$rating_file", 'w') or die("can't open file");
foreach($rows as $row){
//999999::1::4::1357741027
$userid = $row['userid'];
$movieid = $row['movieid'];
#IMDb ID must be 7 chars long
while (strlen($movieid) < 7)
{
$movieid = '0'. $movieid;
}
$rating = $row['rating'];
//0000-00-00 00:00:00
$thetime = $row['time'];
$title = trim($row['title']);
$year = trim($row['year']);
fwrite($fh, $userid . '::' . $movieid . '::' . $rating . '::' . $thetime . "\n");
}
fclose($fh);
}
?>