From 7184c5f85af75a079d9c3d764a2c5c4d7e6da73d Mon Sep 17 00:00:00 2001
From: aldrichvalentino <aldrich.vh97@gmail.com>
Date: Thu, 5 Oct 2017 00:30:23 +0700
Subject: [PATCH] rating in profile page

---
 controller/profile.php | 10 +++++++++-
 css/dashboard.css      |  8 ++++----
 js/profile.js          | 15 ++++++++++++++-
 model/user.php         |  8 ++++++++
 view/dashboard.php     |  1 +
 5 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/controller/profile.php b/controller/profile.php
index ce4c864d..51466f25 100644
--- a/controller/profile.php
+++ b/controller/profile.php
@@ -17,7 +17,15 @@ while($row = mysqli_fetch_array($result)){
 while($row = mysqli_fetch_array($locations)){
     $xml = $xml . '<location>'. $row['location'] .'</location>';
 }
+$result = $user->getDriverRating($user_id);
+if(mysqli_num_rows($result) > 0){
+    while($row = mysqli_fetch_array($result)){
+        $xml = $xml . '<rating>'. $row['driverRating'] .'</rating><votes>'. $row['votes'] .'</votes>';
+    }
+} else {
+    $xml = $xml . '<rating>0</rating><votes>0</votes>';
+}
+
 $xml = $xml . '</user>';
 
 print $xml;
-
diff --git a/css/dashboard.css b/css/dashboard.css
index 7ff2a80e..f3c1d856 100644
--- a/css/dashboard.css
+++ b/css/dashboard.css
@@ -115,15 +115,15 @@
     margin: 5px 0;
 }
 
-#name {
-    font-size: 1.8rem;
+#name, #driver, #email, #phone {
+    font-size: 1.3rem;
+    margin-bottom: 5px;
 }
 
 #email, #phone {
-    font-size: 1.5rem;
     display: inline-block;
 }
 #user-location li {
     font-size: 1.2rem;
     margin-bottom: 5px;
-}
\ No newline at end of file
+}
diff --git a/js/profile.js b/js/profile.js
index 2f7374bd..d676fbd6 100644
--- a/js/profile.js
+++ b/js/profile.js
@@ -4,11 +4,25 @@ function getUserProfile(){
     xmlhttp.onreadystatechange = function () {
         if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
             var result = xmlhttp.responseXML;
+            //Get User's data
             var username = result.getElementsByTagName('username')[0].childNodes[0].nodeValue;
             var name = result.getElementsByTagName('name')[0].childNodes[0].nodeValue;
             var email = result.getElementsByTagName('email')[0].childNodes[0].nodeValue;
             var phone = result.getElementsByTagName('phone')[0].childNodes[0].nodeValue;
             var image = result.getElementsByTagName('image')[0].childNodes[0].nodeValue;
+
+            //Get User Rating if user is a Driver
+            var isDriver = result.getElementsByTagName('driver')[0].childNodes[0].nodeValue;
+            var rating, votes = null;
+            if(isDriver == 1){
+                rating = result.getElementsByTagName('rating')[0].childNodes[0].nodeValue;
+                votes = result.getElementsByTagName('votes')[0].childNodes[0].nodeValue;
+                document.getElementById('driver').innerHTML = 'Driver | ' + Math.round(rating) + ' ('+ votes +' votes)';
+            } else {
+                document.getElementById('driver').innerHTML = 'Non-driver';
+            }
+
+            //Get User Location
             var arrayLocation = result.getElementsByTagName('location');
 
             document.getElementById('username').innerHTML = 'Hello, ' + name + '!';
@@ -18,7 +32,6 @@ function getUserProfile(){
             document.getElementById('phone').innerHTML = phone;
             document.getElementById('user-image').style.backgroundImage = 'url(../img/' + image + ')';
             var listOfLocation = document.getElementById('user-location');
-            console.log(listOfLocation.childElementCount);
             while(listOfLocation.hasChildNodes()){
                 listOfLocation.removeChild(listOfLocation.lastChild);
             }
diff --git a/model/user.php b/model/user.php
index 6b06751c..424fa2a4 100644
--- a/model/user.php
+++ b/model/user.php
@@ -99,4 +99,12 @@ class User {
         mysqli_close($con);
         return $result;
     }
+    function getDriverRating($id){
+        $con = mysqli_connect('localhost','root','','projekers');
+        mysqli_select_db($con,'projekers');
+        $query = "SELECT driver_id AS id, AVG(rating) AS driverRating, COUNT(rating) AS votes FROM transaction WHERE driver_id=" . $id . " GROUP BY(driver_id)";
+        $result = mysqli_query($con,$query);
+        mysqli_close($con);
+        return $result;
+    }
 }
\ No newline at end of file
diff --git a/view/dashboard.php b/view/dashboard.php
index 7978a8bc..98c44fe2 100644
--- a/view/dashboard.php
+++ b/view/dashboard.php
@@ -57,6 +57,7 @@
                 <div id="user-image"></div>
                 <h1 id="profile-username"></h1>
                 <div id="name"></div>
+                <div id="driver"></div>
                 <div id="driver-stats"></div>
                 <img src="../img/mail.png" /><div id="email"></div><br />
                 <img src="../img/phone.png" /><div id="phone"></div><br />
-- 
GitLab