diff --git a/classes/output/workflowhistory/renderer.php b/classes/output/workflowhistory/renderer.php index 53a5ce8..319d2ba 100644 --- a/classes/output/workflowhistory/renderer.php +++ b/classes/output/workflowhistory/renderer.php @@ -24,8 +24,6 @@ namespace tool_trigger\output\workflowhistory; -use single_button; - defined('MOODLE_INTERNAL') || die; require_once(__DIR__.'/run.php'); require_once(__DIR__.'/workflow.php'); @@ -41,15 +39,16 @@ class renderer extends \plugin_renderer_base { /** * Sets the SQL for the table, and renders it. * - * @param \tool_trigger\output\workflowhistory\renderable $renderable the table to render - * @return void + * @param int $workflowid Workflow ID to render a history of. + * @param mixed $run Optional run ID to render specific run history. + * @param null|string $download Data format type. One of csv, xhtml, ods, etc */ - public function render_table($workflowid, $run = null) { + public function render_table(int $workflowid, $run = null, $download = null) { $url = new \moodle_url('/admin/tool/trigger/history.php', ['workflow' => $workflowid, 'run' => $run]); // Decide which table to render if run is given. if (!empty($run)) { - $renderable = new \tool_trigger\output\workflowhistory\runhistory_renderable('runhistory', $url); + $table = new runhistory_renderable('runhistory', $url); $sql = (object) [ 'fields' => '*', 'from' => '{tool_trigger_run_hist}', @@ -57,12 +56,20 @@ public function render_table($workflowid, $run = null) { 'params' => ['workflow' => $workflowid, 'run' => $run] ]; } else { - // We want to ouput some buttons before drawing the table. - $this->rerun_all_historic_button($workflowid); - echo ' '; - $this->rerun_all_current_button($workflowid); - - $renderable = new \tool_trigger\output\workflowhistory\workflowhistory_renderable('triggerhistory', $url); + if (!$download) { + // We want to ouput some buttons before drawing the table. + echo $this->rerun_all_historic_button($workflowid); + echo ' '; + echo $this->rerun_all_current_button($workflowid); + echo \html_writer::tag('br', ''); + } + + $table = new workflowhistory_renderable( + 'triggerhistory', + $url, + 100, + $download + ); $sql = (object) [ 'fields' => '*', 'from' => '{tool_trigger_workflow_hist}', @@ -72,8 +79,8 @@ public function render_table($workflowid, $run = null) { } // Then output the table. - $renderable->sql = $sql; - $renderable->out($renderable->pagesize, true); + $table->sql = $sql; + $table->out($table->pagesize, true); } public function step_actions_button($step) { @@ -192,24 +199,26 @@ public function run_actions_button($run, $statusonly = false) { /** * This function outputs the rerun all historic errors button. * - * @return void + * @param int $workflowid Workflow ID to render a history of. + * @return string */ - private function rerun_all_historic_button($workflowid) { + private function rerun_all_historic_button(int $workflowid) { $url = new \moodle_url('/admin/tool/trigger/history.php', ['action' => 'rerunallhist', 'sesskey' => sesskey(), 'id' => $workflowid, 'workflow' => $workflowid]); $btn = new \single_button($url, get_string('rerunallhist', 'tool_trigger'), 'get', true); - echo $this->render($btn); + return $this->render($btn); } /** * This function outputs the rerun all current errors button. * - * @return void + * @param int $workflowid Workflow ID to render a history of. + * @return string */ private function rerun_all_current_button($workflowid) { $url = new \moodle_url('/admin/tool/trigger/history.php', ['action' => 'rerunallcurr', 'sesskey' => sesskey(), 'id' => $workflowid, 'workflow' => $workflowid]); $btn = new \single_button($url, get_string('rerunallcurr', 'tool_trigger'), 'get', true); - echo $this->render($btn); + return $this->render($btn); } } diff --git a/classes/output/workflowhistory/workflow.php b/classes/output/workflowhistory/workflow.php index 1ff83da..c178588 100644 --- a/classes/output/workflowhistory/workflow.php +++ b/classes/output/workflowhistory/workflow.php @@ -45,8 +45,9 @@ class workflowhistory_renderable extends \table_sql implements \renderable { * @param string $uniqueid Unique id of form. * @param \moodle_url $url Url where this table is displayed. * @param int $perpage Number of rules to display per page. + * @param null|string $download Data format type. One of csv, xhtml, ods, etc */ - public function __construct($uniqueid, \moodle_url $url, $perpage = 100) { + public function __construct($uniqueid, \moodle_url $url, $perpage = 100, $download = null) { parent::__construct($uniqueid); $this->set_attribute('id', 'tooltriggerworkflowhistory_table'); @@ -78,8 +79,10 @@ public function __construct($uniqueid, \moodle_url $url, $perpage = 100) { $this->collapsible(false); $this->sortable(false, 'number', SORT_DESC); $this->pageable(true); - $this->is_downloadable(false); $this->define_baseurl($url); + $this->is_downloadable(true); + $this->show_download_buttons_at([TABLE_P_BOTTOM]); + $this->is_downloading($download, 'workflow_history'); } public function col_id($run) { @@ -118,15 +121,19 @@ public function col_runstatus($run) { global $DB; // Return a badge for the status. if (!empty($run->errorstep)) { - return \html_writer::tag('span', get_string('errorstep', 'tool_trigger', $run->errorstep + 1), + $text = \html_writer::tag('span', get_string('errorstep', 'tool_trigger', $run->errorstep + 1), array('class' => 'badge badge-warning')); // Handle debounce statuses. - } else if (!empty($run->failedstep) && ((int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_CANCELLED)) { - return \html_writer::tag('span', get_string('cancelled'), array('class' => 'badge badge-info')); + } else if (!empty($run->failedstep) && + ((int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_CANCELLED)) { + $text = \html_writer::tag( + 'span', get_string('cancelled'), + array('class' => 'badge badge-info') + ); } else if (!empty($run->failedstep) && ((int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_DEFERRED)) { - return \html_writer::tag('span', get_string('deferred', 'tool_trigger'), array('class' => 'badge badge-info')); + $text = \html_writer::tag('span', get_string('deferred', 'tool_trigger'), array('class' => 'badge badge-info')); } else if (!empty($run->failedstep)) { - return \html_writer::tag('span', get_string('failedstep', 'tool_trigger', $run->failedstep + 1), + $text = \html_writer::tag('span', get_string('failedstep', 'tool_trigger', $run->failedstep + 1), array('class' => 'badge badge-danger')); } else { // Find the number of steps executed. @@ -138,51 +145,29 @@ public function col_runstatus($run) { } else { $string = get_string('runpassednonum', 'tool_trigger'); } - return \html_writer::tag('span', $string, array('class' => 'badge badge-success')); + $text = \html_writer::tag('span', $string, array('class' => 'badge badge-success')); } - } - - public function col_actions($run) { - global $PAGE; - $renderer = $PAGE->get_renderer('tool_trigger', 'workflowhistory'); - - $statusonly = !empty($run->failedstep) && - ((int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_CANCELLED || - (int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_DEFERRED); + if ($this->is_downloading()) { + $text = html_to_text($text); + } - return $renderer->run_actions_button($run, $statusonly); + return $text; } - /** - * This is a copy of the tablelib implementation, - * but removes no-overflow class from the parent div. - * Allows dropdowns to overflow. - */ - public function start_html() { - global $OUTPUT; + public function col_actions($run) { + global $PAGE; - // Render button to allow user to reset table preferences. - echo $this->render_reset_button(); + if ($this->is_downloading()) { + return ''; + } else { + $renderer = $PAGE->get_renderer('tool_trigger', 'workflowhistory'); - // Do we need to print initial bars? - $this->print_initials_bar(); + $statusonly = !empty($run->failedstep) && + ((int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_CANCELLED || + (int) $run->failedstep === \tool_trigger\task\process_workflows::STATUS_DEFERRED); - // Paging bar. - if ($this->use_pages) { - $pagingbar = new \paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl); - $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE]; - echo $OUTPUT->render($pagingbar); + return $renderer->run_actions_button($run, $statusonly); } - - if (in_array(TABLE_P_TOP, $this->showdownloadbuttonsat)) { - echo $this->download_buttons(); - } - - $this->wrap_html_start(); - // Start of main data table. - - echo \html_writer::start_tag('div'); - echo \html_writer::start_tag('table', $this->attributes); } } diff --git a/history.php b/history.php index 0e94fe7..ad5b67f 100644 --- a/history.php +++ b/history.php @@ -37,6 +37,8 @@ $workflowid = required_param('workflow', PARAM_INT); $runid = optional_param('run', null, PARAM_INT); $action = optional_param('action', null, PARAM_TEXT); +$download = optional_param('download', null, PARAM_ALPHA); + if (!empty($action)) { $actionid = required_param('id', PARAM_INT); } @@ -228,16 +230,22 @@ $PAGE->navbar->add(get_string('viewdetailedrun', 'tool_trigger'), $navbarurl); } -// Build the page output if not performing an action and being redirected. -echo $OUTPUT->header(); -echo $OUTPUT->heading(get_string('workflowviewhistory', 'tool_trigger')); $renderer = $PAGE->get_renderer('tool_trigger', 'workflowhistory'); -if (!$workflow->debug) { - \core\notification::add( - get_string('warningdebugging', 'tool_trigger', $workflowid), - \core\output\notification::NOTIFY_WARNING - ); +if ($download) { + \core\session\manager::write_close(); + $renderer->render_table($workflowid, $runid, $download); + die(); +} else { + // Build the page output if not performing an action and being redirected. + if (!$workflow->debug) { + \core\notification::add( + get_string('warningdebugging', 'tool_trigger', $workflowid), + \core\output\notification::NOTIFY_WARNING + ); + } + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('workflowviewhistory', 'tool_trigger')); + $renderer->render_table($workflowid, $runid, $download); + echo $OUTPUT->footer(); } -$renderer->render_table($workflowid, $runid); -echo $OUTPUT->footer();