Repository navigation
Expand file tree
/
Copy pathusageexamples.php
More file actions
164 lines (149 loc) · 5.62 KB
/
Copy pathusageexamples.php
File metadata and controls
164 lines (149 loc) · 5.62 KB
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
<?php
//this provides this file access to the database
require_once 'db.php';
// --- CREATE A USER ---
function createUser($pdo, $tag, $first_name, $last_name, $email, $password, $bio = null, $picture = null) {
$pw_hash = password_hash($password, PASSWORD_BCRYPT);
$stmt = $pdo->prepare("
INSERT INTO Users (tag, first_name, last_name, email, pw_hash, bio, picture)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([$tag, $first_name, $last_name, $email, $pw_hash, $bio, $picture]);
return $tag;
}
// --- GET PUBLIC USER INFO ---
//call this function to actually access the information of a user using their tag (for example, @elonmusk)
function getUserInfo($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT tag, first_name, last_name, bio, picture,
fav_song_1, fav_song_2, fav_song_3, fav_song_4
FROM Users
WHERE tag = ?
");
$stmt->execute([$tag]);
return $stmt->fetch(); // returns one row, or false if not found
}
// --- GET ALL REVIEWS FOR A USER ---
//call this function to get an array of all of the users reviews. used for showing each review one after the other
//returns most recent reviews first
function getUserReviews($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT r.id, r.song_name, r.artist_name, r.album_name,
r.duration, r.rating, r.review, r.review_date
FROM Reviews r
JOIN UserReviews ur ON r.id = ur.review_id
WHERE ur.user_tag = ?
ORDER BY r.review_date DESC
");
$stmt->execute([$tag]);
return $stmt->fetchAll();
}
// --- GET REVIEW COUNT THIS MONTH ---
// returns a number of the amount of reviews in this current month by this user
function getUserReviewCountThisMonth($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT COUNT(*) AS total
FROM Reviews r
JOIN UserReviews ur ON r.id = ur.review_id
WHERE ur.user_tag = ?
AND MONTH(r.review_date) = MONTH(CURDATE())
AND YEAR(r.review_date) = YEAR(CURDATE())
");
$stmt->execute([$tag]);
return $stmt->fetch()['total'];
}
// --- GET REVIEWS SORTED BY HIGHEST RATING ---
// returns the reviews sorted by highest rating instead of recency
function getUserReviewsByRating($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT r.id, r.song_name, r.artist_name, r.album_name,
r.duration, r.rating, r.review, r.review_date
FROM Reviews r
JOIN UserReviews ur ON r.id = ur.review_id
WHERE ur.user_tag = ?
ORDER BY r.rating DESC
");
$stmt->execute([$tag]);
return $stmt->fetchAll();
}
// --- GET REVIEWS FROM THIS WEEK ---
// returns only the reviews that were made in the last 7 days
function getUserReviewsThisWeek($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT r.id, r.song_name, r.artist_name, r.album_name,
r.duration, r.rating, r.review, r.review_date
FROM Reviews r
JOIN UserReviews ur ON r.id = ur.review_id
WHERE ur.user_tag = ?
AND r.review_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
ORDER BY r.review_date DESC
");
$stmt->execute([$tag]);
return $stmt->fetchAll();
}
// --- GET TOTAL REVIEW COUNT ---
//returns the total number of reviews a user has
function getUserReviewCount($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT COUNT(*) AS total
FROM UserReviews
WHERE user_tag = ?
");
$stmt->execute([$tag]);
return $stmt->fetch()['total'];
}
// --- GET AVERAGE RATING ---
//gets the average rating of all of the users reviews
function getUserAverageRating($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT ROUND(AVG(r.rating), 2) AS average
FROM Reviews r
JOIN UserReviews ur ON r.id = ur.review_id
WHERE ur.user_tag = ?
");
$stmt->execute([$tag]);
return $stmt->fetch()['average'];
}
// --- GET FAVORITE SONGS INFO ---
// gets the info corresponding to the 4 favorite songs the user has picked, in favorite order
function getUserFavorites($pdo, $tag) {
$stmt = $pdo->prepare("
SELECT r.id, r.song_name, r.artist_name, r.album_name,
r.duration, r.rating, r.review, r.review_date,
CASE u.fav_song_1 WHEN r.id THEN 1
WHEN u.fav_song_2 THEN 2
WHEN u.fav_song_3 THEN 3
WHEN u.fav_song_4 THEN 4
END AS fav_order
FROM Users u
JOIN Reviews r ON r.id IN (u.fav_song_1, u.fav_song_2, u.fav_song_3, u.fav_song_4)
WHERE u.tag = ?
ORDER BY fav_order ASC
");
$stmt->execute([$tag]);
return $stmt->fetchAll();
}
// --- CREATE A REVIEW ---
// pass all the necessary information from the user to the database to add a review linked to that user
function createReview($pdo, $tag, $song, $artist, $album, $duration, $rating, $review) {
// Insert into Reviews first to get the new ID
$stmt = $pdo->prepare("
INSERT INTO Reviews (song_name, artist_name, album_name, duration, rating, review)
VALUES (?, ?, ?, ?, ?, ?)
");
$stmt->execute([$song, $artist, $album, $duration, $rating, $review]);
$reviewId = $pdo->lastInsertId();
// Then link it to the user in UserReviews
$stmt = $pdo->prepare("
INSERT INTO UserReviews (user_tag, review_id)
VALUES (?, ?)
");
$stmt->execute([$tag, $reviewId]);
return $reviewId;
}
// --- EXAMPLE USAGE ---
// Replace the hardcoded values with the actual user input from an html form
$user = getUserInfo($pdo, 'someuser');
$reviews = getUserReviews($pdo, 'someuser');
$newId = createReview($pdo, 'someuser', 'Bohemian Rhapsody', 'Queen', 'A Night at the Opera', 354, 10, 'A masterpiece.');
?>