Skip to content

Commit

Permalink
Fix error when exercise has no answers and time expired
Browse files Browse the repository at this point in the history
Fix redirect in overview.php see BT#18534
  • Loading branch information
jmontoyaa committed Mar 10, 2021
1 parent 793dce3 commit 74f7e92
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 9 additions & 1 deletion main/exercise/exercise.class.php
Expand Up @@ -3517,7 +3517,7 @@ function onExpiredTimeExercise() {
*
* @return string
*/
public function showTimeControlJS($timeLeft)
public function showTimeControlJS($timeLeft, $redirectToExerciseSubmit = false)
{
$timeLeft = (int) $timeLeft;
$script = 'redirectExerciseToResult();';
Expand All @@ -3528,6 +3528,13 @@ public function showTimeControlJS($timeLeft)
$(\'[name="save_now"]\').trigger(\'click\');';
}

$exerciseSubmitRedirect = '';
if ($redirectToExerciseSubmit) {
$url = api_get_path(WEB_CODE_PATH).
'exercise/exercise_submit.php?'.api_get_cidreq().'&exerciseId='.$this->id;
$exerciseSubmitRedirect = "window.location = '$url'";
}

return "<script>
function openClockWarning() {
$('#clock_warning').dialog({
Expand Down Expand Up @@ -3560,6 +3567,7 @@ function send_form() {
if ($('#exercise_form').length) {
$script
} else {
$exerciseSubmitRedirect
// In exercise_reminder.php
final_submit();
}
Expand Down
2 changes: 1 addition & 1 deletion main/exercise/overview.php
Expand Up @@ -69,7 +69,7 @@
$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[] = $objExercise->showTimeControlJS($time_left);
$htmlHeadXtra[] = $objExercise->showTimeControlJS($time_left, true);
}

if (!in_array($origin, ['learnpath', 'embeddable', 'mobileapp'])) {
Expand Down
27 changes: 23 additions & 4 deletions main/inc/ajax/exercise.ajax.php
Expand Up @@ -559,23 +559,43 @@
error_log('Starting questions loop in save_exercise_by_now');
}

// Check we have at least one non-empty answer in the array
// provided by the user's click on the "Finish test" button.
$now = time();
if ('all' === $type) {
// Check we have at least one non-empty answer in the array
// provided by the user's click on the "Finish test" button.
$atLeastOneAnswer = false;
foreach ($question_list as $my_question_id) {
if (!empty($choice[$my_question_id])) {
$atLeastOneAnswer = true;
break;
}
}

if (!$atLeastOneAnswer) {
// Check if time is over.
if ($objExercise->expired_time != 0) {
$clockExpiredTime = ExerciseLib::get_session_time_control_key(
$objExercise->id,
$learnpath_id,
$learnpath_item_id
);
if (!empty($clockExpiredTime)) {
$timeLeft = api_strtotime($clockExpiredTime, 'UTC') - $now;
if ($timeLeft <= 0) {
// There's no time, but still no answers ...
echo json_encode(['ok' => true, 'savedAnswerMessage' => '']);
exit;
}
}
}

error_log(
'In '.__FILE__.'::action save_exercise_by_now,'.
' from user '.api_get_user_id().
' for track_e_exercises.exe_id = '.$exeId.
', we received an empty set of answers.'.
'Preventing submission to avoid overwriting w/ null.');
'Preventing submission to avoid overwriting w/ null.'
);
echo json_encode(['error' => true]);
exit;
}
Expand Down Expand Up @@ -728,7 +748,6 @@
}

$duration = 0;
$now = time();
if ($type === 'all') {
$exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
}
Expand Down

0 comments on commit 74f7e92

Please sign in to comment.