Here’s something that you might consider:
I saw, you’re setting “AICC_Lesson_Status” only depending on the score. I didn’t test it in a LMS but if I imagine the course running in moodle, whatever the user achieves in the test, “AICC_Lesson_Status” is set to a completion state, i.e. either “completed” or “failed” (which also means “completed”).
If the user doesn’t close the window, your internal attempts counting should work, the running session doesn’t really recognize itself being completed. Values may be overwritten, but …
If the user closes the window, moodle will finish the Scorm attempt. The next time the course is opened, a new Scorm attempt is started. This means the course is again “not attempted” with all variables having their initial values, e.g. “ExamAttempts” = 0.
In summary: You should keep AICC_Lesson_Status “incomplete” (by not changing it) until Right_to_Work_Validation_Score = 100 OR ExamAttempts = 3 (or 2 if you start counting at 0)