/**
 * Get session time control
 */
function get_session_time_control_key($exercise_id, $lp_id = 0, $lp_item_id = 0)
{
    $return_value = 0;
    $time_control_key = get_time_control_key($exercise_id, $lp_id, $lp_item_id);
    if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
        $return_value = $_SESSION['expired_time'][$time_control_key];
    }
    return $return_value;
}
/**
 * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
 * After that in exercise_result.php we call the update_event_exercice() to update the exercise
 * @return $id the last id registered, or false on error
 * @author Julio Montoya <*****@*****.**>
 * @desc Record result of user when an exercice was done
 * @deprecated this function seems to be deprecated
*/
function create_event_exercice($exo_id)
{
    if (empty($exo_id) or intval($exo_id) != $exo_id) {
        return false;
    }
    $tbl_track_exe = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
    $tbl_exe = Database::get_course_table(TABLE_QUIZ_TEST);
    $uid = api_get_user_id();
    $course_id = api_get_course_int_id();
    // First, check the exercise exists
    $sql_exe_id = "SELECT exercises.id FROM {$tbl_exe} as exercises WHERE c_id = {$course_id} AND exercises.id={$exo_id}";
    $res_exe_id = Database::query($sql_exe_id);
    if ($res_exe_id === false) {
        return false;
    }
    //sql error
    if (Database::num_rows($res_exe_id) < 1) {
        return false;
    }
    //exe not found
    $row_exe_id = Database::fetch_row($res_exe_id);
    $exercise_id = intval($row_exe_id[0]);
    // Second, check if the record exists in the database (looking for incomplete records)
    $sql = "SELECT exe_id FROM {$tbl_track_exe} WHERE exe_exo_id =   {$exo_id} AND " . "exe_user_id =  {$uid} AND " . "exe_cours_id = '" . api_get_course_id() . "' AND " . "status = 'incomplete' AND " . "session_id = " . api_get_session_id();
    $res = Database::query($sql);
    if ($res === false) {
        return false;
    }
    if (Database::num_rows($res) > 0) {
        $row = Database::fetch_array($res);
        return $row['exe_id'];
    }
    // No record was found, so create one
    // get expire time to insert into the tracking record
    require_once api_get_path(SYS_CODE_PATH) . 'exercice/exercise.lib.php';
    $current_expired_time_key = get_time_control_key($exercise_id);
    if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
        //Only for exercice of type "One page"
        $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
    } else {
        $expired_date = '0000-00-00 00:00:00';
    }
    $sql = "INSERT INTO {$tbl_track_exe} ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)\n        \tVALUES (  {$uid},  '" . api_get_course_id() . "' ,'{$expired_date}','{$exo_id}','" . api_get_session_id() . "')";
    Database::query($sql);
    $id = Database::insert_id();
    return $id;
}
     }
 }
 // We're inside *one* question. Go through each possible answer for this question
 $result = $objExercise->manage_answer($exe_id, $my_question_id, $my_choice, 'exercise_result', $hot_spot_coordinates, true, false, false, $objExercise->selectPropagateNeg(), $hotspot_delineation_result);
 //  Adding the new score.
 $total_score += $result['score'];
 if ($debug) {
     error_log("total_score: {$total_score} ");
     error_log("total_weight: {$total_weight} ");
 }
 $duration = 0;
 $now = time();
 if ($type == 'all') {
     $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
 }
 $key = get_time_control_key($exercise_id, $exercise_stat_info['orig_lp_id'], $exercise_stat_info['orig_lp_item_id']);
 if (isset($_SESSION['duration_time'][$key]) && !empty($_SESSION['duration_time'][$key])) {
     $duration = $now - $_SESSION['duration_time'][$key];
     if (!empty($exercise_stat_info['exe_duration'])) {
         $duration += $exercise_stat_info['exe_duration'];
     }
     $duration = intval($duration);
 } else {
     if (!empty($exercise_stat_info['exe_duration'])) {
         $duration = $exercise_stat_info['exe_duration'];
     }
 }
 $_SESSION['duration_time'][$key] = time();
 update_event_exercice($exe_id, $objExercise->selectId(), $total_score, $total_weight, $session_id, $exercise_stat_info['orig_lp_id'], $exercise_stat_info['orig_lp_item_id'], $exercise_stat_info['orig_lp_item_view_id'], $duration, $question_list, 'incomplete', $remind_list);
 // Destruction of the Question object
 unset($objQuestionTmp);
}
// If reminder ends we jump to the exercise_reminder
if ($objExercise->review_answers) {
    if ($remind_question_id == -1) {
        header('Location: exercise_reminder.php?origin=' . $origin . '&exerciseId=' . $exerciseId);
        exit;
    }
}
$current_timestamp = time();
$my_remind_list = array();
$time_control = false;
if ($objExercise->expired_time != 0) {
    $time_control = true;
}
// Generating the time control key for the user
$current_expired_time_key = get_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
$_SESSION['duration_time'][$current_expired_time_key] = $current_timestamp;
if ($time_control) {
    // Get the expired time of the current exercice in track_e_exercices
    $total_seconds = $objExercise->expired_time * 60;
}
$show_clock = true;
$user_id = api_get_user_id();
if ($objExercise->selectAttempts() > 0) {
    $attempt_html = '';
    $attempt_count = 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 && $origin != 'learnpath') {
                // Showing latest attempt according with task BT#1628