Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  
  25  namespace core_payment;
  26  
  27  use 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   */
  36  class 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              'archived' => [
  69                  'type' => PARAM_BOOL,
  70                  'default' => false
  71              ],
  72          );
  73      }
  74  
  75      /**
  76       * Account context
  77       *
  78       * @return \context
  79       * @throws \coding_exception
  80       */
  81      public function get_context(): \context {
  82          return \context::instance_by_id($this->get('contextid'));
  83      }
  84  
  85      /**
  86       * Account name ready for display
  87       *
  88       * @return string
  89       * @throws \coding_exception
  90       */
  91      public function get_formatted_name(): string {
  92          return format_string($this->get('name'), true, ['context' => $this->get_context(), 'escape' => false]);
  93      }
  94  
  95      /**
  96       * Manage account url
  97       *
  98       * @param array $extraparams
  99       * @return \moodle_url
 100       * @throws \coding_exception
 101       * @throws \moodle_exception
 102       */
 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);
 106      }
 107  
 108      /**
 109       * List of gateways configured (or possible) for this account
 110       *
 111       * @param bool $enabledpluginsonly only return payment plugins that are enabled
 112       * @return account_gateway[]
 113       * @throws \coding_exception
 114       */
 115      public function get_gateways(bool $enabledpluginsonly = true): array {
 116          $id = $this->get('id');
 117          if (!$id) {
 118              return [];
 119          }
 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]);
 128                  }
 129                  $this->gateways[$gatewayname] = $gateway;
 130              }
 131          }
 132          if ($enabledpluginsonly) {
 133              $enabledplugins = \core\plugininfo\paygw::get_enabled_plugins();
 134              return array_intersect_key($this->gateways, $enabledplugins);
 135          }
 136          return $this->gateways;
 137      }
 138  
 139      /**
 140       * Is this account available (used in management interface)
 141       *
 142       * @return bool
 143       * @throws \coding_exception
 144       */
 145      public function is_available(): bool {
 146          if (!$this->get('id') || !$this->get('enabled')) {
 147              return false;
 148          }
 149          foreach ($this->get_gateways() as $gateway) {
 150              if ($gateway->get('id') && $gateway->get('enabled')) {
 151                  return true;
 152              }
 153          }
 154          return false;
 155      }
 156  }