Commit d9234915 authored by Artem Baranovskyi's avatar Artem Baranovskyi
Browse files

Optimized API Data structure.

parent 0faa1bc2
...@@ -53,13 +53,15 @@ function local_asystgrade_before_footer() ...@@ -53,13 +53,15 @@ function local_asystgrade_before_footer()
$referenceAnswer = $quizQuery->get_reference_answer($qid); $referenceAnswer = $quizQuery->get_reference_answer($qid);
$data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer); $data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer);
foreach ($data['studentAnswers'] as $index => $studentAnswer) {
$userid = $data['studentIds'][$index]; foreach (array_keys($data['studentData']) as $studentId) {
if ($quizQuery->gradesExist($qid, $userid)) { if ($quizQuery->gradesExist($qid, $studentId)) {
return; return;
} }
} }
$inputNames = $data['inputNames'];
$studentData = $data['studentData'];
$inputNames = array_column($studentData, 'inputName');
error_log('Data prepared: ' . print_r($data, true)); error_log('Data prepared: ' . print_r($data, true));
...@@ -76,7 +78,10 @@ function local_asystgrade_before_footer() ...@@ -76,7 +78,10 @@ function local_asystgrade_before_footer()
error_log('ApiClient initiated.'); error_log('ApiClient initiated.');
error_log('Sending data to API and getting grade'); error_log('Sending data to API and getting grade');
$response = $apiClient->send_data($data); $response = $apiClient->send_data([
'referenceAnswer' => $data['referenceAnswer'],
'studentAnswers' => array_column($studentData, 'studentAnswer')
]);
$grades = json_decode($response, true); $grades = json_decode($response, true);
error_log('Grade obtained: ' . print_r($grades, true)); error_log('Grade obtained: ' . print_r($grades, true));
...@@ -113,9 +118,7 @@ function pasteGradedMarks(mixed $grades, mixed $inputNames): void ...@@ -113,9 +118,7 @@ function pasteGradedMarks(mixed $grades, mixed $inputNames): void
*/ */
function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAnswer): array function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAnswer): array
{ {
$studentAnswers = []; $studentData = [];
$inputNames = [];
$studentIds = [];
foreach ($question_attempts as $question_attempt) { foreach ($question_attempts as $question_attempt) {
$quizattempt_steps = $database->get_attempt_steps($question_attempt->id); $quizattempt_steps = $database->get_attempt_steps($question_attempt->id);
...@@ -123,10 +126,16 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns ...@@ -123,10 +126,16 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns
foreach ($quizattempt_steps as $quizattempt_step) { foreach ($quizattempt_steps as $quizattempt_step) {
if ($quizattempt_step->state === 'complete') { if ($quizattempt_step->state === 'complete') {
$studentAnswer = $database->get_student_answer($quizattempt_step->id); $studentAnswer = $database->get_student_answer($quizattempt_step->id);
$studentAnswers[] = $studentAnswer; $studentId = $quizattempt_step->userid;
$inputNames[] = "q" . $question_attempt->questionusageid . ":" . $question_attempt->slot . "_-mark"; $inputName = "q" . $question_attempt->questionusageid . ":" . $question_attempt->slot . "_-mark";
$studentIds[] = $quizattempt_step->userid;
error_log("Student Answer: $studentAnswer, Input Name: " . end($inputNames)); // Adding data to an associative array
$studentData[$studentId] = [
'studentAnswer' => $studentAnswer,
'inputName' => $inputName // identifying name for mark input field updating
];
error_log("Student ID: $studentId, Student Answer: $studentAnswer, Input Name: $inputName");
} }
} }
...@@ -137,9 +146,7 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns ...@@ -137,9 +146,7 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns
return [ return [
'referenceAnswer' => $referenceAnswer, 'referenceAnswer' => $referenceAnswer,
'studentAnswers' => $studentAnswers, 'studentData' => $studentData
'inputNames' => $inputNames,
'studentIds' => $studentIds
]; ];
} }
......
Supports Markdown
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