Skip to content

Commit

Permalink
Merge branch 'wip-97806-m403' into MOODLE_403_STABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed Feb 16, 2024
2 parents bb1a7b9 + d15d0d0 commit 2ee445e
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 682 deletions.
33 changes: 3 additions & 30 deletions local/o365/classes/feature/calsync/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,22 +392,7 @@ public function get_calendars() {
$apiclient = $this->construct_calendar_api($USER->id, false);
$o365upn = utils::get_o365_upn($USER->id);
if ($o365upn) {
$calendarresults = $apiclient->get_calendars($o365upn);
$calendars = $calendarresults['value'];
while (!empty($calendarresults['@odata.nextLink'])) {
$nextlink = parse_url($calendarresults['@odata.nextLink']);
$calendarresults = [];
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skip'])) {
$calendarresults = $apiclient->get_calendars($o365upn, $query['$skip']);
$calendars = array_merge($calendars, $calendarresults['value']);
}
}
}

return (!empty($calendars) && is_array($calendars)) ? $calendars : [];
return $apiclient->get_calendars($o365upn);
} else {
return [];
}
Expand All @@ -424,22 +409,10 @@ public function get_calendars() {
public function get_events($muserid, $o365calid, $since = null) {
$apiclient = $this->construct_calendar_api($muserid, false);
$o365upn = utils::get_o365_upn($muserid);

$events = [];
if ($o365upn) {
$eventresults = $apiclient->get_events($o365calid, $since, $o365upn);
$events = $eventresults['value'];
while (!empty($eventresults['@odata.nextLink'])) {
$nextlink = parse_url($eventresults['@odata.nextLink']);
$eventresults = [];
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skip'])) {
$eventresults = $apiclient->get_events($o365calid, $since, $o365upn, $query['$skip']);
$events = array_merge($events, $eventresults['value']);
}
}
}
$events = $apiclient->get_events($o365calid, $since, $o365upn);
}

return $events;
Expand Down
33 changes: 21 additions & 12 deletions local/o365/classes/feature/calsync/task/importfromoutlook.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute() {
}

// Get calendars set to sync in.
$starttime = time();
$starttimestring = time();

\local_o365\feature\calsync\observers::set_event_import(true);

Expand All @@ -74,29 +74,38 @@ public function execute() {
$events = $calsync->get_events($calsub->user_id, $calsub->o365calid, $laststarttime);
if (!empty($events) && is_array($events)) {
foreach ($events as $event) {
if (!isset($event['Id'])) {
if (!isset($event['id'])) {
$errmsg = 'Skipped an event because of malformed data.';
\local_o365\utils::debug($errmsg, __METHOD__, $event);
mtrace($errmsg);
continue;
}
$idmapexists = $DB->record_exists('local_o365_calidmap', ['outlookeventid' => $event['Id']]);
$idmapexists = $DB->record_exists('local_o365_calidmap', ['outlookeventid' => $event['id']]);
if (is_array($event['start'])) {
$starttimestring = $event['start']['dateTime'] . ' ' . $event['start']['timeZone'];
} else {
$starttimestring = $event['start'];
}
if (is_array($event['end'])) {
$endtimestring = $event['end']['dateTime'] . ' ' . $event['end']['timeZone'];
} else {
$endtimestring = $event['end'];
}
if ($idmapexists === false) {
// Create Moodle event.
$eventparams = [
'name' => $event['Subject'],
'description' => $event['Body']['Content'],
'name' => $event['subject'],
'description' => $event['body']['content'],
'eventtype' => $calsub->caltype,
'repeatid' => 0,
'modulename' => 0,
'instance' => 0,
'timestart' => strtotime($event['Start']),
'timestart' => strtotime($starttimestring),
'visible' => 1,
'uuid' => '',
'sequence' => 1,
];
$end = strtotime($event['End']);
$eventparams['timeduration'] = $end - $eventparams['timestart'];
$eventparams['timeduration'] = strtotime($endtimestring) - strtotime($starttimestring);

// If all day event time is stored in Outlook only as UTC time and not in the local user time.
if (isset($event['isAllDay']) && $event['isAllDay'] == '1') {
Expand All @@ -105,11 +114,11 @@ public function execute() {
if ($user->timezone == 99) {
$user->timezone = core_date::get_server_timezone();
}
$userstart = strtotime(substr($event['Start'], 0, 10) . ' ' . $user->timezone);
$userstart = strtotime(substr($starttimestring, 0, 10) . ' ' . $user->timezone);
if ($userstart) {
$eventparams['timestart'] = $userstart;
}
$userend = strtotime(substr($event['End'], 0, 10) . ' ' . $user->timezone);
$userend = strtotime(substr($endtimestring, 0, 10) . ' ' . $user->timezone);
if ($userstart && $userend) {
$eventparams['timeduration'] = $userend - $userstart - 1;
}
Expand All @@ -125,7 +134,7 @@ public function execute() {
if (!empty($moodleevent) && !empty($moodleevent->id)) {
$idmaprec = [
'eventid' => $moodleevent->id,
'outlookeventid' => $event['Id'],
'outlookeventid' => $event['id'],
'origin' => 'o365',
'userid' => $calsub->user_id
];
Expand All @@ -145,7 +154,7 @@ public function execute() {
$calsubs->close();
\local_o365\feature\calsync\observers::set_event_import(false);

set_config('calsyncinlastrun', $starttime, 'local_o365');
set_config('calsyncinlastrun', $starttimestring, 'local_o365');
return true;
}
}
95 changes: 7 additions & 88 deletions local/o365/classes/feature/coursesync/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,19 +749,7 @@ private function process_courses_without_teams() {
private function restore_group(int $objectrecid, string $objectid, array $objectrecmetadata) : bool {
global $DB;

$deletedgroupsresults = $this->graphclient->list_deleted_groups();
$deletedgroups = $deletedgroupsresults['value'];
while (!empty($deletedgroupsresults['@odata.nextLink'])) {
$nextlink = parse_url($deletedgroupsresults['@odata.nextLink']);
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skiptoken'])) {
$deletedgroupsresults = $this->graphclient->list_deleted_groups($query['$skiptoken']);
$deletedgroups = array_merge($deletedgroups, $deletedgroupsresults['value']);
}
}
}
$deletedgroups = $this->graphclient->list_deleted_groups();

foreach ($deletedgroups as $deletedgroup) {
if (!empty($deletedgroup) && isset($deletedgroup['id']) && $deletedgroup['id'] == $objectid) {
Expand Down Expand Up @@ -807,28 +795,7 @@ public function update_teams_cache() : bool {
}

// Fetch teams from Graph API.
$teams = [];
$teamspart = $this->graphclient->get_teams();
foreach ($teamspart['value'] as $teamitem) {
$teams[$teamitem['id']] = $teamitem;
}
while (!empty($teamspart['@odata.nextLink'])) {
$nextlink = parse_url($teamspart['@odata.nextLink']);
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skiptoken'])) {
$teamspart = $this->graphclient->get_teams($query['$skiptoken']);
foreach ($teamspart['value'] as $teamitem) {
if (!array_key_exists($teamitem['id'], $teams)) {
$teams[$teamitem['id']] = $teamitem;
}
}
} else {
$teamspart = [];
}
}
}
$teams = $this->graphclient->get_teams();

// Build existing teams records cache.
$this->mtrace('Building existing teams cache records', 1);
Expand Down Expand Up @@ -1162,26 +1129,10 @@ public function get_group_members(string $groupobjectid) : array {
$groupmembers = [];

$memberrecords = $this->graphclient->get_group_members($groupobjectid);
foreach ($memberrecords['value'] as $memberrecord) {
foreach ($memberrecords as $memberrecord) {
$groupmembers[$memberrecord['id']] = $memberrecord;
}

while (!empty($memberrecords['@odata.nextLink'])) {
$nextlink = parse_url($memberrecords['@odata.nextLink']);
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skiptoken'])) {
$memberrecords = $this->graphclient->get_group_members($groupobjectid, $query['$skiptoken']);
foreach ($memberrecords['value'] as $memberrecord) {
if (!array_key_exists($memberrecord['id'], $groupmembers)) {
$groupmembers[$memberrecord['id']] = $memberrecord;
}
}
}
}
}

return $groupmembers;
}

Expand All @@ -1195,26 +1146,10 @@ public function get_group_owners(string $groupobjectid) : array {
$groupowners = [];

$ownerresults = $this->graphclient->get_group_owners($groupobjectid);
foreach ($ownerresults['value'] as $ownerresult) {
foreach ($ownerresults as $ownerresult) {
$groupowners[$ownerresult['id']] = $ownerresult;
}

while (!empty($ownerresults['@odata.nextLink'])) {
$nextlink = parse_url($ownerresults['@odata.nextLink']);
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skiptoken'])) {
$ownerresults = $this->graphclient->get_group_owners($groupobjectid, $query['$skiptoken']);
foreach ($ownerresults['value'] as $ownerresult) {
if (!array_key_exists($ownerresult['id'], $groupowners)) {
$groupowners[$ownerresult['id']] = $ownerresult;
}
}
}
}
}

return $groupowners;
}

Expand All @@ -1225,28 +1160,12 @@ public function get_group_owners(string $groupobjectid) : array {
*/
public function get_all_group_ids() : array {
$groupids = [];

$groups = $this->graphclient->get_groups();
foreach ($groups['value'] as $group) {
foreach ($groups as $group) {
$groupids[] = $group['id'];
}
while (!empty($groups['@odata.nextLink'])) {
// Extract skiptoken.
$nextlink = parse_url($groups['@odata.nextLink']);
if (isset($nextlink['query'])) {
$query = [];
parse_str($nextlink['query'], $query);
if (isset($query['$skiptoken'])) {
$groups = $this->graphclient->get_groups($query['$skiptoken']);
foreach ($groups['value'] as $group) {
if (!in_array($group['id'], $groupids)) {
$groupids[] = $group['id'];
}
}
} else {
$groups = [];
}
}
}

return $groupids;
}

Expand Down
Loading

0 comments on commit 2ee445e

Please sign in to comment.