Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  declare(strict_types=1);
  18  
  19  namespace core_badges\reportbuilder\local\entities;
  20  
  21  use lang_string;
  22  use core_reportbuilder\local\entities\base;
  23  use core_reportbuilder\local\filters\{boolean_select, date};
  24  use core_reportbuilder\local\helpers\format;
  25  use core_reportbuilder\local\report\{column, filter};
  26  
  27  /**
  28   * Badge issued entity
  29   *
  30   * @package     core_badges
  31   * @copyright   2022 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class badge_issued extends base {
  35  
  36      /**
  37       * Database tables that this entity uses and their default aliases
  38       *
  39       * @return array
  40       */
  41      protected function get_default_table_aliases(): array {
  42          return ['badge_issued' => 'bi'];
  43      }
  44  
  45      /**
  46       * The default title for this entity
  47       *
  48       * @return lang_string
  49       */
  50      protected function get_default_entity_title(): lang_string {
  51          return new lang_string('badgeissued', 'core_badges');
  52      }
  53  
  54      /**
  55       * Initialise the entity
  56       *
  57       * @return base
  58       */
  59      public function initialise(): base {
  60          $columns = $this->get_all_columns();
  61          foreach ($columns as $column) {
  62              $this->add_column($column);
  63          }
  64  
  65          // All the filters defined by the entity can also be used as conditions.
  66          $filters = $this->get_all_filters();
  67          foreach ($filters as $filter) {
  68              $this
  69                  ->add_filter($filter)
  70                  ->add_condition($filter);
  71          }
  72  
  73          return $this;
  74      }
  75  
  76      /**
  77       * Returns list of all available columns
  78       *
  79       * @return column[]
  80       */
  81      protected function get_all_columns(): array {
  82          $badgeissuedalias = $this->get_table_alias('badge_issued');
  83  
  84          // Date issued.
  85          $columns[] = (new column(
  86              'issued',
  87              new lang_string('dateawarded', 'core_badges'),
  88              $this->get_entity_name()
  89          ))
  90              ->add_joins($this->get_joins())
  91              ->set_type(column::TYPE_TIMESTAMP)
  92              ->add_field("{$badgeissuedalias}.dateissued")
  93              ->set_is_sortable(true)
  94              ->add_callback([format::class, 'userdate']);
  95  
  96          // Date expires.
  97          $columns[] = (new column(
  98              'expire',
  99              new lang_string('expirydate', 'core_badges'),
 100              $this->get_entity_name()
 101          ))
 102              ->add_joins($this->get_joins())
 103              ->set_type(column::TYPE_TIMESTAMP)
 104              ->add_field("{$badgeissuedalias}.dateexpire")
 105              ->set_is_sortable(true)
 106              ->add_callback([format::class, 'userdate']);
 107  
 108          // Visible.
 109          $columns[] = (new column(
 110              'visible',
 111              new lang_string('visible', 'core_badges'),
 112              $this->get_entity_name()
 113          ))
 114              ->add_joins($this->get_joins())
 115              ->set_type(column::TYPE_BOOLEAN)
 116              ->add_fields("{$badgeissuedalias}.visible")
 117              ->add_callback([format::class, 'boolean_as_text']);
 118  
 119          return $columns;
 120      }
 121  
 122      /**
 123       * Return list of all available filters
 124       *
 125       * @return filter[]
 126       */
 127      protected function get_all_filters(): array {
 128          $badgealias = $this->get_table_alias('badge_issued');
 129  
 130          // Date issued.
 131          $filters[] = (new filter(
 132              date::class,
 133              'issued',
 134              new lang_string('dateawarded', 'core_badges'),
 135              $this->get_entity_name(),
 136              "{$badgealias}.dateissued"
 137          ))
 138              ->add_joins($this->get_joins());
 139  
 140          // Date expires.
 141          $filters[] = (new filter(
 142              date::class,
 143              'expires',
 144              new lang_string('expirydate', 'core_badges'),
 145              $this->get_entity_name(),
 146              "{$badgealias}.dateexpire"
 147          ))
 148              ->add_joins($this->get_joins());
 149  
 150          // Visible.
 151          $filters[] = (new filter(
 152              boolean_select::class,
 153              'visible',
 154              new lang_string('visible', 'core_badges'),
 155              $this->get_entity_name(),
 156              "{$badgealias}.visible"
 157          ))
 158              ->add_joins($this->get_joins());
 159  
 160          return $filters;
 161      }
 162  }