From eeae69d999a3dd9a414b77ee698ac80b50c7b844 Mon Sep 17 00:00:00 2001 From: Davor Budimir Date: Sat, 21 Sep 2024 17:53:05 +0200 Subject: [PATCH] revert code back to its original form + add trigger event --- classes/payment/service_provider.php | 7 ++++++- lib.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/classes/payment/service_provider.php b/classes/payment/service_provider.php index 1fb14980..8594e0cc 100644 --- a/classes/payment/service_provider.php +++ b/classes/payment/service_provider.php @@ -160,7 +160,12 @@ public static function deliver_order(string $paymentarea, int $itemid, int $paym } else { $response = $op->credit($item->cost, op::C_PAYMENT, $itemid, $desc); - return $response; + + if ($response) { + return true; + } else { + return false; + } } } } diff --git a/lib.php b/lib.php index dfca9fad..bd59382c 100644 --- a/lib.php +++ b/lib.php @@ -440,6 +440,22 @@ public function enrol_self(stdClass $instance, $user = null, $charge = true) { do { $this->enrol_user($instance, $user->id, $instance->roleid, $timestart, $timeend, null, true); } while (!$DB->record_exists('user_enrolments', $conditions)); + + try { + // Trigger event for successful enrolment. + $params = [ + 'context' => context_course::instance($instance->courseid), + 'objectid' => $instance->id, + 'courseid' => $instance->courseid, + 'relateduserid' => $user->id, + ]; + $event = \core\event\user_enrolment_created::create($params); + $event->trigger(); + } catch (\Exception $e) { + // Log the error but don't stop the enrolment process + debugging('Error triggering enrolment event: ' . $e->getMessage(), DEBUG_DEVELOPER); + } + } catch (\moodle_exception $e) { // Rollback the transaction in case of error. if ($charge) {