Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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          $readers = $this->get_readers();
 172  
 173          // Set up filters.
 174          $filter = new \stdClass();
 175          if (!empty($this->course)) {
 176              $filter->courseid = $this->course->id;
 177              $context = context_course::instance($filter->courseid);
 178              if (!has_capability('moodle/site:viewanonymousevents', $context)) {
 179                  $filter->anonymous = 0;
 180              }
 181          } else {
 182              $filter->courseid = 0;
 183          }
 184          $filter->logreader = $readers[$this->selectedlogreader];
 185          $filter->date = $this->date;
 186          $filter->orderby = $this->order;
 187  
 188          return $filter;
 189      }
 190  
 191      /**
 192       * Set refresh rate of the live updates.
 193       */
 194      protected function set_refresh_rate() {
 195          if (defined('BEHAT_SITE_RUNNING')) {
 196              // Hack for behat tests.
 197              $this->refresh = 5;
 198          } else {
 199              if (defined('REPORT_LOGLIVE_REFRESH')) {
 200                  // Backward compatibility.
 201                  $this->refresh = REPORT_LOGLIVE_REFERESH;
 202              } else {
 203                  // Default.
 204                  $this->refresh = 60;
 205              }
 206          }
 207      }
 208  
 209      /**
 210       * Get refresh rate of the live updates.
 211       */
 212      public function get_refresh_rate() {
 213          return $this->refresh;
 214      }
 215  
 216      /**
 217       * Setup table and return it.
 218       *
 219       * @param bool $ajax If set to true report_loglive_table_log_ajax is set instead of report_loglive_table_log.
 220       *
 221       * @return report_loglive_table_log|report_loglive_table_log_ajax table object
 222       */
 223      public function get_table($ajax = false) {
 224          if (empty($this->tablelog)) {
 225              if ($ajax) {
 226                  $this->setup_table_ajax();
 227              } else {
 228                  $this->setup_table();
 229              }
 230          }
 231          return $this->tablelog;
 232      }
 233  }