diff --git a/api/exercise/submit.php b/api/exercise/submit.php index a1d847778cea91d9fd2c98529909ba76bad6ba3d..f83858dffebfd3367f25e33252fbf81a60a071dc 100644 --- a/api/exercise/submit.php +++ b/api/exercise/submit.php @@ -1,9 +1,13 @@ <?php -function submitQuiz($exerciseId, $selectedOption) +function submitQuiz($exerciseId, $selectedOptions, $userId) { - $selectedOption = rtrim($selectedOption, '.'); + $pairs = array(); + + if ($selectedOptions) { + $selectedOptions = rtrim($selectedOptions, '.'); + $pairs = explode(',', $selectedOptions); + } - $pairs = explode(',', $selectedOption); $submitData = []; @@ -12,9 +16,6 @@ function submitQuiz($exerciseId, $selectedOption) $submitData["question_" . $questionId] = $optionId; } - var_dump($submitData); - var_dump($exerciseId); - $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://express:5000/exercise/result/" . $exerciseId); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @@ -28,17 +29,65 @@ function submitQuiz($exerciseId, $selectedOption) echo 'Error: ' . curl_error($ch); } else { $data = json_decode($response, true); - echo 'Quiz submitted successfully: ' . print_r($data, true); + + $correctResults = $data['correctResults']; + $wrongResults = $data['wrongResults']; + + $correctCount = count($correctResults); + $wrongCount = count($wrongResults); + + $score = $correctCount / ($correctCount + $wrongCount) * 100; + + // echo 'Quiz submitted successfully. Score: ' . $score; } curl_close($ch); + + $baseUrl = 'http://soap:8080/service/gems'; + + $soapRequest = '<x:Envelope + xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" + xmlns:ser="http://service.toco.org/"> + <x:Header/> + <x:Body> + <ser:addGems> + <user_id>' . $userId . '</user_id> + <gem>' . $score . '</gem> + </ser:addGems> + </x:Body> + </x:Envelope>'; + + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $soapRequest); + curl_setopt( + $ch, + CURLOPT_HTTPHEADER, + array( + 'Content-Type: text/xml', + 'SOAPAction: addGems', + 'X-api-key: toco_php' + ) + ); + + $response = curl_exec($ch); + + if (curl_errno($ch)) { + echo 'Curl error: ' . curl_error($ch); + } + + curl_close($ch); + + header('Location: ../../exercise'); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submitQuiz'])) { $exerciseId = $_POST['exerciseId']; - $selectedOption = isset($_POST['selectedOption']) ? $_POST['selectedOption'] : []; + $userId = $_POST['userId']; + $selectedOptions = isset($_POST['selectedOptions']) ? $_POST['selectedOptions'] : []; - submitQuiz($exerciseId, $selectedOption); + submitQuiz($exerciseId, $selectedOptions, $userId); } ?> \ No newline at end of file diff --git a/app/controllers/Exercise.php b/app/controllers/Exercise.php index 60ca4148172c4dd58c739221b1629be7e6908a34..a905b9a554c05be6699894421b2c84f3b1cefdd7 100644 --- a/app/controllers/Exercise.php +++ b/app/controllers/Exercise.php @@ -56,15 +56,20 @@ class Exercise extends Controller $response = curl_exec($ch); $question = json_decode($response, true); - $data["question"] = $question['result'][0]; // ini '0' bisa diganti page + $data["questions"] = $question['result']; - $baseUrl = 'http://express: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']; + foreach ($data["questions"] as &$question) { + $baseUrl = 'http://express: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'); diff --git a/app/views/question/index.php b/app/views/question/index.php index 4182ff7f83a30ad6448b6814643ac2006a99503a..d051ae49fe0d5dde2548d825a113f798a4413e01 100644 --- a/app/views/question/index.php +++ b/app/views/question/index.php @@ -5,48 +5,50 @@ </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): ?> + <?php foreach ($data["questions"] as $question): ?> <div> - <label> - <input type="radio" name="options" value="<?= $option['option_id'] ?>" - onclick="saveSelectedOption('<?= $data['question']['question_id'] ?>', '<?= $option['option_id'] ?>')"> - - <?= $option['option'] ?> - </label> + <?= $question['question'] ?> </div> - + <?php foreach ($question["options"] as $option): ?> + <div> + <label> + <input type="radio" name="options[<?= $question['question_id'] ?>]" value="<?= $option['option_id'] ?>" + onclick="saveSelectedOption('<?= $question['question_id'] ?>', '<?= $option['option_id'] ?>')"> + <?= $option['option'] ?> + </label> + </div> + <?php endforeach; ?> <?php endforeach; ?> </div> <div class="" id="hidden"></div> <input type="hidden" name="exerciseId" value="<?= $data['currentExercise']['exercise_id'] ?>"> + <input type="hidden" name="userId" value="<?= $data["user_id"] ?>"> <button type="submit" name="submitQuiz">Submit</button> </form> <div id="exercise-score"></div> </div> </div> + <script> - const selectedOption = {}; + const selectedOptions = {}; function saveSelectedOption(questionId, optionId) { - const questionKey = questionId.toString(); - selectedOption[questionKey] = optionId; - // selectedOption[2] = 5 + selectedOptions[questionId] = optionId; - let listString = Object.entries(selectedOption) - .map(([key, value]) => `${key}:${value}`) - .join(','); + let listString = Object.entries(selectedOptions) + .map(([key, value]) => `${key}:${value}`) + .join(','); listString += listString ? '.' : ''; + console.log(listString); + const hidden = document.getElementById('hidden'); hidden.innerHTML = ''; hidden.innerHTML = ` - <input type="hidden" name="selectedOption" value="${listString}"> + <input name="selectedOptions" value="${listString}"> `; } -</script> \ No newline at end of file +</script>