Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 403]

   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   * Loglive report renderable class.
  19   *
  20   * @package    report_loglive
  21   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  /**
  28   * Report loglive renderable class.
  29   *
  30   * @since      Moodle 2.7
  31   * @package    report_loglive
  32   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class report_loglive_renderable implements renderable {
  36  
  37      /** @const int number of seconds to show logs from, by default. */
  38      const CUTOFF = 3600;
  39  
  40      /** @var \core\log\manager log manager */
  41      protected $logmanager;
  42  
  43      /** @var string selected log reader pluginname */
  44      public $selectedlogreader = null;
  45  
  46      /** @var int page number */
  47      public $page;
  48  
  49      /** @var int perpage records to show */
  50      public $perpage;
  51  
  52      /** @var stdClass course record */
  53      public $course;
  54  
  55      /** @var moodle_url url of report page */
  56      public $url;
  57  
  58      /** @var int selected date from which records should be displayed */
  59      public $date;
  60  
  61      /** @var string order to sort */
  62      public $order;
  63  
  64      /** @var int group id */
  65      public $groupid;
  66  
  67      /** @var report_loglive_table_log table log which will be used for rendering logs */
  68      public $tablelog;
  69  
  70      /** @var  int refresh rate in seconds */
  71      protected $refresh  = 60;
  72  
  73      /**
  74       * Constructor.
  75       *
  76       * @param string $logreader (optional)reader pluginname from which logs will be fetched.
  77       * @param stdClass|int $course (optional) course record or id
  78       * @param moodle_url|string $url (optional) page url.
  79       * @param int $date date (optional) from which records will be fetched.
  80       * @param int $page (optional) page number.
  81       * @param int $perpage (optional) number of records to show per page.
  82       * @param string $order (optional) sortorder of fetched records
  83       */
  84      public function __construct($logreader = "", $course = 0, $url = "", $date = 0, $page = 0, $perpage = 100,
  85                                  $order = "timecreated DESC") {
  86  
  87          global $PAGE;
  88  
  89          // Use first reader as selected reader, if not passed.
  90          if (empty($logreader)) {
  91              $readers = $this->get_readers();
  92              if (!empty($readers)) {
  93                  reset($readers);
  94                  $logreader = key($readers);
  95              } else {
  96                  $logreader = null;
  97              }
  98          }
  99          $this->selectedlogreader = $logreader;
 100  
 101          // Use page url if empty.
 102          if (empty($url)) {
 103              $url = new moodle_url($PAGE->url);
 104          } else {
 105              $url = new moodle_url($url);
 106          }
 107          $this->url = $url;
 108  
 109          // Use site course id, if course is empty.
 110          if (!empty($course) && is_int($course)) {
 111              $course = get_course($course);
 112          }
 113          $this->course = $course;
 114  
 115          if ($date == 0 ) {
 116              $date = time() - self::CUTOFF;
 117          }
 118          $this->date = $date;
 119  
 120          $this->page = $page;
 121          $this->perpage = $perpage;
 122          $this->order = $order;
 123          $this->set_refresh_rate();
 124      }
 125  
 126      /**
 127       * Get a list of enabled sql_reader objects/name
 128       *
 129       * @param bool $nameonly if true only reader names will be returned.
 130       *
 131       * @return array core\log\sql_reader object or name.
 132       */
 133      public function get_readers($nameonly = false) {
 134          if (!isset($this->logmanager)) {
 135              $this->logmanager = get_log_manager();
 136          }
 137  
 138          $readers = $this->logmanager->get_readers('core\log\sql_reader');
 139          if ($nameonly) {
 140              foreach ($readers as $pluginname => $reader) {
 141                  $readers[$pluginname] = $reader->get_name();
 142              }
 143          }
 144          return $readers;
 145      }
 146  
 147      /**
 148       * Setup table log.
 149       */
 150      protected function setup_table() {
 151          $filter = $this->setup_filters();
 152          $this->tablelog = new report_loglive_table_log('report_loglive', $filter);
 153          $this->tablelog->define_baseurl($this->url);
 154      }
 155  
 156      /**
 157       * Setup table log for ajax output.
 158       */
 159      protected function setup_table_ajax() {
 160          $filter = $this->setup_filters();
 161          $this->tablelog = new report_loglive_table_log_ajax('report_loglive', $filter);
 162          $this->tablelog->define_baseurl($this->url);
 163      }
 164  
 165      /**
 166       * Setup filters
 167       *
 168       * @return stdClass filters
 169       */
 170      protected function setup_filters() {
 171          global $USER;
 172  
 173          $readers = $this->get_readers();
 174  
 175          // Set up filters.
 176          $filter = new \stdClass();
 177          if (!empty($this->course)) {
 178              $filter->courseid = $this->course->id;
 179              $context = context_course::instance($filter->courseid);
 180              if (!has_capability('moodle/site:viewanonymousevents', $context)) {
 181                  $filter->anonymous = 0;
 182              }
 183              if (groups_get_course_groupmode($this->course) == SEPARATEGROUPS &&
 184                  !has_capability('moodle/site:accessallgroups', $context)) {
 185                  if ($cgroups = groups_get_all_groups($this->course->id, $USER->id)) {
 186                      $filter->groups = [];
 187                      foreach ($cgroups as $cgroup) {
 188                          $filter->groups[] = (int)$cgroup->id;
 189                      }
 190                  }
 191              }
 192          } else {
 193              $filter->courseid = 0;
 194          }
 195          $filter->logreader = $readers[$this->selectedlogreader];
 196          $filter->date = $this->date;
 197          $filter->orderby = $this->order;
 198  
 199          return $filter;
 200      }
 201  
 202      /**
 203       * Set refresh rate of the live updates.
 204       */
 205      protected function set_refresh_rate() {
 206          if (defined('BEHAT_SITE_RUNNING')) {
 207              // Hack for behat tests.
 208              $this->refresh = 5;
 209          } else {
 210              if (defined('REPORT_LOGLIVE_REFRESH')) {
 211                  // Backward compatibility.
 212                  $this->refresh = REPORT_LOGLIVE_REFRESH;
 213              } else {
 214                  // Default.
 215                  $this->refresh = 60;
 216              }
 217          }
 218      }
 219  
 220      /**
 221       * Get refresh rate of the live updates.
 222       */
 223      public function get_refresh_rate() {
 224          return $this->refresh;
 225      }
 226  
 227      /**
 228       * Setup table and return it.
 229       *
 230       * @param bool $ajax If set to true report_loglive_table_log_ajax is set instead of report_loglive_table_log.
 231       *
 232       * @return report_loglive_table_log|report_loglive_table_log_ajax table object
 233       */
 234      public function get_table($ajax = false) {
 235          if (empty($this->tablelog)) {
 236              if ($ajax) {
 237                  $this->setup_table_ajax();
 238              } else {
 239                  $this->setup_table();
 240              }
 241          }
 242          return $this->tablelog;
 243      }
 244  }