Skip to content

Commit 16fbd41

Browse files
committed
Refactor: Simplify conditional checks and use null coalescing operator across exercise-related files for cleaner and more consistent code readability - refs BT#23068
1 parent c2ec80a commit 16fbd41

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

main/exercise/exercise.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4516,7 +4516,7 @@ function ($answerId) use ($objAnswerTmp) {
45164516
if (!$switchableAnswerSet) {
45174517
// not switchable answer, must be in the same place than teacher order
45184518
for ($i = 0; $i < count($listCorrectAnswers['words']); $i++) {
4519-
$studentAnswer = isset($choice[$i]) ? $choice[$i] : '';
4519+
$studentAnswer = $choice[$i] ?? '';
45204520
$correctAnswer = $listCorrectAnswers['words'][$i];
45214521

45224522
if ($debug) {
@@ -4594,7 +4594,7 @@ function ($answerId) use ($objAnswerTmp) {
45944594

45954595
$found = false;
45964596
for ($j = 0; $j < count($listTeacherAnswerTemp); $j++) {
4597-
$correctAnswer = isset($listTeacherAnswerTemp[$j]) ? $listTeacherAnswerTemp[$j] : '';
4597+
$correctAnswer = $listTeacherAnswerTemp[$j] ?? '';
45984598
if (is_array($listTeacherAnswerTemp)) {
45994599
$correctAnswer = implode('||', $listTeacherAnswerTemp);
46004600
}

main/exercise/exercise_result.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@
111111
);
112112
}
113113
$exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
114-
$learnpath_id = isset($exercise_stat_info['orig_lp_id']) ? $exercise_stat_info['orig_lp_id'] : 0;
115-
$learnpath_item_id = isset($exercise_stat_info['orig_lp_item_id']) ? $exercise_stat_info['orig_lp_item_id'] : 0;
116-
$learnpath_item_view_id = isset($exercise_stat_info['orig_lp_item_view_id'])
117-
? $exercise_stat_info['orig_lp_item_view_id'] : 0;
118-
$exerciseId = isset($exercise_stat_info['exe_exo_id']) ? $exercise_stat_info['exe_exo_id'] : 0;
114+
$learnpath_id = $exercise_stat_info['orig_lp_id'] ?? 0;
115+
$learnpath_item_id = $exercise_stat_info['orig_lp_item_id'] ?? 0;
116+
$learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'] ?? 0;
117+
$exerciseId = $exercise_stat_info['exe_exo_id'] ?? 0;
119118

120119
$logInfo = [
121120
'tool' => TOOL_QUIZ,
@@ -357,7 +356,7 @@
357356
$template->assign('content', $template->fetch($template->get_template('exercise/result.tpl')));
358357
$template->display_one_col_template();
359358

360-
function showEmbeddableFinishButton()
359+
function showEmbeddableFinishButton(): string
361360
{
362361
$js = '<script>
363362
$(function () {

main/exercise/exercise_submit.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@
123123
$reminder = isset($_REQUEST['reminder']) ? (int) $_REQUEST['reminder'] : 0;
124124
$remind_question_id = isset($_REQUEST['remind_question_id']) ? (int) $_REQUEST['remind_question_id'] : 0;
125125
$exerciseId = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : 0;
126-
$formSent = isset($_REQUEST['formSent']) ? $_REQUEST['formSent'] : null;
127-
$exerciseResult = isset($_REQUEST['exerciseResult']) ? $_REQUEST['exerciseResult'] : null;
128-
$exerciseResultCoordinates = isset($_REQUEST['exerciseResultCoordinates']) ? $_REQUEST['exerciseResultCoordinates'] : null;
129-
$choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
130-
$choice = empty($choice) ? isset($_REQUEST['choice2']) ? $_REQUEST['choice2'] : null : null;
126+
$formSent = $_REQUEST['formSent'] ?? null;
127+
$exerciseResult = $_REQUEST['exerciseResult'] ?? null;
128+
$exerciseResultCoordinates = $_REQUEST['exerciseResultCoordinates'] ?? null;
129+
$choice = $_REQUEST['choice'] ?? null;
130+
$choice = empty($choice) ? $_REQUEST['choice2'] ?? null : null;
131131
$current_question = $currentQuestionFromUrl = isset($_REQUEST['num']) ? (int) $_REQUEST['num'] : null;
132132
$currentAnswer = isset($_REQUEST['num_answer']) ? (int) $_REQUEST['num_answer'] : null;
133133
$logInfo = [
@@ -1081,7 +1081,7 @@
10811081
}
10821082

10831083
$exercise_timeover = false;
1084-
$limit_time_exists = !empty($objExercise->start_time) || !empty($objExercise->end_time) ? true : false;
1084+
$limit_time_exists = !empty($objExercise->start_time) || !empty($objExercise->end_time);
10851085
if ($limit_time_exists) {
10861086
$exercise_start_time = api_strtotime($objExercise->start_time, 'UTC');
10871087
$exercise_end_time = api_strtotime($objExercise->end_time, 'UTC');

main/exercise/fill_blanks.class.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,15 +824,14 @@ public static function getAnswerInfo($userAnswer = '', $isStudentAnswer = false)
824824
$listDetails = explode(':', $listArobaseSplit[0]);
825825

826826
// < number of item after the ::[score]:[size]:[separator_id]@ , here there are 3
827+
$listWeightings = explode(',', $listDetails[0]);
827828
if (count($listDetails) < 3) {
828-
$listWeightings = explode(',', $listDetails[0]);
829829
$listSizeOfInput = [];
830830
for ($i = 0; $i < count($listWeightings); $i++) {
831831
$listSizeOfInput[] = 200;
832832
}
833833
$blankSeparatorNumber = 0; // 0 is [...]
834834
} else {
835-
$listWeightings = explode(',', $listDetails[0]);
836835
$listSizeOfInput = explode(',', $listDetails[1]);
837836
$blankSeparatorNumber = $listDetails[2];
838837
}
@@ -1269,7 +1268,8 @@ public static function getHtmlAnswer(
12691268
$resultsDisabled = false,
12701269
$showTotalScoreAndUserChoices = false,
12711270
$exercise
1272-
) {
1271+
): string
1272+
{
12731273
$hideExpectedAnswer = false;
12741274
$hideUserSelection = false;
12751275
if (!$exercise->showExpectedChoiceColumn()) {
@@ -1414,7 +1414,7 @@ public static function getHtmlWrongAnswer(
14141414
*
14151415
* @return bool
14161416
*/
1417-
public static function isCorrect($answerText)
1417+
public static function isCorrect($answerText): bool
14181418
{
14191419
$answerInfo = self::getAnswerInfo($answerText, true);
14201420
$correctAnswerList = $answerInfo['words'];
@@ -1436,7 +1436,7 @@ public static function isCorrect($answerText)
14361436
*
14371437
* @return string
14381438
*/
1439-
public static function clearStudentAnswer($answer)
1439+
public static function clearStudentAnswer($answer): string
14401440
{
14411441
$answer = htmlentities(api_utf8_encode($answer), ENT_QUOTES);
14421442
$answer = str_replace('&#039;', '&#39;', $answer); // fix apostrophe

main/inc/lib/exercise.lib.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5460,7 +5460,7 @@ public static function displayQuestionListByAttempt(
54605460

54615461
// Category report
54625462
$category_was_added_for_this_test = false;
5463-
if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
5463+
if (!empty($objQuestionTmp->category)) {
54645464
if (!isset($category_list[$objQuestionTmp->category]['score'])) {
54655465
$category_list[$objQuestionTmp->category]['score'] = 0;
54665466
}
@@ -5498,7 +5498,7 @@ public static function displayQuestionListByAttempt(
54985498
$category_list[$objQuestionTmp->category]['total_questions']++;
54995499
$category_was_added_for_this_test = true;
55005500
}
5501-
if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
5501+
if (!empty($objQuestionTmp->category_list)) {
55025502
foreach ($objQuestionTmp->category_list as $category_id) {
55035503
$category_list[$category_id]['score'] += $my_total_score;
55045504
$category_list[$category_id]['total'] += $my_total_weight;
@@ -5507,7 +5507,7 @@ public static function displayQuestionListByAttempt(
55075507
}
55085508

55095509
// No category for this question!
5510-
if ($category_was_added_for_this_test == false) {
5510+
if (!$category_was_added_for_this_test) {
55115511
if (!isset($category_list['none']['score'])) {
55125512
$category_list['none']['score'] = 0;
55135513
}

0 commit comments

Comments
 (0)