/** * Fill table with data * * @return void */ public function table_fill() { if ($this->is_exporting()) { $this->table->set_export($this->export); $this->paging->set_export($this->export); } $total = $this->count_records($this->filter_sql()); if ($this->config->maxrows == 0 or $total <= $this->config->maxrows) { $rs = $this->get_recordset($this->filter_sql(), $this->table->get_sql_sort(), $this->paging->get_limitfrom(), $this->paging->get_limitnum()); foreach ($rs as $row) { $this->table_fill_row($row); } $rs->close(); if ($this->paging->get_perpage() > 0) { $this->paging->set_total($total); } } else { $this->table->set_emptymessage(get_string('toomanyrows', 'local_mr', (object) array('total' => $total, 'max' => $this->config->maxrows))); } }
/** * Generate table rows as JSON * * @param mr_html_table $table Table instance * @param mr_html_paging $paging Paging instance * @return void * @todo Make this not a hack? */ public function mr_html_table_json(mr_html_table $table, mr_html_paging $paging) { if (optional_param('tjson', 0, PARAM_BOOL)) { $json = new stdClass(); $json->recordsReturned = (int) count($table->get_rows()); $json->totalRecords = (int) $paging->get_total(); $json->startIndex = (int) $paging->get_page(); $json->sort = $table->get_sort(); $json->pageSize = (int) $paging->get_perpage(); $json->records = array(); $json->emptyMessage = $table->get_emptymessage(); if ($table->get_order() == SORT_ASC) { $json->dir = 'asc'; } else { $json->dir = 'desc'; } // If we are returning 0 records, we probably don't have any at all if ($json->recordsReturned == 0) { $json->totalRecords = 0; } $columns = $table->get_columns(true); $htmlrows = $this->convert_to_htmlrows($table); foreach ($htmlrows as $htmlrow) { $position = 0; foreach ($columns as $column) { if (array_key_exists($position, $htmlrow->cells)) { $value = $htmlrow->cells[$position]->text; } else { $value = ''; } $record[$column->get_name()] = $value; $position++; } $json->records[] = (object) $record; } echo json_encode($json); die; } }