From 4b40f755caa3a5fdca89a0a043684d299fceb337 Mon Sep 17 00:00:00 2001
From: bewe <93899302+bernarduswillson@users.noreply.github.com>
Date: Mon, 9 Oct 2023 00:01:06 +0700
Subject: [PATCH] fix: video crud create and deletion

---
 api/admin/editVideo.php                | 17 +++++++++++++++++
 api/admin/video.php                    | 11 +++++++++--
 app/controllers/Admin.php              |  3 +++
 app/controllers/Learn.php              |  4 ++++
 app/models/ProgressModel.php           | 19 +++++++++++++++++++
 app/views/admin/create/video/index.php |  1 +
 app/views/admin/edit/video/index.php   |  1 +
 app/views/video/index.php              |  2 +-
 8 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/api/admin/editVideo.php b/api/admin/editVideo.php
index 72af798..2fbb178 100644
--- a/api/admin/editVideo.php
+++ b/api/admin/editVideo.php
@@ -4,8 +4,10 @@ require_once '../../config/config.php';
 require_once '../../app/core/App.php';
 require_once '../../app/core/Database.php';
 require_once '../../app/models/VideoModel.php';
+require_once '../../app/models/ProgressModel.php';
 
 $video_model = new VideoModel();
+$progress_model = new ProgressModel();
 $xml = file_get_contents('php://input');
 $data = json_decode($xml, true);
 
@@ -22,8 +24,23 @@ if (isset($data['order'])) {
 
 if (isset($_POST['delete'])) {
     $video_id = $_POST['video_id'];
+    $module_id = $_POST['module_id'];
+    $user_id = $_POST['user_id'];
 
     $video_model->deleteVideo($video_id);
+
+    $video_count = $video_model->getVideoCountByModuleId($module_id);
+    $video_finished_count = $progress_model->getUserVideoFinishedCount($user_id, $module_id);
+
+    if ($video_count == $video_finished_count) {
+        $isProgress = $progress_model->isProgress($user_id, $module_id);
+        if ($isProgress == 0) {
+            $progress_model->addModuleResult($user_id, $module_id);
+        }
+    }
+    if ($video_count == 0) {
+        $progress_model->deleteProgress($user_id, $module_id);
+    }
     header('Location: ../../../../admin/manage/' . $_POST['language_id'] . '/' . $_POST['module_id']);
 } else if (isset($_POST['videoName']) && isset($_POST['new-video']) && isset($_POST['order']) && isset($_POST['module_id'])) {
     $data['video_id'] = $_POST['video_id'];
diff --git a/api/admin/video.php b/api/admin/video.php
index b2bd2d3..562b1af 100644
--- a/api/admin/video.php
+++ b/api/admin/video.php
@@ -4,8 +4,10 @@ require_once '../../config/config.php';
 require_once '../../app/core/App.php';
 require_once '../../app/core/Database.php';
 require_once '../../app/models/VideoModel.php';
+require_once '../../app/models/ProgressModel.php';
 
 $video_model = new VideoModel();
+$progress_model = new ProgressModel();
 $xml = file_get_contents('php://input');
 $data = json_decode($xml, true);
 
@@ -27,7 +29,12 @@ if (isset($_POST['videoName']) && isset($_POST['new-video']) && isset($_POST['or
     $data['video_order'] = $_POST['order'];
     $data['module_id'] = $_POST['moduleId'];
 
+
     $video_model->addVideo($data);
-    header('Location: /admin/manage/' . $_POST['languageId'] . '/' . $_POST['moduleId']);
-    echo json_encode(array('status' => 'success', 'message' => 'VIdeo created'));
+    $isProgress = $progress_model->isProgress($_POST['user_id'], $data['module_id']);
+    if ($isProgress == 1) {
+        $progress_model->deleteProgress($_POST['user_id'], $data['module_id']);
+    }
+    header('Location: /admin/manage/' . $_POST['languageId'] . '/' . $data['module_id']);
+    echo json_encode(array('status' => 'success', 'message' => 'Video created'));
 }
\ No newline at end of file
diff --git a/app/controllers/Admin.php b/app/controllers/Admin.php
index 8212673..784a57e 100644
--- a/app/controllers/Admin.php
+++ b/app/controllers/Admin.php
@@ -233,6 +233,7 @@ class Admin extends Controller {
         return;
       }
       $this->show404();
+      exit();
     }
   }
 
@@ -242,6 +243,7 @@ class Admin extends Controller {
         return;
       }
       $this->show404();
+      exit();
     }
   }
 
@@ -251,6 +253,7 @@ class Admin extends Controller {
         return;
       }
       $this->show404();
+      exit();
     }
   }
 }
\ No newline at end of file
diff --git a/app/controllers/Learn.php b/app/controllers/Learn.php
index e9d3098..d88a0e5 100644
--- a/app/controllers/Learn.php
+++ b/app/controllers/Learn.php
@@ -93,6 +93,7 @@ class Learn extends Controller {
     // No parameter
     } else {
       $this->show404();
+      exit();
     }
   }
 
@@ -102,6 +103,7 @@ class Learn extends Controller {
         return;
       }
       $this->show404();
+      exit();
     }
   }
 
@@ -111,6 +113,7 @@ class Learn extends Controller {
         return;
       }
       $this->show404();
+      exit();
     }
   }
 
@@ -120,6 +123,7 @@ class Learn extends Controller {
         return;
       }
       $this->show404();
+      exit();
     }
   }
 }
diff --git a/app/models/ProgressModel.php b/app/models/ProgressModel.php
index c3ad62c..f6681fb 100644
--- a/app/models/ProgressModel.php
+++ b/app/models/ProgressModel.php
@@ -57,6 +57,25 @@ class ProgressModel
     return $this->db->execute();
   }
 
+  public function deleteProgress($user_id, $module_id)
+  {
+    $query = 'DELETE FROM modules_result WHERE user_id = :user_id AND module_id = :module_id';
+    $this->db->query($query);
+    $this->db->bind('user_id', $user_id);
+    $this->db->bind('module_id', $module_id);
+    return $this->db->execute();
+  }
+
+  public function isProgress($user_id, $module_id)
+  {
+    $query = 'SELECT COUNT(*) FROM modules_result WHERE user_id = :user_id AND module_id = :module_id';
+    $this->db->query($query);
+    $this->db->bind('user_id', $user_id);
+    $this->db->bind('module_id', $module_id);
+    $temp = $this->db->single();
+    return intval($temp["count"]);
+  }
+
   public function isUserVideoFinished($user_id, $video_id)
   {
     $query = 'SELECT COUNT(*) FROM videos_result WHERE user_id = :user_id AND video_id = :video_id';
diff --git a/app/views/admin/create/video/index.php b/app/views/admin/create/video/index.php
index f9b3de9..7bc18a8 100644
--- a/app/views/admin/create/video/index.php
+++ b/app/views/admin/create/video/index.php
@@ -11,6 +11,7 @@ $video = '/public/video/default.mp4';
     <form action="../../../../../api/admin/video.php" method="post">
       <input type="hidden" name="moduleId" id="module_id" value="<?= $data["moduleId"] ?>">
       <input type="hidden" name="languageId" value="<?= $data["languageId"] ?>">
+      <input type="hidden" name="user_id" value="<?= $_SESSION['user_id'] ?>">
       <div class="text-input-container">
         <label for="videoName" class="text-reg text-black text-xs">Video name</label>
         <input id="name-input" type="text" name="videoName" placeholder="Video name" class="font-reg text-black text-sm" autocomplete="false" required>
diff --git a/app/views/admin/edit/video/index.php b/app/views/admin/edit/video/index.php
index 25f060c..204f6a1 100644
--- a/app/views/admin/edit/video/index.php
+++ b/app/views/admin/edit/video/index.php
@@ -31,6 +31,7 @@ $video = $data["video"]["video_url"];
       <input type="hidden" name="video_id" value="<?= $data["video"]["video_id"] ?>">
       <input type="hidden" name="video_order" value="<?= $data["video"]["video_order"] ?>">
       <input type="hidden" name="language_id" value="<?= $data["languageId"] ?>">
+      <input type="hidden" name="user_id" value="<?= $_SESSION['user_id'] ?>">
       <div class="text-input-container">
         <label for="videoName" class="text-reg text-black text-xs">Video name</label>
         <input id="name-input" type="text" name="videoName" placeholder="Video name" class="font-reg text-black text-sm" autocomplete="false" value="<?= $data["video"]["video_name"] ?>">
diff --git a/app/views/video/index.php b/app/views/video/index.php
index 962e0eb..32ee101 100644
--- a/app/views/video/index.php
+++ b/app/views/video/index.php
@@ -22,7 +22,7 @@
                 <input type="hidden" name="module_id" value="<?= $data["module_id"] ?>">
                 <input type="hidden" name="language_id" value="<?= $data["language_id"] ?>">
                 <input type="hidden" name="user_id" value="<?= $data["user_id"] ?>">
-                <button type="submit" class="distinct-button font-reg text-sm" id="btn-finish">
+                <button type="submit" class="primary-button font-reg text-sm" id="btn-finish">
                     Next
                 </button>
             </div>
-- 
GitLab