@@ -232,6 +232,12 @@ class submission extends table_base implements renderable {
232232 */
233233 protected $ moderatorfeedback ;
234234
235+ /**
236+ * Current state of this submission
237+ * @var int
238+ */
239+ private int $ currentstate ;
240+
235241 /**
236242 * Constructor: takes a DB row from the coursework_submissions table. We don't retrieve it first
237243 * as we may want to overwrite with submitted data or make a new one.
@@ -637,8 +643,22 @@ public function get_final_grade() {
637643 * @throws coding_exception
638644 * @throws dml_exception
639645 */
640- public function get_state (bool $ includefilescheck = false ): int {
646+ public function get_state (bool $ includefilescheck = false ) {
647+ // Keep result temporarily as this is called many times for same submission during loading of the grading page.
648+ if (!isset ($ this ->currentstate )) {
649+ $ this ->currentstate = $ this ->get_state_uncached ($ includefilescheck );
650+ }
651+ return $ this ->currentstate ;
652+ }
641653
654+ /**
655+ * Get the raw state not cached version.
656+ * Usually use @see self::get_state() instead
657+ * @return int
658+ * @throws coding_exception
659+ * @throws dml_exception
660+ */
661+ private function get_state_uncached (bool $ includefilescheck ): int {
642662 if ($ this ->get_coursework () == null ) {
643663 return -1 ;
644664 }
@@ -1743,4 +1763,16 @@ public function get_flagged_plagiarism_status(): string|bool {
17431763 }
17441764 return get_string ('plagiarism_ ' . $ flag ->status , 'mod_coursework ' );
17451765 }
1766+
1767+ /**
1768+ * Reloads the data from the DB columns.
1769+ * Also unset current state which we are storing locally but which may be different now.
1770+ * @param bool $complainifnotfound
1771+ * @return $this
1772+ * @throws dml_exception
1773+ */
1774+ public function reload ($ complainifnotfound = true ) {
1775+ unset($ this ->currentstate );
1776+ return parent ::reload ($ complainifnotfound );
1777+ }
17461778}
0 commit comments