Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Pado
Asyst Moodle Plugin
Commits
d9234915
Commit
d9234915
authored
Aug 08, 2024
by
Artem Baranovskyi
Browse files
Optimized API Data structure.
parent
0faa1bc2
Changes
1
Hide whitespace changes
Inline
Side-by-side
asystgrade/lib.php
View file @
d9234915
...
@@ -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
,
$
useri
d
))
{
if
(
$quizQuery
->
gradesExist
(
$qid
,
$
studentI
d
))
{
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
];
];
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment