Skip to content

Commit

Permalink
Merge pull request #242 from ArloSoftware/ARLO-60
Browse files Browse the repository at this point in the history
ARLO-60: Adding buttong to resync old registrations with an adhoc task
  • Loading branch information
ojnadjarm authored Jan 9, 2025
2 parents 6b2bebb + 1bf7e8c commit 31bd112
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 15 deletions.
10 changes: 10 additions & 0 deletions admin/apiretries.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use enrol_arlo\api;
use enrol_arlo\local\tablesql\apiretries;
use enrol_arlo\adminsettings\configarlostatus;
use enrol_arlo\form\syncold;

require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
Expand All @@ -36,6 +37,7 @@
$action = optional_param('action', null, PARAM_ALPHA);
$course = optional_param('course', null, PARAM_INT);
$regid = optional_param('regid', null, PARAM_INT);
$message = optional_param('message', null, PARAM_TEXT);

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('apiretries', 'enrol_arlo'));
Expand All @@ -50,6 +52,10 @@
echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo'));
}

if (!enrol_arlo_sync_adhoc_queued()) {
echo $OUTPUT->single_button('', get_string('synoldreg', 'enrol_arlo'), '', ['data-action' => 'syncold']);
}

if (!empty($pluginconfig->get('redirectcount'))) {
echo get_string('apifails', 'enrol_arlo') . $pluginconfig->get('redirectcount');
echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo'));
Expand All @@ -72,6 +78,10 @@
}

$report = new apiretries('enrolsettingsarloapiretries');
if ($message) {
echo $OUTPUT->notification(get_string($message, 'enrol_arlo'), \core\output\notification::NOTIFY_INFO);
}
$report->out(apiretries::PAGINATION_MAX_LIMIT, false);
$PAGE->requires->js_call_amd('enrol_arlo/syncold', 'init', ['[data-action="syncold"]', syncold::class]);

echo $OUTPUT->footer();
3 changes: 3 additions & 0 deletions amd/build/syncold.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/syncold.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions amd/src/syncold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ModalForm from 'core_form/modalform';
import Config from 'core/config';
import {get_string as getString} from 'core/str';

export const init = (linkSelector, formClass) => {
let syncbutton = document.querySelector(linkSelector);

syncbutton.addEventListener('click', async(e) => {
e.preventDefault();

const form = new ModalForm(({
formClass,
args: {},
modalConfig: {
title: await getString('dateselector', 'enrol_arlo'),
},
saveButtonText: await getString('synchronize', 'enrol_arlo'),
returnFocus: e.currentTarget
}));

form.addEventListener(form.events.FORM_SUBMITTED, (e) => {
const response = e.detail;
let redirect = `${Config.wwwroot}/enrol/arlo/admin/apiretries.php?`;
let first = false;
for (let key in response) {
if (first) {
first = false;
} else {
redirect = redirect + '&';
}
redirect = redirect + `${key}=${response[key]}`;
}
window.location.assign(redirect);
});

form.show();
});

};
1 change: 0 additions & 1 deletion classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ public static function run_instance_jobs($instanceid, $full = false, $trace = nu
}
}


if (!empty($outcomesjobpersistent)) {
$outcomesjob = \enrol_arlo\local\factory\job_factory::create_from_persistent($outcomesjobpersistent);
$outcomestatus = $outcomesjob->run();
Expand Down
98 changes: 98 additions & 0 deletions classes/form/syncold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace enrol_arlo\form;


defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/formslib.php');
use core_form\dynamic_form;
use enrol_arlo\task\enrolments_adhoc;
use moodle_url;
use stdClass;

class syncold extends dynamic_form {

/**
* Get context.
*
* @return \context
* @throws \dml_exception
*/
protected function get_context_for_dynamic_submission(): \context {
return \context_system::instance();
}

/**
* Check access.
*
* @return void
* @throws \dml_exception
* @throws \required_capability_exception
*/
protected function check_access_for_dynamic_submission(): void {
require_capability('moodle/course:create', \context_system::instance());
}

/**
* Process submission.
*
* @return array
*/
public function process_dynamic_submission() {
$data = $this->get_data();
$task = new enrolments_adhoc();
$task->set_custom_data($data->startdate);
\core\task\manager::queue_adhoc_task($task);
return ['message' => 'taskqueued'];
}

/**
* Set form data.
*
* @return void
* @throws Exception No record in persistent table
*/
public function set_data_for_dynamic_submission(): void {
$this->set_data(new stdClass());
}

/**
* Get page url.
*
* @return moodle_url
*/
protected function get_page_url_for_dynamic_submission(): moodle_url {
return new moodle_url('/enrol/arlo/admin/apiretries.php');
}

public function definition() {
global $DB, $COURSE;
$form = $this->_form;
$form->addElement('date_selector', 'startdate', get_string('syncsince', 'enrol_arlo'));
$form->addHelpButton('startdate', 'syncsince', 'enrol_arlo');
}

public function validation($data, $files) {
$errors = parent::validation($data, $files);
$time = time();
if ($data['startdate'] > $time) {
$errors['startdate'] = get_string('invalidstartdate', 'enrol_arlo');
}
return $errors;
}
}
3 changes: 1 addition & 2 deletions classes/local/job/contact_merge_requests_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ 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;
$lastime = $jobpersistent->get('lastsourcetimemodified');
try {
$hasnext = true;
while ($hasnext) {
Expand Down
9 changes: 4 additions & 5 deletions classes/local/job/memberships_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function sync_resource($resource) {
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function sync_memberships($trace) {
public static function sync_memberships($trace, $timetosync = null) {
global $DB;

$plugin = api::get_enrolment_plugin();
Expand Down Expand Up @@ -294,14 +294,13 @@ 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;
$lastime = empty($timetosync) ? get_config('enrol_arlo', 'lastregtimemodified') : date('c', $timetosync);
$lastregid = empty($timetosync) ? get_config('enrol_arlo', 'lastregid') : 0;
while ($hasnext) {
$hasnext = false; // Break paging by default.
// Update contact merge requests records every page.
$contactmergerequestsjob = job_factory::get_job(['type' => 'contact_merge_requests']);
$contactmergerequestsjob->run();
$contactmergerequestsjob->run($timetosync);
$uri = new RequestUri();
$uri->setHost($pluginconfig->get('platform'));
$uri->setResourcePath('registrations/');
Expand Down
73 changes: 73 additions & 0 deletions classes/task/enrolments_adhoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace enrol_arlo\task;

use core\task\adhoc_task;
use core\task\scheduled_task;
use enrol_arlo\api;
use enrol_arlo\local\job\memberships_job;
use enrol_arlo\manager;
use null_progress_trace;
use text_progress_trace;

defined('MOODLE_INTERNAL') || die();

/**
* Create Moodle enrolments based off Arlo registrations adhoc.
*
* @package enrol_arlo
* @copyright 2020 LearningWorks Ltd {@link http://www.learningworks.co.nz}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrolments_adhoc extends adhoc_task {

/**
* Get schedule task human readable name.
*
* @return string
* @throws \coding_exception
*/
public function get_name() {
return get_string('enrolmentstaskadhoc', 'enrol_arlo');
}

/**
* Execute the task.
*
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
public function execute() {
global $CFG;
require_once($CFG->dirroot . '/enrol/arlo/lib.php');
if (!enrol_is_enabled('arlo')) {
return;
}
$trace = new null_progress_trace();
if ($CFG->debug == DEBUG_DEVELOPER) {
$trace = new text_progress_trace();
}
$timetosync = $this->get_custom_data();
memberships_job::sync_memberships($trace, $timetosync);

$manager = new manager();
$manager->process_email_queue();
return true;
}

}
9 changes: 6 additions & 3 deletions lang/en/enrol_arlo.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,6 @@
$string['useadhoctask_desc'] = 'Enable this if you are planning to perform a large synchronisation between Arlo and Moodle (e.g. 100+ records at once).';
$string['onlyactive'] = 'Only active events';
$string['onlyactive_desc'] = 'Only process registrations for active Arlo courses and online activities.';
$string['disableskip'] = 'Disable registration skip';
$string['disableskip_desc'] = 'Disables the ability to skip processing old registrations (A sync will run for all registrations even the ones already processed).';
$string['enable_multisync'] = 'Enable multisync';
$string['enable_multisync_desc'] = 'When the webhook is enabled the sync is done only trough the webhooks. This setting allows to run the sync through the scheduled task as well.';
$string['technicalcontact'] = 'Contact email';
Expand Down Expand Up @@ -492,4 +490,9 @@
$string['retry_sync'] = 'Retry sync';
$string['connectionstatus'] = '<span>Connection Status: </span>';
$string['apifails'] = '<span>Global API fails: </span>';

$string['synoldreg'] = 'Sync old registrations';
$string['dateselector'] = 'Sync date selector';
$string['syncsince'] = 'Sync since';
$string['syncsince_help'] = 'Sync registrations since selected date.';
$string['invalidstartdate'] = 'Date must be in the past';
$string['taskqueued'] = 'Syncronisation task has been queued.';
11 changes: 11 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,14 @@ function enrol_arlo_update_all_course_registrations($courseid) {
$DB->update_record('enrol_arlo_registration', $registration);
}
}

/**
* Check if there are any adhoc syncs queued.
*
* @return bool
*/
function enrol_arlo_sync_adhoc_queued() {
global $DB;
$sql = "SELECT * FROM {task_adhoc} WHERE classname = '\\enrol_arlo\\task\\enrolments_adhoc'";
return $DB->record_exists_sql($sql);
}
4 changes: 0 additions & 4 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@
$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));

// Only display management category if plugin enabled.
if ($enrol->is_enabled()) {
$name = get_string('managearlo', 'enrol_arlo');
Expand Down

0 comments on commit 31bd112

Please sign in to comment.