Skip to content
Merged
20 changes: 20 additions & 0 deletions local/telconfig/classes/api_client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace local_telconfig;
// This file is part of Moodle - http://moodle.org/
// Moodle is free software: you can redistribute it and/or modify

class api_client {
public function post(string $url, array $data): string|false {
$options = [
'http' => [
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
'timeout' => 5,
]
];
$context = stream_context_create($options);
return @file_get_contents($url, false, $context);
}
}
29 changes: 29 additions & 0 deletions local/telconfig/classes/config_exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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/>.

/**
* Member does not exist exception.
*
* @package local_telconfig
*/

namespace local_telconfig;

class config_exception extends \moodle_exception {
public function __construct($message = 'Missing API config for Findwise') {
parent::__construct('error', 'local_telconfig', '', null, $message);
}
}
63 changes: 63 additions & 0 deletions local/telconfig/classes/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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/>.

/**
* Helper class locally used.
*
* @package local_telconfig
* @copyright
* @license
*/

namespace local_telconfig;
use local_telconfig\api_client;

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

class helper {

/**
* Sends structured data to an external API endpoint.
*
* @param array $data
* @return void
*/
public static function send_findwise_api(array $data, ?api_client $client = null): void {
$indexurl = get_config('local_telconfig', 'findwiseindexurl');
$indexmethod = get_config('local_telconfig', 'findwiseindexmethod');
$collection = get_config('local_telconfig', 'findwisecollection');
$apitoken = get_config('local_telconfig', 'findwiseapitoken');

if (empty($indexurl) || empty($apitoken)) {
return;
}

$indexurl = rtrim($indexurl, '/') . '/' . $indexmethod . '?token=' . urlencode($apitoken);
$apiurl = str_replace('{0}', $collection, $indexurl);

$client ??= new api_client();

try {
$response = $client->post($apiurl, $data);

if ($response === false) {
debugging('send_findwise_api: Failed to send data to findwise API.', DEBUG_DEVELOPER);
}
} catch (\Exception $e) {
debugging('send_findwise_api: Exception occurred while sending data: ' . $e->getMessage(), DEBUG_DEVELOPER);
}
}
}
52 changes: 52 additions & 0 deletions local/telconfig/classes/observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace local_telconfig;
use local_telconfig\helper;
// This file is part of Moodle - http://moodle.org/
// Moodle is free software: you can redistribute it and/or modify

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

class observer {

/**
* Triggered when a self enrolment instance is created, updated, or deleted.
*
* @param \core\event\base $event
* @return void
*/
public static function enrol_instance_changed(\core\event\base $event): void {
global $DB;

// Only act if it's for 'self' enrolment.
if (!isset($event->other['enrol']) || $event->other['enrol'] !== 'self') {
return;
}

try {
// Get enrol instance
$enrol = $DB->get_record('enrol', ['id' => $event->objectid], '*', MUST_EXIST);

// Get course info
$course = $DB->get_record('course', ['id' => $event->courseid], '*', MUST_EXIST);

$data = [
'_id' => $course->id,
'event' => $event->eventname,
'enrolid' => $enrol->id,
'courseid' => $course->id,
'coursename' => $course->fullname,
'shortname' => $course->shortname,
'summary' => $course->summary,
'startdate' => $course->startdate,
'enddate' => $course->enddate,
'enrolstatus' => $enrol->status, // 0 = enabled, 1 = disabled
'time' => time()
];

helper::send_findwise_api($data);
} catch (\dml_exception $e) {
debugging("Failed to fetch course/enrol data: " . $e->getMessage(), DEBUG_DEVELOPER);
}
}
}
10 changes: 10 additions & 0 deletions local/telconfig/db/events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$observers = [
[
'eventname' => '\core\event\enrol_instance_updated',
'callback' => '\local_telconfig\observer::enrol_instance_changed',
'priority' => 9999,
'internal' => false,
],
];
46 changes: 46 additions & 0 deletions local/telconfig/tests/helper_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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/>.

use advanced_testcase;
use local_telconfig\helper;
use local_telconfig\api_client;

class telconfig_helper_test extends advanced_testcase {

public function test_send_findwise_api_makes_api_call() {
define('LOCAL_TELCONFIG_DEV_TEST', true); // Flag to enable strict config check
$this->resetAfterTest();

// Set fake plugin config values.
set_config('findwiseindexurl', 'http://fake.local/api', 'local_telconfig');
set_config('findwiseindexmethod', 'index', 'local_telconfig');
set_config('findwisecollection', 'courses', 'local_telconfig');
set_config('findwiseapitoken', 'faketoken', 'local_telconfig');

// Create a mock API client.
$mock = $this->createMock(api_client::class);
$mock->expects($this->once())
->method('post')
->with(
$this->stringContains('http://fake.local/api/index?token='),
$this->equalTo(['courseid' => 123])
)
->willReturn('{"status":"ok"}');

// Call the method with the mock client.
helper::send_findwise_api(['courseid' => 123], $mock);
}
}
Loading