* Fill in blank option added (2008)
* Cleaning exercises (2010),
* Adding hotspot delineation support (2011)
* Adding reminder + ajax support (2011)
* Modified by hubert.borderiou (2011-10-21 question category)
*/
require_once __DIR__.'/../inc/global.inc.php';
$current_course_tool = TOOL_QUIZ;
$this_section = SECTION_COURSES;
$debug = false;
// Notice for unauthorized people.
api_protect_course_script(true);
$origin = api_get_origin();
$is_allowedToEdit = api_is_allowed_to_edit(null, true);
$courseId = api_get_course_int_id();
$sessionId = api_get_session_id();
$glossaryExtraTools = api_get_setting('show_glossary_in_extra_tools');
$showGlossary = in_array($glossaryExtraTools, ['true', 'exercise', 'exercise_and_lp']);
if ($origin == 'learnpath') {
$showGlossary = in_array($glossaryExtraTools, ['true', 'lp', 'exercise_and_lp']);
}
if ($showGlossary) {
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = api_get_js('jquery.highlight.js');
}
$js = '';
$htmlHeadXtra[] = $js;
$htmlHeadXtra[] = api_get_js('jqueryui-touch-punch/jquery.ui.touch-punch.min.js');
$htmlHeadXtra[] = api_get_js('jquery.jsPlumb.all.js');
$htmlHeadXtra[] = api_get_js('d3/jquery.xcolor.js');
//This library is necessary for the time control feature
//tmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
$htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
$htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
$htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
$htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = '';
if (api_get_configuration_value('quiz_prevent_copy_paste')) {
$htmlHeadXtra[] = '';
}
if (api_get_setting('enable_record_audio') === 'true') {
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = '';
$htmlHeadXtra[] = api_get_js('record_audio/record_audio.js');
}
$template = new Template();
// General parameters passed via POST/GET
$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;
$learnpath_item_view_id = isset($_REQUEST['learnpath_item_view_id']) ? (int) $_REQUEST['learnpath_item_view_id'] : 0;
$reminder = isset($_REQUEST['reminder']) ? (int) $_REQUEST['reminder'] : 0;
$remind_question_id = isset($_REQUEST['remind_question_id']) ? (int) $_REQUEST['remind_question_id'] : 0;
$exerciseId = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : 0;
$formSent = isset($_REQUEST['formSent']) ? $_REQUEST['formSent'] : null;
$exerciseResult = isset($_REQUEST['exerciseResult']) ? $_REQUEST['exerciseResult'] : null;
$exerciseResultCoordinates = isset($_REQUEST['exerciseResultCoordinates']) ? $_REQUEST['exerciseResultCoordinates'] : null;
$choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
$choice = empty($choice) ? isset($_REQUEST['choice2']) ? $_REQUEST['choice2'] : null : null;
// From submit modal
$current_question = isset($_REQUEST['num']) ? (int) $_REQUEST['num'] : null;
$currentAnswer = isset($_REQUEST['num_answer']) ? (int) $_REQUEST['num_answer'] : null;
$endExercise = isset($_REQUEST['end_exercise']) && $_REQUEST['end_exercise'] == 1 ? true : false;
$logInfo = [
'tool' => TOOL_QUIZ,
'tool_id' => $exerciseId,
'tool_id_detail' => 0,
'action' => ((int) $_REQUEST['learnpath_id'] > 0) ? 'learnpath_id' : '',
'action_details' => ((int) $_REQUEST['learnpath_id'] > 0) ? (int) $_REQUEST['learnpath_id'] : '',
];
Event::registerLog($logInfo);
// Error message
$error = '';
$exercise_attempt_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
/* Teacher takes an exam and want to see a preview,
we delete the objExercise from the session in order to get the latest
changes in the exercise */
if (api_is_allowed_to_edit(null, true) &&
isset($_GET['preview']) && $_GET['preview'] == 1
) {
Session::erase('objExercise');
}
// 1. Loading the $objExercise variable
/** @var \Exercise $exerciseInSession */
$exerciseInSession = Session::read('objExercise');
if (!isset($exerciseInSession) || isset($exerciseInSession) && ($exerciseInSession->id != $_GET['exerciseId'])) {
// Construction of Exercise
$objExercise = new Exercise($courseId);
Session::write('firstTime', true);
if ($debug) {
error_log('1. Setting the $objExercise variable');
}
Session::erase('questionList');
// if the specified exercise doesn't exist or is disabled
if (!$objExercise->read($exerciseId) ||
(!$objExercise->selectStatus() && !$is_allowedToEdit && !in_array($origin, ['learnpath', 'embeddable']))
) {
if ($debug) {
error_log('1.1. Error while reading the exercise');
}
unset($objExercise);
$error = get_lang('ExerciseNotFound');
} else {
// Saves the object into the session
Session::write('objExercise', $objExercise);
if ($debug) {
error_log('1.1. $exerciseInSession was unset - set now - end');
}
}
} else {
Session::write('firstTime', false);
}
//2. Checking if $objExercise is set
if (!isset($objExercise) && isset($exerciseInSession)) {
if ($debug) {
error_log('2. Loading $objExercise from session');
}
$objExercise = $exerciseInSession;
}
//3. $objExercise is not set, then return to the exercise list
if (!is_object($objExercise)) {
if ($debug) {
error_log('3. $objExercise was not set, kill the script');
}
header('Location: exercise.php');
exit;
}
// if the user has submitted the form
$exercise_title = $objExercise->selectTitle();
$exercise_sound = $objExercise->selectSound();
// If reminder ends we jump to the exercise_reminder
if ($objExercise->review_answers) {
if ($remind_question_id == -1) {
header('Location: '.api_get_path(WEB_CODE_PATH).
'exercise/exercise_reminder.php?exerciseId='.$exerciseId.'&'.api_get_cidreq());
exit;
}
}
$template->assign('shuffle_answers', $objExercise->random_answers);
$templateName = $template->get_template('exercise/submit.js.tpl');
$htmlHeadXtra[] = $template->fetch($templateName);
$current_timestamp = time();
$myRemindList = [];
$time_control = false;
if ($objExercise->expired_time != 0) {
$time_control = true;
}
// Generating the time control key for the user
$current_expired_time_key = ExerciseLib::get_time_control_key(
$objExercise->id,
$learnpath_id,
$learnpath_item_id
);
Session::write('duration_time_previous', [$current_expired_time_key => $current_timestamp]);
$durationTime = Session::read('duration_time');
if (!empty($durationTime) && isset($durationTime[$current_expired_time_key])) {
Session::write(
'duration_time_previous',
[$current_expired_time_key => $durationTime[$current_expired_time_key]]
);
}
Session::write('duration_time', [$current_expired_time_key => $current_timestamp]);
if ($time_control) {
// Get the expired time of the current exercise in track_e_exercises
$total_seconds = $objExercise->expired_time * 60;
}
$show_clock = true;
$user_id = api_get_user_id();
if ($objExercise->selectAttempts() > 0) {
$messageReachedMax = Display::return_message(
sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()),
'warning',
false
);
$attempt_html = '';
$attempt_count = Event::get_attempt_count(
$user_id,
$exerciseId,
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id
);
if ($attempt_count >= $objExercise->selectAttempts()) {
$show_clock = false;
if (!api_is_allowed_to_edit(null, true)) {
if ($objExercise->results_disabled == 0 && !in_array($origin, ['learnpath', 'embeddable'])) {
// Showing latest attempt according with task BT#1628
$exercise_stat_info = Event::getExerciseResultsByUser(
$user_id,
$exerciseId,
api_get_course_id(),
api_get_session_id()
);
if (!empty($exercise_stat_info)) {
$isQuestionsLimitReached = ExerciseLib::isQuestionsLimitPerDayReached(
$user_id,
count($objExercise->get_validated_question_list()),
$courseId,
$sessionId
);
if ($isQuestionsLimitReached) {
$maxQuestionsAnswered = (int) api_get_course_setting('quiz_question_limit_per_day');
Display::addFlash(
Display::return_message(
sprintf(get_lang('QuizQuestionsLimitPerDayXReached'), $maxQuestionsAnswered),
'warning',
false
)
);
if (in_array($origin, ['learnpath', 'embeddable'])) {
Display::display_reduced_header();
Display::display_reduced_footer();
} else {
Display::display_header(get_lang('Exercises'));
Display::display_footer();
}
exit;
}
$max_exe_id = max(array_keys($exercise_stat_info));
$last_attempt_info = $exercise_stat_info[$max_exe_id];
$attempt_html .= Display::div(
get_lang('Date').': '.api_get_local_time($last_attempt_info['exe_date']),
['id' => '']
);
$attempt_html .= $messageReachedMax;
if (!empty($last_attempt_info['question_list'])) {
foreach ($last_attempt_info['question_list'] as $questions) {
foreach ($questions as $question_data) {
$question_id = $question_data['question_id'];
$marks = $question_data['marks'];
$question_info = Question::read($question_id);
$attempt_html .= Display::div(
$question_info->question,
['class' => 'question_title']
);
$attempt_html .= Display::div(
get_lang('Score').' '.$marks,
['id' => 'question_question_titlescore']
);
}
}
}
$score = ExerciseLib::show_score(
$last_attempt_info['exe_result'],
$last_attempt_info['exe_weighting']
);
$attempt_html .= Display::div(
get_lang('YourTotalScore').' '.$score,
['id' => 'question_score']
);
} else {
$attempt_html .= $messageReachedMax;
}
} else {
$attempt_html .= $messageReachedMax;
}
} else {
$attempt_html .= $messageReachedMax;
}
if (in_array($origin, ['learnpath', 'embeddable'])) {
Display::display_reduced_header();
} else {
Display::display_header(get_lang('Exercises'));
}
echo $attempt_html;
if (!in_array($origin, ['learnpath', 'embeddable'])) {
Display::display_footer();
}
exit;
}
}
/* 5. Getting user exercise info (if the user took the exam before)
generating exe_id */
$exercise_stat_info = $objExercise->get_stat_track_exercise_info(
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id
);
// Fix in order to get the correct question list.
$questionListUncompressed = $objExercise->getQuestionListWithMediasUncompressed();
Session::write('question_list_uncompressed', $questionListUncompressed);
$clock_expired_time = null;
if (empty($exercise_stat_info)) {
$disable = api_get_configuration_value('exercises_disable_new_attempts');
if ($disable) {
api_not_allowed(true);
}
if ($debug) {
error_log('5 $exercise_stat_info is empty ');
}
$total_weight = 0;
$questionList = $objExercise->get_validated_question_list();
foreach ($questionListUncompressed as $question_id) {
$objQuestionTmp = Question::read($question_id);
$total_weight += floatval($objQuestionTmp->weighting);
}
if ($time_control) {
$expected_time = $current_timestamp + $total_seconds;
if ($debug) {
error_log('5.1. $current_timestamp '.$current_timestamp);
}
if ($debug) {
error_log('5.2. $expected_time '.$expected_time);
}
$clock_expired_time = api_get_utc_datetime($expected_time);
if ($debug) {
error_log('5.3. $expected_time '.$clock_expired_time);
}
//Sessions that contain the expired time
$_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
if ($debug) {
error_log(
'5.4. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key]
);
}
}
$exe_id = $objExercise->save_stat_track_exercise_info(
$clock_expired_time,
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id,
$questionList,
$total_weight
);
$exercise_stat_info = $objExercise->get_stat_track_exercise_info(
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id
);
// Send notification at the start
if (!api_is_allowed_to_edit(null, true) &&
!api_is_excluded_user_type()
) {
$objExercise->send_mail_notification_for_exam(
'start',
[],
$origin,
$exe_id
);
}
if ($debug) {
error_log("5.5 exercise_stat_info[] exists getting exe_id $exe_id");
}
} else {
$exe_id = $exercise_stat_info['exe_id'];
// Remember last question id position.
$isFirstTime = Session::read('firstTime');
if ($isFirstTime && $objExercise->type == ONE_PER_PAGE) {
$resolvedQuestions = Event::getAllExerciseEventByExeId($exe_id);
if (!empty($resolvedQuestions) &&
!empty($exercise_stat_info['data_tracking'])
) {
$last = current(end($resolvedQuestions));
$attemptQuestionList = explode(',', $exercise_stat_info['data_tracking']);
$count = 1;
foreach ($attemptQuestionList as $question) {
if ($last['question_id'] == $question) {
break;
}
$count++;
}
$current_question = $count;
}
}
if ($debug) {
error_log("5 exercise_stat_info[] exists getting exe_id $exe_id ");
}
}
$saveDurationUrl = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=update_duration&exe_id='.$exe_id.'&'.api_get_cidreq();
$questionListInSession = Session::read('questionList');
if (!isset($questionListInSession)) {
// Selects the list of question ID
$questionList = $objExercise->getQuestionList();
// Media questions.
$media_is_activated = $objExercise->mediaIsActivated();
// Getting order from random
if ($media_is_activated == false &&
(
$objExercise->isRandom() ||
!empty($objExercise->getRandomByCategory()) ||
$objExercise->getQuestionSelectionType() > 2
) &&
isset($exercise_stat_info) &&
!empty($exercise_stat_info['data_tracking'])
) {
$questionList = explode(',', $exercise_stat_info['data_tracking']);
}
Session::write('questionList', $questionList);
if ($debug > 0) {
error_log('$_SESSION[questionList] was set');
}
} else {
if (isset($objExercise) && isset($exerciseInSession)) {
$questionList = Session::read('questionList');
}
}
// Array to check in order to block the chat
ExerciseLib::create_chat_exercise_session($exe_id);
if ($debug) {
error_log(
'6. $objExercise->get_stat_track_exercise_info function called:: '.print_r(
$exercise_stat_info,
1
)
);
}
if (!empty($exercise_stat_info['questions_to_check'])) {
$myRemindList = $exercise_stat_info['questions_to_check'];
$myRemindList = explode(',', $myRemindList);
$myRemindList = array_filter($myRemindList);
}
$params = "exe_id=$exe_id&exerciseId=$exerciseId&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq().'&reminder='.$reminder;
if ($debug) {
error_log("6.1 params: -> $params");
}
if ($reminder == 2 && empty($myRemindList)) {
if ($debug) {
error_log("6.2 calling the exercise_reminder.php ");
}
header('Location: exercise_reminder.php?'.$params);
exit;
}
/*
* 7. Loading Time control parameters
* If the expired time is major that zero(0) then the expired time is compute on this time.
*/
if ($time_control) {
if ($debug) {
error_log('7.1. Time control is enabled');
error_log('7.2. $current_expired_time_key '.$current_expired_time_key);
error_log(
'7.3. $_SESSION[expired_time][$current_expired_time_key] '.
$_SESSION['expired_time'][$current_expired_time_key]
);
}
if (!isset($_SESSION['expired_time'][$current_expired_time_key])) {
//Timer - Get expired_time for a student
if (!empty($exercise_stat_info)) {
$expired_time_of_this_attempt = $exercise_stat_info['expired_time_control'];
if ($debug) {
error_log('7.4 Seems that the session ends and the user want to retake the exam');
error_log('7.5 $expired_time_of_this_attempt: '.$expired_time_of_this_attempt);
}
// Get the last attempt of an exercise
$last_attempt_date = Event::getLastAttemptDateOfExercise($exercise_stat_info['exe_id']);
/* This means that the user enters the exam but do not answer the
first question we get the date from the track_e_exercises not from
the track_et_attempt see #2069 */
if (empty($last_attempt_date)) {
$diff = $current_timestamp - api_strtotime($exercise_stat_info['start_date'], 'UTC');
$last_attempt_date = api_get_utc_datetime(
api_strtotime($exercise_stat_info['start_date'], 'UTC') + $diff
);
} else {
//Recalculate the time control due #2069
$diff = $current_timestamp - api_strtotime($last_attempt_date, 'UTC');
$last_attempt_date = api_get_utc_datetime(api_strtotime($last_attempt_date, 'UTC') + $diff);
}
//New expired time - it is due to the possible closure of session
$new_expired_time_in_seconds = api_strtotime($expired_time_of_this_attempt, 'UTC') - api_strtotime($last_attempt_date, 'UTC');
$expected_time = $current_timestamp + $new_expired_time_in_seconds;
$clock_expired_time = api_get_utc_datetime($expected_time);
// First we update the attempt to today
/* How the expired time is changed into "track_e_exercises" table,
then the last attempt for this student should be changed too */
$sql = "UPDATE $exercise_attempt_table SET
tms = '".api_get_utc_datetime()."'
WHERE
exe_id = '".$exercise_stat_info['exe_id']."' AND
tms = '".$last_attempt_date."' ";
Database::query($sql);
// Sessions that contain the expired time
$_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
if ($debug) {
error_log('7.6. $last_attempt_date: '.$last_attempt_date);
error_log('7.7. $new_expired_time_in_seconds: '.$new_expired_time_in_seconds);
error_log('7.8. $expected_time1: '.$expected_time);
error_log('7.9. $clock_expired_time: '.$clock_expired_time);
error_log('7.10. $sql: '.$sql);
error_log('7.11. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key]);
}
}
} else {
$clock_expired_time = $_SESSION['expired_time'][$current_expired_time_key];
}
} else {
if ($debug) {
error_log("7 No time control");
}
}
// Get time left for expiring time
$time_left = api_strtotime($clock_expired_time, 'UTC') - time();
/*
* The time control feature is enable here - this feature is enable for a jquery plugin called epiclock
* for more details of how it works see this link : http://eric.garside.name/docs.html?p=epiclock
*/
if ($time_control) { //Sends the exercise form when the expired time is finished
$htmlHeadXtra[] = $objExercise->showTimeControlJS($time_left);
}
//in LP's is enabled the "remember question" feature?
if (!isset($_SESSION['questionList'])) {
// selects the list of question ID
$questionList = $objExercise->get_validated_question_list();
if ($objExercise->isRandom() && !empty($exercise_stat_info['data_tracking'])) {
$questionList = explode(',', $exercise_stat_info['data_tracking']);
}
Session::write('questionList', $questionList);
} else {
if (isset($objExercise) && isset($_SESSION['objExercise'])) {
$questionList = Session::read('questionList');
}
}
if ($debug) {
error_log('8. Question list loaded '.print_r($questionList, 1));
}
//Real question count
$question_count = 0;
if (!empty($questionList)) {
$question_count = count($questionList);
}
if ($current_question > $question_count) {
// If time control then don't change the current question, otherwise there will be a loop.
// @todo
if ($time_control == false) {
$current_question = 0;
}
}
if ($formSent && isset($_POST)) {
if ($debug) {
error_log('9. $formSent was set');
}
// Initializing
if (!is_array($exerciseResult)) {
$exerciseResult = [];
$exerciseResultCoordinates = [];
}
//Only for hotspot
if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
$hotspot_id = (int) $_REQUEST['hidden_hotspot_id'];
$choice = [$hotspot_id => ''];
}
// if the user has answered at least one question
if (is_array($choice)) {
if ($debug) {
error_log('9.1. $choice is an array '.print_r($choice, 1));
}
// Also store hotspot spots in the session ($exerciseResultCoordinates
// will be stored in the session at the end of this script)
if (isset($_POST['hotspot'])) {
$exerciseResultCoordinates = $_POST['hotspot'];
if ($debug) {
error_log('9.2. $_POST[hotspot] data '.print_r($exerciseResultCoordinates, 1));
}
}
if ($objExercise->type == ALL_ON_ONE_PAGE) {
// $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];
//saving each question
if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
$nro_question = $current_question; // - 1;
$questionId = $key;
// gets the student choice for this question
$choice = $exerciseResult[$questionId];
if (isset($exe_id)) {
// Manage the question and answer attempts
if ($debug) {
error_log('8.3. manage_answer exe_id: '.$exe_id.' - $questionId: '.$questionId.' Choice'.print_r($choice, 1));
}
$objExercise->manage_answer(
$exe_id,
$questionId,
$choice,
'exercise_show',
$exerciseResultCoordinates,
true,
false,
false,
$objExercise->propagate_neg,
[]
);
}
//END of saving and qualifying
}
}
}
if ($debug) {
error_log('9.3. $choice is an array - end');
error_log('9.4. $exerciseResult '.print_r($exerciseResult, 1));
}
}
// the script "exercise_result.php" will take the variable $exerciseResult from the session
Session::write('exerciseResult', $exerciseResult);
Session::write('exerciseResultCoordinates', $exerciseResultCoordinates);
// if all questions on one page OR if it is the last question (only for an exercise with one question per page)
if ($objExercise->type == ALL_ON_ONE_PAGE || $current_question >= $question_count) {
if (api_is_allowed_to_session_edit()) {
// goes to the script that will show the result of the exercise
if ($objExercise->type == ALL_ON_ONE_PAGE) {
if ($debug) {
error_log('10. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php');
}
//We check if the user attempts before sending to the exercise_result.php
if ($objExercise->selectAttempts() > 0) {
$attempt_count = Event::get_attempt_count(
api_get_user_id(),
$exerciseId,
$learnpath_id,
$learnpath_item_id,
$learnpath_item_view_id
);
if ($attempt_count >= $objExercise->selectAttempts()) {
echo Display::return_message(
sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()),
'warning',
false
);
if (!in_array($origin, ['learnpath', 'embeddable'])) {
//so we are not in learnpath tool
echo ''; //End glossary div
Display::display_footer();
} else {
echo '