MDL-81337 behat: Fix behat failures
[moodle.git] / payment / classes / account.php
blob4e1e240475fc179e0de15819761962a1595cf732
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Class account
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;
27 use core\persistent;
29 /**
30 * Class account
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 {
37 /**
38 * Database table.
40 const TABLE = 'payment_accounts';
42 /** @var array */
43 protected $gateways;
45 /**
46 * Return the definition of the properties of this model.
48 * @return array
50 protected static function define_properties(): array {
51 return array(
52 'name' => [
53 'type' => PARAM_TEXT,
55 'idnumber' => [
56 'type' => PARAM_RAW_TRIMMED,
58 'contextid' => [
59 'type' => PARAM_INT,
60 'default' => function() {
61 return \context_system::instance()->id;
64 'enabled' => [
65 'type' => PARAM_BOOL,
66 'default' => true
68 'archived' => [
69 'type' => PARAM_BOOL,
70 'default' => false
75 /**
76 * Account context
78 * @return \context
79 * @throws \coding_exception
81 public function get_context(): \context {
82 return \context::instance_by_id($this->get('contextid'));
85 /**
86 * Account name ready for display
88 * @return string
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]);
95 /**
96 * Manage account url
98 * @param array $extraparams
99 * @return \moodle_url
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');
117 if (!$id) {
118 return [];
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]);
125 if (!$gateway) {
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)
142 * @return bool
143 * @throws \coding_exception
145 public function is_available(): bool {
146 if (!$this->get('id') || !$this->get('enabled')) {
147 return false;
149 foreach ($this->get_gateways() as $gateway) {
150 if ($gateway->get('id') && $gateway->get('enabled')) {
151 return true;
154 return false;