Skip to content

Commit

Permalink
Merge branch 'wip-106915-m403' into MOODLE_403_STABLE
Browse files Browse the repository at this point in the history
# Conflicts:
#	local/o365/db/install.xml
#	local/o365/db/upgrade.php
#	local/o365/version.php
  • Loading branch information
weilai-irl committed Oct 9, 2024
2 parents 3d8cc04 + a7fd032 commit 66b6fe9
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 177 deletions.
66 changes: 12 additions & 54 deletions local/o365/classes/feature/cohortsync/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
* Microsoft Group and Moodle cohort mapping feature main class.
*/
class main {
/**
* API error message for group not existing.
*/
const GROUP_DOES_NOT_EXIST_ERROR = "does not exist or one of its queried reference-property objects are not present";

/**
* @var unified Graph client instance.
*/
Expand Down Expand Up @@ -185,7 +180,10 @@ public function delete_mapping_by_id(int $id) : void {
public function fetch_groups_from_cache() : void {
global $DB;

$records = $DB->get_records('local_o365_groups_cache');
$sql = 'SELECT *
FROM {local_o365_groups_cache}
WHERE not_found_since = 0';
$records = $DB->get_records_sql($sql);

$this->grouplist = [];
foreach ($records as $record) {
Expand Down Expand Up @@ -213,56 +211,16 @@ public function fetch_cohorts() : void {
public function update_groups_cache() : bool {
global $DB;

mtrace("... Start updating groups cache.");

try {
$this->fetch_groups_from_microsoft();
} catch (moodle_exception $e) {
mtrace("...... Failed to fetch groups. Error: " . $e->getMessage());
if (utils::update_groups_cache($this->graphclient, 1)) {
$sql = 'SELECT *
FROM {local_o365_groups_cache}
WHERE not_found_since = 0';
$this->grouplist = $DB->get_records_sql($sql);

return true;
} else {
return false;
}

$existingcacherecords = $DB->get_records('local_o365_groups_cache');
$existingcachebyoid = [];
foreach ($existingcacherecords as $existingcacherecord) {
$existingcachebyoid[$existingcacherecord->objectid] = $existingcacherecord;
}

foreach ($this->grouplist as $group) {
if (array_key_exists($group['id'], $existingcachebyoid)) {
$cacherecord = $existingcachebyoid[$group['id']];
$cacherecord->name = $group['displayName'];
$cacherecord->description = $group['description'];
$DB->update_record('local_o365_groups_cache', $cacherecord);
unset($existingcachebyoid[$group['id']]);
} else {
$cacherecord = new stdClass();
$cacherecord->objectid = $group['id'];
$cacherecord->name = $group['displayName'];
$cacherecord->description = $group['description'];
$DB->insert_record('local_o365_groups_cache', $cacherecord);
mtrace("...... Added group ID {$group['id']} to cache.");
}
}

foreach ($existingcachebyoid as $oldcacherecord) {
$DB->delete_records('local_o365_groups_cache', ['id' => $oldcacherecord->id]);
mtrace("...... Deleted group ID {$oldcacherecord->objectid} from cache.");
}

mtrace("... Finished updating groups cache.");

return true;
}

/**
* Fetch all Microsoft groups.
*
* @return void
*/
public function fetch_groups_from_microsoft() : void {
$this->grouplist = $this->graphclient->get_groups();
}

/**
Expand Down Expand Up @@ -299,7 +257,7 @@ public function get_group_owners_and_members(string $groupoid) {
$memberrecords = $this->graphclient->get_group_members($groupoid);
$ownerrecords = $this->graphclient->get_group_owners($groupoid);
} catch (moodle_exception $e) {
if (strpos($e->getMessage(), self::GROUP_DOES_NOT_EXIST_ERROR) !== false) {
if (strpos($e->getMessage(), utils::RESOURCE_NOT_EXIST_ERROR) !== false) {
$DB->delete_records('local_o365_objects', ['objectid' => $groupoid]);
mtrace("...... Deleted mapping for non-existing group ID $groupoid.");
} else {
Expand Down
Loading

0 comments on commit 66b6fe9

Please sign in to comment.