Skip to content

Commit

Permalink
Exercises: question duration add validations BT#17791 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Nov 6, 2020
1 parent 8dfc932 commit 8319b13
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 59 deletions.
116 changes: 59 additions & 57 deletions main/exercise/exercise_submit.php
Expand Up @@ -937,72 +937,74 @@
}
}

$extraFieldValue = new ExtraFieldValue('question');
$value = $extraFieldValue->get_values_by_handler_and_field_variable($objQuestionTmp->iid, 'time');
if (!empty($value) && isset($value['value']) && !empty($value['value'])) {
$seconds = (int) $value['value'];
$now = time();
$loadedQuestions = array_keys(Session::read('question_start', []));
if (!empty($loadedQuestions)) {
if (api_get_configuration_value('allow_time_per_question')) {
$extraFieldValue = new ExtraFieldValue('question');
$value = $extraFieldValue->get_values_by_handler_and_field_variable($objQuestionTmp->iid, 'time');
if (!empty($value) && isset($value['value']) && !empty($value['value'])) {
$seconds = (int) $value['value'];
$now = time();
/*$loadedQuestions = array_keys(Session::read('question_start', []));
if (!empty($loadedQuestions)) {
if (!in_array($objQuestionTmp->iid, $loadedQuestions)) {
api_not_allowed(true);
}
}*/

$timeSpent = Event::getAttemptQuestionDuration($exe_id, $objQuestionTmp->iid);
/*$loadedQuestions = array_keys(Session::read('question_start'));
// Block if another question is loaded at the same time.
if (!in_array($objQuestionTmp->iid, $loadedQuestions)) {
api_not_allowed(true);
}
}

$timeSpent = Event::getAttemptQuestionDuration($exe_id, $objQuestionTmp->iid);
$loadedQuestions = array_keys(Session::read('question_start'));
// Block if another question is loaded at the same time.
if (!in_array($objQuestionTmp->iid, $loadedQuestions)) {
api_not_allowed(true);
}
/*$timePerQuestion = Session::read('time_per_question', []);
if (!empty($timePerQuestion) && isset($timePerQuestion[$objQuestionTmp->iid])) {
$time = $timePerQuestion[$objQuestionTmp->iid];
} else {
$time = $timePerQuestion[$objQuestionTmp->iid] = $now;
Session::write('time_per_question', $timePerQuestion);
}
$timeSpent = $now - $time;*/

/*if (!empty($questionAttempt) && isset($questionAttempt['tms'])) {
var_dump('from DB');
var_dump($questionAttempt['tms']);
$time = api_strtotime($questionAttempt['tms'], 'UTC');
$timeSpent = $now - $time;
} else {
var_dump('from session');
$timePerQuestion = Session::read('time_per_question', []);
}*/
/*$timePerQuestion = Session::read('time_per_question', []);
if (!empty($timePerQuestion) && isset($timePerQuestion[$objQuestionTmp->iid])) {
$time = $timePerQuestion[$objQuestionTmp->iid];
} else {
$time = $timePerQuestion[$objQuestionTmp->iid] = $now;
Session::write('time_per_question', $timePerQuestion);
}
var_dump($timePerQuestion);
$timeSpent = $now - $time;
}*/
//var_dump($timeSpent);
//var_dump(api_get_utc_datetime($now).' - '.api_get_utc_datetime($time));
// Redirect to next question.
if ($timeSpent > $seconds) {
$nextQuestion = (int) $currentQuestionFromUrl + 1;
$nextQuestionUrl = api_get_path(WEB_CODE_PATH).
"exercise/exercise_submit.php?$params&num=$nextQuestion&remind_question_id=$remind_question_id";
api_location($nextQuestionUrl);
}
$timeSpent = $now - $time;*/

$seconds = $seconds - $timeSpent;
//var_dump($seconds);
$questionTimeCondition = "
var timer = new easytimer.Timer();
timer.start({countdown: true, startValues: {seconds: $seconds}});
timer.addEventListener('secondsUpdated', function (e) {
$('#question_timer').html(timer.getTimeValues().toString());
});
timer.addEventListener('targetAchieved', function (e) {
$('.question-validate-btn').click();
});
";
/*if (!empty($questionAttempt) && isset($questionAttempt['tms'])) {
var_dump('from DB');
var_dump($questionAttempt['tms']);
$time = api_strtotime($questionAttempt['tms'], 'UTC');
$timeSpent = $now - $time;
} else {
var_dump('from session');
$timePerQuestion = Session::read('time_per_question', []);
if (!empty($timePerQuestion) && isset($timePerQuestion[$objQuestionTmp->iid])) {
$time = $timePerQuestion[$objQuestionTmp->iid];
} else {
$time = $timePerQuestion[$objQuestionTmp->iid] = $now;
Session::write('time_per_question', $timePerQuestion);
}
var_dump($timePerQuestion);
$timeSpent = $now - $time;
}*/
//var_dump($timeSpent);
//var_dump(api_get_utc_datetime($now).' - '.api_get_utc_datetime($time));
// Redirect to next question.
if ($timeSpent > $seconds) {
$nextQuestion = (int) $currentQuestionFromUrl + 1;
$nextQuestionUrl = api_get_path(WEB_CODE_PATH).
"exercise/exercise_submit.php?$params&num=$nextQuestion&remind_question_id=$remind_question_id";
api_location($nextQuestionUrl);
}

$seconds = $seconds - $timeSpent;
//var_dump($seconds);
$questionTimeCondition = "
var timer = new easytimer.Timer();
timer.start({countdown: true, startValues: {seconds: $seconds}});
timer.addEventListener('secondsUpdated', function (e) {
$('#question_timer').html(timer.getTimeValues().toString());
});
timer.addEventListener('targetAchieved', function (e) {
$('.question-validate-btn').click();
});
";
}
}
}

Expand Down
18 changes: 16 additions & 2 deletions main/inc/ajax/exercise.ajax.php
Expand Up @@ -585,6 +585,22 @@
$total_score = $total_score - $old_result['score'];
}

$questionDuration = 0;
if (api_get_configuration_value('allow_time_per_question')) {
$extraFieldValue = new ExtraFieldValue('question');
$value = $extraFieldValue->get_values_by_handler_and_field_variable($objQuestionTmp->iid, 'time');
if (!empty($value) && isset($value['value']) && !empty($value['value'])) {
$questionDuration = Event::getAttemptQuestionDuration($exeId, $objQuestionTmp->iid);
if (empty($questionDuration)) {
echo 'error';
if ($debug) {
error_log("Question duration = 0, in exeId: $exeId, question_id: $my_question_id");
}
exit;
}
}
}

// Deleting old attempt
if (isset($attemptList) && !empty($attemptList[$my_question_id])) {
if ($debug) {
Expand Down Expand Up @@ -614,8 +630,6 @@
}
}

$questionDuration = Event::getAttemptQuestionDuration($exeId, $objQuestionTmp->iid);

// We're inside *one* question. Go through each possible answer for this question
if ($objQuestionTmp->type === MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
$myChoiceTmp = [];
Expand Down

0 comments on commit 8319b13

Please sign in to comment.