Commit eab63e82 authored by Mikel Martín Corrales's avatar Mikel Martín Corrales
Browse files

MDL-72565 tablelib: Add modifications needed for report card view

- new 'set_columns_attributes' method created to add column attributes included in every column cell.
- new 'get_row_cells_html' method created from 'get_row_html', so it can be overriden individually.
parent aaa7a5a4
......@@ -87,6 +87,9 @@ class flexible_table {
/** @var string[] Columns that are expected to contain a users fullname. */
protected $userfullnamecolumns = ['fullname'];
/** @var array[] Attributes for each column */
private $columnsattributes = [];
/**
* @var bool Whether or not to store table properties in the user_preferences table.
*/
......@@ -426,6 +429,16 @@ class flexible_table {
}
}
/**
* Sets the given $attributes to $this->columnsattributes.
* Column attributes will be added to every cell in the column.
*
* @param array[] $attributes e.g. ['c0_firstname' => ['data-foo' => 'bar']]
*/
public function set_columnsattributes(array $attributes): void {
$this->columnsattributes = $attributes;
}
/**
* Sets all columns' $propertys to the given $value in $this->column_style.
* @param int $property
......@@ -454,12 +467,14 @@ class flexible_table {
$this->columns = array();
$this->column_style = array();
$this->column_class = array();
$this->columnsattributes = [];
$colnum = 0;
foreach ($columns as $column) {
$this->columns[$column] = $colnum++;
$this->column_style[$column] = array();
$this->column_class[$column] = '';
$this->columnsattributes[$column] = [];
$this->column_suppress[$column] = false;
}
}
......@@ -1101,34 +1116,7 @@ class flexible_table {
array('class' => 'tabledivider')), array('colspan' => $colcount));
} else {
$colbyindex = array_flip($this->columns);
foreach ($row as $index => $data) {
$column = $colbyindex[$index];
$attributes = [
'class' => "cell c{$index}" . $this->column_class[$column],
'id' => "{$rowid}_c{$index}",
'style' => $this->make_styles_string($this->column_style[$column]),
];
$celltype = 'td';
if ($this->headercolumn && $column == $this->headercolumn) {
$celltype = 'th';
$attributes['scope'] = 'row';
}
if (empty($this->prefs['collapse'][$column])) {
if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
$content = ' ';
} else {
$content = $data;
}
} else {
$content = ' ';
}
$html .= html_writer::tag($celltype, $content, $attributes);
}
$html .= $this->get_row_cells_html($rowid, $row, $suppress_lastrow);
}
$html .= html_writer::end_tag('tr');
......@@ -1141,6 +1129,49 @@ class flexible_table {
return $html;
}
/**
* Generate html code for the row cells.
*
* @param string $rowid
* @param array $row
* @param array|null $suppresslastrow
* @return string
*/
public function get_row_cells_html(string $rowid, array $row, ?array $suppresslastrow): string {
$html = '';
$colbyindex = array_flip($this->columns);
foreach ($row as $index => $data) {
$column = $colbyindex[$index];
$attributes = [
'class' => "cell c{$index}" . $this->column_class[$column],
'id' => "{$rowid}_c{$index}",
'style' => $this->make_styles_string($this->column_style[$column]),
];
$celltype = 'td';
if ($this->headercolumn && $column == $this->headercolumn) {
$celltype = 'th';
$attributes['scope'] = 'row';
}
$attributes += $this->columnsattributes[$column] ?? [];
if (empty($this->prefs['collapse'][$column])) {
if ($this->column_suppress[$column] && $suppresslastrow !== null && $suppresslastrow[$index] === $data) {
$content = ' ';
} else {
$content = $data;
}
} else {
$content = ' ';
}
$html .= html_writer::tag($celltype, $content, $attributes);
}
return $html;
}
/**
* This function is not part of the public api.
*/
......
......@@ -2,6 +2,9 @@ This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
=== 4.0 ===
* New method flexible_table::set_columnsattributes() has been introduced to add column attributes applied in every cell.
* New method flexible_table::get_row_cells_html() has been introduced, extracted from flexible_table::get_row_html
so it can be overriden individually.
* Since Boxnet has been remove from core then boxnet_client() class has been removed from core too.
* New navigation classes to mimic the new navigation project. The existing navigation callbacks are still available and
will be called. The following behaviour will be the new standard for nodes added via callbacks:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment