1 00:00:00,000 --> 00:00:03,218 For the final step in our book voting system, 2 00:00:03,218 --> 00:00:07,830 I want to be able to update the way the voting works. 3 00:00:07,830 --> 00:00:11,350 Only authenticated users should be able to vote. 4 00:00:11,350 --> 00:00:14,990 And each user only gets one vote per book, but 5 00:00:14,990 --> 00:00:18,430 that user is always able to change their vote. 6 00:00:18,430 --> 00:00:21,430 Let's take a look at our functions_vote file. 7 00:00:22,650 --> 00:00:29,330 The first function gives us the saved vote for a specific book by a specific user. 8 00:00:29,330 --> 00:00:33,657 The user ID is not required, it will default to 0. 9 00:00:33,657 --> 00:00:37,374 The next function is the actual vote, 10 00:00:39,993 --> 00:00:45,600 It accepts a bookId, a score, and an optional userId. 11 00:00:46,630 --> 00:00:50,203 This function calls the clearVote function. 12 00:00:52,252 --> 00:00:56,588 That function accepts a bookId and a userId, and 13 00:00:56,588 --> 00:01:00,810 attempts to delete that user's vote. 14 00:01:00,810 --> 00:01:03,845 If a vote is actually deleted, 15 00:01:03,845 --> 00:01:08,758 the function will return a count greater than 0. 16 00:01:08,758 --> 00:01:14,200 The vote function will count that as the user's vote and return true. 17 00:01:14,200 --> 00:01:20,470 This keeps the vote to 1 point instead of jumping from positive 1 to negative 1. 18 00:01:20,470 --> 00:01:24,780 For more information, see the notes associated with this video. 19 00:01:24,780 --> 00:01:30,140 If no vote was cleared, then we add the user's vote to the database. 20 00:01:31,760 --> 00:01:38,303 Let's update our procedures/vote.php to pass the ID of the authenticated user. 21 00:01:41,272 --> 00:01:45,569 First we'll add requireAuth, 22 00:01:45,569 --> 00:01:52,420 then we can get the user = getAuthenticatedUser. 23 00:01:58,010 --> 00:02:02,120 And then we can pass user id. 24 00:02:03,910 --> 00:02:10,520 Next, we want to update our arrows to show which way the current user has voted, 25 00:02:10,520 --> 00:02:13,990 so let's open book.php. 26 00:02:13,990 --> 00:02:18,250 The first thing that we'll need to do is get the authenticated user. 27 00:02:18,250 --> 00:02:23,916 After we check if the user is authenticated, 28 00:02:23,916 --> 00:02:29,142 we can do user = getAuthenticatedUser. 29 00:02:29,142 --> 00:02:34,650 Now, inside of the class of our up arrow, we're going to add a php block. 30 00:02:40,792 --> 00:02:44,810 We can add as much space as we want, so let's add some new lines. 31 00:02:45,810 --> 00:02:53,790 We're going to add a conditional that checks if, getUserVote, 32 00:02:55,168 --> 00:02:59,616 And we pass the book, 33 00:02:59,616 --> 00:03:04,070 id, and the user id. 34 00:03:05,263 --> 00:03:08,571 And then we check to see if this equals 1. 35 00:03:09,645 --> 00:03:17,127 If it does, then we're going to echo, space, orange. 36 00:03:17,127 --> 00:03:20,985 Make sure that we have a space before our new orange class so 37 00:03:20,985 --> 00:03:24,781 that it doesn't get mixed up with our previous classes. 38 00:03:24,781 --> 00:03:32,080 Let's copy this code block, And we'll add it to our down arrow. 39 00:03:33,420 --> 00:03:36,330 Instead of 1, we wanna look for negative 1. 40 00:03:37,940 --> 00:03:41,424 Now let's go back to the browser and view the Book List page. 41 00:03:44,212 --> 00:03:48,073 We can now only make a single vote per book, 42 00:03:48,073 --> 00:03:53,020 either we have no vote, one vote up, or one vote down.