Passed
Push — master ( 4234e3...bd08a9 )
by Angel Fernando Quiroz
07:04
created

export_pdf_with_html()   D

Complexity

Conditions 21
Paths 100

Size

Total Lines 87
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 58
nop 5
dl 0
loc 87
rs 4.1666
c 0
b 0
f 0
nc 100

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Framework\Container;
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">';
0 ignored issues
show
Bug introduced by
Are you sure api_get_setting('stylesheets') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
    $headers_in_pdf = '<img src="'.api_get_path(WEB_CSS_PATH)./** @scrutinizer ignore-type */ api_get_setting('stylesheets').'/images/header-logo.png">';
Loading history...
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 = Container::getThemeHelper()->getAssetContents('print.css');
132
    $items_per_page = 30;
133
    $count_pages = ceil(count($data_table) / $items_per_page);
134
    $content_table = '';
135
    for ($x = 0; $x < $count_pages; $x++) {
136
        $content_table .= '<table width="100%" border="1" style="border-collapse:collapse">';
137
        // header table
138
        $content_table .= '<tr>';
139
        $i = 0;
140
        if (is_array($headers_table)) {
141
            foreach ($headers_table as $head_table) {
142
                if (!empty($head_table[0])) {
143
                    $width = (!empty($head_table[1]) ? $head_table[1].'%' : '');
144
                    $content_table .= '<th width="'.$width.'">'.$head_table[0].'</th>';
145
                    $i++;
146
                }
147
            }
148
        }
149
        $content_table .= '</tr>';
150
        // body table
151
152
        if (is_array($data_table) && count($data_table) > 0) {
153
            $offset = $x * $items_per_page;
154
            $data_table = array_slice($data_table, $offset, count($data_table));
155
            $i = 1;
156
            $item = $offset + 1;
157
            foreach ($data_table as $data) {
158
                $content_table .= '<tr>';
159
                $content_table .= '<td>'.($item < 10 ? '0'.$item : $item).'</td>';
160
                foreach ($data as $key => $content) {
161
                    if (isset($content)) {
162
                        1 == $key ? $align = 'align="left"' : $align = 'align="center"';
163
                        $content_table .= '<td '.$align.' style="padding:4px;" >'.$content.'</td>';
164
                    }
165
                }
166
                $content_table .= '</tr>';
167
                $i++;
168
                $item++;
169
                if ($i > $items_per_page) {
170
                    break;
171
                }
172
            }
173
        } else {
174
            $content_table .= '<tr colspan="'.$i.'"><td>'.get_lang('You left some fields empty.<br>Use the <b>Back</b> button on your browser and try again.<br>If you ignore your training code, see the Training Program').'</td></tr>';
175
        }
176
        $content_table .= '</table>';
177
        if ($x < ($count_pages - 1)) {
178
            $content_table .= '<pagebreak />';
179
        }
180
    }
181
    $pdf = new PDF();
182
    $pdf->set_custom_footer($footer);
183
    $pdf->set_custom_header($headers_in_pdf);
184
    $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...
185
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
186
}
187
188
/**
189
 * Exports the data as a table on a PDF page.
190
 *
191
 * @param    resource    The PDF object (ezpdf class) used to generate the file
192
 * @param    array        The data array
193
 * @param    array        Table headers
194
 * @param    string        Format (portrait or landscape)
195
 */
196
function export_pdf($pdf, $newarray, $header_names, $format)
197
{
198
    $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
199
    $pdf->ezSetCmMargins(0, 0, 0, 0);
200
    $pdf->ezSetY(('portrait' == $format) ? '820' : '570');
201
    $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
202
    if ('portrait' == $format) {
203
        $pdf->line(40, 790, 540, 790);
204
        $pdf->line(40, 40, 540, 40);
205
    } else {
206
        $pdf->line(40, 540, 790, 540);
207
        $pdf->line(40, 40, 790, 40);
208
    }
209
    $pdf->ezSetY(('portrait' == $format) ? '750' : '520');
210
    $pdf->ezTable($newarray, $header_names, '', [
211
        'showHeadings' => 1,
212
        'shaded' => 1,
213
        'showLines' => 1,
214
        'rowGap' => 3,
215
        'width' => (('portrait' == $format) ? '500' : '750'),
216
    ]);
217
    $pdf->ezStream();
218
}
219