/** * Render a HTML table * * @param object $table {@link html_table} instance containing all the information needed * @return string the HTML to output. */ public function table(html_table $table) { $table = clone $table; $table->prepare(); $attributes = array('id' => $table->id, 'width' => $table->width, 'summary' => $table->summary, 'cellpadding' => $table->cellpadding, 'cellspacing' => $table->cellspacing, 'class' => $table->get_classes_string()); $output = $this->output_start_tag('table', $attributes) . "\n"; $countcols = 0; if (!empty($table->head)) { $countcols = count($table->head); $output .= $this->output_start_tag('thead', array()) . "\n"; $output .= $this->output_start_tag('tr', array()) . "\n"; $keys = array_keys($table->head); $lastkey = end($keys); foreach ($table->head as $key => $heading) { $classes = array('header', 'c' . $key); if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) { $colspan = $table->headspan[$key]; $countcols += $table->headspan[$key] - 1; } else { $colspan = ''; } if ($key == $lastkey) { $classes[] = 'lastcol'; } if (isset($table->colclasses[$key])) { $classes[] = $table->colclasses[$key]; } if ($table->rotateheaders) { // we need to wrap the heading content $heading = $this->output_tag('span', '', $heading); } $attributes = array('style' => $table->align[$key] . $table->size[$key] . 'white-space:nowrap;', 'class' => moodle_renderer_base::prepare_classes($classes), 'scope' => 'col', 'colspan' => $colspan); $output .= $this->output_tag('th', $attributes, $heading) . "\n"; } $output .= $this->output_end_tag('tr') . "\n"; $output .= $this->output_end_tag('thead') . "\n"; } if (!empty($table->data)) { $oddeven = 1; $keys = array_keys($table->data); $lastrowkey = end($keys); $output .= $this->output_start_tag('tbody', array()) . "\n"; foreach ($table->data as $key => $row) { if ($row === 'hr' && $countcols) { $output .= $this->output_tag('td', array('colspan' => $countcols), $this->output_tag('div', array('class' => 'tabledivider'), '')) . "\n"; } else { // Convert array rows to html_table_rows and cell strings to html_table_cell objects if (!$row instanceof html_table_row) { $newrow = new html_table_row(); foreach ($row as $unused => $item) { $cell = new html_table_cell(); $cell->text = $item; $newrow->cells[] = $cell; } $row = $newrow; } $oddeven = $oddeven ? 0 : 1; if (isset($table->rowclasses[$key])) { $row->add_classes(array_unique(moodle_html_component::clean_classes($table->rowclasses[$key]))); } $row->add_class('r' . $oddeven); if ($key == $lastrowkey) { $row->add_class('lastrow'); } $output .= $this->output_start_tag('tr', array('class' => $row->get_classes_string(), 'style' => $row->style, 'id' => $row->id)) . "\n"; $keys2 = array_keys($row->cells); $lastkey = end($keys2); foreach ($row->cells as $key => $cell) { if (isset($table->colclasses[$key])) { $cell->add_classes(array_unique(moodle_html_component::clean_classes($table->colclasses[$key]))); } $cell->add_classes('cell'); $cell->add_classes('c' . $key); if ($key == $lastkey) { $cell->add_classes('lastcol'); } $tdstyle = ''; $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : ''; $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : ''; $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : ''; $tdattributes = array('style' => $tdstyle . $cell->style, 'colspan' => $cell->colspan, 'rowspan' => $cell->rowspan, 'id' => $cell->id, 'class' => $cell->get_classes_string(), 'abbr' => $cell->abbr, 'scope' => $cell->scope); $tagtype = 'td'; if ($cell->header) { $tagtype = 'th'; } $output .= $this->output_tag($tagtype, $tdattributes, $cell->text) . "\n"; } } $output .= $this->output_end_tag('tr') . "\n"; } $output .= $this->output_end_tag('tbody') . "\n"; } $output .= $this->output_end_tag('table') . "\n"; if ($table->rotateheaders && can_use_rotated_text()) { $this->page->requires->yui_lib('event'); $this->page->requires->js('course/report/progress/textrotate.js'); } return $output; }
/** * @see moodle_html_component::prepare() * @return void */ public function prepare() { $this->skipid = self::$idcounter; self::$idcounter += 1; $this->add_class('sideblock'); if (empty($this->blockinstanceid) || !strip_tags($this->title)) { $this->collapsible = self::NOT_HIDEABLE; } if ($this->collapsible == self::HIDDEN) { $this->add_class('hidden'); } if (!empty($this->controls)) { $this->add_class('block_with_controls'); } parent::prepare(); }