renderer.php 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Output rendering for the plugin.
 *
 * @package     tool_task
 * @copyright   2014 Damyon Wiese
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

28 29 30
use core\task\scheduled_task;


31 32 33 34 35 36 37 38 39 40
/**
 * Implements the plugin renderer
 *
 * @copyright 2014 Damyon Wiese
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class tool_task_renderer extends plugin_renderer_base {
    /**
     * This function will render one beautiful table with all the scheduled tasks.
     *
41
     * @param \core\task\scheduled_task[] $tasks - list of all scheduled tasks.
42
     * @param string $lastchanged (optional) the last task edited. Gets highlighted in teh table.
43 44
     * @return string HTML to output.
     */
45
    public function scheduled_tasks_table($tasks, $lastchanged = '') {
46 47
        global $CFG;

48 49
        $showloglink = \core\task\logmanager::has_log_report();

50
        $table = new html_table();
51
        $table->caption = get_string('scheduledtasks', 'tool_task');
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        $table->head = [
            get_string('name'),
            get_string('component', 'tool_task'),
            get_string('edit'),
            get_string('logs'),
            get_string('lastruntime', 'tool_task'),
            get_string('nextruntime', 'tool_task'),
            get_string('taskscheduleminute', 'tool_task'),
            get_string('taskschedulehour', 'tool_task'),
            get_string('taskscheduleday', 'tool_task'),
            get_string('taskscheduledayofweek', 'tool_task'),
            get_string('taskschedulemonth', 'tool_task'),
            get_string('faildelay', 'tool_task'),
            get_string('default', 'tool_task'),
        ];

68
        $table->attributes['class'] = 'admintable generaltable';
69 70 71 72 73 74 75
        $table->colclasses = [];

        if (!$showloglink) {
            // Hide the log links.
            $table->colclasses['3'] = 'hidden';
        }

76
        $data = [];
77 78
        $yes = get_string('yes');
        $no = get_string('no');
79
        $canruntasks = \core\task\manager::is_runnable();
80
        foreach ($tasks as $task) {
81 82 83
            $classname = get_class($task);
            $defaulttask = \core\task\manager::get_default_scheduled_task($classname, false);

84
            $customised = $task->is_customised() ? $no : $yes;
85
            if (empty($CFG->preventscheduledtaskchanges) && !$task->is_overridden()) {
86 87 88 89
                $configureurl = new moodle_url('/admin/tool/task/scheduledtasks.php',
                        ['action' => 'edit', 'task' => $classname]);
                $editlink = $this->output->action_icon($configureurl, new pix_icon('t/edit',
                        get_string('edittaskschedule', 'tool_task', $task->get_name())));
90
            } else {
91 92
                $editlink = $this->render(new pix_icon('t/locked',
                        get_string('scheduledtaskchangesdisabled', 'tool_task')));
93
            }
94

95 96
            $loglink = '';
            if ($showloglink) {
97 98
                $loglink = $this->output->action_icon(
                    \core\task\logmanager::get_url_for_task_class($classname),
99 100 101 102
                    new pix_icon('e/file-text', get_string('viewlogs', 'tool_task', $task->get_name())
                ));
            }

103 104 105 106 107 108 109
            $namecellcontent = $task->get_name() . "\n" .
                html_writer::span('\\' . $classname, 'task-class text-ltr');
            if ($task->is_overridden()) {
                // Let the user know the scheduled task is defined in config.
                $namecellcontent .= "\n" . html_writer::div(get_string('configoverride', 'admin'), 'alert-info');
            }
            $namecell = new html_table_cell($namecellcontent);
110 111
            $namecell->header = true;

112 113 114 115
            $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
            $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false &&
                    !$task->get_run_if_component_disabled();
            $disabled = $plugindisabled || $task->get_disabled();
116

117
            $runnow = '';
118
            if (!$plugindisabled && get_config('tool_task', 'enablerunnow') && $canruntasks ) {
119 120
                $runnow = html_writer::div(html_writer::link(
                        new moodle_url('/admin/tool/task/schedule_task.php',
121
                            ['task' => $classname]),
122 123 124
                        get_string('runnow', 'tool_task')), 'task-runnow');
            }

125
            $faildelaycell = new html_table_cell($task->get_fail_delay());
126
            if ($task->get_fail_delay()) {
127
                $faildelaycell->text .= html_writer::div(html_writer::link(
128
                        new moodle_url('/admin/tool/task/clear_fail_delay.php',
129
                                ['task' => $classname, 'sesskey' => sesskey()]),
130
                        get_string('clear')), 'task-clearfaildelay');
131
                $faildelaycell->attributes['class'] = 'table-danger';
132 133
            }

134
            $row = new html_table_row([
135
                        $namecell,
136
                        new html_table_cell($this->component_name($task->get_component())),
137
                        new html_table_cell($editlink),
138
                        new html_table_cell($loglink),
139 140
                        new html_table_cell($this->last_run_time($task) . $runnow),
                        new html_table_cell($this->next_run_time($task)),
141 142 143 144 145 146 147 148 149
                        $this->time_cell($task->get_minute(), $defaulttask->get_minute()),
                        $this->time_cell($task->get_hour(), $defaulttask->get_hour()),
                        $this->time_cell($task->get_day(), $defaulttask->get_day()),
                        $this->time_cell($task->get_day_of_week(), $defaulttask->get_day_of_week()),
                        $this->time_cell($task->get_month(), $defaulttask->get_month()),
                        $faildelaycell,
                        new html_table_cell($customised)]);

            $classes = [];
150
            if ($disabled) {
151
                $classes[] = 'disabled';
152
            }
153 154 155 156
            if (get_class($task) == $lastchanged) {
                $classes[] = 'table-primary';
            }
            $row->attributes['class'] = implode(' ', $classes);
157 158 159
            $data[] = $row;
        }
        $table->data = $data;
160
        if ($lastchanged) {
161 162
            // IE does not support this, and the ancient version of Firefox we use for Behat
            // has the method, but then errors on 'centre'. So, just try to scroll, and if it fails, don't care.
163
            $this->page->requires->js_init_code(
164
                    'try{document.querySelector("tr.table-primary").scrollIntoView({block: "center"});}catch(e){}');
165
        }
166 167
        return html_writer::table($table);
    }
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    /**
     * Nicely display the name of a component, with its disabled status and internal name.
     *
     * @param string $component component name, e.g. 'core' or 'mod_forum'.
     * @return string HTML.
     */
    public function component_name(string $component): string {
        list($type) = core_component::normalize_component($component);
        if ($type === 'core') {
            return get_string('corecomponent', 'tool_task');
        }

        $plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
        if (!$plugininfo) {
            return $component;
        }

        $plugininfo->init_display_name();

        $componentname = $plugininfo->displayname;
189
        if ($plugininfo->is_enabled() === false) {
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            $componentname .= ' ' . html_writer::span(
                            get_string('disabled', 'tool_task'), 'badge badge-secondary');
        }
        $componentname .= "\n" . html_writer::span($plugininfo->component, 'task-class text-ltr');

        return $componentname;
    }

    /**
     * Standard display of a tasks last run time.
     *
     * @param scheduled_task $task
     * @return string HTML.
     */
    public function last_run_time(scheduled_task $task): string {
        if ($task->get_last_run_time()) {
            return userdate($task->get_last_run_time());
        } else {
            return get_string('never');
        }
    }

    /**
     * Standard display of a tasks next run time.
     *
     * @param scheduled_task $task
     * @return string HTML.
     */
    public function next_run_time(scheduled_task $task): string {
        $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());

        $nextrun = $task->get_next_run_time();
        if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
            $nextrun = get_string('plugindisabled', 'tool_task');
        } else if ($task->get_disabled()) {
            $nextrun = get_string('taskdisabled', 'tool_task');
        } else if ($nextrun > time()) {
            $nextrun = userdate($nextrun);
        } else {
            $nextrun = get_string('asap', 'tool_task');
        }

        return $nextrun;
    }

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    /**
     * Get a table cell to show one time, comparing it to the default.
     *
     * @param string $current the current setting.
     * @param string $default the default setting from the db/tasks.php file.
     * @return html_table_cell for use in the table.
     */
    protected function time_cell(string $current, string $default): html_table_cell {
        $cell = new html_table_cell($current);
        // Cron-style values must always be LTR.
        $cell->attributes['class'] = 'text-ltr';

        // If the current value is default, that is all we want to do.
        if ($default === '*') {
            if ($current === '*') {
                return $cell;
            }
        } else if ($default === 'R' ) {
            if (is_numeric($current)) {
                return $cell;
            }
        } else {
            if ($default === $current) {
                return $cell;
            }
        }

        // Otherwise, highlight and show the default.
        $cell->attributes['class'] .= ' table-warning';
        $cell->text .= ' ' . html_writer::span(
                get_string('defaultx', 'tool_task', $default), 'task-class');
        return $cell;
    }

269 270 271 272 273 274 275 276 277 278
    /**
     * Displays a warning on the page if cron is disabled.
     *
     * @return string HTML code for information about cron being disabled
     * @throws moodle_exception
     */
    public function cron_disabled(): string {
        return $this->output->notification(get_string('crondisabled', 'tool_task'), 'warning');
    }

279 280 281
    /**
     * Renders a link back to the scheduled tasks page (used from the 'run now' screen).
     *
282
     * @param string $taskclassname if specified, the list of tasks will scroll to show this task.
283 284
     * @return string HTML code
     */
285 286 287 288 289 290
    public function link_back($taskclassname = '') {
        $url = new moodle_url('/admin/tool/task/scheduledtasks.php');
        if ($taskclassname) {
            $url->param('lastchanged', $taskclassname);
        }
        return $this->render_from_template('tool_task/link_back', ['url' => $url]);
291
    }
292
}