Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Fix issue #123: make workflow table downloadable #135

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions classes/output/workflowhistory/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -41,28 +39,37 @@ 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}',
'where' => 'workflowid = :workflow AND runid = :run',
'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}',
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
73 changes: 29 additions & 44 deletions classes/output/workflowhistory/workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}
28 changes: 18 additions & 10 deletions history.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();