Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions public/main/exercise/annotation_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,50 @@

require_once __DIR__.'/../inc/global.inc.php';

$questionId = isset($_GET['question_id']) ? (int) $_GET['question_id'] : 0;
$exerciseId = isset($_GET['exe_id']) ? (int) $_GET['exe_id'] : 0;
$courseId = isset($_GET['course_id']) ? (int) $_GET['course_id'] : 0;
$courseInfo = api_get_course_info_by_id($courseId);
$questionId = (int) ($_GET['question_id'] ?? 0);
$exeId = (int) ($_GET['exe_id'] ?? 0);
$courseId = (int) ($_GET['course_id'] ?? 0);

if (empty($courseInfo)) {
return '';
$courseInfo = api_get_course_info_by_id($courseId);
if (empty($courseInfo) || !$questionId) {
header('Content-Type: application/json');
echo json_encode(['error' => 'invalid_request']);
exit;
}

$objQuestion = Question::read($questionId, $courseInfo);

$questionRepo = Container::getQuestionRepository();
/** @var CQuizQuestion $objQuestion */
$objQuestion = $questionRepo->find($questionId);

$answer_type = $objQuestion->getType(); //very important

$resourceFile = $objQuestion->getResourceNode()->getResourceFiles()->first();
$pictureWidth = $resourceFile->getWidth();
$pictureHeight = $resourceFile->getHeight();
$imagePath = $questionRepo->getHotSpotImageUrl($objQuestion);
/** @var CQuizQuestion|null $question */
$question = $questionRepo->find($questionId);

$webBase = rtrim(api_get_path(WEB_PATH), '/');
$imageUrl = '';
$pictureWidth = 0;
$pictureHeight = 0;

try {
$resourceFile = $question?->getResourceNode()?->getResourceFiles()?->first() ?: null;

if ($resourceFile && method_exists($resourceFile, 'getAsset') && $resourceFile->getAsset()) {
$assetRepo = Container::getAssetRepository();
$imageUrl = $webBase . $assetRepo->getAssetUrl($resourceFile->getAsset());
$pictureWidth = (int) ($resourceFile->getWidth() ?? 0);
$pictureHeight = (int) ($resourceFile->getHeight() ?? 0);
} else {
$legacy = (string) $questionRepo->getHotSpotImageUrl($question);
if ($legacy) {
$imageUrl = str_starts_with($legacy, 'http')
? $legacy
: $webBase . '/' . ltrim($legacy, '/');
}
}
} catch (\Throwable $e) {
}

$data = [
'use' => 'user',
'image' => [
'path' => $imagePath,
'width' => $pictureWidth,
'path' => $imageUrl,
'width' => $pictureWidth,
'height' => $pictureHeight,
],
'answers' => [
Expand All @@ -44,33 +61,27 @@
],
];

$attemptList = Event::getAllExerciseEventByExeId($exerciseId);

if (!empty($attemptList) && isset($attemptList[$questionId])) {
$questionAttempt = $attemptList[$questionId][0];
if (!empty($questionAttempt['answer'])) {
$answers = explode('|', $questionAttempt['answer']);
foreach ($answers as $answer) {
$parts = explode(')(', $answer);
$type = array_shift($parts);

switch ($type) {
case 'P':
$points = [];
foreach ($parts as $partPoint) {
$points[] = Geometry::decodePoint($partPoint);
}
$data['answers']['paths'][] = $points;

break;
case 'T':
$text = [
'text' => array_shift($parts),
];
$data['answers']['texts'][] = $text + Geometry::decodePoint($parts[0]);

break;
}
$attemptList = Event::getAllExerciseEventByExeId($exeId);
if (!empty($attemptList) && isset($attemptList[$questionId][0]['answer'])) {
$raw = (string) $attemptList[$questionId][0]['answer'];
$answers = explode('|', $raw);
foreach ($answers as $answer) {
$parts = explode(')(', $answer);
$type = array_shift($parts);

switch ($type) {
case 'P':
$points = [];
foreach ($parts as $partPoint) {
$points[] = Geometry::decodePoint($partPoint);
}
$data['answers']['paths'][] = $points;
break;

case 'T':
$text = ['text' => array_shift($parts)];
$data['answers']['texts'][] = $text + Geometry::decodePoint($parts[0]);
break;
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions public/main/inc/lib/exercise.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function setRemoveLink(dataContext) {
if (!empty($assetIds)) {
$icon = Display::return_icon('file_txt.gif');
$default = "";
$assetRepo = \Chamilo\CoreBundle\Framework\Container::getAssetRepository();
$assetRepo = Container::getAssetRepository();
$basePath = rtrim(api_get_path(WEB_PATH), "/");

foreach ($assetIds as $id) {
Expand Down Expand Up @@ -1736,12 +1736,18 @@ class="window window_left_question window{$questionId}_question">
$questionRepo = Container::getQuestionRepository();
$questionEntity = $questionRepo->find($questionId);
if ($freeze) {
echo Display::img(
$questionRepo->getHotSpotImageUrl($questionEntity),
$objQuestionTmp->selectTitle(),
['width' => '600px']
);

echo '
<div id="annotation-canvas-'.$questionId.'" class="annotation-canvas center-block"></div>
<script>
AnnotationQuestion({
questionId: '.(int)$questionId.',
exerciseId: 0,
relPath: \''.$relPath.'\',
courseId: '.(int)$course_id.',
mode: "preview"
});
</script>
';
return 0;
}
}
Expand Down Expand Up @@ -1803,7 +1809,7 @@ class="window window_left_question window{$questionId}_question">
unset($objAnswerTmp, $objQuestionTmp);
}

return $nbrAnswers;
return $nbrAnswers;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $
$question = $questionRepo->findOneBy(['resourceNode' => $resourceNode]);
if ($question) {
// Check if it's a Hotspot-type question
if (\in_array($question->getType(), [6, 7, 8], true)) { // HOT_SPOT, HOT_SPOT_ORDER, HOT_SPOT_DELINEATION
if (\in_array($question->getType(), [6, 7, 8, 20], true)) { // HOT_SPOT, HOT_SPOT_ORDER, HOT_SPOT_DELINEATION, ANNOTATION
$rel = $this->entityManager
->getRepository(CQuizRelQuestion::class)
->findOneBy(['question' => $question])
Expand Down
Loading