Skip to content
Merged
72 changes: 72 additions & 0 deletions local/telconfig/classes/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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;

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): 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');

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

if (empty($apiurl) || empty($apitoken)) {
debugging('send_findwise_api: API URL or token not set in plugin config.', DEBUG_DEVELOPER);
return;
}

$options = [
'http' => [
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
'timeout' => 5,
],
];

$context = stream_context_create($options);
try {
$response = @file_get_contents($apiurl, false, $context);
if ($response === false) {
debugging('send_findwise_api: Failed to send data to Findwise API.', DEBUG_DEVELOPER);
} else {
debugging('send_findwise_api: Data sent successfully to Findwise API.', DEBUG_DEVELOPER);
}
} catch (\Exception $e) {
debugging('send_findwise_api: Exception occurred while sending data: ' . $e->getMessage(), DEBUG_DEVELOPER);
}
}
}
94 changes: 94 additions & 0 deletions local/telconfig/classes/observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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);
}
}


/**
* Sends data to the configured external API endpoint.
*
* @param array $data
* @return void
*/
private static function send_external_api(array $data): void {
global $CFG;
require_once($CFG->libdir . '/filelib.php');

// Get plugin config values
$apiurl = get_config('local_telconfig', 'apiurl');
$apitoken = get_config('local_telconfig', 'apitoken');

if (empty($apiurl) || empty($apitoken)) {
debugging('API URL or Token not configured in local_telconfig.', DEBUG_DEVELOPER);
return;
}

$curl = new \curl();
$headers = [
"Authorization: Bearer {$apitoken}",
"Content-Type: application/json"
];

$options = [
'CURLOPT_HTTPHEADER' => $headers,
'timeout' => 5,
];

try {
$response = $curl->post($apiurl, json_encode($data), $options);
// Optional: log the response if debugging
if (debugging('', DEBUG_DEVELOPER)) {
debugging('Self enrol API response: ' . $response, DEBUG_DEVELOPER);
}
} catch (\Exception $e) {
debugging('Error sending to external API: ' . $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,
],
];
Loading