Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
<?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();

use core\task\scheduled_task;


/**
 * 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. > * This function will render a table with the summary of all adhoc tasks. * > * * @param \core\task\scheduled_task[] $tasks - list of all scheduled tasks. > * @param array $summary * @param string $lastchanged (optional) the last task edited. Gets highlighted in teh table. > * @return string HTML to output. * @return string HTML to output. > */ */ > public function adhoc_tasks_summary_table(array $summary): string { public function scheduled_tasks_table($tasks, $lastchanged = '') { > $adhocurl = '/admin/tool/task/adhoctasks.php'; global $CFG; > $adhocrunurl = '/admin/tool/task/run_adhoctasks.php'; > $showloglink = \core\task\logmanager::has_log_report(); > // Main tasks table. > $table = new html_table(); $table = new html_table(); > $table->caption = get_string('adhoctasks', 'tool_task'); $table->caption = get_string('scheduledtasks', 'tool_task'); > $table->head = [ $table->head = [ > get_string('component', 'tool_task') . ' / ' . get_string('classname', 'tool_task'), get_string('name'), > get_string('adhoctasksrunning', 'tool_task'), get_string('component', 'tool_task'), > get_string('adhoctasksdue', 'tool_task'), get_string('edit'), > get_string('adhoctasksfuture', 'tool_task'), get_string('logs'), > get_string('adhoctasksfailed', 'tool_task'), get_string('lastruntime', 'tool_task'), > get_string('nextruntime', 'tool_task'), get_string('nextruntime', 'tool_task'), > ]; get_string('taskscheduleminute', 'tool_task'), > get_string('taskschedulehour', 'tool_task'), > $table->attributes['class'] = 'admintable generaltable'; get_string('taskscheduleday', 'tool_task'), > $table->colclasses = []; get_string('taskscheduledayofweek', 'tool_task'), > get_string('taskschedulemonth', 'tool_task'), > // For each task entry (row) show action buttons/logs link depending on the user permissions. get_string('faildelay', 'tool_task'), > $data = []; get_string('default', 'tool_task'), > $canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow'); ]; > foreach ($summary as $component => $classes) { > // Component cell. $table->attributes['class'] = 'admintable generaltable'; > $componentcell = new html_table_cell($component); $table->colclasses = []; > $componentcell->header = true; > $componentcell->id = "tasks-$component"; if (!$showloglink) { > $componentcell->colspan = 6; // Hide the log links. > $table->colclasses['3'] = 'hidden'; > $data[] = new html_table_row([$componentcell]); } > > foreach ($classes as $classname => $stats) { $data = []; > // Task class cell. $yes = get_string('yes'); > $classbits = explode('\\', $classname); $no = get_string('no'); > $classcontent = html_writer::link( $canruntasks = \core\task\manager::is_runnable(); > new moodle_url($adhocurl, ['classname' => $classname]), foreach ($tasks as $task) { > end($classbits) $classname = get_class($task); > ); $defaulttask = \core\task\manager::get_default_scheduled_task($classname, false); > $classcell = new html_table_cell($classcontent); > $classcell->header = true; $customised = $task->is_customised() ? $no : $yes; > $classcell->attributes['class'] = "task-class-summary text-ltr"; if (empty($CFG->preventscheduledtaskchanges)) { > $configureurl = new moodle_url('/admin/tool/task/scheduledtasks.php', > $duecontent = $stats['due']; ['action' => 'edit', 'task' => $classname]); > if ($canruntasks && ($stats['due'] > 0 || $stats['failed'] > 0)) { $editlink = $this->output->action_icon($configureurl, new pix_icon('t/edit', > $duecontent .= html_writer::div( get_string('edittaskschedule', 'tool_task', $task->get_name()))); > html_writer::link( } else { > new moodle_url( $editlink = $this->render(new pix_icon('t/locked', > $adhocrunurl, get_string('scheduledtaskchangesdisabled', 'tool_task'))); > ['classname' => $classname] } > ), > get_string('runclassname', 'tool_task') $loglink = ''; > ), if ($showloglink) { > 'task-runnow' $loglink = $this->output->action_icon( > ); \core\task\logmanager::get_url_for_task_class($classname), > } new pix_icon('e/file-text', get_string('viewlogs', 'tool_task', $task->get_name()) > )); > // Mark cell if has failed tasks. } > $failed = $stats['failed']; > if ($canruntasks && $failed > 0) { $namecell = new html_table_cell($task->get_name() . "\n" . > $failed .= html_writer::div( html_writer::span('\\' . $classname, 'task-class text-ltr')); > html_writer::link( $namecell->header = true; > new moodle_url( > $adhocrunurl, $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component()); > ['classname' => $classname, 'failedonly' => 1] $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && > ), !$task->get_run_if_component_disabled(); > get_string('runclassnamefailedonly', 'tool_task') $disabled = $plugindisabled || $task->get_disabled(); > ), > 'task-runnow' $runnow = ''; > ); if (!$disabled && get_config('tool_task', 'enablerunnow') && $canruntasks ) { > } $runnow = html_writer::div(html_writer::link( > $failedcell = new html_table_cell($failed); new moodle_url('/admin/tool/task/schedule_task.php', > if ($failed > 0) { ['task' => $classname]), > $failedcell->attributes['class'] = 'table-danger'; get_string('runnow', 'tool_task')), 'task-runnow'); > } } > > // Prepares the next run time cell contents. $faildelaycell = new html_table_cell($task->get_fail_delay()); > $nextrun = ''; if ($task->get_fail_delay()) { > if ($stats['due'] > 0) { $faildelaycell->text .= html_writer::div(html_writer::link( > $nextrun = get_string('asap', 'tool_task'); new moodle_url('/admin/tool/task/clear_fail_delay.php', > } else if ($stats['nextruntime']) { ['task' => $classname, 'sesskey' => sesskey()]), > $nextrun = userdate($stats['nextruntime']); get_string('clear')), 'task-clearfaildelay'); > } $faildelaycell->attributes['class'] = 'table-danger'; > } > $data[] = new html_table_row([ > $classcell, $row = new html_table_row([ > new html_table_cell($stats['running']), $namecell, > new html_table_cell($duecontent), new html_table_cell($this->component_name($task->get_component())), > new html_table_cell($stats['count'] - $stats['running'] - $stats['due']), new html_table_cell($editlink), > $failedcell, new html_table_cell($loglink), > new html_table_cell($nextrun), new html_table_cell($this->last_run_time($task) . $runnow), > ]); new html_table_cell($this->next_run_time($task)), > } $this->time_cell($task->get_minute(), $defaulttask->get_minute()), > } $this->time_cell($task->get_hour(), $defaulttask->get_hour()), > $table->data = $data; $this->time_cell($task->get_day(), $defaulttask->get_day()), > return html_writer::table($table); $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)]); > * This function will render a table with all the adhoc tasks for the class. > * $classes = []; > * @param string $classname if ($disabled) { > * @param array $tasks - list of all adhoc tasks. $classes[] = 'disabled'; > * @param array|null $params } > * @return string HTML to output. if (get_class($task) == $lastchanged) { > */ $classes[] = 'table-primary'; > public function adhoc_tasks_class_table(string $classname, array $tasks, ?array $params = []): string { } > $adhocurl = '/admin/tool/task/adhoctasks.php'; $row->attributes['class'] = implode(' ', $classes); > $adhocrunurl = '/admin/tool/task/run_adhoctasks.php'; $data[] = $row; > $showloglink = \core\task\logmanager::has_log_report(); } > $failedonly = !empty($params['failedonly']); $table->data = $data; > $canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow'); if ($lastchanged) { > // IE does not support this, and the ancient version of Firefox we use for Behat > // Depending on the currently set parameters, set up toggle buttons. // has the method, but then errors on 'centre'. So, just try to scroll, and if it fails, don't care. > $failedorall = html_writer::link( $this->page->requires->js_init_code( > new moodle_url( 'try{document.querySelector("tr.table-primary").scrollIntoView({block: "center"});}catch(e){}'); > $adhocurl, } > array_merge($params, ['classname' => $classname, 'failedonly' => !$failedonly]) return html_writer::table($table); > ), } > get_string($failedonly ? 'showall' : 'showfailedonly', 'tool_task') > ); /** > * Nicely display the name of a component, with its disabled status and internal name. > // Main tasks table. * > $table = $this->generate_adhoc_tasks_simple_table($tasks, $canruntasks); * @param string $component component name, e.g. 'core' or 'mod_forum'. > * @return string HTML. > $table->caption = s($classname) . " " */ > . get_string($failedonly ? 'adhoctasksfailed' : 'adhoctasks', 'tool_task'); public function component_name(string $component): string { > $table->head[3] .= " $failedorall"; // Spice up faildelay heading. list($type) = core_component::normalize_component($component); > if ($type === 'core') { > if ($showloglink) { return get_string('corecomponent', 'tool_task'); > // Insert logs as the second col. } > array_splice($table->head, 1, 0, [get_string('logs')]); > array_walk($table->data, function ($row, $idx) use ($classname) { $plugininfo = core_plugin_manager::instance()->get_plugin_info($component); > $loglink = ''; if (!$plugininfo) { > $faildelaycell = $row->cells[3]; return $component; > if ($faildelaycell->attributes['class'] == 'table-danger') { } > // Failed task. > $loglink = $this->output->action_icon( $plugininfo->init_display_name(); > \core\task\logmanager::get_url_for_task_class($classname), > new pix_icon('e/file-text', get_string('viewlogs', 'tool_task', $classname) $componentname = $plugininfo->displayname; > )); if ($plugininfo->is_enabled() === false) { > } $componentname .= ' ' . html_writer::span( > get_string('disabled', 'tool_task'), 'badge badge-secondary'); > array_splice($row->cells, 1, 0, [new html_table_cell($loglink)]); } > }); $componentname .= "\n" . html_writer::span($plugininfo->component, 'task-class text-ltr'); > } > return $componentname; > return html_writer::table($table) } > . html_writer::div( > html_writer::link( /** > new moodle_url( * Standard display of a tasks last run time. > $adhocrunurl, * > array_merge($params, ['classname' => $classname]) * @param scheduled_task $task > ), * @return string HTML. > get_string('runclassname', 'tool_task') */ > ), public function last_run_time(scheduled_task $task): string { > 'task-runnow' if ($task->get_last_run_time()) { > ) return userdate($task->get_last_run_time()); > . html_writer::div( } else { > html_writer::link( return get_string('never'); > new moodle_url( } > $adhocurl } > ), > get_string('showsummary', 'tool_task') /** > ), * Standard display of a tasks next run time. > 'task-show-summary' * > ); * @param scheduled_task $task > } * @return string HTML. > */ > /** public function next_run_time(scheduled_task $task): string { > * This function will render a plain adhoc tasks table. $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component()); > * > * @param array $tasks - list of adhoc tasks. $nextrun = $task->get_next_run_time(); > * @return string HTML to output. if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) { > */ $nextrun = get_string('plugindisabled', 'tool_task'); > public function adhoc_tasks_simple_table(array $tasks): string { } else if ($task->get_disabled()) { > $table = $this->generate_adhoc_tasks_simple_table($tasks); $nextrun = get_string('taskdisabled', 'tool_task'); > } else if ($nextrun > time()) { > return html_writer::table($table); $nextrun = userdate($nextrun); > } } else { > $nextrun = get_string('asap', 'tool_task'); > /** } > * This function will render a plain adhoc tasks table. > * return $nextrun; > * @param array $tasks - list of adhoc tasks. } > * @param bool $wantruntasks add 'Run now' link > * @return html_table /** > */ * Get a table cell to show one time, comparing it to the default. > private function generate_adhoc_tasks_simple_table(array $tasks, bool $wantruntasks = false): html_table { * > $adhocrunurl = '/admin/tool/task/run_adhoctasks.php'; * @param string $current the current setting. > $now = time(); * @param string $default the default setting from the db/tasks.php file. > $failedstr = get_string('failed', 'tool_task'); * @return html_table_cell for use in the table. > */ > // Main tasks table. protected function time_cell(string $current, string $default): html_table_cell { > $table = new html_table(); $cell = new html_table_cell($current); > $table->caption = get_string('adhoctasks', 'tool_task'); // Cron-style values must always be LTR. > $table->head = [ $cell->attributes['class'] = 'text-ltr'; > get_string('taskid', 'tool_task'), > get_string('nextruntime', 'tool_task'), // If the current value is default, that is all we want to do. > get_string('payload', 'tool_task'), if ($default === '*') { > $failedstr if ($current === '*') { > ]; return $cell; > } > $table->attributes['class'] = 'generaltable'; } else if ($default === 'R' ) { > $table->colclasses = []; if (is_numeric($current)) { > return $cell; > // For each task entry (row) show action buttons/logs link depending on the user permissions. } > $data = []; } else { > foreach ($tasks as $task) { if ($default === $current) { > $taskid = $task->get_id(); return $cell; > $started = $task->get_timestarted(); } > } > // Task id cell. > $taskidcellcontent = html_writer::span($taskid, 'task-id'); // Otherwise, highlight and show the default. > $taskidcell = new html_table_cell($taskidcellcontent); $cell->attributes['class'] .= ' table-warning'; > $taskidcell->header = true; $cell->text .= ' ' . html_writer::span( > $taskidcell->id = "task-$taskid"; get_string('defaultx', 'tool_task', $default), 'task-class'); > return $cell; > // Mark cell if task has failed. } > $faildelay = $task->get_fail_delay(); > $faildelaycell = new html_table_cell($faildelay ? $failedstr : ''); /** > if ($faildelay) { * Displays a warning on the page if cron is disabled. > $faildelaycell->attributes['class'] = 'table-danger'; * > } * @return string HTML code for information about cron being disabled > * @throws moodle_exception > // Prepares the next run time cell contents. */ > $nextrun = get_string('started', 'tool_task'); public function cron_disabled(): string { > if (!$started) { return $this->output->notification(get_string('crondisabled', 'tool_task'), 'warning'); > $nextruntime = $task->get_next_run_time(); } > $due = $nextruntime < $now; > $nextrun = $due ? userdate($nextruntime) : get_string('asap', 'tool_task'); /** > * Renders a link back to the scheduled tasks page (used from the 'run now' screen). > if ($wantruntasks && ($faildelay || $due)) { * > $nextrun .= ' '.html_writer::div( * @param string $taskclassname if specified, the list of tasks will scroll to show this task. > html_writer::link( * @return string HTML code > new moodle_url( */ > $adhocrunurl, public function link_back($taskclassname = '') { > ['id' => $taskid] $url = new moodle_url('/admin/tool/task/scheduledtasks.php'); > ), if ($taskclassname) { > get_string('runnow', 'tool_task') $url->param('lastchanged', $taskclassname); > ), } > 'task-runnow' return $this->render_from_template('tool_task/link_back', ['url' => $url]); > ); } > } } > } > > $data[] = new html_table_row([ > $taskidcell, > new html_table_cell($nextrun), > new html_table_cell($task->get_custom_data_as_string()), > $faildelaycell, > ]); > } > $table->data = $data; > > return $table; > } > > /** > * Displays a notification on ad hoc task run request. > * > * @return string HTML notification block for task initiated message > */ > public function adhoc_task_run(): string { > return $this->output->notification(get_string('adhoctaskrun', 'tool_task'), 'info'); > } >
< $canruntasks = \core\task\manager::is_runnable();
> $canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow');
< if (empty($CFG->preventscheduledtaskchanges)) {
> if (empty($CFG->preventscheduledtaskchanges) && !$task->is_overridden()) {
< $namecell = new html_table_cell($task->get_name() . "\n" . < html_writer::span('\\' . $classname, 'task-class text-ltr'));
> $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);
< < $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();
> $namecell->id = scheduled_task::get_html_id($classname);
< if (!$disabled && get_config('tool_task', 'enablerunnow') && $canruntasks ) {
> $canrunthistask = $canruntasks && $task->can_run(); > if ($canrunthistask) {
< $faildelaycell->text .= html_writer::div(html_writer::link(
> $faildelaycell->text .= html_writer::div( > $this->output->single_button(
< ['task' => $classname, 'sesskey' => sesskey()]), < get_string('clear')), 'task-clearfaildelay');
> ['task' => $classname]), > get_string('clear') > ), > 'task-runnow' > );
< if ($disabled) {
> if (!$task->is_enabled()) {
< $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component()); <
< if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
> > if (!$task->is_component_enabled() && !$task->get_run_if_component_disabled()) {