Exemplo n.º 1
0
function organizer_display_printable_table($allowsubmissionsfromdate, $timedue, $columns, $slots, $entriesperpage = false, $textsize = '10', $orientation = 'L', $headerfooter = true, $filename = '')
{
    global $USER;
    list($cm, $course, $organizer, $context) = organizer_get_course_module_data();
    $columnwitdh = array();
    $titles = array();
    $columnformats = array();
    $tsort = isset($_SESSION['organizer_tsort']) ? $_SESSION['organizer_tsort'] : "";
    if ($tsort != "") {
        $order = "ASC";
        if (substr($tsort, strlen($tsort) - strlen("DESC")) == "DESC") {
            $tsort = substr($tsort, 0, strlen($tsort) - strlen("DESC"));
            $order = "DESC";
        }
    }
    $colorder = array();
    $dosort = false;
    $i = 0;
    foreach ($columns as $column) {
        if ($column != "") {
            $titles[] = get_string("th_{$column}", 'organizer');
            if ($tsort == $column) {
                $dosort = true;
            }
            $colorder[$column] = $i++;
            switch ($column) {
                case 'datetime':
                    $columnwitdh[] = array('value' => 64, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'location':
                    $columnwitdh[] = array('value' => 48, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'teacher':
                    $columnwitdh[] = array('value' => 32, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'groupname':
                    $columnwitdh[] = array('value' => 32, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'participant':
                    $columnwitdh[] = array('value' => 32, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'idnumber':
                    $columnwitdh[] = array('value' => 24, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'attended':
                    $columnwitdh[] = array('value' => 12, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'grade':
                    $columnwitdh[] = array('value' => 18, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 0, 'align' => 'C');
                    break;
                case 'feedback':
                    $columnwitdh[] = array('value' => 64, 'mode' => 'Relativ');
                    $columnformats[] = array('fill' => 1, 'align' => 'L');
                    break;
            }
        }
    }
    switch ($tsort) {
        case "datetime":
            $sort = "starttime";
            break;
        case "location":
            $sort = "s.location";
            break;
        case "teacher":
            $sort = "teacherfirstname";
            break;
        case "participant":
            $sort = "u.lastname";
            break;
        case "idnumber":
            $sort = "u.idnumber";
            break;
        case "attended":
            $sort = "a.attended";
            break;
        case "grade":
            $sort = "a.grade";
            break;
        case "feedback":
            $sort = "a.feedback";
            break;
        default:
            $sort = NULL;
    }
    if (!isset($order)) {
        $order = "";
    } else {
        if ($order != "DESC" && $order != "ASC") {
            $order = "";
        }
    }
    if ($dosort) {
        $dosort = $sort . ' ' . $order;
    } else {
        $dosort = "";
    }
    $allowsubmissionsfromdate = $allowsubmissionsfromdate ? userdate($allowsubmissionsfromdate) : get_string('pdf_notactive', 'organizer');
    $timedue = $timedue ? userdate($timedue) : get_string('pdf_notactive', 'organizer');
    $mpdftable = new MTablePDF($orientation, $columnwitdh);
    $mpdftable->SetTitle(get_string('modulename', 'organizer') . " " . $organizer->name . " - " . get_string('printout', 'organizer'));
    $mpdftable->setRowsperPage($entriesperpage);
    $mpdftable->ShowHeaderFooter($headerfooter);
    $mpdftable->SetFontSize($textsize);
    $mpdftable->setHeaderText(get_string('course') . ':', "{$course->idnumber} {$course->fullname}", get_string('availablefrom', 'organizer') . ':', $allowsubmissionsfromdate, get_string('date') . ':', userdate(time()), get_string('modulename', 'organizer') . ':', $organizer->name, get_string('duedate', 'organizer') . ':', $timedue, '', '');
    $mpdftable->setTitles($titles);
    $mpdftable->setColumnFormat($columnformats);
    $entries = fetch_table_entries($slots, $dosort);
    $rowspan = 0;
    foreach ($entries as $entry) {
        $row = array();
        if ($rowspan == 0) {
            $rowspan = $entry->rowspan;
        }
        foreach ($columns as $column) {
            switch ($column) {
                // these columns may have rowspan
                case 'datetime':
                    if ($rowspan != $entry->rowspan) {
                        $row[] = null;
                    } else {
                        $datetime = userdate($entry->starttime, get_string('fulldatetimetemplate', 'organizer')) . ' - ' . userdate($entry->starttime + $entry->duration, get_string('timetemplate', 'organizer'));
                        $row[] = array('data' => $datetime, 'rowspan' => $rowspan - 1);
                    }
                    break;
                case 'location':
                    if ($rowspan != $entry->rowspan) {
                        $row[] = null;
                    } else {
                        $row[] = array('data' => $entry->location, 'rowspan' => $rowspan - 1);
                    }
                    break;
                case 'teacher':
                    if ($rowspan != $entry->rowspan) {
                        $row[] = null;
                    } else {
                        $a = new stdClass();
                        $a->firstname = $entry->teacherfirstname;
                        $a->lastname = $entry->teacherlastname;
                        $name = get_string('fullname_template', 'organizer', $a);
                        $row[] = array('data' => $name, 'rowspan' => $rowspan - 1);
                    }
                    break;
                case 'groupname':
                    if ($rowspan != $entry->rowspan) {
                        $row[] = null;
                    } else {
                        $groupname = isset($entry->groupname) ? $entry->groupname : '';
                        $row[] = array('data' => $groupname, 'rowspan' => $rowspan - 1);
                    }
                    break;
                    // these columns cannot have rowspan
                // these columns cannot have rowspan
                case 'participant':
                    $a = new stdClass();
                    $a->firstname = $entry->firstname;
                    $a->lastname = $entry->lastname;
                    $name = get_string('fullname_template', 'organizer', $a);
                    $row[] = array('data' => $name, 'rowspan' => 0);
                    break;
                case 'idnumber':
                    $idnumber = isset($entry->idnumber) && $entry->idnumber !== '' ? $entry->idnumber : '';
                    $row[] = array('data' => $idnumber, 'rowspan' => 0);
                    break;
                case 'attended':
                    $attended = isset($entry->attended) ? $entry->attended == 1 ? get_string('yes') : get_string('no') : '';
                    $row[] = array('data' => $attended, 'rowspan' => 0);
                    break;
                case 'grade':
                    $grade = isset($entry->grade) ? sprintf("%01.2f", $entry->grade) : '';
                    $row[] = array('data' => $grade, 'rowspan' => 0);
                    break;
                case 'feedback':
                    $feedback = isset($entry->feedback) && $entry->feedback !== '' ? $entry->feedback : '';
                    $row[] = array('data' => $feedback, 'rowspan' => 0);
                    break;
            }
        }
        $mpdftable->addRow($row);
        $rowspan--;
    }
    $format = optional_param('format', 'pdf', PARAM_TEXT);
    switch ($format) {
        case 'xlsx':
            $mpdftable->setOutputFormat(MTablePDF::OUTPUT_FORMAT_XLSX);
            break;
        case 'xls':
            $mpdftable->setOutputFormat(MTablePDF::OUTPUT_FORMAT_XLS);
            break;
        case 'ods':
            $mpdftable->setOutputFormat(MTablePDF::OUTPUT_FORMAT_ODS);
            break;
        case 'csv_comma':
            $mpdftable->setOutputFormat(MTablePDF::OUTPUT_FORMAT_CSV_COMMA);
            break;
        case 'csv_tab':
            $mpdftable->setOutputFormat(MTablePDF::OUTPUT_FORMAT_CSV_TAB);
            break;
        default:
            $mpdftable->setOutputFormat(MTablePDF::OUTPUT_FORMAT_PDF);
            break;
    }
    $mpdftable->generate($filename);
    die;
}
 private function _create_preview_table($columns)
 {
     global $PAGE, $OUTPUT, $cm;
     $jsmodule = array('name' => 'mod_organizer', 'fullpath' => '/mod/organizer/module.js', 'requires' => array('node', 'node-event-delegate'));
     $PAGE->requires->js_init_call('M.mod_organizer.init_organizer_print_slots_form', null, false, $jsmodule);
     $table = new html_table();
     $table->id = 'print_preview';
     $table->attributes['class'] = 'boxaligncenter';
     $tsort = isset($_SESSION['organizer_tsort']) ? $_SESSION['organizer_tsort'] : "";
     if ($tsort != "") {
         $order = "ASC";
         if (substr($tsort, strlen($tsort) - strlen("DESC")) == "DESC") {
             $tsort = substr($tsort, 0, strlen($tsort) - strlen("DESC"));
             $order = "DESC";
         }
     }
     $icon_up = ' <img src="' . $OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="up" />';
     $icon_down = ' <img src=' . $OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="down" />';
     $header = array();
     foreach ($columns as $column) {
         $content = '<a href="?id=' . $cm->id . '&sesskey=' . sesskey() . '&action=print&tsort=' . $column;
         $icon = "";
         if ($column == $tsort) {
             if ($order == "ASC") {
                 $content .= "DESC";
                 $icon = $icon_up;
             } else {
                 $icon = $icon_down;
             }
         }
         $content .= '" name="' . $column . '_cell">' . get_string("th_{$column}", 'organizer') . $icon;
         $content .= '</a>';
         $imgattr = array('src' => $OUTPUT->pix_url('t/switch_minus'), 'alt' => get_string('hide'), 'id' => "toggle_{$column}", 'style' => 'cursor: pointer', 'title' => get_string("th_{$column}", 'organizer'));
         $content .= ' ' . html_writer::empty_tag('img', $imgattr);
         $cell = new html_table_cell($content);
         $cell->header = true;
         $header[] = $cell;
     }
     $table->head = $header;
     switch ($tsort) {
         case "datetime":
             $sort = "starttime";
             break;
         case "location":
             $sort = "s.location";
             break;
         case "teacher":
             $sort = "teacherfirstname";
             break;
         case "participant":
             $sort = "u.lastname";
             break;
         case "idnumber":
             $sort = "u.idnumber";
             break;
         case "attended":
             $sort = "a.attended";
             break;
         case "grade":
             $sort = "a.grade";
             break;
         case "feedback":
             $sort = "a.feedback";
             break;
         default:
             $sort = NULL;
     }
     if (!isset($order)) {
         $order = "";
     } else {
         if ($order != "DESC" && $order != "ASC") {
             $order = "";
         }
     }
     $data =& $this->_customdata;
     $entries = fetch_table_entries($data['slots'], $sort . ' ' . $order);
     $rows = array();
     $rowspan = 0;
     $numcols = 0;
     $evenodd = 0;
     foreach ($entries as $entry) {
         if ($numcols == 10) {
             break;
         }
         $row = $rows[] = new html_table_row();
         foreach ($columns as $column) {
             if ($rowspan == 0) {
                 switch ($column) {
                     case 'datetime':
                         $datetime = userdate($entry->starttime, get_string('fulldatetimetemplate', 'organizer')) . ' - ' . userdate($entry->starttime + $entry->duration, get_string('timetemplate', 'organizer'));
                         $content = "<span name='{$column}_cell'>" . $datetime . '</span>';
                         $cell = new html_table_cell($content);
                         $cell->rowspan = $entry->rowspan;
                         $cell->style = 'vertical-align: middle;';
                         $row->cells[] = $cell;
                         break;
                     case 'location':
                         $location = $entry->location;
                         $content = "<span name='{$column}_cell'>" . $location . '</span>';
                         $cell = new html_table_cell($content);
                         $cell->rowspan = $entry->rowspan;
                         $cell->style = 'vertical-align: middle;';
                         $row->cells[] = $cell;
                         break;
                     case 'teacher':
                         $a = new stdClass();
                         $a->firstname = $entry->teacherfirstname;
                         $a->lastname = $entry->teacherlastname;
                         $name = get_string('fullname_template', 'organizer', $a);
                         $content = "<span name='{$column}_cell'>" . $name . '</span>';
                         $cell = new html_table_cell($content);
                         $cell->rowspan = $entry->rowspan;
                         $cell->style = 'vertical-align: middle;';
                         $row->cells[] = $cell;
                         break;
                     case 'groupname':
                         $groupname = $entry->groupname;
                         $content = "<span name='{$column}_cell'>" . $groupname . '</span>';
                         $cell = new html_table_cell($content);
                         $cell->rowspan = $entry->rowspan;
                         $cell->style = 'vertical-align: middle;';
                         $row->cells[] = $cell;
                         break;
                     default:
                         break;
                 }
             }
             switch ($column) {
                 case 'participant':
                     $a = new stdClass();
                     $a->firstname = $entry->firstname;
                     $a->lastname = $entry->lastname;
                     $name = get_string('fullname_template', 'organizer', $a);
                     $content = "<span name='{$column}_cell'>" . $name . '</span>';
                     $cell = new html_table_cell($content);
                     $cell->style = 'vertical-align: middle;';
                     $row->cells[] = $cell;
                     break;
                 case 'idnumber':
                     $idnumber = isset($entry->idnumber) && $entry->idnumber !== '' ? $entry->idnumber : '';
                     $content = "<span name='{$column}_cell'>" . $idnumber . '</span>';
                     $cell = new html_table_cell($content);
                     $cell->style = 'vertical-align: middle;';
                     $row->cells[] = $cell;
                     break;
                 case 'attended':
                     $attended = isset($entry->attended) ? $entry->attended == 1 ? 'Yes' : 'No' : '';
                     $content = "<span name='{$column}_cell'>" . $attended . '</span>';
                     $cell = new html_table_cell($content);
                     $cell->style = 'vertical-align: middle;';
                     $row->cells[] = $cell;
                     break;
                 case 'grade':
                     $grade = isset($entry->grade) ? sprintf("%01.2f", $entry->grade) : '';
                     $content = "<span name='{$column}_cell'>" . $grade . '</span>';
                     $cell = new html_table_cell($content);
                     $cell->style = 'vertical-align: middle;';
                     $row->cells[] = $cell;
                     break;
                 case 'feedback':
                     $feedback = isset($entry->feedback) && $entry->feedback !== '' ? $entry->feedback : '';
                     $content = "<span name='{$column}_cell'>" . $feedback . '</span>';
                     $cell = new html_table_cell($content);
                     $cell->style = 'vertical-align: middle;';
                     $row->cells[] = $cell;
                     break;
                 case 'datetime':
                 case 'location':
                 case 'teacher':
                 case 'groupname':
                     break;
                 default:
                     print_error("Unsupported column type: {$column}");
             }
         }
         $numcols++;
         $row->attributes['class'] = " r{$evenodd}";
         $rowspan = ($rowspan + 1) % $entry->rowspan;
         if ($rowspan == 0) {
             $evenodd = $evenodd ? 0 : 1;
         }
     }
     $table->data = $rows;
     return organizer_render_table_with_footer($table, false, true);
 }