MDL-69166 core_payment: Show cost on the modal when no gateway is chosen
[moodle.git] / payment / classes / account.php
CommitLineData
895f38cc
MG
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.
13//
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/>.
16
17/**
18 * Class account
19 *
20 * @package core_payment
21 * @copyright 2020 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_payment;
26
27use core\persistent;
28
29/**
30 * Class account
31 *
32 * @package core_payment
33 * @copyright 2020 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class account extends persistent {
37 /**
38 * Database table.
39 */
40 const TABLE = 'payment_accounts';
41
42 /** @var array */
43 protected $gateways;
44
45 /**
46 * Return the definition of the properties of this model.
47 *
48 * @return array
49 */
50 protected static function define_properties() : array {
51 return array(
52 'name' => [
53 'type' => PARAM_TEXT,
54 ],
55 'idnumber' => [
56 'type' => PARAM_RAW_TRIMMED,
57 ],
58 'contextid' => [
59 'type' => PARAM_INT,
60 'default' => function() {
61 return \context_system::instance()->id;
62 }
63 ],
64 'enabled' => [
65 'type' => PARAM_BOOL,
66 'default' => true
67 ],
68 );
69 }
70
71 /**
72 * Account context
73 *
74 * @return \context
75 * @throws \coding_exception
76 */
77 public function get_context(): \context {
78 return \context::instance_by_id($this->get('contextid'));
79 }
80
81 /**
82 * Account name ready for display
83 *
84 * @return string
85 * @throws \coding_exception
86 */
87 public function get_formatted_name(): string {
88 return format_string($this->get('name'), true, ['context' => $this->get_context(), 'escape' => false]);
89 }
90
91 /**
92 * Manage account url
93 *
94 * @param array $extraparams
95 * @return \moodle_url
96 * @throws \coding_exception
97 * @throws \moodle_exception
98 */
99 public function get_edit_url(array $extraparams = []): \moodle_url {
100 return new \moodle_url('/payment/manage_account.php',
101 ($this->get('id') ? ['id' => $this->get('id')] : []) + $extraparams);
102 }
103
104 /**
105 * List of gateways configured (or possible) for this account
106 *
107 * @param bool $enabledpluginsonly only return payment plugins that are enabled
108 * @return account_gateway[]
109 * @throws \coding_exception
110 */
111 public function get_gateways(bool $enabledpluginsonly = true): array {
112 $id = $this->get('id');
113 if (!$id) {
114 return [];
115 }
116 if ($this->gateways === null) {
117 \core_component::get_plugin_list('pg');
118 $this->gateways = [];
119 foreach (\core_component::get_plugin_list('pg') as $gatewayname => $unused) {
120 $gateway = account_gateway::get_record(['accountid' => $id, 'gateway' => $gatewayname]);
121 if (!$gateway) {
122 $gateway = new account_gateway(0, (object)['accountid' => $id, 'gateway' => $gatewayname,
123 'enabled' => false, 'config' => null]);
124 }
125 $this->gateways[$gatewayname] = $gateway;
126 }
127 }
128 if ($enabledpluginsonly) {
129 $enabledplugins = \core\plugininfo\pg::get_enabled_plugins();
130 return array_intersect_key($this->gateways, $enabledplugins);
131 }
132 return $this->gateways;
133 }
134
135 /**
136 * Is this account available (used in management interface)
137 *
138 * @return bool
139 * @throws \coding_exception
140 */
141 public function is_available(): bool {
142 if (!$this->get('id') || !$this->get('enabled')) {
143 return false;
144 }
145 foreach ($this->get_gateways() as $gateway) {
146 if ($gateway->get('id') && $gateway->get('enabled')) {
147 return true;
148 }
149 }
150 return false;
151 }
152}