diff --git a/lbplanner/classes/helpers/modules_helper.php b/lbplanner/classes/helpers/modules_helper.php index bfa1743a..33db49ef 100644 --- a/lbplanner/classes/helpers/modules_helper.php +++ b/lbplanner/classes/helpers/modules_helper.php @@ -71,6 +71,23 @@ class modules_helper { */ const SUBMISSION_STATUS_SUBMITTED = 'submitted'; + /** + * For caching {@see get_assign_module_id()} + * @var ?int $assignmoduleid {@see get_assign_module_id()} + */ + private static ?int $assignmoduleid = null; + + /** + * Gets the ID of the module type "assign" (as opposed to "forum", "quiz", etc.) + */ + public static function get_assign_module_id(): int { + global $DB; + if (self::$assignmoduleid === null) { + self::$assignmoduleid = $DB->get_field('modules', 'id', ['name' => 'assign'], MUST_EXIST); + } + return self::$assignmoduleid; + } + /** * Determins the enum value for a grade. * TODO: this is bullshit. @@ -190,15 +207,20 @@ public static function get_module_status(module $module, int $userid, ?int $plan public static function get_all_modules_by_course(int $courseid, bool $ekenabled): array { global $DB; - $assignments = $DB->get_records(self::ASSIGN_TABLE, ['course' => $courseid]); + $cmodules = $DB->get_records( + self::COURSE_MODULES_TABLE, + [ + 'course' => $courseid, + 'visible' => 1, + 'visibleoncoursepage' => 1, + 'module' => self::get_assign_module_id(), // Must be assign, not forum or other types of modules. + ] + ); $modules = []; - foreach ($assignments as $assign) { - if ($assign === null) { - throw new coding_exception("what the fuck? 1 {$courseid} {$ekenabled}"); - } - $module = module::from_assignobj($assign); + foreach ($cmodules as $cm) { + $module = module::from_cmobj($cm); if ((!$ekenabled) && $module->get_type() === MODULE_TYPE::EK) { continue; } diff --git a/lbplanner/classes/model/module.php b/lbplanner/classes/model/module.php index f534d46e..ed8a4814 100644 --- a/lbplanner/classes/model/module.php +++ b/lbplanner/classes/model/module.php @@ -117,6 +117,19 @@ public static function from_cmid(int $id): self { return $obj; } + /** + * Creates a module object from the course-module object. + * @param \stdClass $cmobj the course-module object from moodle's DB + * @return module a module object with filled-in course-module object + */ + public static function from_cmobj(\stdClass $cmobj): self { + $obj = new self(); + $obj->cmobj = $cmobj; + $obj->cmid = $cmobj->id; + $obj->assignid = $cmobj->instance; + return $obj; + } + /** * Fetches the necessary caches and returns the assignment ID * @return int assign ID @@ -124,9 +137,9 @@ public static function from_cmid(int $id): self { public function get_assignid(): int { if ($this->assignid === null) { if ($this->cmid !== null) { - $this->assignid = $this->get_cmobj()['instance']; + $this->assignid = $this->get_cmobj()->instance; } else { - throw new \coding_exception('requested assignid, but no assignid'); + throw new \coding_exception('requested assignid, but no cmid'); } } return $this->assignid; @@ -169,7 +182,7 @@ public function get_cmobj(): \stdClass { if ($this->cmid !== null) { $res = $DB->get_record( modules_helper::COURSE_MODULES_TABLE, - ['id' => $this->cmid] + ['id' => $this->cmid, 'module' => modules_helper::get_assign_module_id()] ); if ($res === false) { throw new \moodle_exception("couldn't get course module with cmid {$this->cmid}"); @@ -184,7 +197,7 @@ public function get_cmobj(): \stdClass { [ 'course' => $courseid, 'instance' => $this->assignid, - 'module' => 1, + 'module' => modules_helper::get_assign_module_id(), ] ); if ($res === false) {