From 823e5eb9b10a58573d37036d25495ead7b98e85e Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 23 Nov 2020 09:48:58 +0100 Subject: [PATCH] Exercises: Read extra field "blocking_percentage" to block user attempts BT#18068 --- main/exercise/exercise.class.php | 29 +++++++++++++++++++++++++++++ main/inc/lib/events.lib.php | 8 +++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/main/exercise/exercise.class.php b/main/exercise/exercise.class.php index ba436b97b5..3eca526638 100755 --- a/main/exercise/exercise.class.php +++ b/main/exercise/exercise.class.php @@ -6679,6 +6679,35 @@ class Exercise $exerciseAttempts ); $isVisible = false; + } else { + // Check blocking exercise. + $extraFieldValue = new ExtraFieldValue('exercise'); + $blockExercise = $extraFieldValue->get_values_by_handler_and_field_variable( + $this->iId, + 'blocking_percentage' + ); + if ($blockExercise && isset($blockExercise['value']) && !empty($blockExercise['value'])) { + $blockPercentage = (int) $blockExercise['value']; + $userAttempts = Event::getExerciseResultsByUser( + api_get_user_id(), + $this->iId, + $this->course_id, + $this->sessionId, + $lpId, + $lpItemId + ); + + if (!empty($userAttempts)) { + $currentAttempt = current($userAttempts); + if ($currentAttempt['total_percentage'] <= $blockPercentage) { + $message = sprintf( + get_lang('ExerciseBlockBecausePercentageX'), + $blockPercentage + ); + $isVisible = false; + } + } + } } } } diff --git a/main/inc/lib/events.lib.php b/main/inc/lib/events.lib.php index c9b8ca3f49..c2f2624a0f 100644 --- a/main/inc/lib/events.lib.php +++ b/main/inc/lib/events.lib.php @@ -1215,10 +1215,10 @@ class Event if (Database::num_rows($query) > 0) { $attempt = Database::fetch_array($query, 'ASSOC'); - return $attempt['count']; - } else { - return 0; + return (int) $attempt['count']; } + + return 0; } /** @@ -1646,6 +1646,8 @@ class Event if (Database::num_rows($res_revised) > 0) { $row['attempt_revised'] = 1; } + $row['total_percentage'] = ($row['exe_result'] / $row['exe_weighting']) * 100; + $list[$row['exe_id']] = $row; $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exeId";