MDL-49399 task: Add admin 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
309ae892
DW
44 $table = new html_table();
45 $table->head = array(get_string('name'),
b2ad7813 46 get_string('component', 'tool_task'),
309ae892
DW
47 get_string('edit'),
48 get_string('lastruntime', 'tool_task'),
49 get_string('nextruntime', 'tool_task'),
50 get_string('taskscheduleminute', 'tool_task'),
51 get_string('taskschedulehour', 'tool_task'),
52 get_string('taskscheduleday', 'tool_task'),
53 get_string('taskscheduledayofweek', 'tool_task'),
54 get_string('taskschedulemonth', 'tool_task'),
55 get_string('faildelay', 'tool_task'),
56 get_string('default', 'tool_task'));
57 $table->attributes['class'] = 'admintable generaltable';
58 $data = array();
59 $yes = get_string('yes');
60 $no = get_string('no');
61 $never = get_string('never');
0a5aa65b 62 $asap = get_string('asap', 'tool_task');
18247eb0
FM
63 $disabledstr = get_string('taskdisabled', 'tool_task');
64 $plugindisabledstr = get_string('plugindisabled', 'tool_task');
c759ae5d 65 $runnabletasks = tool_task\run_from_cli::is_runnable();
309ae892
DW
66 foreach ($tasks as $task) {
67 $customised = $task->is_customised() ? $no : $yes;
f12f3416
AN
68 if (empty($CFG->preventscheduledtaskchanges)) {
69 $configureurl = new moodle_url('/admin/tool/task/scheduledtasks.php', array('action'=>'edit', 'task' => get_class($task)));
70 $editlink = $this->action_icon($configureurl, new pix_icon('t/edit', get_string('edittaskschedule', 'tool_task', $task->get_name())));
71 } else {
72 $editlink = $this->render(new pix_icon('t/locked', get_string('scheduledtaskchangesdisabled', 'tool_task')));
73 }
309ae892 74
113efed5
FM
75 $namecell = new html_table_cell($task->get_name() . "\n" . html_writer::tag('span', '\\'.get_class($task),
76 array('class' => 'task-class text-ltr')));
309ae892
DW
77 $namecell->header = true;
78
b2ad7813 79 $component = $task->get_component();
18247eb0 80 $plugininfo = null;
b2ad7813
81 list($type, $plugin) = core_component::normalize_component($component);
82 if ($type === 'core') {
83 $componentcell = new html_table_cell(get_string('corecomponent', 'tool_task'));
84 } else {
85 if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) {
86 $plugininfo->init_display_name();
87 $componentcell = new html_table_cell($plugininfo->displayname);
88 } else {
89 $componentcell = new html_table_cell($component);
90 }
91 }
92
18247eb0
FM
93 $lastrun = $task->get_last_run_time() ? userdate($task->get_last_run_time()) : $never;
94 $nextrun = $task->get_next_run_time();
95 $disabled = false;
96 if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
97 $disabled = true;
98 $nextrun = $plugindisabledstr;
99 } else if ($task->get_disabled()) {
100 $disabled = true;
101 $nextrun = $disabledstr;
102 } else if ($nextrun > time()) {
103 $nextrun = userdate($nextrun);
104 } else {
105 $nextrun = $asap;
106 }
107
38fa1ca5 108 $runnow = '';
c759ae5d 109 if ( ! $disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) {
38fa1ca5 110 $runnow = html_writer::div(html_writer::link(
111 new moodle_url('/admin/tool/task/schedule_task.php',
112 array('task' => get_class($task))),
113 get_string('runnow', 'tool_task')), 'task-runnow');
114 }
115
470d59d3 116 $clearfail = '';
117 if ($task->get_fail_delay()) {
118 $clearfail = html_writer::div(html_writer::link(
119 new moodle_url('/admin/tool/task/clear_fail_delay.php',
120 array('task' => get_class($task), 'sesskey' => sesskey())),
121 get_string('clear')), 'task-clearfaildelay');
122 }
123
b2ad7813
124 $row = new html_table_row(array(
125 $namecell,
126 $componentcell,
309ae892 127 new html_table_cell($editlink),
38fa1ca5 128 new html_table_cell($lastrun . $runnow),
309ae892
DW
129 new html_table_cell($nextrun),
130 new html_table_cell($task->get_minute()),
131 new html_table_cell($task->get_hour()),
132 new html_table_cell($task->get_day()),
133 new html_table_cell($task->get_day_of_week()),
134 new html_table_cell($task->get_month()),
470d59d3 135 new html_table_cell($task->get_fail_delay() . $clearfail),
309ae892
DW
136 new html_table_cell($customised)));
137
113efed5
FM
138 // Cron-style values must always be LTR.
139 $row->cells[5]->attributes['class'] = 'text-ltr';
140 $row->cells[6]->attributes['class'] = 'text-ltr';
141 $row->cells[7]->attributes['class'] = 'text-ltr';
142 $row->cells[8]->attributes['class'] = 'text-ltr';
143 $row->cells[9]->attributes['class'] = 'text-ltr';
144
18247eb0 145 if ($disabled) {
24623071
PS
146 $row->attributes['class'] = 'disabled';
147 }
309ae892
DW
148 $data[] = $row;
149 }
150 $table->data = $data;
151 return html_writer::table($table);
152 }
38fa1ca5 153
154 /**
155 * Renders a link back to the scheduled tasks page (used from the 'run now' screen).
156 *
157 * @return string HTML code
158 */
159 public function link_back() {
160 return $this->render_from_template('tool_task/link_back',
161 array('url' => new moodle_url('/admin/tool/task/scheduledtasks.php')));
162 }
309ae892 163}