diff --git a/api/exercise/submit.php b/api/exercise/submit.php new file mode 100644 index 0000000000000000000000000000000000000000..55a2ec28cd6ac159c18624aa0da3a40a007cccd1 --- /dev/null +++ b/api/exercise/submit.php @@ -0,0 +1,44 @@ +<?php +function submitQuiz($exerciseId, $selectedOption) +{ + $selectedOption = rtrim($selectedOption, '.'); + + $pairs = explode(',', $selectedOption); + + $submitData = []; + + foreach ($pairs as $pair) { + list($questionId, $optionId) = explode(':', $pair); + $submitData["question_" . $questionId] = $optionId; + } + + var_dump($submitData); + var_dump($exerciseId); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://172.20.10.2:5000/exercise/result/" . $exerciseId); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submitData)); + 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 'Quiz submitted successfully: ' . print_r($data, true); + } + + curl_close($ch); +} + + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submitQuiz'])) { + $exerciseId = $_POST['exerciseId']; + $selectedOption = isset($_POST['selectedOption']) ? $_POST['selectedOption'] : []; + + submitQuiz($exerciseId, $selectedOption); +} +?> \ No newline at end of file diff --git a/app/controllers/Exercise.php b/app/controllers/Exercise.php index b958022c8c5e1a6dd81ab288b69bcfa8f9dff81f..1d37887a67be374615f8704ed62a2f189152a178 100644 --- a/app/controllers/Exercise.php +++ b/app/controllers/Exercise.php @@ -1,19 +1,30 @@ <?php -class Exercise extends Controller { - public function index() { +class Exercise extends Controller +{ + public function index() + { $this->validateSession(); - + $data["pageTitle"] = "Test your knowledge!"; $data["languages"] = $this->model("LanguageModel")->getAllLanguage(); + $baseUrl = 'http://172.20.10.2:5000/exercise'; + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $exercise = json_decode($response, true); + + $data["exercise"] = $exercise['result']; + $this->view('header/index', $data); $this->view('navbar/index'); $this->view('exercise/index', $data); $this->view('footer/index'); } - public function question($exerciseId = null) { + public function question($exerciseId = null) + { $this->validateSession(); // $this->validateParamQuestion($questionId); @@ -22,9 +33,39 @@ class Exercise extends Controller { if (isset($exerciseId) && !empty($exerciseId)) { $data["pageTitle"] = "Test your knowledge!"; $data["user_id"] = $_SESSION['user_id']; - $data["exercise_id"] = $exerciseId; - // $data["video"] = $this->model("VideoModel")->getVideoById($videoId); + $data["exercise_id"] = intval($exerciseId); + + $baseUrl = 'http://172.20.10.2:5000/exercise'; + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $exercise = json_decode($response, true); + + $data["exercise"] = $exercise['result']; + + foreach ($data["exercise"] as $exercise) { + if ($exercise["exercise_id"] === $data["exercise_id"]) { + $data["currentExercise"] = $exercise; + break; + } + } + + $baseUrl = 'http://172.20.10.2:5000/question/' . $data["exercise_id"]; + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $question = json_decode($response, true); + + $data["question"] = $question['result'][0]; // ini '0' bisa diganti page + + $baseUrl = 'http://172.20.10.2:5000/option/' . $data["question"]["question_id"]; + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $option = json_decode($response, true); + $data["option"] = $option['result']; + $this->view('header/index', $data); $this->view('navbar/index'); $this->view('question/index', $data); @@ -32,7 +73,8 @@ class Exercise extends Controller { } } - public function validateParamQuestion($questionId) { + public function validateParamQuestion($questionId) + { if (isset($questionId) && !empty($questionId)) { // if ($this->model("VideoModel")->validateById($moduleId, $videoId)) { // return; diff --git a/app/controllers/Merchandise.php b/app/controllers/Merchandise.php new file mode 100644 index 0000000000000000000000000000000000000000..fe9e1961f62380426b4ca35dc5da0bee2a7c24ef --- /dev/null +++ b/app/controllers/Merchandise.php @@ -0,0 +1,17 @@ +<?php + +class Merchandise extends Controller +{ + public function index() + { + $this->validateSession(); + + $data["pageTitle"] = "Merch!"; + $data["user_id"] = $_SESSION['user_id']; + + $this->view('header/index', $data); + $this->view('navbar/index'); + $this->view('merchandise/index', $data); + $this->view('footer/index'); + } +} \ No newline at end of file diff --git a/app/views/exercise/index.php b/app/views/exercise/index.php index e8ebb97e7c7150ded8570657ed9c3ca21daff266..5aa938ac78ac350c70c334df9983221a338a3eff 100644 --- a/app/views/exercise/index.php +++ b/app/views/exercise/index.php @@ -1,4 +1,5 @@ <?php +$selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1; ?> <div class="exercise"> @@ -11,22 +12,48 @@ <div class="input-container"> <div class="filter-sort"> <select name="language" id="language-input" class="text-sm font-reg text-black"> - <option value="-1" <?php echo empty($data["languages"]) ? "selected" : ""; ?>>All languages</option> + <option value="-1" <?php echo ($selectedLanguageId === -1) ? "selected" : ""; ?>>All languages</option> <?php foreach ($data["languages"] as $language): ?> - <option value="<?= $language["language_id"] ?>"> + <option value="<?= $language["language_id"] ?>" <?php echo ($selectedLanguageId == $language["language_id"]) ? "selected" : ""; ?>> <?= $language["language_name"] ?> </option> <?php endforeach; ?> </select> </div> </div> - + <div class="card-container" id="exercise-container"> + <?php foreach ($data['exercise'] as $exercise): ?> + <div class="exercise-card"> + <a href="/exercise/question/<?= $exercise['exercise_id'] ?>"> + <div class="exercise-head"> + <div class="content"> + <h2 class="font-bold text-md"> + <?= $exercise['exe_name'] ?> + </h2> + <?php + $selectedLanguage = null; + foreach ($data["languages"] as $language) { + if ($language["language_id"] == $exercise["language_id"]) { + $selectedLanguage = $language; + break; + } + } + ?> + <span class="font-reg text-xs"> + <?= $selectedLanguage["language_name"] ?> ● + <?= $exercise['difficulty'] ?> ● + <?= $exercise['category'] ?> + </span> + </div> + </div> + </a> + </div> + <?php endforeach; ?> </div> </form> </div> </div> <script> const languages = <?= json_encode($data["languages"]) ?>; -</script> -<script src="/public/js/exercise.js"></script> \ No newline at end of file +</script> \ No newline at end of file diff --git a/app/views/question/index.php b/app/views/question/index.php index a13885998ba48f9d725c18b98f42714452529b8e..2989f58124687e7ab76c7a84940d3961fc97072b 100644 --- a/app/views/question/index.php +++ b/app/views/question/index.php @@ -1,23 +1,52 @@ -<?php -?> - <div class="question"> - <div class="container question-container"> - <h1 class="font-bold text-xl text-blue-purple-gradient" id="question-header"> - </h1> - <div class="" id="question-container"> - </div> - <div class="" id="exercise-pagination"> - </div> - <button id="submitBtn" onclick="submitQuiz()">Submit</button> - Score - <div id="exercise-score"> - </div> - </div> + <div class="container question-container"> + <h1 class="font-bold text-xl text-blue-purple-gradient"> + <?= $data['currentExercise']['exe_name'] ?> + </h1> + <form method="post" action="../../../api/exercise/submit.php"> + <div class="" id="question-container"> + <div> + <?= $data['question']['question'] ?> + </div> + <?php foreach ($data["option"] as $option): ?> + <div> + <label> + <input type="radio" name="options" value="<?= $option['option_id'] ?>" + onclick="saveSelectedOption('<?= $data['question']['question_id'] ?>', '<?= $option['option_id'] ?>')"> + + <?= $option['option'] ?> + </label> + </div> - + <?php endforeach; ?> + </div> + <div class="" id="hidden"></div> + <input type="hidden" name="exerciseId" value="<?= $data['currentExercise']['exercise_id'] ?>"> + <button type="submit" name="submitQuiz">Submit</button> + </form> + <div id="exercise-score"></div> + </div> </div> <script> - const exercise = <?= json_encode($data["exercise_id"]) ?>; -</script> -<script src="/public/js/question.js"></script> \ No newline at end of file + const selectedOption = {}; + + function saveSelectedOption(questionId, optionId) { + const questionKey = questionId.toString(); + selectedOption[questionKey] = optionId; + selectedOption[2] = 5 + + let listString = Object.entries(selectedOption) + .map(([key, value]) => `${key}:${value}`) + .join(','); + + listString += listString ? '.' : ''; + + const hidden = document.getElementById('hidden'); + + hidden.innerHTML = ''; + + hidden.innerHTML = ` + <input type="hidden" name="selectedOption" value="${listString}"> + `; + } +</script> \ No newline at end of file diff --git a/public/js/exercise.js b/public/js/exercise.js deleted file mode 100644 index dfc5cb98a3a3dbf1b1b75b697694419613345d71..0000000000000000000000000000000000000000 --- a/public/js/exercise.js +++ /dev/null @@ -1,51 +0,0 @@ -const exerciseContainer = document.getElementById('exercise-container'); -const languageSelect = document.getElementById('language-input'); -languageSelect.addEventListener('change', fetchExercises); - -fetchExercises(); - -function fetchExercises() { - const selectedLanguageId = parseInt(languageSelect.value, 10); - - if (selectedLanguageId === -1) { - link = "http://localhost:5000/exercise" - } else { - link = `http://localhost:5000/exercise/${selectedLanguageId}` - } - - fetch(link) - .then(response => response.json()) - .then(data => { - const exerciseContainer = document.getElementById('exercise-container'); - - exerciseContainer.innerHTML = ''; - - data.result.forEach(exercise => { - const exerciseCard = document.createElement('div'); - exerciseCard.classList.add('exercise-card'); - - for (let i = 0; i < languages.length; i++) { - if (languages[i].language_id === exercise.language_id) { - language = languages[i].language_name; - break; - } - } - - exerciseCard.innerHTML = ` - <a href="/exercise/question/${exercise.exercise_id}"> - <div class="exercise-head"> - <div class="content"> - <h2 class="font-bold text-md">${exercise.exe_name}</h2> - <span class="font-reg text-xs">${language} ● ${exercise.difficulty} ● ${exercise.category}</span> - </div> - </div> - </a> - `; - - exerciseContainer.appendChild(exerciseCard); - }); - }) - .catch(error => { - console.error('Error:', error); - }); -} diff --git a/public/js/merchandise.js b/public/js/merchandise.js new file mode 100644 index 0000000000000000000000000000000000000000..412888c5c1ee40fc4b49f372d2cc46dcdd131f54 --- /dev/null +++ b/public/js/merchandise.js @@ -0,0 +1,36 @@ +// const soapApiKey = 'your_api_key'; + +const soapRequest = `<x:Envelope +xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" +xmlns:ser="http://service.toco.org/"> +<x:Header/> +<x:Body> + <ser:getGems> + <arg0>1</arg0> + </ser:getGems> +</x:Body> +</x:Envelope>`; + +const soapUrl = 'http://localhost:8080/gems'; + +const headers = new Headers({ + 'Content-Type': 'text/xml', + 'SOAPAction': 'getGems', + // 'Authorization': 'ApiKey ' + soapApiKey, +}); + +const requestOptions = { + method: 'POST', + headers: headers, + body: soapRequest, +}; + +fetch(soapUrl, requestOptions) + .then(response => response.text()) + .then(data => { + console.log('Response:', data); + // Handle respons di sini + }) + .catch(error => { + console.error('Error:', error); + }); diff --git a/public/js/question.js b/public/js/question.js index 47708d061e62ebce7d57cd785ed2649e2791d3c1..254824b6746078fdea296be867830bb6002b5883 100644 --- a/public/js/question.js +++ b/public/js/question.js @@ -3,18 +3,6 @@ fetchQuestions(0); let selectedOption = {}; function fetchQuestions(page) { - fetch(`http://localhost:5000/exercise/${exercise}`) - .then(response => response.json()) - .then(exercise => { - const questionHeader = document.getElementById('question-header'); - - questionHeader.innerHTML = ''; - - questionHeader.innerHTML = ` - ${exercise.result[0].exe_name} - `; - }); - fetch(`http://localhost:5000/question/${exercise}`) .then(response => response.json()) .then(question => {