. /** * External: activate / recheck / deactivate MeetLet site registration. * * @package mod_meetletacbfree * @copyright 2026 MeetLet * @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 */ 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'), ]); } }