*/
require_once __DIR__.'/../inc/global.inc.php';
api_protect_course_script();
require_once api_get_path(LIBRARY_PATH).'geometry.lib.php';
/** @var Exercise $objExercise */
$objExercise = Session::read('objExercise');
$exerciseResult = Session::read('exerciseResult');
if (empty($objExercise)) {
api_not_allowed();
}
$feedbackType = $objExercise->getFeedbackType();
$exerciseType = $objExercise->type;
if (!in_array($feedbackType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])) {
api_not_allowed();
}
$learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : 0;
$learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : 0;
$questionList = Session::read('questionList');
$exerciseId = (int) $_GET['exerciseId'];
$questionNum = (int) $_GET['num'];
$questionId = $questionList[$questionNum];
$choiceValue = isset($_GET['choice']) ? $_GET['choice'] : '';
$hotSpot = isset($_GET['hotspot']) ? $_GET['hotspot'] : '';
$loaded = isset($_GET['loaded']);
if (empty($choiceValue) && isset($exerciseResult[$questionId])) {
$choiceValue = $exerciseResult[$questionId];
}
if (!empty($hotSpot)) {
if (isset($hotSpot[$questionId])) {
$hotSpot = $hotSpot[$questionId];
}
}
if (!empty($choiceValue)) {
if (isset($choiceValue[$questionId])) {
$choiceValue = $choiceValue[$questionId];
}
}
echo '';
echo '
';
// Getting the options by js
if (empty($choiceValue) && empty($hotSpot) && $loaded) {
$nextQuestion = $questionNum + 1;
$destinationId = isset($questionList[$nextQuestion]) ? $questionList[$nextQuestion] : -1;
$icon = Display::return_icon(
'reload.png',
'',
['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
);
$links = '
'.get_lang('Try again').' '.$icon.' ';
// the link to finish the test
if ($destinationId == -1) {
$links .= Display::return_icon(
'finish.gif',
'',
['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
).'
'.get_lang('End of activity').'';
} else {
// the link to other question
if (in_array($destinationId, $questionList)) {
$num_value_array = array_keys($questionList, $destinationId);
$icon = Display::return_icon(
'quiz.png',
'',
['style' => 'padding-left:0px;padding-right:5px;']
);
$links .= '
'.
get_lang('Question').' '.$num_value_array[0].' ';
$links .= $icon;
}
}
echo '
';
exit;
}
if (empty($choiceValue) && empty($hotSpot)) {
echo "';
exit;
}
$choice = [];
$choice[$questionId] = isset($choiceValue) ? $choiceValue : null;
if (!is_array($exerciseResult)) {
$exerciseResult = [];
}
// if the user has answered at least one question
if (is_array($choice)) {
if (in_array($exerciseType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])) {
// $exerciseResult receives the content of the form.
// Each choice of the student is stored into the array $choice
$exerciseResult = $choice;
} else {
// gets the question ID from $choice. It is the key of the array
list($key) = array_keys($choice);
// if the user didn't already answer this question
if (!isset($exerciseResult[$key])) {
// stores the user answer into the array
$exerciseResult[$key] = $choice[$key];
}
}
}
// the script "exercise_result.php" will take the variable $exerciseResult from the session
Session::write('exerciseResult', $exerciseResult);
$objQuestionTmp = Question::read($questionId);
$answerType = $objQuestionTmp->selectType();
$showResult = false;
$objAnswerTmp = new Answer($questionId, api_get_course_int_id());
if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) {
$showResult = true;
}
switch ($answerType) {
case MULTIPLE_ANSWER:
if (is_array($choiceValue)) {
$choiceValue = array_combine(array_values($choiceValue), array_values($choiceValue));
}
break;
case UNIQUE_ANSWER:
if (is_array($choiceValue) && isset($choiceValue[0])) {
$choiceValue = $choiceValue[0];
}
break;
case DRAGGABLE:
break;
case HOT_SPOT_DELINEATION:
$showResult = true;
if (is_array($hotSpot)) {
$choiceValue = isset($hotSpot[1]) ? $hotSpot[1] : '';
$_SESSION['exerciseResultCoordinates'][$questionId] = $choiceValue; //needed for exercise_result.php
$delineation_cord = $objAnswerTmp->selectHotspotCoordinates(1);
$answer_delineation_destination = $objAnswerTmp->selectDestination(1);
$_SESSION['hotspot_coord'][$questionId][1] = $delineation_cord;
$_SESSION['hotspot_dest'][$questionId][1] = $answer_delineation_destination;
}
break;
case CALCULATED_ANSWER:
/*$_SESSION['calculatedAnswerId'][$questionId] = mt_rand(
1,
$nbrAnswers
);*/
//var_dump($_SESSION['calculatedAnswerId'][$questionId]);
break;
}
ob_start();
$result = $objExercise->manage_answer(
0,
$questionId,
$choiceValue,
'exercise_result',
null,
false,
false,
$showResult,
null,
[],
true,
false,
true
);
$manageAnswerHtmlContent = ob_get_clean();
$contents = '';
$answerCorrect = false;
if (!empty($result)) {
switch ($answerType) {
case UNIQUE_ANSWER:
case MULTIPLE_ANSWER:
case DRAGGABLE:
case HOT_SPOT_DELINEATION:
case CALCULATED_ANSWER:
if ($result['score'] == $result['weight']) {
$answerCorrect = true;
}
break;
}
}
if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) {
if (isset($result['correct_answer_id'])) {
/** @var Answer $answer */
$answerId = $result['correct_answer_id'];
$contents = $objAnswerTmp->selectComment($answerId);
}
} else {
$contents = Display::return_message(get_lang('Incorrect'), 'warning');
if ($answerCorrect) {
$contents = Display::return_message(get_lang('Correct'), 'success');
}
}
if ($answerType === HOT_SPOT_DELINEATION) {
$contents = $manageAnswerHtmlContent;
}
$links = '';
if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) {
if (isset($choiceValue) && $choiceValue == -1) {
if ($answerType != HOT_SPOT_DELINEATION) {
$links .= '
'.get_lang('Choose an answer').'';
}
}
}
$destinationId = null;
if (isset($result['answer_destination'])) {
$itemList = explode('@@', $result['answer_destination']);
$try = $itemList[0];
$lp = $itemList[1];
$destinationId = $itemList[2];
$url = $itemList[3];
}
// the link to retry the question
if (isset($try) && $try == 1) {
$num_value_array = array_keys($questionList, $questionId);
$links .= Display:: return_icon(
'reload.gif',
'',
['style' => 'padding-left:0px;padding-right:5px;']
).'
'.get_lang('Try again').'';
}
// the link to theory (a learning path)
if (!empty($lp)) {
$lp_url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp;
$links .= Display:: return_icon(
'theory.gif',
'',
['style' => 'padding-left:0px;padding-right:5px;']
).'
'.get_lang('Theory link').'';
}
$links .= '
';
// the link to an external website or link
if (!empty($url) && $url != -1) {
$links .= Display:: return_icon(
'link.gif',
'',
['style' => 'padding-left:0px;padding-right:5px;']
).'
'.get_lang('Visit this link').'';
}
$nextQuestion = $questionNum + 1;
$destinationId = isset($questionList[$nextQuestion]) ? $questionList[$nextQuestion] : -1;
// the link to finish the test
if ($destinationId == -1) {
$links .= Display:: return_icon(
'finish.gif',
'',
['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
).'
'.get_lang('End of activity').'';
} else {
// the link to other question
if (in_array($destinationId, $questionList)) {
$num_value_array = array_keys($questionList, $destinationId);
$icon = Display::return_icon(
'quiz.png',
'',
['style' => 'padding-left:0px;padding-right:5px;']
);
$links .= '
'.
get_lang('Question').' '.$num_value_array[0].' ';
$links .= $icon;
}
}
if (!empty($links)) {
echo '
'.$contents.'
';
echo '
'.$links.'
';
echo '
';
} else {
$questionNum++;
echo '';
}
echo '';