Passed
Push — master ( 3af017...447a4c )
by Julito
09:24
created

export_pdf_attendance()   F

Complexity

Conditions 21
Paths 800

Size

Total Lines 125
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 75
nc 800
nop 5
dl 0
loc 125
rs 0.2777
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A export_pdf() 0 22 5

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