Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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