Skip to content

Commit

Permalink
Exercises: Fix remind list when using "block_category_questions"
Browse files Browse the repository at this point in the history
BT#17789
  • Loading branch information
jmontoyaa committed Nov 9, 2020
1 parent 351d56a commit 87bdefa
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 54 deletions.
56 changes: 29 additions & 27 deletions main/exercise/exercise.class.php
Expand Up @@ -7076,31 +7076,32 @@ public function addAllQuestionToRemind($exeId, $questionList = [])
}

/**
* @param int $exe_id
* @param int $question_id
* @param int $exeId
* @param int $questionId
* @param string $action
*/
public function editQuestionToRemind($exe_id, $question_id, $action = 'add')
public function editQuestionToRemind($exeId, $questionId, $action = 'add')
{
$exercise_info = self::get_stat_track_exercise_info_by_exe_id($exe_id);
$question_id = (int) $question_id;
$exe_id = (int) $exe_id;
$track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$exercise_info = self::get_stat_track_exercise_info_by_exe_id($exeId);
$questionId = (int) $questionId;
$exeId = (int) $exeId;

if ($exercise_info) {
$track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
if (empty($exercise_info['questions_to_check'])) {
if ($action == 'add') {
if ($action === 'add') {
$sql = "UPDATE $track_exercises
SET questions_to_check = '$question_id'
WHERE exe_id = $exe_id ";
SET questions_to_check = '$questionId'
WHERE exe_id = $exeId ";
Database::query($sql);
}
} else {
$remind_list = explode(',', $exercise_info['questions_to_check']);
$remind_list_string = '';
if ($action == 'add') {
if (!in_array($question_id, $remind_list)) {
if ($action === 'add') {
if (!in_array($questionId, $remind_list)) {
$newRemindList = [];
$remind_list[] = $question_id;
$remind_list[] = $questionId;
$questionListInSession = Session::read('questionList');
if (!empty($questionListInSession)) {
foreach ($questionListInSession as $originalQuestionId) {
Expand All @@ -7111,11 +7112,11 @@ public function editQuestionToRemind($exe_id, $question_id, $action = 'add')
}
$remind_list_string = implode(',', $newRemindList);
}
} elseif ($action == 'delete') {
} elseif ($action === 'delete') {
if (!empty($remind_list)) {
if (in_array($question_id, $remind_list)) {
if (in_array($questionId, $remind_list)) {
$remind_list = array_flip($remind_list);
unset($remind_list[$question_id]);
unset($remind_list[$questionId]);
$remind_list = array_flip($remind_list);

if (!empty($remind_list)) {
Expand All @@ -7129,7 +7130,7 @@ public function editQuestionToRemind($exe_id, $question_id, $action = 'add')
$value = Database::escape_string($remind_list_string);
$sql = "UPDATE $track_exercises
SET questions_to_check = '$value'
WHERE exe_id = $exe_id ";
WHERE exe_id = $exeId ";
Database::query($sql);
}
}
Expand Down Expand Up @@ -10244,8 +10245,12 @@ public function getReminderTable($questionList, $exercise_stat_info)
$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;

$remind_list = $exercise_stat_info['questions_to_check'];
$remind_list = explode(',', $remind_list);
if (empty($exercise_stat_info)) {
return '';
}

$remindList = $exercise_stat_info['questions_to_check'];
$remindList = explode(',', $remindList);

$exeId = $exercise_stat_info['exe_id'];
$exerciseId = $exercise_stat_info['exe_exo_id'];
Expand All @@ -10257,14 +10262,10 @@ public function getReminderTable($questionList, $exercise_stat_info)
$counter = 0;
// Loop over all question to show results for each of them, one by one
foreach ($questionList as $questionId) {
// destruction of the Question object
unset($objQuestionTmp);
// creates a temporary Question object
$objQuestionTmp = Question:: read($questionId);
$objQuestionTmp = Question::read($questionId);
$check_id = 'remind_list['.$questionId.']';

$attributes = ['id' => $check_id, 'onclick' => "save_remind_item(this, '$questionId');"];
if (in_array($questionId, $remind_list)) {
if (in_array($questionId, $remindList)) {
$attributes['checked'] = 1;
}

Expand All @@ -10280,7 +10281,7 @@ public function getReminderTable($questionList, $exercise_stat_info)
</div>';
$counter++;
$questionTitle = $counter.'. '.strip_tags($objQuestionTmp->selectTitle());
// Check if the question doesn't have an answer
// Check if the question doesn't have an answer.
if (!in_array($questionId, $exercise_result)) {
$questionTitle = Display::label($questionTitle, 'danger');
}
Expand All @@ -10290,6 +10291,7 @@ public function getReminderTable($questionList, $exercise_stat_info)
$questionTitle = Display::tag('label', $checkbox.$questionTitle, $label_attributes);
$table .= Display::div($questionTitle, ['class' => 'exercise_reminder_item ']);
}

$content .= Display::div('', ['id' => 'message']).
Display::div($table, ['class' => 'question-check-test']);

Expand All @@ -10301,7 +10303,7 @@ public function getReminderTable($questionList, $exercise_stat_info)
});
function final_submit() {
// Normal inputs
// Normal inputs.
window.location = "'.api_get_path(WEB_CODE_PATH).'exercise/exercise_result.php?'.api_get_cidreq().'&exe_id='.$exeId.'&" + lp_data;
}
Expand Down
4 changes: 4 additions & 0 deletions main/exercise/exercise_question_reminder.php
Expand Up @@ -84,6 +84,10 @@
SET blocked_categories = '$value'
WHERE exe_id = $exeId";
Database::query($sql);

// Cleaning old remind list.
$objExercise->removeAllQuestionToRemind($exeId);

api_location($url.'&num='.$currentQuestion);
}

Expand Down
11 changes: 11 additions & 0 deletions main/exercise/exercise_reminder.php
Expand Up @@ -101,6 +101,17 @@
if ($time_control) {
echo $objExercise->returnTimeLeftDiv();
}

$selectionType = $objExercise->getQuestionSelectionType();
if (api_get_configuration_value('block_category_questions') &&
ONE_PER_PAGE == $objExercise->type &&
EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM == $selectionType
) {
// get last category question list
$categoryList = Session::read('categoryList');
$question_list = end($categoryList);
}

echo $objExercise->getReminderTable($question_list, $exercise_stat_info);

$exerciseActions = Display::url(
Expand Down
17 changes: 10 additions & 7 deletions main/exercise/exercise_submit.php
Expand Up @@ -641,14 +641,19 @@
$count++;
}

// Use reminder list to get the current question.
if (2 === $reminder && !empty($myRemindList)) {
$remindQuestionId = current($myRemindList);
$questionCheck = Question::read($remindQuestionId);
}

$categoryId = 0;
if (null !== $questionCheck) {
$categoryId = $questionCheck->category;
}

if (!empty($categoryId)) {
$categoryInfo = $categoryList[$categoryId];

$count = 1;
$total = count($categoryList[$categoryId]);
foreach ($categoryList[$categoryId] as $checkQuestionId) {
Expand All @@ -673,9 +678,9 @@
}
}

// Blocked if category was already answered
// Blocked if category was already answered.
if ($categoryId && in_array($categoryId, $blockedCategories)) {
// Redirect to category intro
// Redirect to category intro.
$url = api_get_path(WEB_CODE_PATH).'exercise/exercise_question_reminder.php?'.
$params.'&num='.$current_question.'&category_id='.$isLastQuestionInCategory;
api_location($url);
Expand Down Expand Up @@ -841,9 +846,7 @@
}

if ($question_count != 0) {
if ($objExercise->type == ALL_ON_ONE_PAGE ||
$current_question > $question_count
) {
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) {
Expand Down Expand Up @@ -1108,11 +1111,11 @@
if (!in_array($origin, ['learnpath', 'embeddable'])) {
echo '<div id="highlight-plugin" class="glossary-content">';
}

if ($reminder == 2) {
$data_tracking = $exercise_stat_info['data_tracking'];
$data_tracking = explode(',', $data_tracking);
$current_question = 1; //set by default the 1st question

if (!empty($myRemindList)) {
// Checking which questions we are going to call from the remind list
for ($i = 0; $i < count($data_tracking); $i++) {
Expand Down
42 changes: 22 additions & 20 deletions main/inc/ajax/exercise.ajax.php
Expand Up @@ -351,27 +351,29 @@
if (empty($objExercise) || empty($exeId)) {
echo 0;
exit;
} else {
$option = isset($_GET['option']) ? $_GET['option'] : '';
switch ($option) {
case 'add_all':
$questionListInSession = Session::read('questionList');
$objExercise->addAllQuestionToRemind($exeId, $questionListInSession);
break;
case 'remove_all':
$objExercise->removeAllQuestionToRemind($exeId);
break;
default:
$objExercise->editQuestionToRemind(
$exeId,
$_REQUEST['question_id'],
$_REQUEST['action']
);
break;
}
echo 1;
exit;
}

$option = isset($_GET['option']) ? $_GET['option'] : '';
switch ($option) {
case 'add_all':
$questionListInSession = Session::read('questionList');
$objExercise->addAllQuestionToRemind($exeId, $questionListInSession);
break;
case 'remove_all':
$objExercise->removeAllQuestionToRemind($exeId);
break;
default:
$objExercise->editQuestionToRemind(
$exeId,
$_REQUEST['question_id'],
$_REQUEST['action']
);
break;
}

echo 1;
exit;

break;
case 'save_exercise_by_now':
$course_info = api_get_course_info_by_id($course_id);
Expand Down

0 comments on commit 87bdefa

Please sign in to comment.