From afab397e6a3d7dfad02f1ddf34d0931fde86333c Mon Sep 17 00:00:00 2001
From: bewe <93899302+bernarduswillson@users.noreply.github.com>
Date: Tue, 14 Nov 2023 22:48:01 +0700
Subject: [PATCH] feat: routing, score, etc for exercise

---
 api/exercise/submit.php      | 42 ++++++++++++++++++--
 app/controllers/Exercise.php | 75 ++++++++++++++++++++++++++++--------
 app/views/exercise/index.php | 25 +++++++++++-
 app/views/question/index.php |  3 +-
 4 files changed, 122 insertions(+), 23 deletions(-)

diff --git a/api/exercise/submit.php b/api/exercise/submit.php
index f83858d..971c373 100644
--- a/api/exercise/submit.php
+++ b/api/exercise/submit.php
@@ -1,6 +1,7 @@
 <?php
 function submitQuiz($exerciseId, $selectedOptions, $userId)
 {
+    // rest submit exercise
     $pairs = array();
 
     if ($selectedOptions) {
@@ -17,7 +18,8 @@ function submitQuiz($exerciseId, $selectedOptions, $userId)
     }
 
     $ch = curl_init();
-    curl_setopt($ch, CURLOPT_URL, "http://express:5000/exercise/result/" . $exerciseId);
+    // curl_setopt($ch, CURLOPT_URL, "http://express:5000/exercise/result/" . $exerciseId);
+    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.11:5000/exercise/result/" . $exerciseId);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submitData));
@@ -43,6 +45,35 @@ function submitQuiz($exerciseId, $selectedOptions, $userId)
 
     curl_close($ch);
 
+
+    // rest add progress
+    $ch = curl_init();
+    // curl_setopt($ch, CURLOPT_URL, "http://express:5000/progress/create");
+    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.11:5000/progress/create");
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+    curl_setopt($ch, CURLOPT_POST, 1);
+    curl_setopt(
+        $ch,
+        CURLOPT_POSTFIELDS,
+        json_encode(
+            array(
+                "user_id" => (int)$userId,
+                "exercise_id" => (int)$exerciseId,
+                "score" => $score
+            )
+        )
+    );
+    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
+    $response = curl_exec($ch);
+    if ($response === false) {
+        echo 'Error: ' . curl_error($ch);
+    } else {
+        $data = json_decode($response, true);
+        // echo 'Progress added successfully. Progress id: ' . $data['result']['progress_id'];
+    }
+
+
+    // soap add gems
     $baseUrl = 'http://soap:8080/service/gems';
 
     $soapRequest = '<x:Envelope
@@ -78,16 +109,19 @@ function submitQuiz($exerciseId, $selectedOptions, $userId)
     }
 
     curl_close($ch);
-
-    header('Location: ../../exercise');
 }
 
 
 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submitQuiz'])) {
     $exerciseId = $_POST['exerciseId'];
     $userId = $_POST['userId'];
+    $isDone = $_POST['isDone'];
     $selectedOptions = isset($_POST['selectedOptions']) ? $_POST['selectedOptions'] : [];
 
-    submitQuiz($exerciseId, $selectedOptions, $userId);
+    if (!$isDone) {
+        submitQuiz($exerciseId, $selectedOptions, $userId);
+    }
+
+    header('Location: ../../exercise');
 }
 ?>
\ No newline at end of file
diff --git a/app/controllers/Exercise.php b/app/controllers/Exercise.php
index a905b9a..92ce73c 100644
--- a/app/controllers/Exercise.php
+++ b/app/controllers/Exercise.php
@@ -5,11 +5,14 @@ class Exercise extends Controller
   public function index()
   {
     $this->validateSession();
-    
+
     $data["pageTitle"] = "Test your knowledge!";
+    $data["user_id"] = $_SESSION['user_id'];
     $data["languages"] = $this->model("LanguageModel")->getAllLanguage();
 
-    $baseUrl = 'http://express:5000/exercise';
+    // get exercises
+    // $baseUrl = 'http://express:5000/exercise';
+    $baseUrl = 'http://192.168.0.11:5000/exercise';
     $ch = curl_init($baseUrl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $response = curl_exec($ch);
@@ -17,6 +20,16 @@ class Exercise extends Controller
 
     $data["exercise"] = $exercise['result'];
 
+    // progress
+    // $baseUrl = 'http://express:5000/progress/user/' . $data["user_id"];
+    $baseUrl = 'http://192.168.0.11:5000/progress/user/' . $data["user_id"];
+    $ch = curl_init($baseUrl);
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+    $response = curl_exec($ch);
+    $progress = json_decode($response, true);
+
+    $data["progress"] = $progress['result'];
+
     $this->view('header/index', $data);
     $this->view('navbar/index');
     $this->view('exercise/index', $data);
@@ -26,16 +39,17 @@ class Exercise extends Controller
   public function question($exerciseId = null)
   {
     $this->validateSession();
-
-    // $this->validateParamQuestion($questionId);
+    $this->validateParamExercise($exerciseId);
 
     // Question
     if (isset($exerciseId) && !empty($exerciseId)) {
       $data["pageTitle"] = "Test your knowledge!";
+      $data["exercise_id"] = intval($exerciseId);
       $data["user_id"] = $_SESSION['user_id'];
-      $data["exercise_id"] =  intval($exerciseId);
 
-      $baseUrl = 'http://express:5000/exercise';
+      // selected exercise
+      // $baseUrl = 'http://express:5000/exercise';
+      $baseUrl = 'http://192.168.0.11:5000/exercise';
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -50,7 +64,26 @@ class Exercise extends Controller
         }
       }
 
-      $baseUrl = 'http://express:5000/question/' . $data["exercise_id"];
+      // progress
+      // $baseUrl = 'http://express:5000/progress/user/' . $data["user_id"];
+      $baseUrl = 'http://192.168.0.11:5000/progress/user/' . $data["user_id"];
+      $ch = curl_init($baseUrl);
+      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+      $response = curl_exec($ch);
+      $progress = json_decode($response, true);
+
+      $data["progress"] = $progress['result'];
+      $data["isDone"] = false;
+      foreach ($data["progress"] as $progress) {
+        if ($progress["exercise_id"] === $data["exercise_id"]) {
+          $data["isDone"] = true;
+          break;
+        }
+      }
+
+      // questions
+      // $baseUrl = 'http://express:5000/question/' . $data["exercise_id"];
+      $baseUrl = 'http://192.168.0.11:5000/question/' . $data["exercise_id"];
       $ch = curl_init($baseUrl);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $response = curl_exec($ch);
@@ -58,19 +91,21 @@ class Exercise extends Controller
 
       $data["questions"] = $question['result'];
 
+      // options
       foreach ($data["questions"] as &$question) {
-        $baseUrl = 'http://express:5000/option/' . $question["question_id"];
+        // $baseUrl = 'http://express:5000/option/' . $question["question_id"];
+        $baseUrl = 'http://192.168.0.11:5000/option/' . $question["question_id"];
         $ch = curl_init($baseUrl);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($ch);
         $option = json_decode($response, true);
-    
+
         $question["options"] = $option['result'];
-    
+
         curl_close($ch);
       }
       unset($question);
-      
+
       $this->view('header/index', $data);
       $this->view('navbar/index');
       $this->view('question/index', $data);
@@ -78,12 +113,20 @@ class Exercise extends Controller
     }
   }
 
-  public function validateParamQuestion($questionId)
+  public function validateParamExercise($exerciseId)
   {
-    if (isset($questionId) && !empty($questionId)) {
-      // if ($this->model("VideoModel")->validateById($moduleId, $videoId)) {
-      //   return;
-      // }
+    if (isset($exerciseId) && !empty($exerciseId)) {
+      // $baseUrl = 'http://express:5000/exercise/validate/' . $exerciseId;
+      $baseUrl = 'http://192.168.0.11:5000/exercise/validate/' . $exerciseId;
+      $ch = curl_init($baseUrl);
+      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+      $response = curl_exec($ch);
+      $valid = json_decode($response, true);
+
+      if ($valid['result'] === true) {
+        return;
+      }
+      
       $this->show404();
       exit();
     }
diff --git a/app/views/exercise/index.php b/app/views/exercise/index.php
index 5aa938a..d950ad0 100644
--- a/app/views/exercise/index.php
+++ b/app/views/exercise/index.php
@@ -25,7 +25,18 @@ $selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1;
       <div class="card-container" id="exercise-container">
         <?php foreach ($data['exercise'] as $exercise): ?>
           <div class="exercise-card">
-            <a href="/exercise/question/<?= $exercise['exercise_id'] ?>">
+            <?php
+            $isProgressExists = false;
+            foreach ($data["progress"] as $progress) {
+              if ($progress["exercise_id"] == $exercise['exercise_id']) {
+                $isProgressExists = true;
+                break;
+              }
+            }
+            ?>
+            <?php if (!$isProgressExists): ?>
+              <a href="/exercise/question/<?= $exercise['exercise_id'] ?>">
+              <?php endif; ?>
               <div class="exercise-head">
                 <div class="content">
                   <h2 class="font-bold text-md">
@@ -46,8 +57,18 @@ $selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1;
                     <?= $exercise['category'] ?>
                   </span>
                 </div>
+
+                <?php foreach ($data["progress"] as $progress): ?>
+                  <?php if ($progress["exercise_id"] == $exercise['exercise_id']): ?>
+                    <div class="score font-bold text-md text-orange">
+                      <?= $progress['score'] ?>
+                    </div>
+                  <?php endif; ?>
+                <?php endforeach; ?>
               </div>
-            </a>
+              <?php if (!$isProgressExists): ?>
+              </a>
+            <?php endif; ?>
           </div>
         <?php endforeach; ?>
       </div>
diff --git a/app/views/question/index.php b/app/views/question/index.php
index d051ae4..bcbb87c 100644
--- a/app/views/question/index.php
+++ b/app/views/question/index.php
@@ -23,6 +23,7 @@
             <div class="" id="hidden"></div>
             <input type="hidden" name="exerciseId" value="<?= $data['currentExercise']['exercise_id'] ?>">
             <input type="hidden" name="userId" value="<?= $data["user_id"] ?>">
+            <input type="hidden" name="isDone" value="<?= $data["isDone"] ?>">
             <button type="submit" name="submitQuiz">Submit</button>
         </form>
         <div id="exercise-score"></div>
@@ -48,7 +49,7 @@
         hidden.innerHTML = '';
 
         hidden.innerHTML = `
-            <input name="selectedOptions" value="${listString}">
+            <input type="hidden" name="selectedOptions" value="${listString}">
         `;
     }
 </script>
-- 
GitLab