2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @package core_payment
21 * @copyright 2020 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_payment
;
32 * @package core_payment
33 * @copyright 2020 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class account
extends persistent
{
40 const TABLE
= 'payment_accounts';
46 * Return the definition of the properties of this model.
50 protected static function define_properties(): array {
56 'type' => PARAM_RAW_TRIMMED
,
60 'default' => function() {
61 return \context_system
::instance()->id
;
79 * @throws \coding_exception
81 public function get_context(): \context
{
82 return \context
::instance_by_id($this->get('contextid'));
86 * Account name ready for display
89 * @throws \coding_exception
91 public function get_formatted_name(): string {
92 return format_string($this->get('name'), true, ['context' => $this->get_context(), 'escape' => false]);
98 * @param array $extraparams
100 * @throws \coding_exception
101 * @throws \moodle_exception
103 public function get_edit_url(array $extraparams = []): \moodle_url
{
104 return new \
moodle_url('/payment/manage_account.php',
105 ($this->get('id') ?
['id' => $this->get('id')] : []) +
$extraparams);
109 * List of gateways configured (or possible) for this account
111 * @param bool $enabledpluginsonly only return payment plugins that are enabled
112 * @return account_gateway[]
113 * @throws \coding_exception
115 public function get_gateways(bool $enabledpluginsonly = true): array {
116 $id = $this->get('id');
120 if ($this->gateways
=== null) {
121 \core_component
::get_plugin_list('paygw');
122 $this->gateways
= [];
123 foreach (\core_component
::get_plugin_list('paygw') as $gatewayname => $unused) {
124 $gateway = account_gateway
::get_record(['accountid' => $id, 'gateway' => $gatewayname]);
126 $gateway = new account_gateway(0, (object)['accountid' => $id, 'gateway' => $gatewayname,
127 'enabled' => false, 'config' => null]);
129 $this->gateways
[$gatewayname] = $gateway;
132 if ($enabledpluginsonly) {
133 $enabledplugins = \core\plugininfo\paygw
::get_enabled_plugins();
134 return array_intersect_key($this->gateways
, $enabledplugins);
136 return $this->gateways
;
140 * Is this account available (used in management interface)
143 * @throws \coding_exception
145 public function is_available(): bool {
146 if (!$this->get('id') ||
!$this->get('enabled')) {
149 foreach ($this->get_gateways() as $gateway) {
150 if ($gateway->get('id') && $gateway->get('enabled')) {