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 402] [Versions 310 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   * Running tasks table.
  19   *
  20   * @package    tool_task
  21   * @copyright  2019 The Open University
  22   * @copyright  2020 Mikhail Golenkov <golenkovm@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_task;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->libdir . '/tablelib.php');
  31  
  32  /**
  33   * Table to display list of running task.
  34   *
  35   * @copyright  2019 The Open University
  36   * @copyright  2020 Mikhail Golenkov <golenkovm@gmail.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class running_tasks_table extends \table_sql {
  40  
  41      /**
  42       * Constructor for the running tasks table.
  43       */
  44      public function __construct() {
  45          parent::__construct('runningtasks');
  46  
  47          $columnheaders = [
  48              'classname'    => get_string('classname', 'tool_task'),
  49              'type'         => get_string('tasktype', 'admin'),
  50              'time'         => get_string('time'),
  51              'timestarted'  => get_string('started', 'tool_task'),
  52              'hostname'     => get_string('hostname', 'tool_task'),
  53              'pid'          => get_string('pid', 'tool_task'),
  54          ];
  55          $this->define_columns(array_keys($columnheaders));
  56          $this->define_headers(array_values($columnheaders));
  57  
  58          // The name column is a header.
  59          $this->define_header_column('classname');
  60  
  61          // This table is not collapsible.
  62          $this->collapsible(false);
  63  
  64          // Allow pagination.
  65          $this->pageable(true);
  66      }
  67  
  68      /**
  69       * Query the db. Store results in the table object for use by build_table.
  70       *
  71       * @param int $pagesize size of page for paginated displayed table.
  72       * @param bool $useinitialsbar do you want to use the initials bar. Bar
  73       * will only be used if there is a fullname column defined for the table.
  74       * @throws \dml_exception
  75       */
  76      public function query_db($pagesize, $useinitialsbar = true) {
  77          $sort = $this->get_sql_sort();
  78          $this->rawdata = \core\task\manager::get_running_tasks($sort);
  79      }
  80  
  81      /**
  82       * Format the classname cell.
  83       *
  84       * @param   \stdClass $row
  85       * @return  string
  86       */
  87      public function col_classname($row) : string {
  88          $output = $row->classname;
  89          if ($row->type == 'scheduled') {
  90              if (class_exists($row->classname)) {
  91                  $task = new $row->classname;
  92                  if ($task instanceof \core\task\scheduled_task) {
  93                      $output .= \html_writer::tag('div', $task->get_name(), ['class' => 'task-class']);
  94                  }
  95              }
  96          } else if ($row->type == 'adhoc') {
  97              $output .= \html_writer::tag('div',
  98                  get_string('adhoctaskid', 'tool_task', $row->id), ['class' => 'task-class']);
  99          }
 100          return $output;
 101      }
 102  
 103      /**
 104       * Format the type cell.
 105       *
 106       * @param   \stdClass $row
 107       * @return  string
 108       * @throws  \coding_exception
 109       */
 110      public function col_type($row) : string {
 111          if ($row->type == 'scheduled') {
 112              $output = \html_writer::span(get_string('scheduled', 'tool_task'), 'badge badge-primary');
 113          } else if ($row->type == 'adhoc') {
 114              $output = \html_writer::span(get_string('adhoc', 'tool_task'), 'badge badge-warning');
 115          } else {
 116              // This shouldn't ever happen.
 117              $output = '';
 118          }
 119          return $output;
 120      }
 121  
 122      /**
 123       * Format the time cell.
 124       *
 125       * @param   \stdClass $row
 126       * @return  string
 127       */
 128      public function col_time($row) : string {
 129          return format_time($row->time);
 130      }
 131  
 132      /**
 133       * Format the timestarted cell.
 134       *
 135       * @param   \stdClass $row
 136       * @return  string
 137       */
 138      public function col_timestarted($row) : string {
 139          return userdate($row->timestarted);
 140      }
 141  }