Skip to content
Snippets Groups Projects
Commit afab397e authored by bewe's avatar bewe
Browse files

feat: routing, score, etc for exercise

parent 7aaeaa19
Branches
No related merge requests found
<?php <?php
function submitQuiz($exerciseId, $selectedOptions, $userId) function submitQuiz($exerciseId, $selectedOptions, $userId)
{ {
// rest submit exercise
$pairs = array(); $pairs = array();
if ($selectedOptions) { if ($selectedOptions) {
...@@ -17,7 +18,8 @@ function submitQuiz($exerciseId, $selectedOptions, $userId) ...@@ -17,7 +18,8 @@ function submitQuiz($exerciseId, $selectedOptions, $userId)
} }
$ch = curl_init(); $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_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submitData)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($submitData));
...@@ -43,6 +45,35 @@ function submitQuiz($exerciseId, $selectedOptions, $userId) ...@@ -43,6 +45,35 @@ function submitQuiz($exerciseId, $selectedOptions, $userId)
curl_close($ch); 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'; $baseUrl = 'http://soap:8080/service/gems';
$soapRequest = '<x:Envelope $soapRequest = '<x:Envelope
...@@ -78,16 +109,19 @@ function submitQuiz($exerciseId, $selectedOptions, $userId) ...@@ -78,16 +109,19 @@ function submitQuiz($exerciseId, $selectedOptions, $userId)
} }
curl_close($ch); curl_close($ch);
header('Location: ../../exercise');
} }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submitQuiz'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submitQuiz'])) {
$exerciseId = $_POST['exerciseId']; $exerciseId = $_POST['exerciseId'];
$userId = $_POST['userId']; $userId = $_POST['userId'];
$isDone = $_POST['isDone'];
$selectedOptions = isset($_POST['selectedOptions']) ? $_POST['selectedOptions'] : []; $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
...@@ -5,11 +5,14 @@ class Exercise extends Controller ...@@ -5,11 +5,14 @@ class Exercise extends Controller
public function index() public function index()
{ {
$this->validateSession(); $this->validateSession();
$data["pageTitle"] = "Test your knowledge!"; $data["pageTitle"] = "Test your knowledge!";
$data["user_id"] = $_SESSION['user_id'];
$data["languages"] = $this->model("LanguageModel")->getAllLanguage(); $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); $ch = curl_init($baseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); $response = curl_exec($ch);
...@@ -17,6 +20,16 @@ class Exercise extends Controller ...@@ -17,6 +20,16 @@ class Exercise extends Controller
$data["exercise"] = $exercise['result']; $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('header/index', $data);
$this->view('navbar/index'); $this->view('navbar/index');
$this->view('exercise/index', $data); $this->view('exercise/index', $data);
...@@ -26,16 +39,17 @@ class Exercise extends Controller ...@@ -26,16 +39,17 @@ class Exercise extends Controller
public function question($exerciseId = null) public function question($exerciseId = null)
{ {
$this->validateSession(); $this->validateSession();
$this->validateParamExercise($exerciseId);
// $this->validateParamQuestion($questionId);
// Question // Question
if (isset($exerciseId) && !empty($exerciseId)) { if (isset($exerciseId) && !empty($exerciseId)) {
$data["pageTitle"] = "Test your knowledge!"; $data["pageTitle"] = "Test your knowledge!";
$data["exercise_id"] = intval($exerciseId);
$data["user_id"] = $_SESSION['user_id']; $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); $ch = curl_init($baseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); $response = curl_exec($ch);
...@@ -50,7 +64,26 @@ class Exercise extends Controller ...@@ -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); $ch = curl_init($baseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); $response = curl_exec($ch);
...@@ -58,19 +91,21 @@ class Exercise extends Controller ...@@ -58,19 +91,21 @@ class Exercise extends Controller
$data["questions"] = $question['result']; $data["questions"] = $question['result'];
// options
foreach ($data["questions"] as &$question) { 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); $ch = curl_init($baseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); $response = curl_exec($ch);
$option = json_decode($response, true); $option = json_decode($response, true);
$question["options"] = $option['result']; $question["options"] = $option['result'];
curl_close($ch); curl_close($ch);
} }
unset($question); unset($question);
$this->view('header/index', $data); $this->view('header/index', $data);
$this->view('navbar/index'); $this->view('navbar/index');
$this->view('question/index', $data); $this->view('question/index', $data);
...@@ -78,12 +113,20 @@ class Exercise extends Controller ...@@ -78,12 +113,20 @@ class Exercise extends Controller
} }
} }
public function validateParamQuestion($questionId) public function validateParamExercise($exerciseId)
{ {
if (isset($questionId) && !empty($questionId)) { if (isset($exerciseId) && !empty($exerciseId)) {
// if ($this->model("VideoModel")->validateById($moduleId, $videoId)) { // $baseUrl = 'http://express:5000/exercise/validate/' . $exerciseId;
// return; $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(); $this->show404();
exit(); exit();
} }
......
...@@ -25,7 +25,18 @@ $selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1; ...@@ -25,7 +25,18 @@ $selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1;
<div class="card-container" id="exercise-container"> <div class="card-container" id="exercise-container">
<?php foreach ($data['exercise'] as $exercise): ?> <?php foreach ($data['exercise'] as $exercise): ?>
<div class="exercise-card"> <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="exercise-head">
<div class="content"> <div class="content">
<h2 class="font-bold text-md"> <h2 class="font-bold text-md">
...@@ -46,8 +57,18 @@ $selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1; ...@@ -46,8 +57,18 @@ $selectedLanguageId = isset($_GET['language']) ? (int) $_GET['language'] : -1;
<?= $exercise['category'] ?> <?= $exercise['category'] ?>
</span> </span>
</div> </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> </div>
</a> <?php if (!$isProgressExists): ?>
</a>
<?php endif; ?>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
<div class="" id="hidden"></div> <div class="" id="hidden"></div>
<input type="hidden" name="exerciseId" value="<?= $data['currentExercise']['exercise_id'] ?>"> <input type="hidden" name="exerciseId" value="<?= $data['currentExercise']['exercise_id'] ?>">
<input type="hidden" name="userId" value="<?= $data["user_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> <button type="submit" name="submitQuiz">Submit</button>
</form> </form>
<div id="exercise-score"></div> <div id="exercise-score"></div>
...@@ -48,7 +49,7 @@ ...@@ -48,7 +49,7 @@
hidden.innerHTML = ''; hidden.innerHTML = '';
hidden.innerHTML = ` hidden.innerHTML = `
<input name="selectedOptions" value="${listString}"> <input type="hidden" name="selectedOptions" value="${listString}">
`; `;
} }
</script> </script>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment