Skip to content

NOISS: Moodle roster integrations#909

Open
kjporter wants to merge 8 commits into
mainfrom
moodle-roster-integrations
Open

NOISS: Moodle roster integrations#909
kjporter wants to merge 8 commits into
mainfrom
moodle-roster-integrations

Conversation

@kjporter
Copy link
Copy Markdown
Contributor

This PR will:

  • Synchronizes the facility roster with Moodle's user table and assigns appropriate cohort for home/visitor
  • Adds requested visitor to Moodle and assigns cohort

@kjporter kjporter requested a review from a team as a code owner May 10, 2026 01:00
@kjporter kjporter added enhancement New feature or request high priority labels May 10, 2026
Comment thread app/Console/Commands/MoodleSync.php Outdated
}

private function update_moodle_user(User|Visitor $user): null|int {
$cid = (class_basename($user) == 'User') ? $user->id : $user->cid;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For code clarity, can we break these out into a separate variable (perhaps isHomeController or similar?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no problem. Done.

['id' => (count($moodle_user) > 0) ? $moodle_user[0] : null, 'username' => $cid],
$query_parameters
);
$moodle_user = DB::connection('moodle')->table('user')->where('username', $cid)->first();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily in need of immediate change, but this routine seems like it's going to absolutely slam the database (both of them, but mostly the Moodle one) with like 4-5 queries per user.. can we optimize this maybe with like RETURNING id for example? Or, not querying for the entire object to get one field off it... stuff like that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point taken and yes, I see it too. Unfortunately, query builder is just very inefficient.

}
}

private function add_visit_request_cohort(int $cid): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this function is duplicating the effort of sync_visit_requests. I understand why it's separate (this one only assigns it to one user), but I don't think we need two functions that do the same thing. Is it possible to merge these into one function and just pass an optional parameter?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I made a decision to break this out to keep the Moodle stuff separate. Don't know if you recall, but years ago we had lots of Moodle integrations throughout the website. We got rid of Moodle and I asked everyone on staff if I should save the integrations. They swore up and down that we'd never have another Moodle again because of the VATUSA academy. Now we're back to Moodle. I'm trying to make this easier on the next WM who confronts this situation by keeping the Moodle code together.

Comment thread app/Console/Commands/MoodleSync.php Outdated
]);
}
}
$this->cohorts = DB::connection('moodle')->table('cohort')->select(['id', 'name'])->pluck('name', 'id')->toArray();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below about database optimizations; we make this query twice, when upserts (or just INSERT IGNORE) would do the trick

Copy link
Copy Markdown
Contributor Author

@kjporter kjporter May 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a check to only run second select statement in the rare case where we do perform insert operations. upsert won't work - it is only for models (this is a query builder) and insert ignore would cause us to hit the database 3 more times per operation (one for each of the 3 cohorts).

Comment thread app/Console/Commands/MoodleSync.php Outdated
$moodle_user_id = $this->update_moodle_user($user);
$cohort_id = null;
if ($user->status == 1) {
$cohort_id = array_search('Home', $this->cohorts);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We search this array a lot.. a map might be better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed all array_search() operations by re-indexing the array by cohort name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request high priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants