-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
208 lines (141 loc) · 6.92 KB
/
script.js
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
$(function () {
var rating_data = 0; //The rating must be 1,2,3,4, or 5. So min is 1 and max is 5
$('#add_review').on('click', function () {
$('#review_modal').modal('show');
});
$(document).on('mouseenter', '.submit_star', function () {
reset_background();
// $(this) refers to the star on which the mouse is currently on
var rating = $(this).data('rating');
for (var count = 1; count <= rating; count += 1) {
$('#submit_star_' + count).addClass('text-warning');
}
});
function reset_background() {
for (var count = 1; count <= 5; count += 1) {
$('#submit_star_' + count).removeClass('text-warning').addClass('star-light');
}
}
$(document).on('mouseleave', '.submit_star', function () {
reset_background();
for (var count = 1; count <= rating_data; count++) {
$('#submit_star_' + count).removeClass('star-light').addClass('text-warning');
}
});
$(document).on('click', '.submit_star', function () {
// $(this) refers to the star on which the mouse is currently on
rating_data = $(this).data('rating');
});
$('#save_review').on('click', function () {
var user_name = $('#user_name').val();
var user_review = $('#user_review').val();
if (user_name == '' || user_review == '') {
alert("Please Fill in All Fields");
}
else if(rating_data == 0){
//The rating must be 1,2,3,4, or 5. So min is 1 and max is 5.
// So if rating_data is still = 0, this means the user has not CLICKED on stars.
alert("Please Specify The Number of Stars Based on Your Opinion");
}
else {
$.ajax({
url: 'submit_rating.php',
method: 'POST',
data: {
rating_data: rating_data,
user_name: user_name,
user_review: user_review
},
success: function (data) {
// Hide the Modal From the Webpage
$('#review_modal').modal('hide');
// alert(data);
// Now, clear the modal's input fields
$('input[name="user_name"]').val("");
$('input[name="user_review"]').val("");
reset_background(); //Clear the stars (Make them all gray)
// Now after adding the review to DB, you can invoke this function that makes ajax request to load data, so you can see the new added review without the need to refresh page.
load_rating_data();
}
});
}
});
// ===========================================
load_rating_data();
function load_rating_data() {
$.ajax({
url: "load_data.php",
method: "POST",
data: { action: 'load_data' },
dataType: "JSON",
success: function (data) {
// Now the response is returned as a JSON object
console.log(data);
$('#average_rating').text(data.average_rating);
$('#total_review').text(data.total_reviews);
var count_star = 0;
$('.main_star').each(function () {
count_star++;
if (Math.round(data.average_rating) >= count_star) {
$(this).addClass('text-warning').removeClass('star-light');
}
});
$('#total_five_star_review').text(data.all_star_reviews.five_star_review);
$('#total_four_star_review').text(data.all_star_reviews.four_star_review);
$('#total_three_star_review').text(data.all_star_reviews.three_star_review);
$('#total_two_star_review').text(data.all_star_reviews.two_star_review);
$('#total_one_star_review').text(data.all_star_reviews.one_star_review);
$('#five_star_progress').css({
width:
(data.all_star_reviews.five_star_review / data.total_reviews) * 100 + '%'
});
$('#four_star_progress').css({
width:
(data.all_star_reviews.four_star_review / data.total_reviews) * 100 + '%'
});
$('#three_star_progress').css({
width:
(data.all_star_reviews.three_star_review / data.total_reviews) * 100 + '%'
});
$('#two_star_progress').css({
width:
(data.all_star_reviews.two_star_review / data.total_reviews) * 100 + '%'
});
$('#one_star_progress').css({
width:
(data.all_star_reviews.one_star_review / data.total_reviews) * 100 + '%'
});
// =============================================
if (data.review_data.length > 0) {
var html = '';
for (var count = 0; count < data.review_data.length; count += 1) {
html += '<div class="row mb-3">';
html += '<div class="col-sm-1"><div class="rounded-circle bg-danger text-white pt-2 pb-2"><h3 class="text-center">' + data.review_data[count].user_name.charAt(0) + '</h3></div></div>';
html += '<div class="col-sm-11">';
html += '<div class="card">';
html += '<div class="card-header"><strong>' + data.review_data[count].user_name + '</strong></div>';
html += '<div class="card-body">';
for (var star = 1; star <= 5; star += 1) {
var class_name = '';
if (data.review_data[count].rating >= star) {
class_name = 'text-warning';
}
else {
class_name = 'star-light';
}
html += '<i class="fas fa-star ' + class_name + ' mr-1"></i>';
}// end for loop
html += '<br />';
html += data.review_data[count].user_review;
html += '</div>'; //end card-body
html += '<div class="card-footer text-right">On ' + data.review_data[count].submit_time + '</div>';
html += '</div>'; //end card
html += '</div>'; // end col-sm-11
html += '</div>'; // end row
}// end outer for loop
$('#review_content').html(html);
}
}// end success function
})
} //end function load_rating_data()
});