I am trying to add lesson activity in a course. While saving lesson activity, moodle recalculates grades for the course. While recalculating grades it get grade items from 'mdl_grade_items' table in 'grade_regrade_final_grades' method,
located in 'moodle\lib\gradelib.php'. It checks 'needsupdate' flag for each fetched grade item. If the 'needsupdate' is set then it calculates grades for the course.
Then for each grade item it gets grades for all users from 'mdl_grade_grades' table in 'regrade_final_grades' method located in 'moodle\lib\grade\grade_item.php'.
Which are thousands in numbers, These grades are iterated in foreach loop for grades calculation.
It stucks in the loop due to huge number of iterations and system gives 504 gateway timeout exception.
I tried by unseting the 'needsupdate' field in mdl_grade_items table. But when the grade items are fetched in code this field is always set.