Skip to main content

Internal AJAX Endpoint

MIH exposes a single internal AJAX endpoint at /local/integrationhub/ajax.php. This endpoint is used by the plugin's own JavaScript UI — it is not a public API.


Endpoint

GET|POST /local/integrationhub/ajax.php

Authentication

All requests require:

  • An active Moodle session (require_login())
  • The local/integrationhub:manage capability
  • A valid sesskey parameter (CSRF protection)

Actions

action=preview_payload

Previews a payload template with mock event data. Used by the "Preview Payload" button in the rule form.

Request Parameters

ParameterTypeRequiredDescription
actionstringYesMust be preview_payload
templatestringYesJSON template with {{variable}} placeholders
eventnamestringNoEvent name for context (used in mock data)
sesskeystringYesMoodle session key

Mock Data Used for Preview

$mockdata = [
'eventname' => $eventname ?: '\core\event\user_created',
'userid' => 5,
'objectid' => 42,
'courseid' => 10,
'contextid' => 1,
'contextlevel' => 50,
'timecreated' => time(),
'ip' => '192.168.1.100',
'crud' => 'c',
'edulevel' => 2,
];

Success Response

{
"success": true,
"payload": {
"event": "\\core\\event\\user_created",
"user_id": 5,
"timestamp": 1708258939
},
"raw": "{\"event\": \"\\\\core\\\\event\\\\user_created\", \"user_id\": 5, \"timestamp\": 1708258939}"
}
FieldTypeDescription
successboolAlways true for this response
payloadobjectThe decoded JSON payload (for display)
rawstringThe raw JSON string (for copy-paste)

Error Response (Invalid JSON Template)

{
"success": false,
"error": "Syntax error",
"raw": "{\"event\": \"{{eventname}\""
}
FieldTypeDescription
successboolAlways false for this response
errorstringJSON error description
rawstringThe raw (invalid) template string

JavaScript Usage

The AJAX endpoint is called from amd/src/rules.js:

// In rules.js
document.getElementById('ih-btn-preview').addEventListener('click', function() {
const template = document.getElementById('ih-template').value;
const eventname = document.getElementById('ih-eventname').value;

fetch(M.cfg.wwwroot + '/local/integrationhub/ajax.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
action: 'preview_payload',
template: template,
eventname: eventname,
sesskey: M.cfg.sesskey,
})
})
.then(r => r.json())
.then(data => {
if (data.success) {
// Show formatted payload in modal
} else {
// Show error message
}
});
});

Security Notes

  • The endpoint validates sesskey on every request to prevent CSRF attacks
  • The require_capability('local/integrationhub:manage', ...) check ensures only administrators can use it
  • Template preview uses mock data only — no real database queries or external calls are made
  • The endpoint does not accept or process any data that modifies the database

Extending the Endpoint

To add a new AJAX action, add a new case to the switch in ajax.php:

switch ($action) {
case 'preview_payload':
// ... existing code
break;

case 'my_new_action':
require_sesskey();
require_capability('local/integrationhub:manage', $context);
// ... your logic
echo json_encode(['success' => true, 'data' => $result]);
break;

default:
http_response_code(400);
echo json_encode(['error' => 'Unknown action']);
}