diff --git a/lib/tablelib.php b/lib/tablelib.php index 7be66836066..d2ec1e38642 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -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) { diff --git a/lib/tests/tablelib_test.php b/lib/tests/tablelib_test.php index 033b3d6a809..dba5b3ae9bf 100644 --- a/lib/tests/tablelib_test.php +++ b/lib/tests/tablelib_test.php @@ -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.*Next + // Search for pagination controls containing '1.*2.*Next'. $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.*Next + // Search for pagination controls containing '1.*2.*Next'. $this->expectOutputRegex('/' . get_string('sortby') . '/'); $this->run_table_test( diff --git a/mod/quiz/report/statistics/report.php b/mod/quiz/report/statistics/report.php index a0101c505b0..39a929863f6 100644 --- a/mod/quiz/report/statistics/report.php +++ b/mod/quiz/report/statistics/report.php @@ -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); } /**