Skip to content

Commit 76cb706

Browse files
authored
Merge pull request #7125 from christianbeeznest/ofaj-23118
Exercise: Fix per-attempt audio feedback loading - refs BT#23118
2 parents f1ab088 + 1d359cd commit 76cb706

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

public/main/inc/ajax/record_audio_rtc.ajax.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,25 @@
4444
break;
4545

4646
case Asset::EXERCISE_FEEDBACK:
47+
/** @var TrackEExercise|null $exeAttempt */
48+
$exeAttempt = Container::getTrackEExerciseRepository()->find($trackExerciseId);
49+
50+
if (null === $exeAttempt) {
51+
exit;
52+
}
53+
54+
// Make feedback asset unique per attempt + question (not only per question)
55+
$assetTitle = sprintf('feedback_%d_%d', $questionId, $trackExerciseId);
4756
$asset = (new Asset())
4857
->setCategory(Asset::EXERCISE_FEEDBACK)
49-
->setTitle("feedback_$questionId")
58+
->setTitle($assetTitle)
5059
;
5160

5261
$asset = $assetRepo->createFromRequest($asset, $_FILES['audio_blob']);
5362

5463
$attemptFeedback = (new AttemptFeedback())
5564
->setAsset($asset);
5665

57-
/** @var TrackEExercise $exeAttempt */
58-
$exeAttempt = Container::getTrackEExerciseRepository()->find($trackExerciseId);
5966
$attempt = $exeAttempt->getAttemptByQuestionId($questionId);
6067

6168
if (null === $attempt) {

public/main/inc/lib/exercise.lib.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5593,7 +5593,7 @@ public static function getOralFileAudio(int $trackExerciseId, int $questionId, b
55935593
*/
55945594
public static function getOralFeedbackAudio(int $attemptId, int $questionId): string
55955595
{
5596-
/** @var TrackEExercise $tExercise */
5596+
/** @var TrackEExercise|null $tExercise */
55975597
$tExercise = Container::getTrackEExerciseRepository()->find($attemptId);
55985598

55995599
if (null === $tExercise) {
@@ -5606,23 +5606,45 @@ public static function getOralFeedbackAudio(int $attemptId, int $questionId): st
56065606
return '';
56075607
}
56085608

5609-
$assetRepo = Container::getAssetRepository();
5610-
$html = '';
5609+
$feedbacks = $qAttempt->getAttemptFeedbacks();
56115610

5612-
// Keep only the latest audio feedback to avoid duplicated players
5613-
foreach ($qAttempt->getAttemptFeedbacks() as $attemptFeedback) {
5614-
$html = Display::tag(
5615-
'audio',
5616-
'',
5617-
[
5618-
'src' => $assetRepo->getAssetUrl($attemptFeedback->getAsset()),
5619-
'controls' => '',
5620-
]
5611+
if ($feedbacks->isEmpty()) {
5612+
return '';
5613+
}
56215614

5622-
);
5615+
$latestFeedback = null;
5616+
5617+
foreach ($feedbacks as $feedback) {
5618+
// Skip feedbacks without asset, just in case
5619+
if (null === $feedback->getAsset()) {
5620+
continue;
5621+
}
5622+
5623+
if (null === $latestFeedback) {
5624+
$latestFeedback = $feedback;
5625+
continue;
5626+
}
5627+
5628+
// Choose the feedback with the latest createdAt
5629+
if ($feedback->getCreatedAt() > $latestFeedback->getCreatedAt()) {
5630+
$latestFeedback = $feedback;
5631+
}
56235632
}
56245633

5625-
return $html;
5634+
if (null === $latestFeedback) {
5635+
return '';
5636+
}
5637+
5638+
$assetRepo = Container::getAssetRepository();
5639+
5640+
return Display::tag(
5641+
'audio',
5642+
'',
5643+
[
5644+
'src' => $assetRepo->getAssetUrl($latestFeedback->getAsset()),
5645+
'controls' => '',
5646+
]
5647+
);
56265648
}
56275649

56285650
public static function getUploadAnswerFiles(int $trackExerciseId, int $questionId, bool $returnUrls = false)

0 commit comments

Comments
 (0)