Skip to content

Commit

Permalink
Merge pull request #217 from ArloSoftware/ARLO-37b
Browse files Browse the repository at this point in the history
ARLO-37: Adding multisync support and adding extra settings
  • Loading branch information
ojnadjarm authored Feb 19, 2024
2 parents 6befbae + 90b2042 commit 775e37d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions classes/Arlo/AuthAPI/Resource/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class AbstractCollection implements \IteratorAggregate {
*/
protected $links = array();
/**
* @return ArrayIterator
* @return \ArrayIterator
*/
public function getIterator() {
public function getIterator() : \Traversable {
return new \ArrayIterator($this->collection);
}

Expand Down
4 changes: 1 addition & 3 deletions classes/Arlo/AuthAPI/XmlDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public function deserialize($data) {
}

$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();

$dom = new \DOMDocument();
$dom->loadXML($data, $this->loadOptions);
$dom->loadXML($data, $this->loadOptions | LIBXML_NOENT);

libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);

if ($error = libxml_get_last_error()) {
libxml_clear_errors();
Expand Down
9 changes: 8 additions & 1 deletion classes/local/job/contact_merge_requests_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class contact_merge_requests_job extends job {
public function run() {
$pluginconfig = new arlo_plugin_config();
$jobpersistent = $this->get_job_persistent();
$disableskip = get_config('enrol_arlo', 'disableskip');
$lastime = empty($disableskip) ? $jobpersistent->get('lastsourcetimemodified') : 0;
try {
$hasnext = true;
while ($hasnext) {
Expand All @@ -77,7 +79,11 @@ public function run() {
$uri->setHost($pluginconfig->get('platform'));
$uri->setResourcePath('contactmergerequests/');
$uri->addExpand('ContactMergeRequest');
$filter = "(CreatedDateTime gt datetime('" . $jobpersistent->get('lastsourcetimemodified') . "'))";
if (!empty($lastime)) {
$filter = "(CreatedDateTime gt datetime('" . $lastime . "'))";
} else {
$filter = "";
}
$uri->setFilterBy($filter);
$uri->setOrderBy('CreatedDateTime ASC');
$request = new Request('GET', $uri->output(true));
Expand Down Expand Up @@ -109,6 +115,7 @@ public function run() {
// Update scheduling information on persistent after successfull save.
// Note, lastsourceid doesn't get updated as is a GUID.
$jobpersistent->set('timelastrequest', time());
$lastime = time();
$jobpersistent->set('lastsourcetimemodified', $sourcecreated);
$jobpersistent->update();
} catch (moodle_exception $exception) {
Expand Down
21 changes: 15 additions & 6 deletions classes/local/job/memberships_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ public static function sync_memberships($trace) {
// adjusting and the filter each call so we get all records and don't end up
// getting same 250 each call.
$hasnext = true;
$disableskip = get_config('enrol_arlo', 'disableskip');
$lastime = empty($disableskip) ? get_config('enrol_arlo', 'lastregtimemodified') : date('c', 0);
$lastregid = empty($disableskip) ? get_config('enrol_arlo', 'lastregid') : 0;
while ($hasnext) {
$hasnext = false; // Break paging by default.
// Update contact merge requests records every page.
Expand All @@ -308,9 +311,7 @@ public static function sync_memberships($trace) {
$uri->addExpand('Registration/OnlineActivity');
$uri->addExpand('Registration/Contact');
$uri->setPagingTop(250);
$timemodified = get_config('enrol_arlo', 'lastregtimemodified');
$timemodified = empty($timemodified) ? date('c', 0) : $timemodified;
$lastregid = get_config('enrol_arlo', 'lastregid');
$timemodified = empty($lastime) ? date('c', 0) : $lastime;
$filter = "(LastModifiedDateTime gt datetime('" . $timemodified . "'))";
if ($lastregid) {
$filter .= " OR ";
Expand Down Expand Up @@ -340,8 +341,10 @@ public static function sync_memberships($trace) {
$trace->output('Lock timeout');
}
}
set_config('lastregtimemodified', $resource->LastModifiedDateTime, 'enrol_arlo');
set_config('lastregid', $resource->RegistrationID, 'enrol_arlo');
$lastregid = $resource->RegistrationID;
$lastime = $resource->LastModifiedDateTime;
set_config('lastregtimemodified', $lastime, 'enrol_arlo');
set_config('lastregid', $lastregid, 'enrol_arlo');
$hasnext = (bool) $collection->hasNext();
}
}
Expand All @@ -361,8 +364,11 @@ public static function sync_memberships($trace) {
*/
public static function sync_membership($resource, $trace) {
global $DB;

$onlyactive = get_config('enrol_arlo', 'onlyactive');
if ($event = $resource->getEvent()) {
if ($onlyactive && $event->Status != 'Active') {
return true;
}
$type = arlo_type::EVENT;
$persistent = event_persistent::get_record([
'sourceguid' => $event->UniqueIdentifier
Expand All @@ -373,6 +379,9 @@ public static function sync_membership($resource, $trace) {
return false;
}
} else if ($onlineactivity = $resource->getOnlineActivity()) {
if ($onlyactive && $onlineactivity->Status != 'Active') {
return true;
}
$type = arlo_type::ONLINEACTIVITY;
$persistent = online_activity_persistent::get_record([
'sourceguid' => $onlineactivity->UniqueIdentifier
Expand Down
3 changes: 2 additions & 1 deletion classes/task/enrolments.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function execute() {
$trace = new text_progress_trace();
}
$useweebhooks = get_config('enrol_arlo', 'enablewebhook');
if (empty($useweebhooks)) {
$multisync = get_config('enrol_arlo', 'enable_multisync');
if (empty($useweebhooks) || $multisync) {
memberships_job::sync_memberships($trace);
}
$manager = new manager();
Expand Down
4 changes: 2 additions & 2 deletions db/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
[
'classname' => 'enrol_arlo\task\enrolments',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'minute' => '59',
'hour' => '23',
'day' => '*',
'dayofweek' => '*',
'month' => '*',
Expand Down
6 changes: 6 additions & 0 deletions lang/en/enrol_arlo.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@
$string['enablewebhook_desc'] = 'Enable webhook for Arlo.';
$string['useadhoctask'] = 'Use webhook adhoc task';
$string['useadhoctask_desc'] = 'Use adhoc task for Arlo weebhook events.';
$string['onlyactive'] = 'Only active events';
$string['onlyactive_desc'] = 'Only process registrations for active events and online activities.';
$string['disableskip'] = 'Disable registration skip';
$string['disableskip_desc'] = 'Disable the ability to skip process of old registrations(The task will run sync for all registrations even the ones already processed).';
$string['enable_multisync'] = 'Enable multisync';
$string['enable_multisync_desc'] = 'Enable sync task when webhook is enabled.';
$string['webhookname'] = 'Arlo webhook name';
$string['technicalcontact'] = 'Contact email';
$string['regcreated'] = 'Registration created';
Expand Down
20 changes: 17 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,28 @@
$name = get_string('apipassword', 'enrol_arlo');
$settings->add(new admin_setting_configpasswordunmask('enrol_arlo/apipassword', $name, $description, ''));

$description = get_string('onlyactive_desc', 'enrol_arlo');
$name = get_string('onlyactive', 'enrol_arlo');
$settings->add(new admin_setting_configcheckbox('enrol_arlo/onlyactive', $name, $description, 0));

$description = get_string('disableskip_desc', 'enrol_arlo');
$name = get_string('disableskip', 'enrol_arlo');
$settings->add(new admin_setting_configcheckbox('enrol_arlo/disableskip', $name, $description, 0));

$description = get_string('enablewebhook_desc', 'enrol_arlo');
$name = get_string('enablewebhook', 'enrol_arlo');
$settings->add(new admin_setting_configcheckbox('enrol_arlo/enablewebhook', $name, $description, 0));

$description = get_string('useadhoctask_desc', 'enrol_arlo');
$name = get_string('useadhoctask', 'enrol_arlo');
$settings->add(new admin_setting_configcheckbox('enrol_arlo/useadhoctask', $name, $description, 0));
$webhookenable = get_config('enrol_arlo', 'enablewebhook');
if ($webhookenable) {
$description = get_string('useadhoctask_desc', 'enrol_arlo');
$name = get_string('useadhoctask', 'enrol_arlo');
$settings->add(new admin_setting_configcheckbox('enrol_arlo/useadhoctask', $name, $description, 0));

$description = get_string('enable_multisync_desc', 'enrol_arlo');
$name = get_string('enable_multisync', 'enrol_arlo');
$settings->add(new admin_setting_configcheckbox('enrol_arlo/enable_multisync', $name, $description, 0));
}
// Only display management category if plugin enabled.
if ($enrol->is_enabled()) {
$name = get_string('managearlo', 'enrol_arlo');
Expand Down

0 comments on commit 775e37d

Please sign in to comment.