Files
meetletacbfree/classes/external/registration_action.php
T
mrxzamani be252e49e2 Address Moodle Marketplace review for mod_meetletacbfree 1.1.3.
Migrate legacy ajax/JS to external services and AMD, enable TLS verification, scope admin CSS with RTLCSS-safe rules, and fix README/lang/boilerplate gaps.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-20 12:41:47 +03:30

125 lines
5.3 KiB
PHP

<?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: activate / recheck / deactivate MeetLet site registration.
*
* @package mod_meetletacbfree
* @copyright 2026 MeetLet <info@meetlet.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_meetletacbfree\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use mod_meetletacbfree\registration\error_mapper;
use mod_meetletacbfree\registration\manager;
use mod_meetletacbfree\registration\registration_exception;
defined('MOODLE_INTERNAL') || die();
/**
* Ajax-capable registration actions for admin settings.
*/
class registration_action extends external_api {
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'action' => new external_value(PARAM_ALPHA, 'activate|recheck|deactivate|status'),
'registration_api_url' => new external_value(PARAM_URL, 'Optional API base URL', VALUE_DEFAULT, ''),
]);
}
public static function execute(string $action, string $registrationapiurl = ''): array {
$params = self::validate_parameters(self::execute_parameters(), [
'action' => $action,
'registration_api_url' => $registrationapiurl,
]);
$context = \context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
try {
if ($params['registration_api_url'] !== '') {
set_config(manager::CONFIG_API_URL, rtrim($params['registration_api_url'], '/'), 'meetletacbfree');
}
$message = '';
if ($params['action'] === 'activate') {
manager::activate();
$message = get_string('registration_activated', 'meetletacbfree');
} else if (in_array($params['action'], ['recheck', 'refresh', 'validate'], true)) {
manager::recheck(true);
$message = get_string('registration_rechecked', 'meetletacbfree');
} else if ($params['action'] === 'deactivate') {
manager::deactivate();
$message = get_string('registration_deactivated', 'meetletacbfree');
} else if ($params['action'] === 'status') {
$message = '';
} else {
throw new \moodle_exception('invalidparameter');
}
return self::pack_response(true, $message, '');
} catch (\Throwable $e) {
$ex = registration_exception::from_throwable($e);
$mapped = error_mapper::from_exception($ex);
return self::pack_response(
false,
'',
(string)($mapped['message'] ?? $ex->get_api_message())
);
}
}
/**
* @return array<string,mixed>
*/
protected static function pack_response(bool $success, string $message, string $error): array {
$summary = manager::status_summary();
return [
'success' => $success,
'message' => $message,
'error' => $error,
'active' => !empty($summary['active']),
'status' => (string)($summary['status'] ?? ''),
'status_label' => (string)($summary['status_label'] ?? ''),
'product_label' => (string)($summary['product_label'] ?? ''),
'domain' => (string)($summary['domain'] ?? ''),
'installation_id' => (string)($summary['installation_id'] ?? ''),
'can_deactivate' => !empty($summary['can_deactivate']),
];
}
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'success' => new external_value(PARAM_BOOL, 'Whether the action succeeded'),
'message' => new external_value(PARAM_TEXT, 'Success message'),
'error' => new external_value(PARAM_TEXT, 'Error message when unsuccessful'),
'active' => new external_value(PARAM_BOOL, 'Whether activation is active'),
'status' => new external_value(PARAM_ALPHANUMEXT, 'Raw status code'),
'status_label' => new external_value(PARAM_TEXT, 'Human status label'),
'product_label' => new external_value(PARAM_TEXT, 'Product label'),
'domain' => new external_value(PARAM_TEXT, 'Site domain'),
'installation_id' => new external_value(PARAM_TEXT, 'Installation id'),
'can_deactivate' => new external_value(PARAM_BOOL, 'Whether deactivate is allowed'),
]);
}
}