From 774549ce4a760f838ffc5495e8ff352f47060903 Mon Sep 17 00:00:00 2001 From: Eric Merrill Date: Thu, 13 Jun 2024 21:31:09 -0400 Subject: [PATCH 01/13] Release 4.1.6 --- version.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version.php b/version.php index 56625eab030..d5177677e0d 100644 --- a/version.php +++ b/version.php @@ -28,9 +28,9 @@ $plugin = new stdClass(); -$plugin->version = 2024030102; +$plugin->version = 2024053000; $plugin->requires = 2022112800; // See http://docs.moodle.org/dev/Moodle_Version $plugin->component = 'enrol_arlo'; // Full name of the plugin (used for diagnostics). -$plugin->release = '4.1.4'; // Human-friendly version name. +$plugin->release = '4.1.6'; // Human-friendly version name. $plugin->maturity = MATURITY_STABLE; // This version's maturity level. $plugin->dependencies = []; From 7189a0f4b26cc59bbf6d3f24540a44fe59bc43bd Mon Sep 17 00:00:00 2001 From: Eric Merrill Date: Fri, 14 Jun 2024 08:22:51 -0400 Subject: [PATCH 02/13] Release 4.1.7 --- version.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version.php b/version.php index d5177677e0d..b6f61e5fc1c 100644 --- a/version.php +++ b/version.php @@ -28,9 +28,9 @@ $plugin = new stdClass(); -$plugin->version = 2024053000; +$plugin->version = 2024061400; $plugin->requires = 2022112800; // See http://docs.moodle.org/dev/Moodle_Version $plugin->component = 'enrol_arlo'; // Full name of the plugin (used for diagnostics). -$plugin->release = '4.1.6'; // Human-friendly version name. +$plugin->release = '4.1.7'; // Human-friendly version name. $plugin->maturity = MATURITY_STABLE; // This version's maturity level. $plugin->dependencies = []; From 52d1c126121691d0c277475748d700478eb59f79 Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Wed, 19 Jun 2024 09:45:57 -0500 Subject: [PATCH 03/13] ARLO-50: Fixing lock issue --- classes/local/job/memberships_job.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/classes/local/job/memberships_job.php b/classes/local/job/memberships_job.php index a6ede2f33df..a745b97a308 100644 --- a/classes/local/job/memberships_job.php +++ b/classes/local/job/memberships_job.php @@ -167,17 +167,16 @@ public function run() { $collection = response_processor::process($response); if ($collection->count() > 0) { foreach ($collection as $resource) { - $lockfactory = static::get_lock_factory(); - $lock = $lockfactory->get_lock('Registration: ' . + $reglock = $lockfactory->get_lock('Registration: ' . $resource->RegistrationID, self::TIME_LOCK_TIMEOUT); - if ($lock) { + if ($reglock) { try{ $this->sync_resource($resource, $trace); } catch (moodle_exception $exception) { debugging($exception->getMessage(), DEBUG_DEVELOPER); } finally { - $lock->release(); - } + $reglock->release(); + } } else { $trace->output('Lock timeout'); } From 6b0f1984e612937a9dcae35aabf9a6c1df0e964d Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Thu, 20 Jun 2024 10:02:59 -0500 Subject: [PATCH 04/13] ARLO-50: Fixing lock issues, retries issue and email max retries issue --- admin/apiretries.php | 25 ++++++--- classes/local/job/outcomes_job.php | 73 +++++++++---------------- classes/task/api_retry_notification.php | 38 +++++++------ lang/en/enrol_arlo.php | 21 ++++--- locallib.php | 57 ++++++++++++++++++- settings.php | 6 +- 6 files changed, 135 insertions(+), 85 deletions(-) diff --git a/admin/apiretries.php b/admin/apiretries.php index 76554c64cb5..38b3dde3304 100644 --- a/admin/apiretries.php +++ b/admin/apiretries.php @@ -29,25 +29,32 @@ require_once(__DIR__ . '/../../../config.php'); require_once($CFG->libdir . '/adminlib.php'); require_once($CFG->libdir . '/tablelib.php'); +require_once($CFG->dirroot . '/enrol/arlo/locallib.php'); admin_externalpage_setup('enrolsettingsarloapiretries'); $action = optional_param('action', null, PARAM_ALPHA); +$course = optional_param('course', null, PARAM_INT); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('apiretries', 'enrol_arlo')); if ($action === 'enablecommunication') { - $plugin = api::get_enrolment_plugin(); - $pluginconfig = $plugin->get_plugin_config(); - set_config('enablecommunication', 1, 'enrol_arlo'); - $pluginconfig->set('enablecommunication', get_config('enrol_arlo','enablecommunication')); - set_config('redirectcount', 0, 'enrol_arlo'); - $pluginconfig->set('redirectcount', get_config('enrol_arlo','redirectcount')); + enrol_arlo_enablecommunication(); echo $OUTPUT->notification(get_string('communication_enabled_message', 'enrol_arlo'),\core\output\notification::NOTIFY_SUCCESS); } + +if ($action === 'resetredirects') { + enrol_arlo_reset_redirects(); + echo $OUTPUT->notification(get_string('resetretries_message', 'enrol_arlo'),\core\output\notification::NOTIFY_SUCCESS); +} + +// This will be hidden from the user interface, will only work if the params are set manually through the URL. +if ($action === 'updateall' && !empty($course)) { + enrol_arlo_update_all_course_registrations($course); +} + $report = new apiretries('enrolsettingsarloapiretries'); $report->out(apiretries::PAGINATION_MAX_LIMIT, false); -$url = new moodle_url($PAGE->url, ['action'=>'resubmit']); -$url = new moodle_url($PAGE->url, ['action' => 'enablecommunication']); -echo $OUTPUT->single_button($url, "Enable communication", 'get'); +echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo')); +echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo')); echo $OUTPUT->footer(); \ No newline at end of file diff --git a/classes/local/job/outcomes_job.php b/classes/local/job/outcomes_job.php index 66ea6d5c77c..598456f5dd3 100644 --- a/classes/local/job/outcomes_job.php +++ b/classes/local/job/outcomes_job.php @@ -119,38 +119,11 @@ public function can_run() { $this->add_reasons(get_string('onlineactivityresultpushingdisabled', 'enrol_arlo')); return false; } - $maxpluginredirects = get_config('enrol_arlo','maxpluginredirects'); - $this->trace->output('redirects are '.$pluginconfig->get('redirectcount')); - $this->trace->output('enabled is '.$pluginconfig->get('enablecommunication')); - if (get_config('enrol_arlo', 'redirectcount')>=$maxpluginredirects){ - // Notify about failure - global $CFG, $SITE; - require_once($CFG->dirroot . '/enrol/arlo/locallib.php'); + $redirectcount = $pluginconfig->get('redirectcount'); + $this->trace->output('redirects are '. $redirectcount); + $this->trace->output('enabled is '. $pluginconfig->get('enablecommunication')); - - $admins = get_admins(); - $allVariables = get_defined_vars(); - -// Display the list - //$this->trace->output(json_encode($allVariables)); - $manager = new \enrol_arlo\manager(); - $manager->add_max_redirect_notification_to_queue(); - foreach ($admins as $admin) { - //$this->trace->output(json_encode($admin)); - - $this->trace->output(sendfailurenotification($admin)); - } - - } - if (get_config('enrol_arlo', 'redirectcount')>=$maxpluginredirects && $pluginconfig->get('enablecommunication') == 1 ) { - $this->add_reasons(get_string('redirectcountmaxlimit', 'enrol_arlo')); - //sychronize the plugin config persistent settings with the current database values. - $pluginconfig->set('redirectcount', get_config('enrol_arlo','redirectcount')); - set_config('enablecommunication', 0, 'enrol_arlo'); - $pluginconfig->set('enablecommunication', get_config('enrol_arlo','enablecommunication')); - return false; - } if($pluginconfig->get('enablecommunication') == 0) { $this->add_reasons(get_string('communication_disabled_message', 'enrol_arlo')); return false; @@ -201,19 +174,11 @@ public function run() { $user = core_user::get_user($registrationpersistent->get('userid')); $apiretryerrorpt1 = get_string('apiretryerrorpt1', 'enrol_arlo'); $apiretryerrorpt2 = get_string('apiretryerrorpt2', 'enrol_arlo'); - $redirectcounter = $registrationpersistent->get('redirectcounter'); - $maxredirects = $pluginconfig->get('retriesperrecord'); - $retrylog = new retry_log_persistent(); - $retrylog->set('timelogged', time()); - $retrylog->set('userid', $user->id); - $retrylog->set('participantname', "$user->lastname, $user->firstname"); - $retrylog->set('courseid', $course->id); - $retrylog->set('coursename', $course->fullname); - if ( $redirectcounter >= $maxredirects) { + $recordcounter = $registrationpersistent->get('redirectcounter'); + $maxrecordretries = $pluginconfig->get('retriesperrecord'); + if ($recordcounter >= $maxrecordretries) { // Display retry error to admin on job page $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); - // Create and save a log of the failure - $retrylog->save(); } else { try { if (!$user) { @@ -226,19 +191,33 @@ public function run() { if (!empty($data)) { $this->trace->output(implode(',', $data)); external::patch_registration_resource($sourceregistration, $data); - // Check API status code. If it's a 3xx, increment the redirectcounter by 1 + // Check API status code. If it's a 3xx, increment the redirectcounter by 1. $apistatus = $pluginconfig->get('apistatus'); if ($apistatus >= 300 && $apistatus <= 399) { - $registrationpersistent->set('redirectcounter', ++$redirectcounter); + $registrationpersistent->set('redirectcounter', ++$recordcounter); $pluginredirectcount = $pluginconfig->get('redirectcount'); + $pluginmaxredirects = get_config('enrol_arlo', 'maxretries'); $pluginconfig->set('redirectcount', ++$pluginredirectcount); - } else { + if (!empty($pluginmaxredirects) && ++$pluginredirectcount >= $pluginmaxredirects) { + $pluginconfig->set('enablecommunication', 0); + } + if (!empty($maxrecordretries) && $recordcounter >= $maxrecordretries) { + // Display retry error to admin on job page + $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); + // Create and save a log of the failure + $retrylog = new retry_log_persistent(); + $retrylog->set('timelogged', time()); + $retrylog->set('userid', $user->id); + $retrylog->set('participantname', "$user->lastname, $user->firstname"); + $retrylog->set('courseid', $course->id); + $retrylog->set('coursename', $course->fullname); + $retrylog->save(); + } + } elseif ($recordcounter > 0) { $registrationpersistent->set('redirectcounter', 0); } + $apistatus == 200 ? $registrationpersistent->set('updatesource', 0) : null; $registrationpersistent->set('timelastrequest', time()); - // Reset update flag. - $registrationpersistent->set('updatesource', 0); - $retrylog->save(); $registrationpersistent->save(); } } catch (Exception $exception) { diff --git a/classes/task/api_retry_notification.php b/classes/task/api_retry_notification.php index 78670c8b302..a4566e114f0 100644 --- a/classes/task/api_retry_notification.php +++ b/classes/task/api_retry_notification.php @@ -17,7 +17,7 @@ namespace enrol_arlo\task; use core\message\message; - +use enrol_arlo\api; /** * Arlo Retry Log Monitor Task @@ -50,25 +50,29 @@ public function execute() { global $CFG; require_once($CFG->dirroot . '/enrol/arlo/locallib.php'); $newentries = check_arlo_api_retry_log(); - + $admins = get_admins(); if (!empty($newentries)) { // Notify all Moodle administrators about the new entries. - $admins = get_admins(); - $apiretrylogurl = new \moodle_url('/enrol/arlo/admin/apiretries.php'); - $manager = new \enrol_arlo\manager(); - $manager->add_max_redirect_notification_to_queue(); - $message = new message(); - $message->component = 'enrol_arlo'; - $message->name = 'arlo_retry_log_notification'; - $message->userfrom = \core_user::get_noreply_user(); - $message->subject = get_string('arlo_retry_log_subject', 'enrol_arlo'); - $message->fullmessage = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out()); - $message->fullmessageformat = FORMAT_PLAIN; - $message->fullmessagehtml = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out()); foreach ($admins as $admin) { - $message->userto = $admin; - message_send($message); - sendfailurenotification($admin); + $plugin = api::get_enrolment_plugin(); + $pluginconfig = $plugin->get_plugin_config(); + if ($pluginconfig->get('enablecommunication') == 0) { + sendfailurenotification($admin); + } else { + $apiretrylogurl = new \moodle_url('/enrol/arlo/admin/apiretries.php'); + $manager = new \enrol_arlo\manager(); + $manager->add_max_redirect_notification_to_queue(); + $message = new message(); + $message->component = 'enrol_arlo'; + $message->name = 'arlo_retry_log_notification'; + $message->userfrom = \core_user::get_noreply_user(); + $message->subject = get_string('arlo_retry_log_subject', 'enrol_arlo'); + $message->fullmessage = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out()); + $message->fullmessageformat = FORMAT_PLAIN; + $message->fullmessagehtml = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out()); + $message->userto = $admin; + message_send($message); + } } } } diff --git a/lang/en/enrol_arlo.php b/lang/en/enrol_arlo.php index 0c5a66e2319..352b999a198 100644 --- a/lang/en/enrol_arlo.php +++ b/lang/en/enrol_arlo.php @@ -192,8 +192,10 @@

If no match is found using any of the Methods a Moodle user account will be created based on Arlo Contact details.

'; -$string['maxretries'] = 'Maxiumum retries per record'; -$string['maxretries_desc'] = 'Maximum number of retries per record allowed before halting API communication.'; +$string['retriesperrecord'] = 'Maxiumum retries per record'; +$string['retriesperrecord_desc'] = 'Maximum number of retries per record allowed, when reached the record will not be processed further until the retries are reset.'; +$string['maxretries'] = 'Maxiumum retries permitted on the API'; +$string['maxretries_desc'] = 'The maximum number of retries allowed before the API communication is halted, see the API retries error report for more information or to reset the retries.'; $string['maxretires_email'] = 'Maximum retries error email'; $string['maxretires_email_desc'] = 'The email that error reports are sent to when a user reaches the maximum retires per record.'; $string['maxretries_exception'] = 'Too many failed attempts, contact your admin.'; @@ -475,9 +477,12 @@ $string['communication_enabled_message'] = 'Communication has been successfully enabled.'; $string['communication_disabled_message'] = 'Communication has been disabled.'; $string['emailsubject'] = 'Alert: Maximum Retries Reached for PATCH Requests on {$a}'; -$string['emailbody'] = 'Dear {$a->fullname},\n\n' . - 'The site "{$a->shortname}" ({$a->url}) has reached the maximum number of retries ({$a->maxretries}) for registration PATCH requests.\n' . - 'Please review the issue as soon as possible.\n' . - 'Report: {$a->reportlink}\n\n' . - 'Regards,\nMoodle System'; -$string['notifymaxredirects'] = 'Notify on maximum redirects'; \ No newline at end of file +$string['emailbody'] = 'Dear {$a->fullname},
' . + 'The site "{$a->shortname}" {$a->url} has reached the maximum number of retries {$a->maxretries} for registration PATCH requests.
' . + 'Please review the issue as soon as possible.
' . + 'Report: {$a->reportlink}
' . + 'Regards,
Moodle System'; +$string['notifymaxredirects'] = 'Notify on maximum redirects'; +$string['enablecommunication'] = 'Enable communication'; +$string['resetredirects'] = 'Reset redirects'; +$string['resetretries_message'] = 'The retries for registration PATCH requests have been reset.'; diff --git a/locallib.php b/locallib.php index 2087430dba2..4fe2ded2153 100644 --- a/locallib.php +++ b/locallib.php @@ -296,12 +296,14 @@ function check_arlo_api_retry_log() { global $DB; - $lastchecktime = strtotime('-1 day'); + $lastchecktime = get_config('enrol_arlo', 'lastapiretrychecktime'); + // Query the database for new entries since the last check. $sql = "SELECT * FROM {enrol_arlo_retrylog} WHERE timelogged > :lastchecktime"; $params = ['lastchecktime' => $lastchecktime]; $newentries = $DB->get_records_sql($sql, $params); + set_config('lastapiretrychecktime', time(), 'enrol_arlo'); return $newentries; } @@ -311,7 +313,8 @@ function sendfailurenotification( $admininfo) { $noreplyuser = \core_user::get_noreply_user(); $apiretrylogurl = new \moodle_url('/enrol/arlo/admin/apiretries.php'); $siteinfo = $SITE; - $maxpluginredirects = get_config('maxpluginredirects','enrol_arlo'); + $maxpluginredirects = get_config('enrol_arlo', 'maxretries'); + // Subject of the email $emailsubject = get_string('emailsubject', 'enrol_arlo', $siteinfo->shortname); @@ -319,7 +322,8 @@ function sendfailurenotification( $admininfo) { $emailbody = get_string('emailbody', 'enrol_arlo', [ 'fullname' => $admininfo->firstname . ' ' . $admininfo->lastname, 'shortname' => $siteinfo->shortname, - 'maxpluginredirects' => $maxpluginredirects, + 'url' => $CFG->wwwroot, + 'maxretries' => $maxpluginredirects, 'reportlink' => $apiretrylogurl->out() ]); @@ -328,3 +332,50 @@ function sendfailurenotification( $admininfo) { return ('Email sent to ' . $admininfo->email); } +/** + * Enable communication with Arlo and reset the redirect counter of the plugin. + */ +function enrol_arlo_enablecommunication() { + $plugin = api::get_enrolment_plugin(); + $pluginconfig = $plugin->get_plugin_config(); + $pluginconfig->set('enablecommunication', 1); + $pluginconfig->set('redirectcount', 0); +} + +/** + * Reset the redirect counter for all registrations that have reached the maximum number of retries. + */ +function enrol_arlo_reset_redirects() { + global $DB; + $plugin = api::get_enrolment_plugin(); + $pluginconfig = $plugin->get_plugin_config(); + $pluginconfig->set('redirectcount', 0); + $maxretries = get_config('enrol_arlo', 'retriesperrecord'); + $sql = "SELECT * FROM {enrol_arlo_registration} + WHERE redirectcounter >= :maxretries + AND updatesource = 1"; + $registrations = $DB->get_records_sql($sql, ['maxretries' => $maxretries]); + foreach ($registrations as $registration) { + $registration->redirectcounter = 0; + $DB->update_record('enrol_arlo_registration', $registration); + } +} + +/** + * Update all registrations in a course that have not been graded or have no outcome. + * + * @param $courseid + */ +function enrol_arlo_update_all_course_registrations($courseid) { + global $DB; + $enrolid = $DB->get_field('enrol', 'id', ['enrol' => 'arlo', 'courseid' => $courseid]); + $sql = "SELECT * FROM {enrol_arlo_registration} + WHERE enrolid = :enrolid + AND grade IS NULL + AND outcome IS NULL"; + $registrations = $DB->get_records_sql($sql, ['enrolid' => $enrolid]); + foreach ($registrations as $registration) { + $registration->updatesource = 1; + $DB->update_record('enrol_arlo_registration', $registration); + } +} diff --git a/settings.php b/settings.php index 37cf4ab81a7..f9c8def62c8 100644 --- a/settings.php +++ b/settings.php @@ -77,9 +77,13 @@ $name = get_string('disableskip', 'enrol_arlo'); $settings->add(new admin_setting_configcheckbox('enrol_arlo/disableskip', $name, $description, 0)); + $name = get_string('retriesperrecord', 'enrol_arlo'); + $description = get_string('retriesperrecord_desc', 'enrol_arlo'); + $settings->add(new admin_setting_configtext('enrol_arlo/retriesperrecord', $name, $description, 5, PARAM_INT)); + $name = get_string('maxretries', 'enrol_arlo'); $description = get_string('maxretries_desc', 'enrol_arlo'); - $settings->add(new admin_setting_configtext('enrol_arlo/retriesperrecord', $name, $description, 5, PARAM_INT)); + $settings->add(new admin_setting_configtext('enrol_arlo/maxretries', $name, $description, 500, PARAM_INT)); $name = get_string('maxretires_email', 'enrol_arlo'); $description = get_string('maxretires_email_desc', 'enrol_arlo'); From 9ec6c73e248f0c8e5a9f5e9d886f17470e89bf12 Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Thu, 20 Jun 2024 10:10:16 -0500 Subject: [PATCH 05/13] ARLO-50: Adding validation for enable comunication and reset redirects buttons --- admin/apiretries.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/admin/apiretries.php b/admin/apiretries.php index 38b3dde3304..7527e245aa1 100644 --- a/admin/apiretries.php +++ b/admin/apiretries.php @@ -55,6 +55,13 @@ $report = new apiretries('enrolsettingsarloapiretries'); $report->out(apiretries::PAGINATION_MAX_LIMIT, false); -echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo')); -echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo')); +$plugin = api::get_enrolment_plugin(); +$pluginconfig = $plugin->get_plugin_config(); +if (empty($pluginconfig->get('enablecommunication'))) { + echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo')); +} + +if (!empty($pluginconfig->get('redirectcount'))) { + echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo')); +} echo $OUTPUT->footer(); \ No newline at end of file From 48380df4ebeb7a8dad07abba335ec5f1aad48abc Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Thu, 20 Jun 2024 10:37:39 -0500 Subject: [PATCH 06/13] ARLO-50: Get config change --- classes/local/job/outcomes_job.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/local/job/outcomes_job.php b/classes/local/job/outcomes_job.php index 598456f5dd3..e8b443595b1 100644 --- a/classes/local/job/outcomes_job.php +++ b/classes/local/job/outcomes_job.php @@ -175,7 +175,7 @@ public function run() { $apiretryerrorpt1 = get_string('apiretryerrorpt1', 'enrol_arlo'); $apiretryerrorpt2 = get_string('apiretryerrorpt2', 'enrol_arlo'); $recordcounter = $registrationpersistent->get('redirectcounter'); - $maxrecordretries = $pluginconfig->get('retriesperrecord'); + $maxrecordretries = get_config('enrol_arlo', 'retriesperrecord'); if ($recordcounter >= $maxrecordretries) { // Display retry error to admin on job page $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); From 409a5052e41010b3f86867e9d1f4663238b5c106 Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Thu, 20 Jun 2024 10:42:14 -0500 Subject: [PATCH 07/13] ARLO-50: Reseting plugin fail count on success --- classes/local/job/outcomes_job.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/classes/local/job/outcomes_job.php b/classes/local/job/outcomes_job.php index e8b443595b1..293bd6bfd16 100644 --- a/classes/local/job/outcomes_job.php +++ b/classes/local/job/outcomes_job.php @@ -213,8 +213,11 @@ public function run() { $retrylog->set('coursename', $course->fullname); $retrylog->save(); } - } elseif ($recordcounter > 0) { - $registrationpersistent->set('redirectcounter', 0); + } else { + if ($recordcounter > 0) { + $registrationpersistent->set('redirectcounter', 0); + } + $pluginconfig->set('redirectcount', 0); } $apistatus == 200 ? $registrationpersistent->set('updatesource', 0) : null; $registrationpersistent->set('timelastrequest', time()); From 3259d4fdba00558f508383fd119265019ffdac11 Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Thu, 20 Jun 2024 11:43:47 -0500 Subject: [PATCH 08/13] ARLO-50: Improving query --- locallib.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/locallib.php b/locallib.php index 4fe2ded2153..088fff420f9 100644 --- a/locallib.php +++ b/locallib.php @@ -368,12 +368,12 @@ function enrol_arlo_reset_redirects() { */ function enrol_arlo_update_all_course_registrations($courseid) { global $DB; - $enrolid = $DB->get_field('enrol', 'id', ['enrol' => 'arlo', 'courseid' => $courseid]); - $sql = "SELECT * FROM {enrol_arlo_registration} - WHERE enrolid = :enrolid - AND grade IS NULL - AND outcome IS NULL"; - $registrations = $DB->get_records_sql($sql, ['enrolid' => $enrolid]); + $sql = "SELECT ear.* FROM {enrol_arlo_registration} ear + JOIN {enrol} e ON e.enrol = 'arlo' AND e.courseid = :courseid + WHERE ear.enrolid = e.id + AND ear.grade IS NULL + AND ear.outcome IS NULL"; + $registrations = $DB->get_records_sql($sql, ['courseid' => $courseid]); foreach ($registrations as $registration) { $registration->updatesource = 1; $DB->update_record('enrol_arlo_registration', $registration); From 77926dc890c751654b93ff6ff9ddc59464ca88bc Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Mon, 8 Jul 2024 08:45:01 -0500 Subject: [PATCH 09/13] ARLO-50: Addin per record retry button and minor UI changes --- admin/apiretries.php | 27 ++++++++----- classes/adminsettings/configarlostatus.php | 18 ++++++--- classes/local/job/outcomes_job.php | 34 +++++++++------- classes/local/tablesql/apiretries.php | 20 +++++++++ lang/en/enrol_arlo.php | 26 +++++++----- locallib.php | 19 ++++++--- settings.php | 47 ++++++++++++---------- styles.css | 4 ++ 8 files changed, 128 insertions(+), 67 deletions(-) diff --git a/admin/apiretries.php b/admin/apiretries.php index 7527e245aa1..9bffa93a822 100644 --- a/admin/apiretries.php +++ b/admin/apiretries.php @@ -25,6 +25,7 @@ use enrol_arlo\api; use enrol_arlo\local\tablesql\apiretries; +use enrol_arlo\adminsettings\configarlostatus; require_once(__DIR__ . '/../../../config.php'); require_once($CFG->libdir . '/adminlib.php'); @@ -34,9 +35,26 @@ admin_externalpage_setup('enrolsettingsarloapiretries'); $action = optional_param('action', null, PARAM_ALPHA); $course = optional_param('course', null, PARAM_INT); +$regid = optional_param('regid', null, PARAM_INT); + echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('apiretries', 'enrol_arlo')); +[$arlostatus, $desc] = configarlostatus::api_status_render(); +echo '
'; +echo 'Connection Status: ' . $arlostatus; + +$plugin = api::get_enrolment_plugin(); +$pluginconfig = $plugin->get_plugin_config(); +if (empty($pluginconfig->get('enablecommunication'))) { + echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo')); +} + +if (!empty($pluginconfig->get('redirectcount'))) { + echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo')); +} +echo '
'; + if ($action === 'enablecommunication') { enrol_arlo_enablecommunication(); echo $OUTPUT->notification(get_string('communication_enabled_message', 'enrol_arlo'),\core\output\notification::NOTIFY_SUCCESS); @@ -55,13 +73,4 @@ $report = new apiretries('enrolsettingsarloapiretries'); $report->out(apiretries::PAGINATION_MAX_LIMIT, false); -$plugin = api::get_enrolment_plugin(); -$pluginconfig = $plugin->get_plugin_config(); -if (empty($pluginconfig->get('enablecommunication'))) { - echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo')); -} - -if (!empty($pluginconfig->get('redirectcount'))) { - echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo')); -} echo $OUTPUT->footer(); \ No newline at end of file diff --git a/classes/adminsettings/configarlostatus.php b/classes/adminsettings/configarlostatus.php index 753eef092c0..10a327bce5a 100644 --- a/classes/adminsettings/configarlostatus.php +++ b/classes/adminsettings/configarlostatus.php @@ -77,6 +77,16 @@ public function write_setting($data) { * @throws moodle_exception */ public function output_html($data, $query = '') { + [$element, $description] = self::api_status_render(); + return format_admin_setting($this, '', $element, $description, false, '', null, $query); + } + + /** + * Render the API status. + * + * @return array An array containing the status element and description. + */ + public static function api_status_render() { global $OUTPUT; $apistatus = get_config('enrol_arlo', 'apistatus'); $apilastrequested = (int) get_config('enrol_arlo', 'apitimelastrequest'); @@ -94,7 +104,6 @@ public function output_html($data, $query = '') { $statusicon = $OUTPUT->pix_icon('t/go', get_string('ok', 'enrol_arlo')); } $reason = get_string('apistatusok', 'enrol_arlo', userdate($apilastrequested)); - $description = ''; } else if (0 == $apistatus || ($apistatus >= 400 && $apistatus < 499)) { if ($useimageiconclass) { $statusicon = $OUTPUT->image_icon('t/stop', get_string('notok', 'enrol_arlo')); @@ -111,12 +120,11 @@ public function output_html($data, $query = '') { $statusicon = $OUTPUT->pix_icon('t/stop', get_string('notok', 'enrol_arlo')); } $reason = get_string('apistatusservererror', 'enrol_arlo'); - $url = new moodle_url('/enrol/arlo/admin/apirequests.php'); + $url = new \moodle_url('/enrol/arlo/admin/apirequests.php'); $description = get_string('pleasecheckrequestlog', 'enrol_arlo', $url->out()); } else { - return ''; + return ['', '']; } - $element = '
'.$statusicon.' '.$reason.'
'; - return format_admin_setting($this, '', $element, $description, false, '', null, $query); + return ['
'.$statusicon.' '.$reason.'
', $description]; } } diff --git a/classes/local/job/outcomes_job.php b/classes/local/job/outcomes_job.php index 293bd6bfd16..fc645b99c1d 100644 --- a/classes/local/job/outcomes_job.php +++ b/classes/local/job/outcomes_job.php @@ -191,28 +191,32 @@ public function run() { if (!empty($data)) { $this->trace->output(implode(',', $data)); external::patch_registration_resource($sourceregistration, $data); - // Check API status code. If it's a 3xx, increment the redirectcounter by 1. + // Check API status code. If it's a 3xx, increment the registration counter by 1. + // Now we count the errors 4xx and 5xx as well. But this don't increase the counter of the registration. $apistatus = $pluginconfig->get('apistatus'); - if ($apistatus >= 300 && $apistatus <= 399) { - $registrationpersistent->set('redirectcounter', ++$recordcounter); + if ($apistatus >= 300 && $apistatus <= 599) { $pluginredirectcount = $pluginconfig->get('redirectcount'); $pluginmaxredirects = get_config('enrol_arlo', 'maxretries'); $pluginconfig->set('redirectcount', ++$pluginredirectcount); + if ($apistatus >= 300 && $apistatus <= 399) { + $registrationpersistent->set('redirectcounter', ++$recordcounter); + if (!empty($maxrecordretries) && $recordcounter >= $maxrecordretries) { + // Display retry error to admin on job page + $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); + // Create and save a log of the failure + $retrylog = new retry_log_persistent(); + $retrylog->set('timelogged', time()); + $retrylog->set('userid', $user->id); + $retrylog->set('participantname', "$user->lastname, $user->firstname"); + $retrylog->set('courseid', $course->id); + $retrylog->set('coursename', $course->fullname); + $retrylog->save(); + } + } + // We have reached the maximum number of errors allowed. Disable communication. if (!empty($pluginmaxredirects) && ++$pluginredirectcount >= $pluginmaxredirects) { $pluginconfig->set('enablecommunication', 0); } - if (!empty($maxrecordretries) && $recordcounter >= $maxrecordretries) { - // Display retry error to admin on job page - $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); - // Create and save a log of the failure - $retrylog = new retry_log_persistent(); - $retrylog->set('timelogged', time()); - $retrylog->set('userid', $user->id); - $retrylog->set('participantname', "$user->lastname, $user->firstname"); - $retrylog->set('courseid', $course->id); - $retrylog->set('coursename', $course->fullname); - $retrylog->save(); - } } else { if ($recordcounter > 0) { $registrationpersistent->set('redirectcounter', 0); diff --git a/classes/local/tablesql/apiretries.php b/classes/local/tablesql/apiretries.php index 5215a659179..89c6afb3f14 100644 --- a/classes/local/tablesql/apiretries.php +++ b/classes/local/tablesql/apiretries.php @@ -45,6 +45,9 @@ public function __construct($uniqueid) { $headers[] = get_string('courseid', 'enrol_arlo'); $columns[] = 'coursename'; $headers[] = get_string('coursename', 'enrol_arlo'); + $columns[] = 'action'; + $headers[] = get_string('action'); + $this->define_columns($columns); $this->define_headers($headers); $this->define_baseurl("/enrol/arlo/admin/apiretries.php"); @@ -58,4 +61,21 @@ public function __construct($uniqueid) { public function col_timelogged($values) { return userdate($values->timelogged); } + + public function col_action($values) { + global $DB, $OUTPUT; + $sql = "SELECT r.id, r.redirectcounter + FROM {enrol_arlo_registration} r + JOIN {enrol} e ON e.id = r.enrolid + WHERE r.userid = :userid + AND e.courseid = :courseid"; + $params = ['userid' => $values->userid, 'courseid' => $values->courseid]; + $record = $DB->get_record_sql($sql, $params); + $retry = get_string('retry_sync', 'enrol_arlo'); + $moodeurl = new \moodle_url('/enrol/arlo/admin/apiretries.php', ['action' => 'resetredirects', 'regid' => $record->id]); + $maxretries = get_config('enrol_arlo', 'retriesperrecord'); + $renderlink = !empty($record->redirectcounter) && ($record->redirectcounter >= $maxretries); + $output = $renderlink ? '' . $retry . '' : ''; + return $output; + } } \ No newline at end of file diff --git a/lang/en/enrol_arlo.php b/lang/en/enrol_arlo.php index 352b999a198..7e6cb11ec48 100644 --- a/lang/en/enrol_arlo.php +++ b/lang/en/enrol_arlo.php @@ -54,6 +54,7 @@ $string['apipassword'] = 'Arlo password'; $string['apipassword_desc'] = 'Forgot your password? Learn how to reset it.'; $string['arloconnection'] = 'Arlo connection'; +$string['arlosettings'] = 'Arlo settings'; $string['arlocontact'] = 'Arlo contact'; $string['arlocoursecode'] = 'Arlo course code'; $string['arloenrolment'] = 'Arlo enrolment'; @@ -193,11 +194,11 @@ If no match is found using any of the Methods a Moodle user account will be created based on Arlo Contact details.

'; $string['retriesperrecord'] = 'Maxiumum retries per record'; -$string['retriesperrecord_desc'] = 'Maximum number of retries per record allowed, when reached the record will not be processed further until the retries are reset.'; +$string['retriesperrecord_desc'] = 'Maximum number of retries per record allowed before halting API communication for that record.'; $string['maxretries'] = 'Maxiumum retries permitted on the API'; -$string['maxretries_desc'] = 'The maximum number of retries allowed before the API communication is halted, see the API retries error report for more information or to reset the retries.'; +$string['maxretries_desc'] = 'Maximum number of retries your connection is allowed before halting API communication.'; $string['maxretires_email'] = 'Maximum retries error email'; -$string['maxretires_email_desc'] = 'The email that error reports are sent to when a user reaches the maximum retires per record.'; +$string['maxretires_email_desc'] = 'The email that error reports are sent to when your connection reaches the maximum retries.'; $string['maxretries_exception'] = 'Too many failed attempts, contact your admin.'; $string['newaccountdetails'] = 'New account details'; $string['newuserdefaultemail'] = 'New user default email'; @@ -445,16 +446,15 @@ $string['webhookapiurl'] = 'Arlo webhook API URL'; $string['webhookapiurl_desc'] = 'Arlo webhook API URL for webhook requests.'; $string['enablewebhook'] = 'Enable webhook'; -$string['enablewebhook_desc'] = 'Enable webhook for Arlo.'; +$string['enablewebhook_desc'] = ' Webhooks allows real-time synchronisation between Arlo and Moodle.'; $string['useadhoctask'] = 'Use webhook adhoc task'; -$string['useadhoctask_desc'] = 'Use adhoc task for Arlo weebhook events.'; +$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 events and online activities.'; +$string['onlyactive_desc'] = 'Only process registrations for active Arlo courses 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['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'] = 'Enable sync task when webhook is enabled.'; -$string['webhookname'] = 'Arlo webhook name'; +$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'; $string['regcreated'] = 'Registration created'; $string['regupdated'] = 'Registration updated'; @@ -467,6 +467,9 @@ $string['webhookinactive'] = ''; $string['webhookdisabled'] = ''; $string['webhookstatus'] = 'Webhook status'; +$string['errorhandling'] = 'Error handling'; +$string['syncsettings'] = 'Sync settings'; +$string['webhooks'] = 'Webhooks'; $string['nomembershipjobfound'] = 'No membership job found for enrolment instance {$a}'; $string['nooutcomejobfound'] = 'No outcome job found for enrolment instance {$a}'; $string['api_retry_notification'] = 'API Retry Log Notification'; @@ -483,6 +486,7 @@ 'Report: {$a->reportlink}
' . 'Regards,
Moodle System'; $string['notifymaxredirects'] = 'Notify on maximum redirects'; -$string['enablecommunication'] = 'Enable communication'; -$string['resetredirects'] = 'Reset redirects'; +$string['enablecommunication'] = 'Re-stablish communication'; +$string['resetredirects'] = 'Retry all'; $string['resetretries_message'] = 'The retries for registration PATCH requests have been reset.'; +$string['retry_sync'] = 'Retry sync'; diff --git a/locallib.php b/locallib.php index 088fff420f9..c95a383706b 100644 --- a/locallib.php +++ b/locallib.php @@ -344,17 +344,24 @@ function enrol_arlo_enablecommunication() { /** * Reset the redirect counter for all registrations that have reached the maximum number of retries. + * + * @param $regid */ -function enrol_arlo_reset_redirects() { +function enrol_arlo_reset_redirects($regid = false) { global $DB; $plugin = api::get_enrolment_plugin(); $pluginconfig = $plugin->get_plugin_config(); $pluginconfig->set('redirectcount', 0); - $maxretries = get_config('enrol_arlo', 'retriesperrecord'); - $sql = "SELECT * FROM {enrol_arlo_registration} - WHERE redirectcounter >= :maxretries - AND updatesource = 1"; - $registrations = $DB->get_records_sql($sql, ['maxretries' => $maxretries]); + $params = []; + $sql = "SELECT * FROM {enrol_arlo_registration}"; + if ($regid) { + $sql .= " WHERE id = :regid"; + $params['regid'] = $regid; + } else { + $sql .= " WHERE redirectcounter >= :maxretries"; + $params['maxretries'] = get_config('enrol_arlo', 'retriesperrecord'); + } + $registrations = $DB->get_records_sql($sql, $params); foreach ($registrations as $registration) { $registration->redirectcounter = 0; $DB->update_record('enrol_arlo_registration', $registration); diff --git a/settings.php b/settings.php index f9c8def62c8..bc640f7930e 100644 --- a/settings.php +++ b/settings.php @@ -35,9 +35,12 @@ use enrol_arlo\adminsettings\configlockedtext; if ($hassiteconfig) { - $name = get_string('arloconnection', 'enrol_arlo'); + $name = get_string('arlosettings', 'enrol_arlo'); $settings = new admin_settingpage('enrolsettingsarlo', $name, 'moodle/site:config', $enrol->is_enabled() === false); + $name = get_string('arloconnection', 'enrol_arlo'); + $settings->add(new admin_setting_heading('enrol_arlo/arloconnection', $name, '')); + $settings->add(new configarlostatus('apistatus', get_string('pluginstatus', 'enrol_arlo'))); $name = get_string('platform', 'enrol_arlo'); @@ -69,13 +72,26 @@ $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)); + $name = get_string('webhooks', 'enrol_arlo'); + $settings->add(new admin_setting_heading('enrol_arlo/webhooks', $name, '')); - $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)); + + $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)); + } + + $name = get_string('errorhandling', 'enrol_arlo'); + $settings->add(new admin_setting_heading('enrol_arlo/errorhandling', $name, '')); $name = get_string('retriesperrecord', 'enrol_arlo'); $description = get_string('retriesperrecord_desc', 'enrol_arlo'); @@ -90,6 +106,9 @@ $default = 'moodleconnections@arlo.co'; $settings->add(new configemail('enrol_arlo/apierroremail', $name, $description, $default)); + $name = get_string('syncsettings', 'enrol_arlo'); + $settings->add(new admin_setting_heading('enrol_arlo/syncsettings', $name, '')); + $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)); @@ -98,20 +117,6 @@ $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)); - - $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'); diff --git a/styles.css b/styles.css index e6d27913960..57d30e28f16 100644 --- a/styles.css +++ b/styles.css @@ -2672,3 +2672,7 @@ float: none !important; } } + +.communication-buttons { + gap: 20px; +} \ No newline at end of file From f741969faebfbb4dd14a5cf942a56074f0646a7f Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Mon, 8 Jul 2024 09:00:36 -0500 Subject: [PATCH 10/13] ARLO-50: Avoiding global reset on specific record reset --- locallib.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/locallib.php b/locallib.php index c95a383706b..433991db758 100644 --- a/locallib.php +++ b/locallib.php @@ -349,15 +349,16 @@ function enrol_arlo_enablecommunication() { */ function enrol_arlo_reset_redirects($regid = false) { global $DB; - $plugin = api::get_enrolment_plugin(); - $pluginconfig = $plugin->get_plugin_config(); - $pluginconfig->set('redirectcount', 0); + $params = []; $sql = "SELECT * FROM {enrol_arlo_registration}"; if ($regid) { $sql .= " WHERE id = :regid"; $params['regid'] = $regid; } else { + $plugin = api::get_enrolment_plugin(); + $pluginconfig = $plugin->get_plugin_config(); + $pluginconfig->set('redirectcount', 0); $sql .= " WHERE redirectcounter >= :maxretries"; $params['maxretries'] = get_config('enrol_arlo', 'retriesperrecord'); } From f7d081f228294e4033d851aaaf289f1820d94d99 Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Mon, 8 Jul 2024 10:32:33 -0500 Subject: [PATCH 11/13] ARLO-50: Changing string and config retrieval --- admin/apiretries.php | 7 ++++--- classes/local/job/outcomes_job.php | 8 ++++---- lang/en/enrol_arlo.php | 5 ++++- locallib.php | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/admin/apiretries.php b/admin/apiretries.php index 9bffa93a822..2ef92c80d7e 100644 --- a/admin/apiretries.php +++ b/admin/apiretries.php @@ -41,16 +41,17 @@ echo $OUTPUT->heading(get_string('apiretries', 'enrol_arlo')); [$arlostatus, $desc] = configarlostatus::api_status_render(); -echo '
'; -echo 'Connection Status: ' . $arlostatus; - $plugin = api::get_enrolment_plugin(); $pluginconfig = $plugin->get_plugin_config(); +echo '
'; +echo get_string('connectionstatus', 'enrol_arlo') . $arlostatus; + if (empty($pluginconfig->get('enablecommunication'))) { echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo')); } 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')); } echo '
'; diff --git a/classes/local/job/outcomes_job.php b/classes/local/job/outcomes_job.php index fc645b99c1d..483a0a477ca 100644 --- a/classes/local/job/outcomes_job.php +++ b/classes/local/job/outcomes_job.php @@ -175,7 +175,7 @@ public function run() { $apiretryerrorpt1 = get_string('apiretryerrorpt1', 'enrol_arlo'); $apiretryerrorpt2 = get_string('apiretryerrorpt2', 'enrol_arlo'); $recordcounter = $registrationpersistent->get('redirectcounter'); - $maxrecordretries = get_config('enrol_arlo', 'retriesperrecord'); + $maxrecordretries = $pluginconfig->get('retriesperrecord'); if ($recordcounter >= $maxrecordretries) { // Display retry error to admin on job page $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); @@ -191,12 +191,12 @@ public function run() { if (!empty($data)) { $this->trace->output(implode(',', $data)); external::patch_registration_resource($sourceregistration, $data); - // Check API status code. If it's a 3xx, increment the registration counter by 1. - // Now we count the errors 4xx and 5xx as well. But this don't increase the counter of the registration. + // Check API status code. If it's a 3xx, increment the registration and global retry counter. + // Now we count the errors 4xx and 5xx globally, but we don't increase the registration retry counter. $apistatus = $pluginconfig->get('apistatus'); if ($apistatus >= 300 && $apistatus <= 599) { $pluginredirectcount = $pluginconfig->get('redirectcount'); - $pluginmaxredirects = get_config('enrol_arlo', 'maxretries'); + $pluginmaxredirects = $pluginconfig->get('maxretries'); $pluginconfig->set('redirectcount', ++$pluginredirectcount); if ($apistatus >= 300 && $apistatus <= 399) { $registrationpersistent->set('redirectcounter', ++$recordcounter); diff --git a/lang/en/enrol_arlo.php b/lang/en/enrol_arlo.php index 7e6cb11ec48..c273e22061c 100644 --- a/lang/en/enrol_arlo.php +++ b/lang/en/enrol_arlo.php @@ -487,6 +487,9 @@ 'Regards,
Moodle System'; $string['notifymaxredirects'] = 'Notify on maximum redirects'; $string['enablecommunication'] = 'Re-stablish communication'; -$string['resetredirects'] = 'Retry all'; +$string['resetredirects'] = 'Reset all fail counters'; $string['resetretries_message'] = 'The retries for registration PATCH requests have been reset.'; $string['retry_sync'] = 'Retry sync'; +$string['connectionstatus'] = 'Connection Status: '; +$string['apifails'] = 'Global API fails: '; + diff --git a/locallib.php b/locallib.php index 433991db758..b81886f6173 100644 --- a/locallib.php +++ b/locallib.php @@ -360,7 +360,7 @@ function enrol_arlo_reset_redirects($regid = false) { $pluginconfig = $plugin->get_plugin_config(); $pluginconfig->set('redirectcount', 0); $sql .= " WHERE redirectcounter >= :maxretries"; - $params['maxretries'] = get_config('enrol_arlo', 'retriesperrecord'); + $params['maxretries'] = $pluginconfig->get('retriesperrecord'); } $registrations = $DB->get_records_sql($sql, $params); foreach ($registrations as $registration) { From 3e1ca36cfed079e3ae4c06572e98a71a011733da Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Mon, 8 Jul 2024 10:33:26 -0500 Subject: [PATCH 12/13] ARLO-50: Adding param to function call --- admin/apiretries.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/apiretries.php b/admin/apiretries.php index 2ef92c80d7e..eb0a4ebe84b 100644 --- a/admin/apiretries.php +++ b/admin/apiretries.php @@ -62,7 +62,7 @@ } if ($action === 'resetredirects') { - enrol_arlo_reset_redirects(); + enrol_arlo_reset_redirects($regid); echo $OUTPUT->notification(get_string('resetretries_message', 'enrol_arlo'),\core\output\notification::NOTIFY_SUCCESS); } From ad3b7114b5212e31c1da5b737d4fc3b343590b31 Mon Sep 17 00:00:00 2001 From: Oscar Nadjar Date: Mon, 8 Jul 2024 11:08:02 -0500 Subject: [PATCH 13/13] ARLO-50: Refactoring count management --- classes/local/job/outcomes_job.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/classes/local/job/outcomes_job.php b/classes/local/job/outcomes_job.php index 483a0a477ca..ea6d4e533a4 100644 --- a/classes/local/job/outcomes_job.php +++ b/classes/local/job/outcomes_job.php @@ -195,11 +195,12 @@ public function run() { // Now we count the errors 4xx and 5xx globally, but we don't increase the registration retry counter. $apistatus = $pluginconfig->get('apistatus'); if ($apistatus >= 300 && $apistatus <= 599) { - $pluginredirectcount = $pluginconfig->get('redirectcount'); + $pluginredirectcount = $pluginconfig->get('redirectcount') + 1; + $pluginconfig->set('redirectcount', $pluginredirectcount); $pluginmaxredirects = $pluginconfig->get('maxretries'); - $pluginconfig->set('redirectcount', ++$pluginredirectcount); - if ($apistatus >= 300 && $apistatus <= 399) { - $registrationpersistent->set('redirectcounter', ++$recordcounter); + if ($apistatus <= 399) { + $recordcounter++; + $registrationpersistent->set('redirectcounter', $recordcounter); if (!empty($maxrecordretries) && $recordcounter >= $maxrecordretries) { // Display retry error to admin on job page $this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2"); @@ -214,7 +215,7 @@ public function run() { } } // We have reached the maximum number of errors allowed. Disable communication. - if (!empty($pluginmaxredirects) && ++$pluginredirectcount >= $pluginmaxredirects) { + if (!empty($pluginmaxredirects) && $pluginredirectcount >= $pluginmaxredirects) { $pluginconfig->set('enablecommunication', 0); } } else {