Example #1
0
/**
 * Print a nicely formatted table.
 *
 * @param array $table is an object with several properties.
 * <ul>
 *     <li>$table->head - An array of heading names.
 *     <li>$table->align - An array of column alignments
 *     <li>$table->size  - An array of column sizes
 *     <li>$table->wrap - An array of "nowrap"s or nothing
 *     <li>$table->data[] - An array of arrays containing the data.
 *     <li>$table->width  - A percentage of the page
 *     <li>$table->tablealign  - Align the whole table
 *     <li>$table->cellpadding  - Padding on each cell
 *     <li>$table->cellspacing  - Spacing between cells
 *     <li>$table->class - class attribute to put on the table
 *     <li>$table->id - id attribute to put on the table.
 *     <li>$table->rowclass[] - classes to add to particular rows. (space-separated string)
 *     <li>$table->colclass[] - classes to add to every cell in a particular colummn. (space-separated string)
 *     <li>$table->summary - Description of the contents for screen readers.
 *     <li>$table->headspan can be used to make a heading span multiple columns.
 *     <li>$table->rotateheaders - Causes the contents of the heading cells to be rotated 90 degrees.
 * </ul>
 * @param bool $return whether to return an output string or echo now
 * @return boolean or $string
 * @todo Finish documenting this function
 */
function print_table($table, $return = false)
{
    $output = '';
    if (isset($table->align)) {
        foreach ($table->align as $key => $aa) {
            if ($aa) {
                $align[$key] = ' text-align:' . fix_align_rtl($aa) . ';';
                // Fix for RTL languages
            } else {
                $align[$key] = '';
            }
        }
    }
    if (isset($table->size)) {
        foreach ($table->size as $key => $ss) {
            if ($ss) {
                $size[$key] = ' width:' . $ss . ';';
            } else {
                $size[$key] = '';
            }
        }
    }
    if (isset($table->wrap)) {
        foreach ($table->wrap as $key => $ww) {
            if ($ww) {
                $wrap[$key] = ' white-space:nowrap;';
            } else {
                $wrap[$key] = '';
            }
        }
    }
    if (empty($table->width)) {
        $table->width = '80%';
    }
    if (empty($table->tablealign)) {
        $table->tablealign = 'center';
    }
    if (!isset($table->cellpadding)) {
        $table->cellpadding = '5';
    }
    if (!isset($table->cellspacing)) {
        $table->cellspacing = '1';
    }
    if (empty($table->class)) {
        $table->class = 'generaltable';
    }
    if (!empty($table->rotateheaders)) {
        $table->class .= ' rotateheaders';
    } else {
        $table->rotateheaders = false;
        // Makes life easier later.
    }
    $tableid = empty($table->id) ? '' : 'id="' . $table->id . '"';
    $output .= '<table width="' . $table->width . '" ';
    if (!empty($table->summary)) {
        $output .= " summary=\"{$table->summary}\"";
    }
    $output .= " cellpadding=\"{$table->cellpadding}\" cellspacing=\"{$table->cellspacing}\" class=\"{$table->class} boxalign{$table->tablealign}\" {$tableid}>\n";
    $countcols = 0;
    if (!empty($table->head)) {
        $countcols = count($table->head);
        $output .= '<tr>';
        $keys = array_keys($table->head);
        $lastkey = end($keys);
        foreach ($table->head as $key => $heading) {
            $classes = array('header', 'c' . $key);
            if (!isset($size[$key])) {
                $size[$key] = '';
            }
            if (!isset($align[$key])) {
                $align[$key] = '';
            }
            if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
                $colspan = ' colspan="' . $table->headspan[$key] . '"';
            } else {
                $colspan = '';
            }
            if ($key == $lastkey) {
                $classes[] = 'lastcol';
            }
            if (isset($table->colclasses[$key])) {
                $classes[] = $table->colclasses[$key];
            }
            if ($table->rotateheaders) {
                $wrapperstart = '<span>';
                $wrapperend = '</span>';
            } else {
                $wrapperstart = '';
                $wrapperend = '';
            }
            $output .= '<th style="' . $align[$key] . $size[$key] . ';white-space:nowrap;" class="' . implode(' ', $classes) . '" scope="col"' . $colspan . '>' . $wrapperstart . $heading . $wrapperend . '</th>';
        }
        $output .= '</tr>' . "\n";
    }
    if (!empty($table->data)) {
        $oddeven = 1;
        $keys = array_keys($table->data);
        $lastrowkey = end($keys);
        foreach ($table->data as $key => $row) {
            $oddeven = $oddeven ? 0 : 1;
            if (!isset($table->rowclass[$key])) {
                $table->rowclass[$key] = '';
            }
            if ($key == $lastrowkey) {
                $table->rowclass[$key] .= ' lastrow';
            }
            $output .= '<tr class="r' . $oddeven . ' ' . $table->rowclass[$key] . '">' . "\n";
            if ($row == 'hr' and $countcols) {
                $output .= '<td colspan="' . $countcols . '"><div class="tabledivider"></div></td>';
            } else {
                /// it's a normal row of data
                $keys2 = array_keys($row);
                $lastkey = end($keys2);
                foreach ($row as $key => $item) {
                    $classes = array('cell', 'c' . $key);
                    if (!isset($size[$key])) {
                        $size[$key] = '';
                    }
                    if (!isset($align[$key])) {
                        $align[$key] = '';
                    }
                    if (!isset($wrap[$key])) {
                        $wrap[$key] = '';
                    }
                    if ($key == $lastkey) {
                        $classes[] = 'lastcol';
                    }
                    if (isset($table->colclasses[$key])) {
                        $classes[] = $table->colclasses[$key];
                    }
                    $output .= '<td style="' . $align[$key] . $size[$key] . $wrap[$key] . '" class="' . implode(' ', $classes) . '">' . $item . '</td>';
                }
            }
            $output .= '</tr>' . "\n";
        }
    }
    $output .= '</table>' . "\n";
    if ($table->rotateheaders && can_use_rotated_text()) {
        require_js(array('yui_yahoo', 'yui_event', 'yui_dom'));
        require_js('course/report/progress/textrotate.js');
    }
    if ($return) {
        return $output;
    }
    echo $output;
    return true;
}
Example #2
0
    header('Content-Disposition: attachment; filename=progress.'.
        preg_replace('/[^a-z0-9-]/','_',textlib::strtolower(strip_tags($shortname))).'.csv');
    // Unicode byte-order mark for Excel
    if ($excel) {
        header('Content-Type: text/csv; charset=UTF-16LE');
        print chr(0xFF).chr(0xFE);
        $sep="\t".chr(0);
        $line="\n".chr(0);
    } else {
        header('Content-Type: text/csv; charset=UTF-8');
        $sep=",";
        $line="\n";
    }
} else {
    // Use SVG to draw sideways text if supported
    $svgcleverness = can_use_rotated_text();

    // Navigation and header
    $strreports = get_string("reports");
    $strcompletion = get_string('activitycompletion', 'completion');

    $PAGE->set_title($strcompletion);
    $PAGE->set_heading($course->fullname);
    echo $OUTPUT->header();

    if ($svgcleverness) {
        $PAGE->requires->js('/report/progress/textrotate.js');
        $PAGE->requires->js_function_call('textrotate_init', null, true);
    }

    // Handle groups (if enabled)
Example #3
0
 /**
  * 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;
 }