Total Complexity | 79 |
Total Lines | 680 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CourseHome often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseHome, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class CourseHome |
||
12 | { |
||
13 | /** |
||
14 | * Gets the tools of a certain category. Returns an array expected |
||
15 | * by show_tools_category(). |
||
16 | * |
||
17 | * @param string $course_tool_category contains the category of tools to |
||
18 | * display: "toolauthoring", "toolinteraction", "tooladmin", |
||
19 | * "tooladminplatform", "toolplugin" |
||
20 | * @param int $courseId Optional |
||
21 | * @param int $sessionId Optional |
||
22 | * |
||
23 | * @return array |
||
24 | */ |
||
25 | public static function get_tools_category( |
||
26 | $course_tool_category, |
||
27 | $courseId = 0, |
||
28 | $sessionId = 0 |
||
29 | ) { |
||
30 | $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST); |
||
31 | $is_platform_admin = api_is_platform_admin(); |
||
32 | $all_tools_list = []; |
||
33 | |||
34 | // Condition for the session |
||
35 | $sessionId = $sessionId ?: api_get_session_id(); |
||
36 | $course_id = $courseId ?: api_get_course_int_id(); |
||
37 | $courseInfo = api_get_course_info_by_id($course_id); |
||
38 | $userId = api_get_user_id(); |
||
39 | $user = api_get_user_entity($userId); |
||
40 | $condition_session = api_get_session_condition( |
||
41 | $sessionId, |
||
42 | true, |
||
43 | true, |
||
44 | 't.session_id' |
||
45 | ); |
||
46 | |||
47 | $studentView = api_is_student_view_active(); |
||
48 | $orderBy = ' ORDER BY id '; |
||
49 | |||
50 | $em = Database::getManager(); |
||
51 | $repo = $em->getRepository('ChamiloCourseBundle:CTool'); |
||
52 | $qb = $repo->createQueryBuilder('tool'); |
||
53 | |||
54 | $criteria = \Doctrine\Common\Collections\Criteria::create(); |
||
55 | |||
56 | switch ($course_tool_category) { |
||
57 | case TOOL_STUDENT_VIEW: |
||
58 | $criteria |
||
59 | ->where(Criteria::expr()->eq('visibility', 1)) |
||
60 | ->andWhere(Criteria::expr()->in('category', ['authoring', 'interaction'])) |
||
61 | ; |
||
62 | |||
63 | /*if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && !$studentView) { |
||
64 | $conditions = ' WHERE ( |
||
65 | visibility = 1 AND ( |
||
66 | category = "authoring" OR |
||
67 | category = "interaction" OR |
||
68 | category = "plugin" |
||
69 | ) OR (t.name = "'.TOOL_TRACKING.'") |
||
70 | )'; |
||
71 | }*/ |
||
72 | break; |
||
73 | case TOOL_AUTHORING: |
||
74 | $criteria |
||
75 | ->where(Criteria::expr()->in('category', ['authoring'])) |
||
76 | ; |
||
77 | break; |
||
78 | case TOOL_INTERACTION: |
||
79 | $criteria |
||
80 | ->where(Criteria::expr()->in('category', ['interaction'])) |
||
81 | ; |
||
82 | break; |
||
83 | case TOOL_ADMIN_VISIBLE: |
||
84 | $criteria |
||
85 | ->where(Criteria::expr()->eq('visibility', 1)) |
||
86 | ->andWhere(Criteria::expr()->in('category', ['admin'])) |
||
87 | ; |
||
88 | break; |
||
89 | case TOOL_ADMIN_PLATFORM: |
||
90 | $criteria |
||
91 | ->andWhere(Criteria::expr()->in('category', ['admin'])) |
||
92 | ; |
||
93 | break; |
||
94 | case TOOL_DRH: |
||
95 | $criteria |
||
96 | ->andWhere(Criteria::expr()->in('tool.tool.name', ['tracking'])) |
||
97 | ; |
||
98 | break; |
||
99 | /*case TOOL_COURSE_PLUGIN: |
||
100 | //Other queries recover id, name, link, image, visibility, admin, address, added_tool, target, category and session_id |
||
101 | // but plugins are not present in the tool table, only globally and inside the course_settings table once configured |
||
102 | $sql = "SELECT * FROM $course_tool_table t |
||
103 | WHERE category = 'plugin' AND name <> 'courseblock' AND c_id = $course_id $condition_session |
||
104 | ";*/ |
||
105 | break; |
||
106 | } |
||
107 | |||
108 | $criteria |
||
109 | ->andWhere(Criteria::expr()->eq('course', api_get_course_entity($courseId))) |
||
110 | ; |
||
111 | |||
112 | //$condition_session = $condition_add." ( $session_field = $session_id OR $session_field = 0 OR $session_field IS NULL) "; |
||
113 | /*$criteria |
||
114 | ->andWhere(Criteria::expr()->eq('session', $courseId)) |
||
115 | ;*/ |
||
116 | |||
117 | $qb->addCriteria($criteria); |
||
118 | |||
119 | return $qb->getQuery()->getResult(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Shows the general data for a particular meeting. |
||
124 | * |
||
125 | * @param int $id_session |
||
126 | * |
||
127 | * @return string session data |
||
128 | */ |
||
129 | public static function show_session_data($id_session) |
||
130 | { |
||
131 | $sessionInfo = api_get_session_info($id_session); |
||
132 | |||
133 | if (empty($sessionInfo)) { |
||
134 | return ''; |
||
135 | } |
||
136 | |||
137 | $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
138 | $sql = 'SELECT name FROM '.$table.' |
||
139 | WHERE id = "'.intval($sessionInfo['session_category_id']).'"'; |
||
140 | $rs_category = Database::query($sql); |
||
141 | $session_category = ''; |
||
142 | if (Database::num_rows($rs_category) > 0) { |
||
143 | $rows_session_category = Database::store_result($rs_category); |
||
144 | $rows_session_category = $rows_session_category[0]; |
||
145 | $session_category = $rows_session_category['name']; |
||
146 | } |
||
147 | |||
148 | $coachInfo = api_get_user_info($sessionInfo['id_coach']); |
||
149 | |||
150 | $output = ''; |
||
151 | if (!empty($session_category)) { |
||
152 | $output .= '<tr><td>'.get_lang('Sessions categories').': '.'<b>'.$session_category.'</b></td></tr>'; |
||
153 | } |
||
154 | $dateInfo = SessionManager::parseSessionDates($sessionInfo); |
||
155 | |||
156 | $msgDate = $dateInfo['access']; |
||
157 | $output .= '<tr> |
||
158 | <td style="width:50%">'.get_lang('Session name').': '.'<b>'.$sessionInfo['name'].'</b></td> |
||
159 | <td>'.get_lang('General coach').': '.'<b>'.$coachInfo['complete_name'].'</b></td></tr>'; |
||
160 | $output .= '<tr> |
||
161 | <td>'.get_lang('Identifier of session').': '. |
||
162 | Display::return_icon('star.png', ' ', ['align' => 'absmiddle']).' |
||
163 | </td> |
||
164 | <td>'.get_lang('Date').': '.'<b>'.$msgDate.'</b> |
||
165 | </td> |
||
166 | </tr>'; |
||
167 | |||
168 | return $output; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Retrieves the name-field within a tool-record and translates it on necessity. |
||
173 | * |
||
174 | * @param array $tool the input record |
||
175 | * |
||
176 | * @return string returns the name of the corresponding tool |
||
177 | */ |
||
178 | public static function translate_tool_name(CTool $tool) |
||
179 | { |
||
180 | static $already_translated_icons = [ |
||
181 | 'file_html.gif', |
||
182 | 'file_html_na.gif', |
||
183 | 'file_html.png', |
||
184 | 'file_html_na.png', |
||
185 | 'scormbuilder.gif', |
||
186 | 'scormbuilder_na.gif', |
||
187 | 'blog.gif', |
||
188 | 'blog_na.gif', |
||
189 | 'external.gif', |
||
190 | 'external_na.gif', |
||
191 | ]; |
||
192 | |||
193 | $toolName = Security::remove_XSS(stripslashes(strip_tags($tool->getTool()->getName()))); |
||
194 | |||
195 | return $toolName; |
||
|
|||
196 | |||
197 | if (isset($tool['image']) && in_array($tool['image'], $already_translated_icons)) { |
||
198 | return $toolName; |
||
199 | } |
||
200 | |||
201 | $toolName = api_underscore_to_camel_case($toolName); |
||
202 | |||
203 | if (isset($tool['category']) && 'plugin' !== $tool['category'] && |
||
204 | isset($GLOBALS['Tool'.$toolName]) |
||
205 | ) { |
||
206 | return get_lang('Tool'.$toolName); |
||
207 | } |
||
208 | |||
209 | return $toolName; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get published learning path id from link inside course home. |
||
214 | * |
||
215 | * @param string Link to published lp |
||
216 | * |
||
217 | * @return int Learning path id |
||
218 | */ |
||
219 | public static function getPublishedLpIdFromLink($link) |
||
220 | { |
||
221 | $lpId = 0; |
||
222 | $param = strstr($link, 'lp_id='); |
||
223 | if (!empty($param)) { |
||
224 | $paramList = explode('=', $param); |
||
225 | if (isset($paramList[1])) { |
||
226 | $lpId = (int) $paramList[1]; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | return $lpId; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Get published learning path category from link inside course home. |
||
235 | * |
||
236 | * @param string $link |
||
237 | * |
||
238 | * @return CLpCategory |
||
239 | */ |
||
240 | public static function getPublishedLpCategoryFromLink($link) |
||
241 | { |
||
242 | $query = parse_url($link, PHP_URL_QUERY); |
||
243 | parse_str($query, $params); |
||
244 | $id = isset($params['id']) ? (int) $params['id'] : 0; |
||
245 | $em = Database::getManager(); |
||
246 | /** @var CLpCategory $category */ |
||
247 | $category = $em->find('ChamiloCourseBundle:CLpCategory', $id); |
||
248 | |||
249 | return $category; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Show a navigation menu. |
||
254 | */ |
||
255 | public static function show_navigation_menu() |
||
256 | { |
||
257 | $blocks = self::getUserBlocks(); |
||
258 | $class = null; |
||
259 | $idLearn = null; |
||
260 | $item = null; |
||
261 | $marginLeft = 160; |
||
262 | |||
263 | $html = '<div id="toolnav">'; |
||
264 | $html .= '<ul id="toolnavbox">'; |
||
265 | |||
266 | $showOnlyText = 'text' === api_get_setting('show_navigation_menu'); |
||
267 | $showOnlyIcons = 'icons' === api_get_setting('show_navigation_menu'); |
||
268 | |||
269 | foreach ($blocks as $block) { |
||
270 | $blockItems = $block['content']; |
||
271 | foreach ($blockItems as $item) { |
||
272 | $html .= '<li>'; |
||
273 | if ($showOnlyText) { |
||
274 | $class = 'text'; |
||
275 | $marginLeft = 170; |
||
276 | $show = $item['name']; |
||
277 | } elseif ($showOnlyIcons) { |
||
278 | $class = 'icons'; |
||
279 | $marginLeft = 25; |
||
280 | $show = $item['tool']['only_icon_small']; |
||
281 | } else { |
||
282 | $class = 'icons-text'; |
||
283 | $show = $item['name'].$item['tool']['only_icon_small']; |
||
284 | } |
||
285 | |||
286 | $item['url_params']['class'] = 'btn btn-default text-left '.$class; |
||
287 | $html .= Display::url( |
||
288 | $show, |
||
289 | $item['only_href'], |
||
290 | $item['url_params'] |
||
291 | ); |
||
292 | $html .= '</li>'; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | $html .= '</ul>'; |
||
297 | $html .= '<script>$(function() { |
||
298 | $("#toolnavbox a").stop().animate({"margin-left":"-'.$marginLeft.'px"},1000); |
||
299 | $("#toolnavbox > li").hover( |
||
300 | function () { |
||
301 | $("a",$(this)).stop().animate({"margin-left":"-2px"},200); |
||
302 | $("span",$(this)).css("display","block"); |
||
303 | }, |
||
304 | function () { |
||
305 | $("a",$(this)).stop().animate({"margin-left":"-'.$marginLeft.'px"},200); |
||
306 | $("span",$(this)).css("display","initial"); |
||
307 | } |
||
308 | ); |
||
309 | });</script>'; |
||
310 | $html .= '</div>'; |
||
311 | |||
312 | return $html; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Show a toolbar with shortcuts to the course tool. |
||
317 | * |
||
318 | * @param int $orientation |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public static function show_navigation_tool_shortcuts($orientation = SHORTCUTS_HORIZONTAL) |
||
323 | { |
||
324 | $origin = api_get_origin(); |
||
325 | $courseInfo = api_get_course_info(); |
||
326 | if ('learnpath' === $origin) { |
||
327 | return ''; |
||
328 | } |
||
329 | |||
330 | $blocks = self::getUserBlocks(); |
||
331 | $html = ''; |
||
332 | if (!empty($blocks)) { |
||
333 | $styleId = 'toolshortcuts_vertical'; |
||
334 | if (SHORTCUTS_HORIZONTAL == $orientation) { |
||
335 | $styleId = 'toolshortcuts_horizontal'; |
||
336 | } |
||
337 | $html .= '<div id="'.$styleId.'">'; |
||
338 | |||
339 | $html .= Display::url( |
||
340 | Display::return_icon('home.png', get_lang('Course home'), '', ICON_SIZE_MEDIUM), |
||
341 | $courseInfo['course_public_url'], |
||
342 | ['class' => 'items-icon'] |
||
343 | ); |
||
344 | |||
345 | foreach ($blocks as $block) { |
||
346 | $blockItems = $block['content']; |
||
347 | foreach ($blockItems as $item) { |
||
348 | $item['url_params']['id'] = ''; |
||
349 | $item['url_params']['class'] = 'items-icon'; |
||
350 | $html .= Display::url( |
||
351 | $item['tool']['only_icon_medium'], |
||
352 | $item['only_href'], |
||
353 | $item['url_params'] |
||
354 | ); |
||
355 | if (SHORTCUTS_VERTICAL == $orientation) { |
||
356 | $html .= '<br />'; |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | $html .= '</div>'; |
||
361 | } |
||
362 | |||
363 | return $html; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * List course homepage tools from authoring and interaction sections. |
||
368 | * |
||
369 | * @param int $courseId The course ID (guessed from context if not provided) |
||
370 | * @param int $sessionId The session ID (guessed from context if not provided) |
||
371 | * |
||
372 | * @return array List of all tools data from the c_tools table |
||
373 | */ |
||
374 | public static function toolsIconsAction($courseId = null, $sessionId = null) |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @param int $editIcon |
||
408 | * |
||
409 | * @return array |
||
410 | */ |
||
411 | public static function getTool($editIcon) |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @return string |
||
426 | */ |
||
427 | public static function getCustomSysIconPath() |
||
428 | { |
||
429 | // Check if directory exists or create it if it doesn't |
||
430 | $dir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/'; |
||
431 | if (!is_dir($dir)) { |
||
432 | mkdir($dir, api_get_permissions_for_new_directories(), true); |
||
433 | } |
||
434 | |||
435 | return $dir; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @return string |
||
440 | */ |
||
441 | public static function getCustomWebIconPath() |
||
442 | { |
||
443 | // Check if directory exists or create it if it doesn't |
||
444 | $dir = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/'; |
||
445 | |||
446 | return $dir; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @param string $icon |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | public static function getDisableIcon($icon) |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @param int $id |
||
463 | * @param array $values |
||
464 | */ |
||
465 | public static function updateTool($id, $values) |
||
466 | { |
||
467 | $table = Database::get_course_table(TABLE_TOOL_LIST); |
||
468 | $params = [ |
||
469 | 'name' => $values['name'], |
||
470 | 'link' => $values['link'], |
||
471 | 'target' => $values['target'], |
||
472 | 'visibility' => $values['visibility'], |
||
473 | 'description' => $values['description'], |
||
474 | ]; |
||
475 | |||
476 | if (isset($_FILES['icon']['size']) && 0 !== $_FILES['icon']['size']) { |
||
477 | /*$dir = self::getCustomSysIconPath(); |
||
478 | |||
479 | // Resize image if it is larger than 64px |
||
480 | $temp = new Image($_FILES['icon']['tmp_name']); |
||
481 | $picture_infos = $temp->get_image_info(); |
||
482 | if ($picture_infos['width'] > 64) { |
||
483 | $thumbwidth = 64; |
||
484 | } else { |
||
485 | $thumbwidth = $picture_infos['width']; |
||
486 | } |
||
487 | if ($picture_infos['height'] > 64) { |
||
488 | $new_height = 64; |
||
489 | } else { |
||
490 | $new_height = $picture_infos['height']; |
||
491 | } |
||
492 | $temp->resize($thumbwidth, $new_height, 0); |
||
493 | |||
494 | //copy the image to the course upload folder |
||
495 | $path = $dir.$_FILES['icon']['name']; |
||
496 | $result = $temp->send_image($path); |
||
497 | |||
498 | $temp = new Image($path); |
||
499 | $r = $temp->convert2bw(); |
||
500 | $ext = pathinfo($path, PATHINFO_EXTENSION); |
||
501 | $bwPath = substr($path, 0, -(strlen($ext) + 1)).'_na.'.$ext; |
||
502 | |||
503 | if (false === $r) { |
||
504 | error_log('Conversion to B&W of '.$path.' failed in '.__FILE__.' at line '.__LINE__); |
||
505 | } else { |
||
506 | $temp->send_image($bwPath); |
||
507 | $iconName = $_FILES['icon']['name']; |
||
508 | $params['custom_icon'] = $iconName; |
||
509 | }*/ |
||
510 | } |
||
511 | |||
512 | Database::update( |
||
513 | $table, |
||
514 | $params, |
||
515 | [' iid = ?' => [$id]] |
||
516 | ); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @param int $id |
||
521 | */ |
||
522 | public static function deleteIcon($id) |
||
553 | ); |
||
554 | } |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * @param string $toolName |
||
559 | * @param int $courseId |
||
560 | * @param int $sessionId Optional. |
||
561 | * |
||
562 | * @return bool |
||
563 | */ |
||
564 | public static function getToolVisibility($toolName, $courseId, $sessionId = 0) |
||
565 | { |
||
566 | $allowEditionInSession = api_get_configuration_value('allow_edit_tool_visibility_in_session'); |
||
567 | |||
568 | $em = Database::getManager(); |
||
569 | $toolRepo = $em->getRepository('ChamiloCourseBundle:CTool'); |
||
570 | |||
571 | /** @var CTool $tool */ |
||
572 | $tool = $toolRepo->findOneBy(['cId' => $courseId, 'sessionId' => 0, 'name' => $toolName]); |
||
573 | $visibility = $tool->getVisibility(); |
||
574 | |||
575 | if ($allowEditionInSession && $sessionId) { |
||
576 | $tool = $toolRepo->findOneBy( |
||
577 | ['cId' => $courseId, 'sessionId' => $sessionId, 'name' => $toolName] |
||
578 | ); |
||
579 | |||
580 | if ($tool) { |
||
581 | $visibility = $tool->getVisibility(); |
||
582 | } |
||
583 | } |
||
584 | |||
585 | return $visibility; |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * Filter tool icons. Only show if $patronKey is = :teacher |
||
590 | * Example dataIcons[i]['name']: parameter titleIcons1:teacher || titleIcons2 || titleIcons3:teacher. |
||
591 | * |
||
592 | * @param array $dataIcons array Reference to icons |
||
593 | * @param string $courseToolCategory Current tools category |
||
594 | * |
||
595 | * @return array |
||
596 | */ |
||
597 | private static function filterPluginTools($dataIcons, $courseToolCategory) |
||
598 | { |
||
599 | $patronKey = ':teacher'; |
||
600 | |||
601 | if (TOOL_STUDENT_VIEW == $courseToolCategory) { |
||
602 | //Fix only coach can see external pages - see #8236 - icpna |
||
603 | if (api_is_coach()) { |
||
604 | foreach ($dataIcons as $index => $array) { |
||
605 | if (isset($array['name'])) { |
||
606 | $dataIcons[$index]['name'] = str_replace($patronKey, '', $array['name']); |
||
607 | } |
||
608 | } |
||
609 | |||
610 | return $dataIcons; |
||
611 | } |
||
612 | |||
613 | $flagOrder = false; |
||
614 | |||
615 | foreach ($dataIcons as $index => $array) { |
||
616 | if (!isset($array['name'])) { |
||
617 | continue; |
||
618 | } |
||
619 | |||
620 | $pos = strpos($array['name'], $patronKey); |
||
621 | |||
622 | if (false !== $pos) { |
||
623 | unset($dataIcons[$index]); |
||
624 | $flagOrder = true; |
||
625 | } |
||
626 | } |
||
627 | |||
628 | if ($flagOrder) { |
||
629 | return array_values($dataIcons); |
||
630 | } |
||
631 | |||
632 | return $dataIcons; |
||
633 | } |
||
634 | |||
635 | // clean patronKey of name icons |
||
636 | foreach ($dataIcons as $index => $array) { |
||
637 | if (isset($array['name'])) { |
||
638 | $dataIcons[$index]['name'] = str_replace($patronKey, '', $array['name']); |
||
639 | } |
||
640 | } |
||
641 | |||
642 | return $dataIcons; |
||
643 | } |
||
644 | |||
645 | /** |
||
646 | * Find the tool icon when homepage_view is activity_big. |
||
647 | * |
||
648 | * @param int $iconSize |
||
649 | * @param bool $generateId |
||
650 | * |
||
651 | * @return string |
||
652 | */ |
||
653 | private static function getToolIcon(array $item, $iconSize, $generateId = true) |
||
691 | ); |
||
692 | } |
||
693 | } |
||
694 |