Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  namespace core_admin\local\systemreports;
  18  
  19  use context_system;
  20  use core_admin\local\entities\task_log;
  21  use core_reportbuilder\local\entities\user;
  22  use core_reportbuilder\local\report\action;
  23  use lang_string;
  24  use moodle_url;
  25  use pix_icon;
  26  use core_reportbuilder\system_report;
  27  
  28  /**
  29   * Task logs system report class implementation
  30   *
  31   * @package    core_admin
  32   * @copyright  2021 David Matamoros <davidmc@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class task_logs extends system_report {
  36  
  37      /**
  38       * Initialise report, we need to set the main table, load our entities and set columns/filters
  39       */
  40      protected function initialise(): void {
  41          // Our main entity, it contains all of the column definitions that we need.
  42          $entitymain = new task_log();
  43          $entitymainalias = $entitymain->get_table_alias('task_log');
  44  
  45          $this->set_main_table('task_log', $entitymainalias);
  46          $this->add_entity($entitymain);
  47  
  48          // Any columns required by actions should be defined here to ensure they're always available.
  49          $this->add_base_fields("{$entitymainalias}.id");
  50  
  51          // We can join the "user" entity to our "main" entity and use the fullname column from the user entity.
  52          $entityuser = new user();
  53          $entituseralias = $entityuser->get_table_alias('user');
  54          $this->add_entity($entityuser->add_join(
  55              "LEFT JOIN {user} {$entituseralias} ON {$entituseralias}.id = {$entitymainalias}.userid"
  56          ));
  57  
  58          // Now we can call our helper methods to add the content we want to include in the report.
  59          $this->add_columns();
  60          $this->add_filters();
  61          $this->add_actions();
  62  
  63          // Set if report can be downloaded.
  64          $this->set_downloadable(true, get_string('tasklogs', 'admin'));
  65      }
  66  
  67      /**
  68       * Validates access to view this report
  69       *
  70       * @return bool
  71       */
  72      protected function can_view(): bool {
  73          return has_capability('moodle/site:config', context_system::instance());
  74      }
  75  
  76      /**
  77       * Get the visible name of the report
  78       *
  79       * @return string
  80       */
  81      public static function get_name(): string {
  82          return get_string('entitytasklog', 'admin');
  83      }
  84  
  85      /**
  86       * Adds the columns we want to display in the report
  87       *
  88       * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
  89       * unique identifier
  90       */
  91      public function add_columns(): void {
  92          $columns = [
  93              'task_log:name',
  94              'task_log:type',
  95              'user:fullname',
  96              'task_log:starttime',
  97              'task_log:duration',
  98              'task_log:hostname',
  99              'task_log:pid',
 100              'task_log:database',
 101              'task_log:result',
 102          ];
 103  
 104          $this->add_columns_from_entities($columns);
 105  
 106          // It's possible to override the display name of a column, if you don't want to use the value provided by the entity.
 107          if ($column = $this->get_column('user:fullname')) {
 108              $column->set_title(new lang_string('user', 'admin'));
 109          }
 110  
 111          // It's possible to set a default initial sort direction for one column.
 112          $this->set_initial_sort_column('task_log:starttime', SORT_DESC);
 113      }
 114  
 115      /**
 116       * Adds the filters we want to display in the report
 117       *
 118       * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
 119       * unique identifier
 120       */
 121      protected function add_filters(): void {
 122          $filters = [
 123              'task_log:name',
 124              'task_log:output',
 125              'task_log:result',
 126              'task_log:timestart',
 127              'task_log:duration',
 128          ];
 129  
 130          $this->add_filters_from_entities($filters);
 131      }
 132  
 133      /**
 134       * Add the system report actions. An extra column will be appended to each row, containing all actions added here
 135       *
 136       * Note the use of ":id" placeholder which will be substituted according to actual values in the row
 137       */
 138      protected function add_actions(): void {
 139  
 140          // Action to view individual task log on a popup window.
 141          $this->add_action((new action(
 142              new moodle_url('/admin/tasklogs.php', ['logid' => ':id']),
 143              new pix_icon('e/search', ''),
 144              [],
 145              true,
 146              new lang_string('view'),
 147          )));
 148  
 149          // Action to download individual task log.
 150          $this->add_action((new action(
 151              new moodle_url('/admin/tasklogs.php', ['logid' => ':id', 'download' => true]),
 152              new pix_icon('t/download', ''),
 153              [],
 154              false,
 155              new lang_string('download'),
 156          )));
 157      }
 158  }