Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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'),
1441 ariadna 52
            'progress'     => get_string('progress', 'core'),
1 efrain 53
            'timestarted'  => get_string('started', 'tool_task'),
54
            'hostname'     => get_string('hostname', 'tool_task'),
55
            'pid'          => get_string('pid', 'tool_task'),
56
        ];
57
        $this->define_columns(array_keys($columnheaders));
58
        $this->define_headers(array_values($columnheaders));
59
 
60
        // The name column is a header.
61
        $this->define_header_column('classname');
62
 
63
        // This table is not collapsible.
64
        $this->collapsible(false);
65
 
66
        // Allow pagination.
67
        $this->pageable(true);
68
    }
69
 
70
    /**
71
     * Query the db. Store results in the table object for use by build_table.
72
     *
73
     * @param int $pagesize size of page for paginated displayed table.
74
     * @param bool $useinitialsbar do you want to use the initials bar. Bar
75
     * will only be used if there is a fullname column defined for the table.
76
     * @throws \dml_exception
77
     */
78
    public function query_db($pagesize, $useinitialsbar = true) {
79
        $sort = $this->get_sql_sort();
80
        $this->rawdata = \core\task\manager::get_running_tasks($sort);
81
    }
82
 
83
    /**
84
     * Format the classname cell.
85
     *
86
     * @param   \stdClass $row
87
     * @return  string
88
     */
89
    public function col_classname($row): string {
90
        $output = $row->classname;
91
        if ($row->type == 'scheduled') {
92
            if (class_exists($row->classname)) {
93
                $task = new $row->classname;
94
                if ($task instanceof \core\task\scheduled_task) {
95
                    $output .= \html_writer::tag('div', $task->get_name(), ['class' => 'task-class']);
96
                }
97
            }
98
        } else if ($row->type == 'adhoc') {
99
            $output .= \html_writer::tag('div',
100
                get_string('adhoctaskid', 'tool_task', $row->id), ['class' => 'task-class']);
101
        }
102
        return $output;
103
    }
104
 
105
    /**
106
     * Format the type cell.
107
     *
108
     * @param   \stdClass $row
109
     * @return  string
110
     * @throws  \coding_exception
111
     */
112
    public function col_type($row): string {
113
        if ($row->type == 'scheduled') {
114
            $output = \html_writer::span(get_string('scheduled', 'tool_task'), 'badge bg-primary text-white');
115
        } else if ($row->type == 'adhoc') {
116
            $output = \html_writer::span(get_string('adhoc', 'tool_task'), 'badge bg-dark text-white');
117
        } else {
118
            // This shouldn't ever happen.
119
            $output = '';
120
        }
121
        return $output;
122
    }
123
 
124
    /**
125
     * Format the time cell.
126
     *
127
     * @param   \stdClass $row
128
     * @return  string
129
     */
130
    public function col_time($row): string {
131
        global $OUTPUT;
132
 
133
        $taskmethod = "{$row->type}_task_from_record";
134
        $task = manager::$taskmethod($row);
135
 
136
        $result = $task->get_runtime_result();
137
        $extra = '';
138
        if ($result->get_status() != $result::OK) {
139
            $extra = '<br>';
140
            $extra .= $OUTPUT->check_result($result);
141
            $extra .= ' ';
142
            $extra .= $result->get_details();
143
        }
144
 
145
        return format_time($row->time) . $extra;
146
    }
147
 
148
    /**
149
     * Format the timestarted cell.
150
     *
151
     * @param   \stdClass $row
152
     * @return  string
153
     */
154
    public function col_timestarted($row): string {
155
        return userdate($row->timestarted);
156
    }
1441 ariadna 157
 
158
    /**
159
     * Format the progress column.
160
     *
161
     * @param \stdClass $row
162
     * @return string
163
     */
164
    public function col_progress($row): string {
165
        // Check to see if there is a stored progress record for this task.
166
        if ($row->type === 'adhoc') {
167
            $idnumber = \core\output\stored_progress_bar::convert_to_idnumber($row->classname, $row->id);
168
        } else {
169
            $idnumber = \core\output\stored_progress_bar::convert_to_idnumber($row->classname);
170
        }
171
 
172
        $bar = \core\output\stored_progress_bar::get_by_idnumber($idnumber);
173
        if ($bar) {
174
            return $bar->get_content();
175
        } else {
176
            return '-';
177
        }
178
    }
179
 
1 efrain 180
}