mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
MDL-41755 tablelib : added a function to add multiple rows at once
This commit is contained in:
parent
692d247a3a
commit
ac3e5ed7ba
@ -629,19 +629,44 @@ class flexible_table {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a row of data to the table. This function takes an array with
|
||||
* column names as keys.
|
||||
* Add a row of data to the table. This function takes an array or object with
|
||||
* column names as keys or property names.
|
||||
*
|
||||
* It ignores any elements with keys that are not defined as columns. It
|
||||
* puts in empty strings into the row when there is no element in the passed
|
||||
* array corresponding to a column in the table. It puts the row elements in
|
||||
* the proper order.
|
||||
* @param $rowwithkeys array
|
||||
* the proper order (internally row table data is stored by in arrays with
|
||||
* a numerical index corresponding to the column number).
|
||||
*
|
||||
* @param object|array $rowwithkeys array keys or object property names are column names,
|
||||
* as defined in call to define_columns.
|
||||
* @param string $classname CSS class name to add to this row's tr tag.
|
||||
*/
|
||||
function add_data_keyed($rowwithkeys, $classname = '') {
|
||||
$this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a number of rows to the table at once. And optionally finish output after they have been added.
|
||||
*
|
||||
* @param (object|array|null)[] $rowstoadd Array of rows to add to table, a null value in array adds a separator row. Or a
|
||||
* object or array is added to table. We expect properties for the row array as would be
|
||||
* passed to add_data_keyed.
|
||||
* @param bool $finish
|
||||
*/
|
||||
public function format_and_add_array_of_rows($rowstoadd, $finish = true) {
|
||||
foreach ($rowstoadd as $row) {
|
||||
if (is_null($row)) {
|
||||
$this->add_separator();
|
||||
} else {
|
||||
$this->add_data_keyed($this->format_row($row));
|
||||
}
|
||||
}
|
||||
if ($finish) {
|
||||
$this->finish_output(!$this->is_downloading());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a seperator line to table.
|
||||
*/
|
||||
@ -715,11 +740,19 @@ class flexible_table {
|
||||
}
|
||||
|
||||
/**
|
||||
* Call appropriate methods on this table class to perform any processing on values before displaying in table.
|
||||
* Takes raw data from the database and process it into human readable format, perhaps also adding html linking when
|
||||
* displaying table as html, adding a div wrap, etc.
|
||||
*
|
||||
* @param array $row row of data from db used to make one row of the table.
|
||||
* See for example col_fullname below which will be called for a column whose name is 'fullname'.
|
||||
*
|
||||
* @param array|object $row row of data from db used to make one row of the table.
|
||||
* @return array one row for the table, added using add_data_keyed method.
|
||||
*/
|
||||
function format_row($row) {
|
||||
if (is_array($row)) {
|
||||
$row = (object)$row;
|
||||
}
|
||||
$formattedrow = array();
|
||||
foreach (array_keys($this->columns) as $column) {
|
||||
$colmethodname = 'col_'.$column;
|
||||
@ -743,9 +776,13 @@ class flexible_table {
|
||||
* then you need to override $this->useridfield to point at the correct
|
||||
* field for the user id.
|
||||
*
|
||||
* @param object $row the data from the db containing all fields from the
|
||||
* users table necessary to construct the full name of the user in
|
||||
* current language.
|
||||
* @return string contents of cell in column 'fullname', for this row.
|
||||
*/
|
||||
function col_fullname($row) {
|
||||
global $COURSE, $CFG;
|
||||
global $COURSE;
|
||||
|
||||
$name = fullname($row);
|
||||
if ($this->download) {
|
||||
|
@ -67,7 +67,39 @@ class core_tablelib_testcase extends basic_testcase {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a table with properties as passed in params, add data and output html.
|
||||
*
|
||||
* @param string[] $columns
|
||||
* @param string[] $headers
|
||||
* @param bool $sortable
|
||||
* @param bool $collapsible
|
||||
* @param string[] $suppress
|
||||
* @param string[] $nosorting
|
||||
* @param (array|object)[] $data
|
||||
* @param int $pagesize
|
||||
*/
|
||||
protected function run_table_test($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, $data, $pagesize) {
|
||||
$table = $this->create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting);
|
||||
$table->pagesize($pagesize, count($data));
|
||||
foreach ($data as $row) {
|
||||
$table->add_data_keyed($row);
|
||||
}
|
||||
$table->finish_output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a table with properties as passed in params.
|
||||
*
|
||||
* @param string[] $columns
|
||||
* @param string[] $headers
|
||||
* @param bool $sortable
|
||||
* @param bool $collapsible
|
||||
* @param string[] $suppress
|
||||
* @param string[] $nosorting
|
||||
* @return flexible_table
|
||||
*/
|
||||
protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting) {
|
||||
$table = new flexible_table('tablelib_test');
|
||||
|
||||
$table->define_columns($columns);
|
||||
@ -84,13 +116,8 @@ class core_tablelib_testcase extends basic_testcase {
|
||||
$table->no_sorting($column);
|
||||
}
|
||||
|
||||
|
||||
$table->setup();
|
||||
$table->pagesize($pagesize, count($data));
|
||||
foreach ($data as $row) {
|
||||
$table->add_data_keyed($row);
|
||||
}
|
||||
$table->finish_output();
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function test_empty_table() {
|
||||
@ -113,7 +140,7 @@ class core_tablelib_testcase extends basic_testcase {
|
||||
$columns = $this->generate_columns(2);
|
||||
$headers = $this->generate_headers(2);
|
||||
|
||||
// Search for pagination controls containing 1.*2</a>.*Next</a>
|
||||
// Search for pagination controls containing '1.*2</a>.*Next</a>'.
|
||||
$this->expectOutputRegex('/1.*2<\/a>.*' . get_string('next') . '<\/a>/');
|
||||
|
||||
$this->run_table_test(
|
||||
@ -134,7 +161,7 @@ class core_tablelib_testcase extends basic_testcase {
|
||||
$columns = $this->generate_columns(2);
|
||||
$headers = $this->generate_headers(2);
|
||||
|
||||
// Search for 'hide' links in the column headers
|
||||
// Search for 'hide' links in the column headers.
|
||||
$this->expectOutputRegex('/' . get_string('hide') . '/');
|
||||
|
||||
$this->run_table_test(
|
||||
@ -179,7 +206,7 @@ class core_tablelib_testcase extends basic_testcase {
|
||||
$columns = $this->generate_columns(2);
|
||||
$headers = $this->generate_headers(2);
|
||||
|
||||
// Search for pagination controls containing 1.*2</a>.*Next</a>
|
||||
// Search for pagination controls containing '1.*2</a>.*Next</a>'.
|
||||
$this->expectOutputRegex('/' . get_string('sortby') . '/');
|
||||
|
||||
$this->run_table_test(
|
||||
|
@ -416,25 +416,15 @@ class quiz_statistics_report extends quiz_default_report {
|
||||
* variants.
|
||||
*/
|
||||
protected function output_quiz_structure_analysis_table($questionstats) {
|
||||
$tooutput = array();
|
||||
foreach ($questionstats->get_all_slots() as $slot) {
|
||||
// Output the data for these question statistics.
|
||||
$this->table->add_data_keyed($this->table->format_row($questionstats->for_slot($slot)));
|
||||
$tooutput[] = $questionstats->for_slot($slot);
|
||||
|
||||
$limitvariants = !$this->table->is_downloading();
|
||||
$this->add_array_of_rows_to_table($questionstats->all_subq_and_variant_stats_for_slot($slot, $limitvariants));
|
||||
|
||||
}
|
||||
|
||||
$this->table->finish_output(!$this->table->is_downloading());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \core_question\statistics\questions\calculated[] $statstoadd
|
||||
*/
|
||||
protected function add_array_of_rows_to_table($statstoadd) {
|
||||
foreach ($statstoadd as $stattoadd) {
|
||||
$this->table->add_data_keyed($this->table->format_row($stattoadd));
|
||||
$tooutput = array_merge($tooutput, $questionstats->all_subq_and_variant_stats_for_slot($slot, $limitvariants));
|
||||
}
|
||||
$this->table->format_and_add_array_of_rows($tooutput);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user