catalyst / moodle-tool_webanalytics

A Moodle admin tool adding Web Analytics to your Moodle site.
https://moodle.org/plugins/tool_webanalytics
7 stars 11 forks source link

Add auto-provisioning for matomo #52

Open SimonasAdams opened 1 year ago

SimonasAdams commented 1 year ago

This commit contains the ability to auto provision sites based on either a value in the config.php file or via the instance form on the front end.

To auto provision on install or upgrade add a setting watool_matomo into config.php as an array, e.g:

$CFG->watool_matomo = [
        'instance1' => [        
                'apitoken' => 'abcdef123456',
                'siteurl' => 'https://example1.com'
        ],
        'instance2' => [
                'apitoken' => 'abcdef123456',
                'siteurl' => 'https://example2.com'
        ],
];

Where the apitoken is the auth_token of the user that is allowed to do API requests on the Matomo server, and the 'siteurl' is the URL of the Matomo server instance.

During install if the above setting is present it will loop through the array and attempt to create a site on the remote Matomo instance per array item. If successful, it create new records locally and stores the siteid, the apitoken, and the siteurl per instance created.

When manually adding a new instance via the front end, an auto provision attempt will be made if the form submission contains both the siteurl and the apitoken. If successful, the siteid is retrieved and stored against the local record. If the form is submitted with a siteid, there will be not attempt to register with the remote server.

Additionally, at the point of submisision the current CFG->wwwroot is stored against that instance locally. On page loads that check if the tag should be output, a check is made to see if the current CFG->wwwroot matches that stored against the local record. If it does not match, an attempt is made to update the remotely registered instance with the new url. If successful, this new url is then stored locally. This is to allow the remote to track if and when the url of the server changes.