Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions lbplanner/classes/helpers/modules_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 17 additions & 4 deletions lbplanner/classes/model/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,29 @@ 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
*/
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;
Expand Down Expand Up @@ -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}");
Expand All @@ -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) {
Expand Down
Loading