Conditions | 33 |
Paths | > 20000 |
Total Lines | 233 |
Code Lines | 164 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
104 | function getReport($userId, $from, $to, $addTime = false) |
||
105 | { |
||
106 | $sessionCategories = UserManager::get_sessions_by_category($userId, false, true, true); |
||
107 | $report = []; |
||
108 | $minLogin = 0; |
||
109 | $maxLogin = 0; |
||
110 | $totalDuration = 0; |
||
111 | |||
112 | foreach ($sessionCategories as $category) { |
||
113 | foreach ($category['sessions'] as $session) { |
||
114 | $sessionId = $session['session_id']; |
||
115 | $courseList = $session['courses']; |
||
116 | foreach ($courseList as $course) { |
||
117 | $courseInfo = api_get_course_info_by_id($course['real_id']); |
||
118 | $result = MySpace::get_connections_to_course_by_date( |
||
119 | $userId, |
||
120 | $courseInfo, |
||
121 | $sessionId, |
||
122 | $from, |
||
123 | $to |
||
124 | ); |
||
125 | |||
126 | $partialMinLogin = 0; |
||
127 | $partialMaxLogin = 0; |
||
128 | $partialDuration = 0; |
||
129 | |||
130 | foreach ($result as $item) { |
||
131 | $record = [ |
||
132 | customDate($item['login'], true), |
||
133 | customDate($item['logout'], true), |
||
134 | api_format_time($item['duration'], 'lang'), |
||
135 | ]; |
||
136 | |||
137 | $totalDuration += $item['duration']; |
||
138 | |||
139 | if (empty($minLogin)) { |
||
140 | $minLogin = api_strtotime($item['login'], 'UTC'); |
||
141 | } |
||
142 | if ($minLogin > api_strtotime($item['login'], 'UTC')) { |
||
143 | $minLogin = api_strtotime($item['login'], 'UTC'); |
||
144 | } |
||
145 | if (api_strtotime($item['logout']) > $maxLogin) { |
||
146 | $maxLogin = api_strtotime($item['logout'], 'UTC'); |
||
147 | } |
||
148 | |||
149 | // Partials |
||
150 | $partialDuration += $item['duration']; |
||
151 | if (empty($partialMinLogin)) { |
||
152 | $partialMinLogin = api_strtotime($item['login'], 'UTC'); |
||
153 | } |
||
154 | if ($partialMinLogin > api_strtotime($item['login'], 'UTC')) { |
||
155 | $partialMinLogin = api_strtotime($item['login'], 'UTC'); |
||
156 | } |
||
157 | if (api_strtotime($item['logout'], 'UTC') > $partialMaxLogin) { |
||
158 | $partialMaxLogin = api_strtotime($item['logout'], 'UTC'); |
||
159 | } |
||
160 | |||
161 | $report[$sessionId]['courses'][$course['real_id']][] = $record; |
||
162 | $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].' ('.$session['session_name'].')'; |
||
163 | } |
||
164 | |||
165 | if (!empty($result)) { |
||
166 | $record = [ |
||
167 | customDate($partialMinLogin, true), |
||
168 | customDate($partialMaxLogin, true), |
||
169 | api_format_time($partialDuration, 'lang'), |
||
170 | ]; |
||
171 | $report[$sessionId]['courses'][$course['real_id']][] = $record; |
||
172 | $report[$sessionId]['name'][$course['real_id']] = $courseInfo['title'].' ('.$session['session_name'].')'; |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | $courses = CourseManager::returnCourses($userId); |
||
179 | $courses = array_merge($courses['in_category'], $courses['not_category']); |
||
180 | |||
181 | if ($addTime) { |
||
182 | $fromFirst = api_get_local_time($from.' 00:00:00'); |
||
183 | $toEnd = api_get_local_time($from.' 23:59:59'); |
||
184 | |||
185 | $from = api_get_utc_datetime($fromFirst); |
||
186 | $to = api_get_utc_datetime($toEnd); |
||
187 | } |
||
188 | |||
189 | foreach ($courses as $course) { |
||
190 | $result = MySpace::get_connections_to_course_by_date( |
||
191 | $userId, |
||
192 | $course, |
||
193 | 0, |
||
194 | $from, |
||
195 | $to |
||
196 | ); |
||
197 | $partialMinLogin = 0; |
||
198 | $partialMaxLogin = 0; |
||
199 | $partialDuration = 0; |
||
200 | |||
201 | foreach ($result as $item) { |
||
202 | $record = [ |
||
203 | customDate($item['login'], true), |
||
204 | customDate($item['logout'], true), |
||
205 | api_format_time($item['duration'], 'lang'), |
||
206 | ]; |
||
207 | $report[0]['courses'][$course['course_id']][] = $record; |
||
208 | $report[0]['name'][$course['course_id']] = $course['title']; |
||
209 | |||
210 | $totalDuration += $item['duration']; |
||
211 | |||
212 | if (empty($minLogin)) { |
||
213 | $minLogin = api_strtotime($item['login'], 'UTC'); |
||
214 | } |
||
215 | if ($minLogin > api_strtotime($item['login'], 'UTC')) { |
||
216 | $minLogin = api_strtotime($item['login'], 'UTC'); |
||
217 | } |
||
218 | if (api_strtotime($item['logout'], 'UTC') > $maxLogin) { |
||
219 | $maxLogin = api_strtotime($item['logout'], 'UTC'); |
||
220 | } |
||
221 | |||
222 | // Partials |
||
223 | $partialDuration += $item['duration']; |
||
224 | if (empty($partialMinLogin)) { |
||
225 | $partialMinLogin = api_strtotime($item['login'], 'UTC'); |
||
226 | } |
||
227 | if ($partialMinLogin > api_strtotime($item['login'], 'UTC')) { |
||
228 | $partialMinLogin = api_strtotime($item['login'], 'UTC'); |
||
229 | } |
||
230 | if (api_strtotime($item['logout'], 'UTC') > $partialMaxLogin) { |
||
231 | $partialMaxLogin = api_strtotime($item['logout'], 'UTC'); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | if (!empty($result)) { |
||
236 | $record = [ |
||
237 | customDate($partialMinLogin, true), |
||
238 | customDate($partialMaxLogin, true), |
||
239 | api_format_time($partialDuration, 'lang'), |
||
240 | ]; |
||
241 | |||
242 | $report[0]['courses'][$course['course_id']][] = $record; |
||
243 | $report[0]['name'][$course['course_id']] = $course['title']; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | $table = new HTML_Table(['class' => 'table table-hover table-striped data_table']); |
||
248 | $headers = [ |
||
249 | get_lang('MinStartDate'), |
||
250 | get_lang('MaxEndDate'), |
||
251 | get_lang('TotalDuration'), |
||
252 | ]; |
||
253 | $row = 0; |
||
254 | $column = 0; |
||
255 | foreach ($headers as $header) { |
||
256 | $table->setHeaderContents($row, $column, $header); |
||
257 | $column++; |
||
258 | } |
||
259 | $row++; |
||
260 | $column = 0; |
||
261 | $table->setCellContents($row, $column++, customDate($minLogin)); |
||
262 | $table->setCellContents($row, $column++, customDate($maxLogin)); |
||
263 | $table->setRowAttributes($row, ['style' => 'font-weight:bold']); |
||
264 | $table->setCellContents($row, $column++, api_format_time($totalDuration, 'lang')); |
||
265 | |||
266 | $first = $table->toHtml(); |
||
267 | |||
268 | $courseSessionTable = ''; |
||
269 | $courseSessionTableData = []; |
||
270 | $iconCourse = Display::return_icon('course.png', null, [], ICON_SIZE_SMALL); |
||
271 | foreach ($report as $sessionId => $data) { |
||
272 | foreach ($data['courses'] as $courseId => $courseData) { |
||
273 | if (empty($courseData)) { |
||
274 | continue; |
||
275 | } |
||
276 | $courseSessionTable .= '<div class="data-title">'.Display::page_subheader3( |
||
277 | $iconCourse.$data['name'][$courseId] |
||
278 | ).'</div>'; |
||
279 | $table = new HTML_Table(['class' => 'table table-hover table-striped data_table']); |
||
280 | $headers = [ |
||
281 | get_lang('StartDate'), |
||
282 | get_lang('EndDate'), |
||
283 | get_lang('Duration'), |
||
284 | ]; |
||
285 | $row = 0; |
||
286 | $column = 0; |
||
287 | foreach ($headers as $header) { |
||
288 | $table->setHeaderContents($row, $column, $header); |
||
289 | $column++; |
||
290 | } |
||
291 | $row++; |
||
292 | $countData = count($courseData); |
||
293 | foreach ($courseData as $record) { |
||
294 | $column = 0; |
||
295 | foreach ($record as $item) { |
||
296 | $table->setCellContents($row, $column++, $item); |
||
297 | if ($row == $countData) { |
||
298 | $courseSessionTableData[$data['name'][$courseId]] = $item; |
||
299 | $table->setRowAttributes($row, ['style' => 'font-weight:bold']); |
||
300 | } |
||
301 | } |
||
302 | $row++; |
||
303 | } |
||
304 | $courseSessionTable .= $table->toHtml(); |
||
305 | } |
||
306 | } |
||
307 | $totalCourseSessionTable = ''; |
||
308 | if ($courseSessionTableData) { |
||
309 | $table = new HTML_Table(['class' => 'table table-hover table-striped data_table']); |
||
310 | $headers = [ |
||
311 | get_lang('Course'), |
||
312 | get_lang('TotalDuration'), |
||
313 | ]; |
||
314 | $row = 0; |
||
315 | $column = 0; |
||
316 | foreach ($headers as $header) { |
||
317 | $table->setHeaderContents($row, $column, $header); |
||
318 | $column++; |
||
319 | } |
||
320 | $row++; |
||
321 | foreach ($courseSessionTableData as $name => $duration) { |
||
322 | $column = 0; |
||
323 | $table->setCellContents($row, $column++, $name); |
||
324 | $table->setCellContents($row, $column++, $duration); |
||
325 | $row++; |
||
326 | } |
||
327 | $totalCourseSessionTable = $table->toHtml(); |
||
328 | } |
||
329 | |||
330 | $result = []; |
||
331 | $result['first'] = $first; |
||
332 | $result['second'] = $courseSessionTable; |
||
333 | $result['third'] = $totalCourseSessionTable; |
||
334 | $result['total'] = $totalDuration; |
||
335 | |||
336 | return $result; |
||
337 | } |
||
602 |
If you suppress an error, we recommend checking for the error condition explicitly: