diff --git a/asystgrade/classes/db/QuizQuery.php b/asystgrade/classes/db/QuizQuery.php
index bb75f58e95e3a750293dbe801e1d4fa1c508082c..92417b3920303add343645768049a642bc61f9a5 100755
--- a/asystgrade/classes/db/QuizQuery.php
+++ b/asystgrade/classes/db/QuizQuery.php
@@ -72,4 +72,24 @@ class QuizQuery implements QuizQueryInterface
             MUST_EXIST
         )->value;
     }
+
+    /**
+     * Checks if scores exist for a given quizid and userid.
+     * *
+     * * @param int $quizid quiz ID.
+     * * @param int $userid user ID.
+     * * @return bool Returns true if scores exist for a given quizid and userid, otherwise false.
+ */
+    public function gradesExist(int $quizid, int $userid): bool {
+        global $DB;
+
+        // Check for the presence of an entry in the mdl_quiz_grades table for this quizid and userid
+        return $DB->record_exists(
+            'quiz_grades',
+            [
+                'quiz' => $quizid,
+                'userid' => $userid
+            ]
+        );
+    }
 }
diff --git a/asystgrade/lib.php b/asystgrade/lib.php
index 11d96b6876c08defded5420d67bf1c0008e09208..07136c253a39b1171a7bd6d813565b8121efae55 100755
--- a/asystgrade/lib.php
+++ b/asystgrade/lib.php
@@ -44,11 +44,21 @@ function local_asystgrade_before_footer()
     if ($PAGE->url->compare(new moodle_url('/mod/quiz/report.php'), URL_MATCH_BASE) && $slot) {
         $quizQuery = new QuizQuery();
 
+        if ($quizQuery->gradesExist($qid, $slot)) {
+            error_log('Grades already exist in the database.');
+            return;
+        }
+
         $question_attempts = $quizQuery->get_question_attempts($qid, $slot);
         $referenceAnswer = $quizQuery->get_reference_answer($qid);
 
         $data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer);
-
+        foreach ($data['studentAnswers'] as $index => $studentAnswer) {
+            $userid = $data['studentIds'][$index];
+            if ($quizQuery->gradesExist($qid, $userid)) {
+                return;
+            }
+        }
         $inputNames = $data['inputNames'];
 
         error_log('Data prepared: ' . print_r($data, true));
@@ -105,6 +115,7 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns
 {
     $studentAnswers = [];
     $inputNames = [];
+    $studentIds = [];
 
     foreach ($question_attempts as $question_attempt) {
         $quizattempt_steps = $database->get_attempt_steps($question_attempt->id);
@@ -114,7 +125,7 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns
                 $studentAnswer = $database->get_student_answer($quizattempt_step->id);
                 $studentAnswers[] = $studentAnswer;
                 $inputNames[] = "q" . $question_attempt->questionusageid . ":" . $question_attempt->slot . "_-mark";
-
+                $studentIds[] = $quizattempt_step->userid;
                 error_log("Student Answer: $studentAnswer, Input Name: " . end($inputNames));
             }
         }
@@ -127,7 +138,8 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns
     return [
         'referenceAnswer' => $referenceAnswer,
         'studentAnswers' => $studentAnswers,
-        'inputNames' => $inputNames
+        'inputNames' => $inputNames,
+        'studentIds' => $studentIds
     ];
 }