Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/gradebook/lib/fe/exportgradebook.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * Script.
5
 */
6
7
/**
8
 * Prints an HTML page with a table containing the gradebook data.
9
 *
10
 * @param    array    Array containing the data to be printed in the table
11
 * @param    array    Table headers
12
 * @param    string    View to print as a title for the table
13
 * @param    string    Course name to print as title for the table
14
 *
15
 * @return string
16
 */
17
function print_table($data_array, $header_names, $view, $coursename)
18
{
19
    $printdata = '<!DOCTYPE html
20
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
21
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
22
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.api_get_language_isocode().'" lang="'.api_get_language_isocode().'">
23
<head>
24
<title>'.get_lang('Print').'</title>
25
<meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
26
27
28
<style type="text/css">
29
body {
30
    font-size: 12px;
31
    color: #000;
32
    margin: 10px;
33
    padding: 0;
34
}
35
36
a:link {text-decoration: none; font-weight : bold; color : black;}
37
a:visited {text-decoration: none; font-weight : bold; color : black;}
38
a:active {text-decoration: none; font-weight : bold;  color : black;}
39
40
.data_table{
41
    border-collapse: collapse;
42
    width: 100%;
43
    padding: 5px;
44
    border: 1px;
45
}
46
.data_table th{
47
    padding: 5px;
48
    vertical-align: top;
49
    border-top: 1px solid black;
50
    border-bottom: 1px solid black;
51
    border-right: 1px solid black;
52
    border-left: 1px solid black;
53
}
54
.data_table tr.row_odd{
55
    background-color: #fafafa;
56
  }
57
.data_table tr.row_even{
58
    background-color: #fff;
59
}
60
.data_table td{
61
    padding: 5px;
62
      vertical-align: top;
63
    border-bottom: 1px solid black;
64
    border-right: 1px solid black;
65
    border-left: 1px solid black;
66
}
67
</style>
68
</head>
69
<body dir="'.api_get_text_direction().'"><div id="main">';
70
71
    $printdata .= '<h2>'.$view.' : '.$coursename.'</h2>';
72
    //@todo not necessary here
73
74
    $printdata .= '<table border="1" width="90%" cellspacing="1" cellpadding="1">';
75
    foreach ($header_names as $header) {
76
        $printdata .= '<th>'.$header.'</th>';
77
    }
78
79
    foreach ($data_array as $data) {
80
        $printdata .= '<tr>';
81
        foreach ($data as $rowdata) {
82
            $printdata .= '<td>'.$rowdata.'</td>';
83
        }
84
        $printdata .= '</tr>';
85
    }
86
    $printdata .= '</table></div></body></html>';
87
88
    return $printdata;
89
}
90
91
/**
92
 * This function get a content html for export inside a pdf file.
93
 *
94
 * @param    array    table headers
95
 * @param    array    table body
96
 * @param    array    pdf headers
97
 * @param    array    pdf footers
98
 */
99
function export_pdf_with_html($headers_table, $data_table, $headers_pdf, $footers_pdf, $title_pdf)
100
{
101
    $headers_in_pdf = '<img src="'.api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/images/header-logo.png">';
102
103
    if (is_array($headers_pdf)) {
104
        // preparing headers pdf
105
        $header = '<br/><br/>
106
                        <table width="100%" cellspacing="1" cellpadding="5" border="0" class="strong">
107
                        <tr>
108
                            <td width="100%" style="text-align: center;" class="title" colspan="4">
109
                            <h1>'.$title_pdf.'</h1></td></tr>';
110
        foreach ($headers_pdf as $header_pdf) {
111
            if (!empty($header_pdf[0]) && !empty($header_pdf[1])) {
112
                $header .= '<tr><td><strong>'.$header_pdf[0].'</strong> </td><td>'.$header_pdf[1].'</td></tr>';
113
            }
114
        }
115
        $header .= '</table><br />';
116
    }
117
118
    // preparing footer pdf
119
    $footer = '<table width="100%" cellspacing="2" cellpadding="10" border="0">';
120
    if (is_array($footers_pdf)) {
121
        $footer .= '<tr>';
122
        foreach ($footers_pdf as $foot_pdf) {
123
            $footer .= '<td width="33%" style="text-align: center;">'.$foot_pdf.'</td>';
124
        }
125
        $footer .= '</tr>';
126
    }
127
    $footer .= '</table>';
128
    $footer .= '<div align="right" style="font-weight: bold;">{PAGENO}/{nb}</div>';
129
130
    // preparing content pdf
131
    $css = api_get_print_css();
132
    $items_per_page = 30;
133
    $count_pages = ceil(count($data_table) / $items_per_page);
134
    for ($x = 0; $x < $count_pages; $x++) {
135
        $content_table .= '<table width="100%" border="1" style="border-collapse:collapse">';
136
        // header table
137
        $content_table .= '<tr>';
138
        $i = 0;
139
        if (is_array($headers_table)) {
140
            foreach ($headers_table as $head_table) {
141
                if (!empty($head_table[0])) {
142
                    $width = (!empty($head_table[1]) ? $head_table[1].'%' : '');
143
                    $content_table .= '<th width="'.$width.'">'.$head_table[0].'</th>';
144
                    $i++;
145
                }
146
            }
147
        }
148
        $content_table .= '</tr>';
149
        // body table
150
151
        if (is_array($data_table) && count($data_table) > 0) {
152
            $offset = $x * $items_per_page;
153
            $data_table = array_slice($data_table, $offset, count($data_table));
154
            $i = 1;
155
            $item = $offset + 1;
156
            foreach ($data_table as $data) {
157
                $content_table .= '<tr>';
158
                $content_table .= '<td>'.($item < 10 ? '0'.$item : $item).'</td>';
159
                foreach ($data as $key => $content) {
160
                    if (isset($content)) {
161
                        $key == 1 ? $align = 'align="left"' : $align = 'align="center"';
162
                        $content_table .= '<td '.$align.' style="padding:4px;" >'.$content.'</td>';
163
                    }
164
                }
165
                $content_table .= '</tr>';
166
                $i++;
167
                $item++;
168
                if ($i > $items_per_page) {
169
                    break;
170
                }
171
            }
172
        } else {
173
            $content_table .= '<tr colspan="'.$i.'"><td>'.get_lang('Empty').'</td></tr>';
174
        }
175
        $content_table .= '</table>';
176
        if ($x < ($count_pages - 1)) {
177
            $content_table .= '<pagebreak />';
178
        }
179
    }
180
    $pdf = new PDF();
181
    $pdf->set_custom_footer($footer);
182
    $pdf->set_custom_header($headers_in_pdf);
183
    $pdf->content_to_pdf($header.$content_table, $css, $title_pdf);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $header does not seem to be defined for all execution paths leading up to this point.
Loading history...
184
    exit;
185
}
186
187
/**
188
 * Exports the data as a table on a PDF page.
189
 *
190
 * @param    resource    The PDF object (ezpdf class) used to generate the file
191
 * @param    array        The data array
192
 * @param    array        Table headers
193
 * @param    string        Format (portrait or landscape)
194
 */
195
function export_pdf($pdf, $newarray, $header_names, $format)
196
{
197
    $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
198
    $pdf->ezSetCmMargins(0, 0, 0, 0);
199
    $pdf->ezSetY(($format == 'portrait') ? '820' : '570');
200
    $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
201
    if ('portrait' == $format) {
202
        $pdf->line(40, 790, 540, 790);
203
        $pdf->line(40, 40, 540, 40);
204
    } else {
205
        $pdf->line(40, 540, 790, 540);
206
        $pdf->line(40, 40, 790, 40);
207
    }
208
    $pdf->ezSetY(($format == 'portrait') ? '750' : '520');
209
    $pdf->ezTable($newarray, $header_names, '', [
210
        'showHeadings' => 1,
211
        'shaded' => 1,
212
        'showLines' => 1,
213
        'rowGap' => 3,
214
        'width' => (($format == 'portrait') ? '500' : '750'),
215
    ]);
216
    $pdf->ezStream();
217
}
218