From e0df6c5b3e0109f4cdc4630ea1f2bef91b7ece7d Mon Sep 17 00:00:00 2001 From: Riedler Date: Mon, 17 Feb 2025 23:01:30 +0100 Subject: [PATCH 1/2] fix: more checks in module->get_cmobj --- lbplanner/classes/model/module.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lbplanner/classes/model/module.php b/lbplanner/classes/model/module.php index b0ba0acb..fdafabdb 100644 --- a/lbplanner/classes/model/module.php +++ b/lbplanner/classes/model/module.php @@ -156,21 +156,31 @@ public function get_cmobj(): \stdClass { global $DB; if ($this->cmobj === null) { if ($this->cmid !== null) { - $this->cmobj = $DB->get_record( + $res = $DB->get_record( modules_helper::COURSE_MODULES_TABLE, ['id' => $this->cmid] ); + if ($res === false) { + throw new \moodle_exception("couldn't get course module with cmid {$this->cmid}"); + } } else { - assert($this->assignid !== null); - $this->cmobj = $DB->get_record( + if ($this->assignid === null) { + throw new \coding_exception('tried to query cmid on a module object without assignid'); + } + $courseid = $this->get_courseid(); + $res = $DB->get_record( modules_helper::COURSE_MODULES_TABLE, [ - 'course' => $this->get_courseid(), + 'course' => $courseid, 'instance' => $this->assignid, 'module' => 1, ] ); + if ($res === false) { + throw new \moodle_exception("couldn't get course module with assignid {$this->assignid} and courseid {$courseid}"); + } } + $this->cmobj = $res; } return $this->cmobj; } From 4a29df7dfaf2862db01824d8fc8e19f62a0b05de Mon Sep 17 00:00:00 2001 From: Riedler Date: Tue, 18 Feb 2025 01:11:46 +0100 Subject: [PATCH 2/2] style: appeased moodle code checker --- lbplanner/classes/model/module.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lbplanner/classes/model/module.php b/lbplanner/classes/model/module.php index fdafabdb..6e7c77ec 100644 --- a/lbplanner/classes/model/module.php +++ b/lbplanner/classes/model/module.php @@ -177,7 +177,9 @@ public function get_cmobj(): \stdClass { ] ); if ($res === false) { - throw new \moodle_exception("couldn't get course module with assignid {$this->assignid} and courseid {$courseid}"); + throw new \moodle_exception( + "couldn't get course module with assignid {$this->assignid} and courseid {$courseid}" + ); } } $this->cmobj = $res;