Skip to content

Commit

Permalink
MDL-78337 tool_brickfield: Validate registration in realtime adhoc task
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlkin authored and learningtechnologyservices committed Sep 30, 2024
1 parent ab291aa commit d0fc13d
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 3 deletions.
10 changes: 10 additions & 0 deletions admin/tool/brickfield/amd/build/registration.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 admin/tool/brickfield/amd/build/registration.min.js.map

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

70 changes: 70 additions & 0 deletions admin/tool/brickfield/amd/src/registration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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/>.

import Ajax from 'core/ajax';
import Template from 'core/templates';
import Log from 'core/log';

/**
* Handles polling registration status and replaces the alert block with the new one.
*
* @module tool_brickfield/registration
* @copyright 2024 onward Brickfield Education Labs Ltd, https://www.brickfield.ie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
export function init() {
Ajax.call([{
methodname: 'tool_brickfield_check_toolkit_validation',
args: {},
done: (data) => {
if (data.status == -1) {
// Status of -1 indicates the registration adhoc task is running.
setTimeout(() => {
init();
}, 2000);
} else {
replaceAlertBlock(data.message, data.level);
}
},
fail: (err) => {
Log.error(err);
}
}]);
}

/**
* Replaces the existing alert block with a new one.
* @param {string} message The notification message
* @param {string} level The notification level
*/
const replaceAlertBlock = (message, level) => {
Template.render('core/notification', {
message: message,
issuccess: level == 'success',
iswarning: level == 'warning',
iserror: level == 'error',
closebutton: true
}).then((html) => {
const oldAlert = document.querySelector('.alert-block');
let temp = document.createElement('div');
temp.innerHTML = html;
let newAlert = temp.firstChild;
oldAlert.parentNode.insertBefore(newAlert, oldAlert);
oldAlert.parentNode.removeChild(oldAlert);
return;
}).catch((err) => {
Log.error(err);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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 tool_brickfield\external;

use core\task\manager as taskmanager;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use tool_brickfield\registration;
use tool_brickfield\manager;

/**
* Service to check the status of the toolkit registrtion.
*
* @package tool_brickfield
* @copyright 2024 onward Brickfield Education Labs Ltd, https://www.brickfield.ie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class check_toolkit_validation extends \core_external\external_api {

/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function execute_parameters() {
return new external_function_parameters([]);
}

/**
* Check results of the adhoc task to check validation.
*
* @return bool
*/
public static function execute() {
$tasks = taskmanager::get_adhoc_tasks('\tool_brickfield\task\validate_registration', false, true);
if (empty($tasks)) {
$status = get_config('tool_brickfield', registration::STATUS);
if ($status == registration::NOT_ENTERED || $status == registration::EXPIRED || $status == registration::INVALID) {
$message = get_string('inactive', manager::PLUGINNAME);
$level = 'error';
} else if ($status == registration::PENDING) {
$message = get_string('notvalidated', manager::PLUGINNAME);
$level = 'warning';
} else if ($status == registration::VALIDATED) {
$message = get_string('activated', manager::PLUGINNAME);
$level = 'success';
} else if ($status == registration::ERROR) {
$message = get_string('validationerror', manager::PLUGINNAME);
$level = 'error';
}
return ['status' => $status, 'message' => $message, 'level' => $level];
} else {
return ['status' => -1]; // Indicate task is still in progress.
}
}

/**
* Returns description of method result value
* @return external_description
*/
public static function execute_returns() {
return new external_single_structure([
'status' => new external_value(PARAM_INT, 'Registration validation status'),
'message' => new external_value(PARAM_TEXT, 'Status message', VALUE_OPTIONAL),
'level' => new external_value(PARAM_TEXT, 'Status level', VALUE_OPTIONAL),
]);
}
}
23 changes: 23 additions & 0 deletions admin/tool/brickfield/classes/registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,29 @@ protected function get_status(): int {
return (int)$status;
}

/**
* Return the current registration status as a string.
*
* @return array The status, label, and notification level.
*/
public function get_status_display(): array {
$status = $this->get_status();
if ($status == self::NOT_ENTERED || $status == self::EXPIRED || $status == self::INVALID) {
$message = get_string('inactive', manager::PLUGINNAME);
$level = 'error';
} else if ($status == self::PENDING) {
$message = get_string('notvalidated', manager::PLUGINNAME);
$level = 'warning';
} else if ($status == self::VALIDATED) {
$message = get_string('activated', manager::PLUGINNAME);
$level = 'success';
} else if ($status == self::ERROR) {
$message = get_string('validationerror', manager::PLUGINNAME);
$level = 'error';
}
return ['status' => $status, 'message' => $message, 'level' => $level];
}

/**
* Set the time of the last registration check.
* @param int $time
Expand Down
34 changes: 34 additions & 0 deletions admin/tool/brickfield/classes/task/validate_registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 tool_brickfield\task;
use tool_brickfield\registration;

/**
* Adhoc task to validate registration details.
*
* @package tool_brickfield
* @copyright 2024 onward Brickfield Education Labs Ltd, https://www.brickfield.ie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class validate_registration extends \core\task\adhoc_task {
/**
* Execute the task
*/
public function execute() {
(new registration())->validate();
}
}
47 changes: 47 additions & 0 deletions admin/tool/brickfield/db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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/>.

/**
* External functions and service declaration for Accessibility toolkit
*
* Documentation: {@link https://moodledev.io/docs/apis/subsystems/external/description}
*
* @package tool_brickfield
* @category webservice
* @copyright 2024 onward Brickfield Education Labs Ltd, https://www.brickfield.ie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

$functions = [
'tool_brickfield_check_toolkit_validation' => [
'classname' => 'tool_brickfield\external\check_toolkit_validation',
'methodname' => 'execute',
'classpath' => 'classes/external/check_toolkit_validation.php',
'description' => 'Service to check the status of the toolkit registration.',
'type' => 'read',
'ajax' => true,
],
];

$services = [
'Accessibility toolkit' => [
'functions' => ['tool_brickfield_check_toolkit_validation'],
'restrictedusers' => 0,
'enabled' => 1,
],
];
2 changes: 1 addition & 1 deletion admin/tool/brickfield/lang/en/tool_brickfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
$string['noerrorsfound'] = 'No common accessibility errors were found for your search parameters. Congratulations!';
$string['norecords'] = 'No relevant records were found for your search parameters.';
$string['notregistered'] = 'Your accessibility toolkit needs to be registered.';
$string['notvalidated'] = 'Your accessibility toolkit is functional while being validated.';
$string['notvalidated'] = 'Your accessibility toolkit is functional while being validated. {$a}';
$string['numinstances'] = 'Number of instances';
$string['pagedesc:checktype'] = '<p>In order to summarise and analyse the results of the various checks conducted, we group these checks into different content types. Hence, all image-related accessibility check results are in the "Image" content type group, all layout-related accessibility check results are in the "Layout" content type group, and so on.</p><p>Activities are included as either activities, resources or content areas relating to the courses themselves.</p><p>The content type chart page displays the error breakdown per content type group: Image, Layout, Link, Media, Table, and Text.</p>';
$string['pagedesc:pertarget'] = '<p>In order to summarise and analyse the check results per activity, we group these check results into the different activities detected.</p><p>Activities are included as either activities, resources, or other content areas relating to the courses themselves. Each activity with no detected errors is counted as passed, each activity with one or more detected errors is counted as failed. The ratio of passed to failed activities is then displayed.</p><p>The activity breakdown chart page displays the ratio of passed to failed instances in total, per activity, such as assignment, course, label etc.</p>';
Expand Down
11 changes: 10 additions & 1 deletion admin/tool/brickfield/registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@
if ($registration->validation_error()) {
echo $OUTPUT->notification(get_string('validationerror', manager::PLUGINNAME), 'error');
} else {
echo $OUTPUT->notification(get_string('notvalidated', manager::PLUGINNAME), 'warning');
echo $OUTPUT->notification(
get_string('notvalidated',
manager::PLUGINNAME,
$OUTPUT->render_from_template('core/loading', [])),
'warning'
);
// Start validation task and update page with JS.
$PAGE->requires->js_call_amd('tool_brickfield/registration', 'init');
$task = new \tool_brickfield\task\validate_registration();
\core\task\manager::queue_adhoc_task($task, true);
}
} else {
echo $OUTPUT->notification(get_string('activated', manager::PLUGINNAME), 'success');
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/brickfield/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'tool_brickfield';
$plugin->version = 2023100900;
$plugin->version = 2023100901;
$plugin->requires = 2023100400;

0 comments on commit d0fc13d

Please sign in to comment.