MDL-49399 tool_task: Link to log viewer
[moodle.git] / admin / tool / task / renderer.php
CommitLineData
309ae892
DW
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Output rendering for the plugin.
20 *
21 * @package tool_task
22 * @copyright 2014 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Implements the plugin renderer
30 *
31 * @copyright 2014 Damyon Wiese
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_task_renderer extends plugin_renderer_base {
35 /**
36 * This function will render one beautiful table with all the scheduled tasks.
37 *
5a1e6e35 38 * @param \core\task\scheduled_task[] $tasks - list of all scheduled tasks.
309ae892
DW
39 * @return string HTML to output.
40 */
41 public function scheduled_tasks_table($tasks) {
f12f3416
AN
42 global $CFG;
43
251ee97c
AN
44 $showloglink = \core\task\logmanager::has_log_report();
45
309ae892 46 $table = new html_table();
251ee97c
AN
47 $table->head = [
48 get_string('name'),
49 get_string('component', 'tool_task'),
50 get_string('edit'),
51 get_string('logs'),
52 get_string('lastruntime', 'tool_task'),
53 get_string('nextruntime', 'tool_task'),
54 get_string('taskscheduleminute', 'tool_task'),
55 get_string('taskschedulehour', 'tool_task'),
56 get_string('taskscheduleday', 'tool_task'),
57 get_string('taskscheduledayofweek', 'tool_task'),
58 get_string('taskschedulemonth', 'tool_task'),
59 get_string('faildelay', 'tool_task'),
60 get_string('default', 'tool_task'),
61 ];
62
309ae892 63 $table->attributes['class'] = 'admintable generaltable';
251ee97c
AN
64 $table->colclasses = [];
65
66 if (!$showloglink) {
67 // Hide the log links.
68 $table->colclasses['3'] = 'hidden';
69 }
70
309ae892
DW
71 $data = array();
72 $yes = get_string('yes');
73 $no = get_string('no');
74 $never = get_string('never');
0a5aa65b 75 $asap = get_string('asap', 'tool_task');
18247eb0
FM
76 $disabledstr = get_string('taskdisabled', 'tool_task');
77 $plugindisabledstr = get_string('plugindisabled', 'tool_task');
c759ae5d 78 $runnabletasks = tool_task\run_from_cli::is_runnable();
309ae892
DW
79 foreach ($tasks as $task) {
80 $customised = $task->is_customised() ? $no : $yes;
f12f3416
AN
81 if (empty($CFG->preventscheduledtaskchanges)) {
82 $configureurl = new moodle_url('/admin/tool/task/scheduledtasks.php', array('action'=>'edit', 'task' => get_class($task)));
83 $editlink = $this->action_icon($configureurl, new pix_icon('t/edit', get_string('edittaskschedule', 'tool_task', $task->get_name())));
84 } else {
85 $editlink = $this->render(new pix_icon('t/locked', get_string('scheduledtaskchangesdisabled', 'tool_task')));
86 }
309ae892 87
251ee97c
AN
88 $loglink = '';
89 if ($showloglink) {
90 $loglink = $this->action_icon(
91 \core\task\logmanager::get_url_for_task_class(get_class($task)),
92 new pix_icon('e/file-text', get_string('viewlogs', 'tool_task', $task->get_name())
93 ));
94 }
95
113efed5
FM
96 $namecell = new html_table_cell($task->get_name() . "\n" . html_writer::tag('span', '\\'.get_class($task),
97 array('class' => 'task-class text-ltr')));
309ae892
DW
98 $namecell->header = true;
99
b2ad7813 100 $component = $task->get_component();
18247eb0 101 $plugininfo = null;
b2ad7813
102 list($type, $plugin) = core_component::normalize_component($component);
103 if ($type === 'core') {
104 $componentcell = new html_table_cell(get_string('corecomponent', 'tool_task'));
105 } else {
106 if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) {
107 $plugininfo->init_display_name();
108 $componentcell = new html_table_cell($plugininfo->displayname);
109 } else {
110 $componentcell = new html_table_cell($component);
111 }
112 }
113
18247eb0
FM
114 $lastrun = $task->get_last_run_time() ? userdate($task->get_last_run_time()) : $never;
115 $nextrun = $task->get_next_run_time();
116 $disabled = false;
117 if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
118 $disabled = true;
119 $nextrun = $plugindisabledstr;
120 } else if ($task->get_disabled()) {
121 $disabled = true;
122 $nextrun = $disabledstr;
123 } else if ($nextrun > time()) {
124 $nextrun = userdate($nextrun);
125 } else {
126 $nextrun = $asap;
127 }
128
38fa1ca5 129 $runnow = '';
c759ae5d 130 if ( ! $disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) {
38fa1ca5 131 $runnow = html_writer::div(html_writer::link(
132 new moodle_url('/admin/tool/task/schedule_task.php',
133 array('task' => get_class($task))),
134 get_string('runnow', 'tool_task')), 'task-runnow');
135 }
136
470d59d3 137 $clearfail = '';
138 if ($task->get_fail_delay()) {
139 $clearfail = html_writer::div(html_writer::link(
140 new moodle_url('/admin/tool/task/clear_fail_delay.php',
141 array('task' => get_class($task), 'sesskey' => sesskey())),
142 get_string('clear')), 'task-clearfaildelay');
143 }
144
b2ad7813
145 $row = new html_table_row(array(
146 $namecell,
147 $componentcell,
309ae892 148 new html_table_cell($editlink),
251ee97c 149 new html_table_cell($loglink),
38fa1ca5 150 new html_table_cell($lastrun . $runnow),
309ae892
DW
151 new html_table_cell($nextrun),
152 new html_table_cell($task->get_minute()),
153 new html_table_cell($task->get_hour()),
154 new html_table_cell($task->get_day()),
155 new html_table_cell($task->get_day_of_week()),
156 new html_table_cell($task->get_month()),
470d59d3 157 new html_table_cell($task->get_fail_delay() . $clearfail),
309ae892
DW
158 new html_table_cell($customised)));
159
113efed5 160 // Cron-style values must always be LTR.
113efed5
FM
161 $row->cells[6]->attributes['class'] = 'text-ltr';
162 $row->cells[7]->attributes['class'] = 'text-ltr';
163 $row->cells[8]->attributes['class'] = 'text-ltr';
164 $row->cells[9]->attributes['class'] = 'text-ltr';
251ee97c 165 $row->cells[10]->attributes['class'] = 'text-ltr';
113efed5 166
18247eb0 167 if ($disabled) {
24623071
PS
168 $row->attributes['class'] = 'disabled';
169 }
309ae892
DW
170 $data[] = $row;
171 }
172 $table->data = $data;
173 return html_writer::table($table);
174 }
38fa1ca5 175
176 /**
177 * Renders a link back to the scheduled tasks page (used from the 'run now' screen).
178 *
179 * @return string HTML code
180 */
181 public function link_back() {
182 return $this->render_from_template('tool_task/link_back',
183 array('url' => new moodle_url('/admin/tool/task/scheduledtasks.php')));
184 }
309ae892 185}