Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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_course\reportbuilder\local\entities;
  20  
  21  use core_reportbuilder\local\entities\base;
  22  use core_reportbuilder\local\filters\date;
  23  use core_reportbuilder\local\helpers\format;
  24  use core_reportbuilder\local\report\column;
  25  use core_reportbuilder\local\report\filter;
  26  use lang_string;
  27  use stdClass;
  28  
  29  /**
  30   * Course access entity implementation
  31   *
  32   * @package     core_course
  33   * @copyright   2022 David Matamoros <davidmc@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class access extends base {
  37  
  38      /**
  39       * Database tables that this entity uses and their default aliases
  40       *
  41       * @return array
  42       */
  43      protected function get_default_table_aliases(): array {
  44          return ['user_lastaccess' => 'ula', 'user' => 'u'];
  45      }
  46  
  47      /**
  48       * The default title for this entity in the list of columns/conditions/filters in the report builder
  49       *
  50       * @return lang_string
  51       */
  52      protected function get_default_entity_title(): lang_string {
  53          return new lang_string('courseaccess', 'course');
  54      }
  55  
  56      /**
  57       * Initialise the entity
  58       *
  59       * @return base
  60       */
  61      public function initialise(): base {
  62          foreach ($this->get_all_columns() as $column) {
  63              $this->add_column($column);
  64          }
  65  
  66          // All the filters defined by the entity can also be used as conditions.
  67          foreach ($this->get_all_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          $tablealias = $this->get_table_alias('user_lastaccess');
  83          $user = $this->get_table_alias('user');
  84  
  85          // Last course access column.
  86          $columns[] = (new column(
  87              'timeaccess',
  88              new lang_string('lastcourseaccess', 'moodle'),
  89              $this->get_entity_name()
  90          ))
  91              ->add_joins($this->get_joins())
  92              ->set_type(column::TYPE_TIMESTAMP)
  93              ->add_field("{$tablealias}.timeaccess")
  94              ->add_field("{$user}.id", 'userid')
  95              ->set_is_sortable(true)
  96              ->add_callback([format::class, 'userdate'])
  97              ->add_callback(static function(string $value, stdClass $row): string {
  98                  if (!$row->userid) {
  99                      return '';
 100                  }
 101                  if ($value === '') {
 102                      return get_string('never');
 103                  }
 104                  return $value;
 105              });
 106  
 107          return $columns;
 108      }
 109  
 110      /**
 111       * Return list of all available filters
 112       *
 113       * @return filter[]
 114       */
 115      protected function get_all_filters(): array {
 116          $tablealias = $this->get_table_alias('user_lastaccess');
 117  
 118          // Last course access filter.
 119          $filters[] = (new filter(
 120              date::class,
 121              'timeaccess',
 122              new lang_string('lastcourseaccess', 'moodle'),
 123              $this->get_entity_name(),
 124              "{$tablealias}.timeaccess"
 125          ))
 126              ->add_joins($this->get_joins())
 127              ->set_limited_operators([
 128                  date::DATE_ANY,
 129                  date::DATE_NOT_EMPTY,
 130                  date::DATE_EMPTY,
 131                  date::DATE_RANGE,
 132                  date::DATE_LAST,
 133                  date::DATE_CURRENT,
 134              ]);
 135  
 136          return $filters;
 137      }
 138  }