Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

return_breadcrumb()   F

Complexity

Conditions 62
Paths > 20000

Size

Total Lines 245
Code Lines 151

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 62
eloc 151
nc 132000
nop 3
dl 0
loc 245
rs 2
c 0
b 0
f 0

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
use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
5
use ChamiloSession as Session;
6
7
/**
8
 * Code
9
 * @todo use globals or parameters or add this file in the template
10
 * @package chamilo.include
11
 */
12
13
/**
14
 * Determines the possible tabs (=sections) that are available.
15
 * This function is used when creating the tabs in the third header line and
16
 * all the sections that do not appear there (as determined by the
17
 * platform admin on the Dokeos configuration settings page)
18
 * will appear in the right hand menu that appears on several other pages
19
 * @return array containing all the possible tabs
20
 * @author Patrick Cool <[email protected]>, Ghent University
21
 */
22
function get_tabs($courseId = null)
23
{
24
    $courseInfo = api_get_course_info($courseId);
25
26
    $navigation = [];
27
28
    // Campus Homepage
29
    $navigation[SECTION_CAMPUS]['url'] = api_get_path(WEB_PATH).'index.php';
30
    $navigation[SECTION_CAMPUS]['title'] = get_lang('CampusHomepage');
31
    $navigation[SECTION_CAMPUS]['key'] = 'homepage';
32
    $navigation[SECTION_CAMPUS]['icon'] = 'homepage.png';
33
34
    $navigation[SECTION_CATALOG]['url'] = api_get_path(WEB_PATH).'main/auth/courses.php';
35
    $navigation[SECTION_CATALOG]['title'] = get_lang('Courses');
36
    $navigation[SECTION_CATALOG]['key'] = 'catalog';
37
    $navigation[SECTION_CATALOG]['icon'] = 'catalog.png';
38
39
    // My Courses
40
    if (api_is_allowed_to_create_course()) {
41
        // Link to my courses for teachers
42
        $navigation['mycourses']['url'] = api_get_path(WEB_PATH).'user_portal.php?nosession=true';
43
    } else {
44
        // Link to my courses for students
45
        $navigation['mycourses']['url'] = api_get_path(WEB_PATH).'user_portal.php';
46
    }
47
    $navigation['mycourses']['title'] = get_lang('MyCourses');
48
    $navigation['mycourses']['key'] = 'my-course';
49
    $navigation['mycourses']['icon'] = 'my-course.png';
50
51
    // My Profile
52
    $navigation['myprofile']['url'] = api_get_path(WEB_CODE_PATH).'auth/profile.php'
53
        .(!empty($courseInfo['path']) ? '?coursePath='.$courseInfo['path'].'&amp;courseCode='.$courseInfo['official_code'] : '');
54
    $navigation['myprofile']['title'] = get_lang('ModifyProfile');
55
    $navigation['myprofile']['key'] = 'profile';
56
    $navigation['myprofile']['icon'] = 'profile.png';
57
    // Link to my agenda
58
    $navigation['myagenda']['url'] = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?type=personal';
59
    $navigation['myagenda']['title'] = get_lang('MyAgenda');
60
    $navigation['myagenda']['key'] = 'agenda';
61
    $navigation['myagenda']['icon'] = 'agenda.png';
62
63
    // Gradebook
64
    if (api_get_setting('gradebook_enable') == 'true') {
65
        $navigation['mygradebook']['url'] = api_get_path(WEB_CODE_PATH)
66
            .'gradebook/gradebook.php'
67
            .(!empty($courseInfo['path']) ? '?coursePath='.$courseInfo['path'].'&amp;courseCode='.$courseInfo['official_code'] : '');
68
        $navigation['mygradebook']['title'] = get_lang('MyGradebook');
69
        $navigation['mygradebook']['key'] = 'gradebook';
70
        $navigation['mygradebook']['icon'] = 'gradebook.png';
71
    }
72
73
    // Reporting
74
    if (api_is_teacher() || api_is_drh() || api_is_session_admin()) {
75
        // Link to my space
76
        $navigation['session_my_space']['url'] = api_get_path(WEB_CODE_PATH).'mySpace/'
77
            .(api_is_drh() ? 'session.php' : '');
78
        $navigation['session_my_space']['title'] = get_lang('MySpace');
79
        $navigation['session_my_space']['key'] = 'my-space';
80
        $navigation['session_my_space']['icon'] = 'my-space.png';
81
    } else {
82
        if (api_is_student_boss()) {
83
            $navigation['session_my_space']['url'] = api_get_path(WEB_CODE_PATH).'mySpace/student.php';
84
            $navigation['session_my_space']['title'] = get_lang('MySpace');
85
            $navigation['session_my_space']['key'] = 'my-space';
86
            $navigation['session_my_space']['icon'] = 'my-space.png';
87
        } else {
88
            $navigation['session_my_progress']['url'] = api_get_path(WEB_CODE_PATH);
89
            // Link to my progress
90
            switch (api_get_setting('gamification_mode')) {
91
                case 1:
92
                    $navigation['session_my_progress']['url'] .= 'gamification/my_progress.php';
93
                    break;
94
                default:
95
                    $navigation['session_my_progress']['url'] .= 'auth/my_progress.php';
96
            }
97
98
            $navigation['session_my_progress']['title'] = get_lang('MyProgress');
99
            $navigation['session_my_progress']['key'] = 'my-progress';
100
            $navigation['session_my_progress']['icon'] = 'my-progress.png';
101
        }
102
    }
103
104
    // Social
105
    if (api_get_setting('allow_social_tool') == 'true') {
106
        $navigation['social']['url'] = api_get_path(WEB_CODE_PATH).'social/home.php';
107
        $navigation['social']['title'] = get_lang('SocialNetwork');
108
        $navigation['social']['key'] = 'social-network';
109
        $navigation['social']['icon'] = 'social-network.png';
110
    }
111
112
    // Dashboard
113
    if (api_is_platform_admin() || api_is_drh() || api_is_session_admin()) {
114
        $navigation['dashboard']['url'] = api_get_path(WEB_CODE_PATH).'dashboard/index.php';
115
        $navigation['dashboard']['title'] = get_lang('Dashboard');
116
        $navigation['dashboard']['key'] = 'dashboard';
117
        $navigation['dashboard']['icon'] = 'dashboard.png';
118
    }
119
120
    // Custom Tabs See BT#7180
121
    $customTabs = getCustomTabs();
122
    if (!empty($customTabs)) {
123
        foreach ($customTabs as $tab) {
124
            if (api_get_setting($tab['variable'], $tab['subkey']) == 'true') {
125
                if (!empty($tab['comment']) && $tab['comment'] !== 'ShowTabsComment') {
126
                    $navigation[$tab['subkey']]['url'] = $tab['comment'];
127
                    // $tab['title'] value must be included in trad4all.inc.php
128
                    $navigation[$tab['subkey']]['title'] = get_lang($tab['title']);
129
                    $navigation[$tab['subkey']]['key'] = $tab['subkey'];
130
                }
131
            }
132
        }
133
    }
134
    // End Custom Tabs
135
136
    // Platform administration
137
    if (api_is_platform_admin(true)) {
138
        $navigation['platform_admin']['url'] = api_get_path(WEB_CODE_PATH).'admin/';
139
        $navigation['platform_admin']['title'] = get_lang('PlatformAdmin');
140
        $navigation['platform_admin']['key'] = 'admin';
141
        $navigation['platform_admin']['icon'] = 'admin.png';
142
    }
143
144
    return $navigation;
145
}
146
147
/**
148
 * This function returns the custom tabs
149
 *
150
 * @return array
151
 */
152
function getCustomTabs()
153
{
154
    $urlId = api_get_current_access_url_id();
155
    $tableSettingsCurrent = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
156
    $sql = "SELECT * FROM $tableSettingsCurrent
157
            WHERE 
158
                variable = 'show_tabs' AND
159
                subkey LIKE 'custom_tab_%' AND access_url = $urlId ";
160
    $result = Database::query($sql);
161
    $customTabs = [];
162
    while ($row = Database::fetch_assoc($result)) {
163
        $shouldAdd = true;
164
        if (strpos($row['subkey'], Plugin::TAB_FILTER_NO_STUDENT) !== false && api_is_student()) {
165
            $shouldAdd = false;
166
        } elseif (strpos($row['subkey'], Plugin::TAB_FILTER_ONLY_STUDENT) !== false && !api_is_student()) {
167
            $shouldAdd = false;
168
        }
169
170
        if ($shouldAdd) {
171
            $customTabs[] = $row;
172
        }
173
    }
174
175
    return $customTabs;
176
}
177
178
/**
179
 * Return the active logo of the portal, based on a series of settings
180
 * @param string $theme The name of the theme folder from web/css/themes/
181
 * @return string HTML string with logo as an HTML element
182
 */
183
function return_logo($theme = '')
184
{
185
    $siteName = api_get_setting('siteName');
186
187
    return ChamiloApi::getPlatformLogo(
188
        $theme,
189
        [
190
            'title' => $siteName,
191
            'class' => 'img-responsive',
192
            'id' => 'header-logo',
193
        ]
194
    );
195
}
196
197
/**
198
 * Return HTML string of a list as <li> items
199
 * @return string
200
 */
201
function returnNotificationMenu()
202
{
203
    $courseInfo = api_get_course_info();
204
    $course_id = 0;
205
    if (!empty($courseInfo)) {
206
        $course_id = $courseInfo['code'];
207
    }
208
209
    $user_id = api_get_user_id();
210
    $sessionId = api_get_session_id();
211
    $html = '';
212
213
    if ((api_get_setting('showonline', 'world') == 'true' && !$user_id) ||
214
        (api_get_setting('showonline', 'users') == 'true' && $user_id) ||
215
        (api_get_setting('showonline', 'course') == 'true' && $user_id && $course_id)
216
    ) {
217
        $number = getOnlineUsersCount();
218
        $number_online_in_course = getOnlineUsersInCourseCount($user_id, $courseInfo);
219
220
        // Display the who's online of the platform
221
        if ($number &&
222
            (api_get_setting('showonline', 'world') == 'true' && !$user_id) ||
223
            (api_get_setting('showonline', 'users') == 'true' && $user_id)
224
        ) {
225
            $html .= '<li><a href="'.api_get_path(WEB_PATH).'whoisonline.php" target="_self" title="'
226
                .get_lang('UsersOnline').'" >'
227
                .Display::return_icon('user.png', get_lang('UsersOnline'), [], ICON_SIZE_TINY)
228
                .' '.$number.'</a></li>';
229
        }
230
231
        // Display the who's online for the course
232
        if ($number_online_in_course &&
233
            (
234
                is_array($courseInfo) &&
235
                api_get_setting('showonline', 'course') == 'true' && isset($courseInfo['sysCode'])
236
            )
237
        ) {
238
            $html .= '<li><a href="'.api_get_path(WEB_PATH).'whoisonline.php?cidReq='.$courseInfo['sysCode']
239
                .'" target="_self">'
240
                .Display::return_icon('course.png', get_lang('UsersOnline').' '.get_lang('InThisCourse'), [], ICON_SIZE_TINY)
241
                .' '.$number_online_in_course.' </a></li>';
242
        }
243
244
        if (isset($user_id) && $sessionId != 0) {
245
            $numberOnlineInSession = getOnlineUsersInSessionCount($sessionId);
246
247
            $html .= '<li><a href="'.api_get_path(WEB_PATH).'whoisonlinesession.php?id_coach='.$user_id.'" target="_self">'
248
                .Display::return_icon('session.png', get_lang('UsersConnectedToMySessions'), [], ICON_SIZE_TINY)
249
                .' '.$numberOnlineInSession
250
                .'</a></li>';
251
        }
252
    }
253
254
    return $html;
255
}
256
257
/**
258
 * Return an array with different navigation mennu elements
259
 * @return array [menu_navigation[], navigation[], possible_tabs[]]
260
 */
261
function return_navigation_array()
262
{
263
    $navigation = [];
264
    $menu_navigation = [];
265
    $possible_tabs = get_tabs();
266
267
    // Campus Homepage
268
    if (api_get_setting('show_tabs', 'campus_homepage') == 'true') {
269
        $navigation[SECTION_CAMPUS] = $possible_tabs[SECTION_CAMPUS];
270
    } else {
271
        $menu_navigation[SECTION_CAMPUS] = $possible_tabs[SECTION_CAMPUS];
272
    }
273
274
    if (api_get_setting('course_catalog_published') == 'true' && api_is_anonymous()) {
275
        $navigation[SECTION_CATALOG] = $possible_tabs[SECTION_CATALOG];
276
    }
277
278
    if (api_get_user_id() && !api_is_anonymous()) {
279
        // My Courses
280
        if (api_get_setting('show_tabs', 'my_courses') == 'true') {
281
            $navigation['mycourses'] = $possible_tabs['mycourses'];
282
        } else {
283
            $menu_navigation['mycourses'] = $possible_tabs['mycourses'];
284
        }
285
286
        // My Profile
287
        if (api_get_setting('show_tabs', 'my_profile') == 'true' &&
288
            api_get_setting('allow_social_tool') != 'true'
289
        ) {
290
            $navigation['myprofile'] = $possible_tabs['myprofile'];
291
        } else {
292
            $menu_navigation['myprofile'] = $possible_tabs['myprofile'];
293
        }
294
295
        // My Agenda
296
        if (api_get_setting('show_tabs', 'my_agenda') == 'true') {
297
            $navigation['myagenda'] = $possible_tabs['myagenda'];
298
        } else {
299
            $menu_navigation['myagenda'] = $possible_tabs['myagenda'];
300
        }
301
302
        // Gradebook
303
        if (api_get_setting('gradebook_enable') == 'true') {
304
            if (api_get_setting('show_tabs', 'my_gradebook') == 'true') {
305
                $navigation['mygradebook'] = $possible_tabs['mygradebook'];
306
            } else {
307
                $menu_navigation['mygradebook'] = $possible_tabs['mygradebook'];
308
            }
309
        }
310
311
        // Reporting
312
        if (api_get_setting('show_tabs', 'reporting') == 'true') {
313
            if (api_is_teacher() || api_is_drh() || api_is_session_admin() || api_is_student_boss()) {
314
                $navigation['session_my_space'] = $possible_tabs['session_my_space'];
315
            } else {
316
                $navigation['session_my_space'] = $possible_tabs['session_my_progress'];
317
            }
318
        } else {
319
            if (api_is_teacher() || api_is_drh() || api_is_session_admin() || api_is_student_boss()) {
320
                $menu_navigation['session_my_space'] = $possible_tabs['session_my_space'];
321
            } else {
322
                $menu_navigation['session_my_space'] = $possible_tabs['session_my_progress'];
323
            }
324
        }
325
326
        // Social Networking
327
        if (api_get_setting('show_tabs', 'social') == 'true') {
328
            if (api_get_setting('allow_social_tool') == 'true') {
329
                $navigation['social'] = isset($possible_tabs['social']) ? $possible_tabs['social'] : null;
330
            }
331
        } else {
332
            $menu_navigation['social'] = isset($possible_tabs['social']) ? $possible_tabs['social'] : null;
333
        }
334
335
        // Dashboard
336
        if (api_get_setting('show_tabs', 'dashboard') == 'true') {
337
            if (api_is_platform_admin() || api_is_drh() || api_is_session_admin()) {
338
                $navigation['dashboard'] = isset($possible_tabs['dashboard']) ? $possible_tabs['dashboard'] : null;
339
            }
340
        } else {
341
            $menu_navigation['dashboard'] = isset($possible_tabs['dashboard']) ? $possible_tabs['dashboard'] : null;
342
        }
343
344
        ///if (api_is_student()) {
345
        if (true) {
0 ignored issues
show
Bug introduced by
Avoid IF statements that are always true or false
Loading history...
346
            $params = ['variable = ? AND subkey = ?' => ['status', 'studentfollowup']];
347
            $result = api_get_settings_params_simple($params);
348
            $plugin = StudentFollowUpPlugin::create();
349
            if (!empty($result) && $result['selected_value'] === 'installed') {
350
                // Students
351
                $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/posts.php';
352
                if (api_is_platform_admin() || api_is_drh() || api_is_teacher()) {
353
                    $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/my_students.php';
354
                }
355
                $navigation['follow_up']['url'] = $url;
356
                $navigation['follow_up']['title'] = $plugin->get_lang('CareDetailView');
357
                $navigation['follow_up']['key'] = 'homepage';
358
                $navigation['follow_up']['icon'] = 'homepage.png';
359
            }
360
        }
361
362
        // Administration
363
        if (api_is_platform_admin(true)) {
364
            if (api_get_setting('show_tabs', 'platform_administration') == 'true') {
365
                $navigation['platform_admin'] = $possible_tabs['platform_admin'];
366
            } else {
367
                $menu_navigation['platform_admin'] = $possible_tabs['platform_admin'];
368
            }
369
        }
370
371
        // Custom tabs
372
        $customTabs = getCustomTabs();
373
        if (!empty($customTabs)) {
374
            foreach ($customTabs as $tab) {
375
                if (api_get_setting($tab['variable'], $tab['subkey']) == 'true' &&
376
                    isset($possible_tabs[$tab['subkey']])
377
                ) {
378
                    $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
379
                    $navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
380
                } else {
381
                    if (isset($possible_tabs[$tab['subkey']])) {
382
                        $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
383
                        $menu_navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
384
                    }
385
                }
386
            }
387
        }
388
    } else {
389
        // Show custom tabs that are specifically marked as public
390
        $customTabs = getCustomTabs();
391
        if (!empty($customTabs)) {
392
            foreach ($customTabs as $tab) {
393
                if (api_get_setting($tab['variable'], $tab['subkey']) == 'true' &&
394
                    isset($possible_tabs[$tab['subkey']]) &&
395
                    api_get_plugin_setting(strtolower(str_replace('Tabs', '', $tab['subkeytext'])), 'public_main_menu_tab') == 'true'
396
                ) {
397
                    $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
398
                    $navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
399
                } else {
400
                    if (isset($possible_tabs[$tab['subkey']])) {
401
                        $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
402
                        $menu_navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
403
                    }
404
                }
405
            }
406
        }
407
    }
408
409
    return [
410
        'menu_navigation' => $menu_navigation,
411
        'navigation' => $navigation,
412
        'possible_tabs' => $possible_tabs,
413
    ];
414
}
415
416
/**
417
 * Return the navigation menu elements as a flat array
418
 * @return array
419
 */
420
function menuArray()
421
{
422
    $mainNavigation = return_navigation_array();
423
    unset($mainNavigation['possible_tabs']);
424
    unset($mainNavigation['menu_navigation']);
425
    //$navigation = $navigation['navigation'];
426
    // Get active language
427
    $lang = api_get_setting('platformLanguage');
428
    if (!empty($_SESSION['user_language_choice'])) {
429
        $lang = $_SESSION['user_language_choice'];
430
    } elseif (!empty($_SESSION['_user']['language'])) {
431
        $lang = $_SESSION['_user']['language'];
432
    }
433
434
    // Preparing home folder for multiple urls
435
    if (api_get_multiple_access_url()) {
436
        $access_url_id = api_get_current_access_url_id();
437
        if ($access_url_id != -1) {
438
            // If not a dead URL
439
            $urlInfo = api_get_access_url($access_url_id);
440
            $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $urlInfo['url']));
441
            $cleanUrl = api_replace_dangerous_char($url);
442
            $cleanUrl = str_replace('/', '-', $cleanUrl);
443
            $cleanUrl .= '/';
444
            $homepath = api_get_path(SYS_HOME_PATH).$cleanUrl; //homep for Home Path
445
446
            //we create the new dir for the new sites
447
            if (!is_dir($homepath)) {
448
                mkdir($homepath, api_get_permissions_for_new_directories());
449
            }
450
        }
451
    } else {
452
        $homepath = api_get_path(SYS_HOME_PATH);
453
    }
454
    $ext = '.html';
455
    $menuTabs = 'home_tabs';
456
    $menuTabsLoggedIn = 'home_tabs_logged_in';
457
    $pageContent = '';
458
    // Get the extra page content, containing the links to add to the tabs
459
    if (is_file($homepath.$menuTabs.'_'.$lang.$ext) && is_readable($homepath.$menuTabs.'_'.$lang.$ext)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $homepath does not seem to be defined for all execution paths leading up to this point.
Loading history...
460
        $pageContent = @(string) file_get_contents($homepath.$menuTabs.'_'.$lang.$ext);
461
    } elseif (is_file($homepath.$menuTabs.$lang.$ext) && is_readable($homepath.$menuTabs.$lang.$ext)) {
462
        $pageContent = @(string) file_get_contents($homepath.$menuTabs.$lang.$ext);
463
    }
464
    // Sanitize page content
465
    $pageContent = api_to_system_encoding($pageContent, api_detect_encoding(strip_tags($pageContent)));
466
    $open = str_replace('{rel_path}', api_get_path(REL_PATH), $pageContent);
467
    $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
468
    // Get the extra page content, containing the links to add to the tabs
469
    //  that are only for users already logged in
470
    $openMenuTabsLoggedIn = '';
471
    if (api_get_user_id() && !api_is_anonymous()) {
472
        if (is_file($homepath.$menuTabsLoggedIn.'_'.$lang.$ext) && is_readable(
473
                $homepath.$menuTabsLoggedIn.'_'.$lang.$ext
474
            )
475
        ) {
476
            $pageContent = @(string) file_get_contents($homepath.$menuTabsLoggedIn.'_'.$lang.$ext);
477
            $pageContent = str_replace('::private', '', $pageContent);
478
        } elseif (is_file($homepath.$menuTabsLoggedIn.$lang.$ext) && is_readable(
479
                $homepath.$menuTabsLoggedIn.$lang.$ext
480
            )
481
        ) {
482
            $pageContent = @(string) file_get_contents($homepath.$menuTabsLoggedIn.$lang.$ext);
483
            $pageContent = str_replace('::private', '', $pageContent);
484
        }
485
486
        $pageContent = api_to_system_encoding($pageContent, api_detect_encoding(strip_tags($pageContent)));
487
        $openMenuTabsLoggedIn = str_replace('{rel_path}', api_get_path(REL_PATH), $pageContent);
488
        $openMenuTabsLoggedIn = api_to_system_encoding(
489
            $openMenuTabsLoggedIn,
490
            api_detect_encoding(strip_tags($openMenuTabsLoggedIn))
491
        );
492
    }
493
    if (!empty($open) || !empty($openMenuTabsLoggedIn)) {
494
        if (strpos($open.$openMenuTabsLoggedIn, 'show_menu') === false) {
495
            if (api_is_anonymous()) {
496
                $mainNavigation['navigation'][SECTION_CAMPUS] = null;
497
            }
498
        } else {
499
            $list = explode("\n", api_get_user_id() && !api_is_anonymous() ? $openMenuTabsLoggedIn : $open);
500
501
            foreach ($list as $link) {
502
                if (strpos($link, 'class="hide_menu"') !== false) {
503
                    continue;
504
                }
505
506
                $matches = [];
507
                $match = preg_match('$href="([^"]*)" target="([^"]*)">([^<]*)</a>$', $link, $matches);
508
509
                if (!$match) {
510
                    continue;
511
                }
512
513
                $mainNavigation['navigation'][$matches[3]] = [
514
                    'url' => $matches[1],
515
                    'target' => $matches[2],
516
                    'title' => $matches[3],
517
                    'key' => 'page-'.str_replace(' ', '-', strtolower($matches[3])),
518
                ];
519
            }
520
        }
521
    }
522
523
    if (count($mainNavigation['navigation']) > 0) {
524
        //$pre_lis = '';
525
        $activeSection = '';
526
        foreach ($mainNavigation['navigation'] as $section => $navigation_info) {
527
            $key = (!empty($navigation_info['key']) ? 'tab-'.$navigation_info['key'] : '');
528
529
            if (isset($GLOBALS['this_section'])) {
530
                $tempSection = $section;
531
                if ($section == 'social') {
532
                    $tempSection = 'social-network';
533
                }
534
                if ($tempSection == $GLOBALS['this_section']) {
535
                    $activeSection = $section;
536
                }
537
                // If we're on the index page and a specific extra link has been
538
                // loaded
539
                if ($GLOBALS['this_section'] == SECTION_CAMPUS) {
540
                    if (!empty($_GET['include'])) {
541
                        $name = str_replace(' ', '-', strtolower($navigation_info['title'])).'_'.$lang.$ext;
542
                        if (strtolower($_GET['include']) == $name) {
543
                            $activeSection = $section;
544
                        }
545
                    }
546
                }
547
            } else {
548
                $current = '';
549
            }
550
            $mainNavigation['navigation'][$section]['current'] = '';
551
        }
552
        if (!empty($activeSection)) {
553
            $mainNavigation['navigation'][$activeSection]['current'] = 'active';
554
        }
555
    }
556
    unset($mainNavigation['navigation']['myprofile']);
557
558
    return $mainNavigation['navigation'];
559
}
560
561
/**
562
 * Return the breadcrumb menu elements as an array of <li> items
563
 * @param array $interbreadcrumb The elements to add to the breadcrumb
564
 * @param string $language_file Deprecated
565
 * @param string $nameTools The name of the current tool (not linked)
566
 * @return string HTML string of <li> items
567
 */
568
function return_breadcrumb($interbreadcrumb, $language_file, $nameTools)
0 ignored issues
show
Unused Code introduced by
The parameter $language_file is not used and could be removed. ( Ignorable by Annotation )

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

568
function return_breadcrumb($interbreadcrumb, /** @scrutinizer ignore-unused */ $language_file, $nameTools)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
569
{
570
    $courseInfo = api_get_course_info();
571
    $user_id = api_get_user_id();
572
    $additionalBlocks = '';
573
574
    /*  Plugins for banner section */
575
    $web_course_path = api_get_path(WEB_COURSE_PATH);
576
577
    /* If the user is a coach he can see the users who are logged in its session */
578
    $navigation = [];
579
580
    $sessionId = api_get_session_id();
581
    // part 1: Course Homepage. If we are in a course then the first breadcrumb
582
    // is a link to the course homepage
583
    if (!empty($courseInfo) && !isset($_GET['hide_course_breadcrumb'])) {
584
        $sessionName = '';
585
        if (!empty($sessionId)) {
586
            /** @var \Chamilo\CoreBundle\Entity\Session $session */
587
            $session = Database::getManager()->find('ChamiloCoreBundle:Session', $sessionId);
588
            $sessionName = $session ? ' ('.cut($session->getName(), MAX_LENGTH_BREADCRUMB).')' : '';
589
        }
590
591
        $courseInfo['name'] = api_htmlentities($courseInfo['name']);
592
        $course_title = cut($courseInfo['name'], MAX_LENGTH_BREADCRUMB);
593
594
        switch (api_get_setting('breadcrumbs_course_homepage')) {
595
            case 'get_lang':
596
                $itemTitle = Display::return_icon(
597
                    'home.png',
598
                    get_lang('CourseHomepageLink'),
599
                    [],
600
                    ICON_SIZE_TINY
601
                );
602
                break;
603
            case 'course_code':
604
                $itemTitle = Display::return_icon(
605
                    'home.png',
606
                    $courseInfo['official_code'],
607
                    [],
608
                    ICON_SIZE_TINY
609
                )
610
                .' '.$courseInfo['official_code'];
611
                break;
612
            case 'session_name_and_course_title':
613
            default:
614
                $itemTitle = Display::return_icon(
615
                    'home.png',
616
                    $courseInfo['name'].$sessionName,
617
                    [],
618
                    ICON_SIZE_TINY
619
                )
620
                .' '.$course_title.$sessionName;
621
622
                if (!empty($sessionId) && ($session->getDuration() && !api_is_allowed_to_edit())) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $session does not seem to be defined for all execution paths leading up to this point.
Loading history...
623
                    $daysLeft = SessionManager::getDayLeftInSession(
624
                        ['id' => $session->getId(), 'duration' => $session->getDuration()],
625
                        $user_id
626
                    );
627
628
                    if ($daysLeft >= 0) {
629
                        $additionalBlocks .= Display::return_message(
630
                            sprintf(get_lang('SessionDurationXDaysLeft'), $daysLeft),
631
                            'information'
632
                        );
633
                    } else {
634
                        $additionalBlocks .= Display::return_message(
635
                            get_lang('YourSessionTimeHasExpired'),
636
                            'warning'
637
                        );
638
                    }
639
                }
640
                break;
641
        }
642
643
        /**
644
         * @todo could be useful adding the My courses in the breadcrumb
645
         * $navigation_item_my_courses['title'] = get_lang('MyCourses');
646
         * $navigation_item_my_courses['url'] = api_get_path(WEB_PATH).'user_portal.php';
647
         * $navigation[] = $navigation_item_my_courses;
648
         */
649
        $navigation[] = [
650
            'url' => $web_course_path.$courseInfo['path'].'/index.php?id_session='.$sessionId,
651
            'title' => $itemTitle
652
        ];
653
    }
654
655
    /* part 2: Interbreadcrumbs. If there is an array $interbreadcrumb
656
    defined then these have to appear before the last breadcrumb
657
    (which is the tool itself)*/
658
    if (isset($interbreadcrumb) && is_array($interbreadcrumb)) {
659
        foreach ($interbreadcrumb as $breadcrumb_step) {
660
            if (isset($breadcrumb_step['type']) && $breadcrumb_step['type'] == 'right') {
661
                continue;
662
            }
663
            if ($breadcrumb_step['url'] != '#') {
664
                $sep = strrchr($breadcrumb_step['url'], '?') ? '&' : '?';
665
                $courseParams = strpos($breadcrumb_step['url'], 'cidReq') === false ? api_get_cidreq() : '';
666
                $navigation_item['url'] = $breadcrumb_step['url'].$sep.$courseParams;
667
            } else {
668
                $navigation_item['url'] = '#';
669
            }
670
            $navigation_item['title'] = $breadcrumb_step['name'];
671
            // titles for shared folders
672
            if ($breadcrumb_step['name'] == 'shared_folder') {
673
                $navigation_item['title'] = get_lang('UserFolders');
674
            } elseif (strstr($breadcrumb_step['name'], 'shared_folder_session_')) {
675
                $navigation_item['title'] = get_lang('UserFolders');
676
            } elseif (strstr($breadcrumb_step['name'], 'sf_user_')) {
677
                $userinfo = api_get_user_info(substr($breadcrumb_step['name'], 8));
678
                $navigation_item['title'] = $userinfo['complete_name'];
679
            } elseif ($breadcrumb_step['name'] == 'chat_files') {
680
                $navigation_item['title'] = get_lang('ChatFiles');
681
            } elseif ($breadcrumb_step['name'] == 'images') {
682
                $navigation_item['title'] = get_lang('Images');
683
            } elseif ($breadcrumb_step['name'] == 'video') {
684
                $navigation_item['title'] = get_lang('Video');
685
            } elseif ($breadcrumb_step['name'] == 'audio') {
686
                $navigation_item['title'] = get_lang('Audio');
687
            } elseif ($breadcrumb_step['name'] == 'flash') {
688
                $navigation_item['title'] = get_lang('Flash');
689
            } elseif ($breadcrumb_step['name'] == 'gallery') {
690
                $navigation_item['title'] = get_lang('Gallery');
691
            }
692
            // Fixes breadcrumb title now we applied the Security::remove_XSS and
693
            // we cut the string depending of the MAX_LENGTH_BREADCRUMB value
694
            $navigation_item['title'] = cut($navigation_item['title'], MAX_LENGTH_BREADCRUMB);
695
            $navigation_item['title'] = Security::remove_XSS($navigation_item['title']);
696
697
            $navigation[] = $navigation_item;
698
        }
699
    }
700
701
    $navigation_right = [];
702
    if (isset($interbreadcrumb) && is_array($interbreadcrumb)) {
703
        foreach ($interbreadcrumb as $breadcrumb_step) {
704
            if (isset($breadcrumb_step['type']) && $breadcrumb_step['type'] == 'right') {
705
                if ($breadcrumb_step['url'] != '#') {
706
                    $sep = (strrchr($breadcrumb_step['url'], '?') ? '&amp;' : '?');
707
                    $navigation_item['url'] = $breadcrumb_step['url'].$sep.api_get_cidreq();
708
                } else {
709
                    $navigation_item['url'] = '#';
710
                }
711
                $breadcrumb_step['title'] = cut($navigation_item['title'], MAX_LENGTH_BREADCRUMB);
712
                $breadcrumb_step['title'] = Security::remove_XSS($navigation_item['title']);
713
                $navigation_right[] = $breadcrumb_step;
714
            }
715
        }
716
    }
717
718
    // part 3: The tool itself. If we are on the course homepage we do not want
719
    // to display the title of the course because this
720
    // is the same as the first part of the breadcrumbs (see part 1)
721
    if (isset($nameTools)) {
722
        $navigation_item['url'] = '#';
723
        $navigation_item['title'] = $nameTools;
724
        $navigation[] = $navigation_item;
725
    }
726
727
    $final_navigation = [];
728
    $counter = 0;
729
    foreach ($navigation as $index => $navigation_info) {
730
        if (!empty($navigation_info['title'])) {
731
            if ($navigation_info['url'] == '#') {
732
                $final_navigation[$index] = $navigation_info['title'];
733
            } else {
734
                $final_navigation[$index] = '<a href="'.$navigation_info['url'].'" target="_self">'.$navigation_info['title'].'</a>';
735
            }
736
            $counter++;
737
        }
738
    }
739
740
    $html = '';
741
742
    /* Part 4 . Show the teacher view/student view button at the right of the breadcrumb */
743
    $view_as_student_link = null;
744
    if ($user_id && !empty($courseInfo)) {
745
        if ((
746
                api_is_course_admin() ||
747
                api_is_platform_admin() ||
748
                api_is_coach(null, null, false)
749
            ) &&
750
            api_get_setting('student_view_enabled') === 'true' && api_get_course_info()
751
        ) {
752
            $view_as_student_link = api_display_tool_view_option();
753
754
            // Only show link if LP can be editable
755
            /** @var learnpath $learnPath */
756
            $learnPath = Session::read('oLP');
757
            if (!empty($learnPath) && !empty($view_as_student_link)) {
758
                if ((int) $learnPath->get_lp_session_id() != (int) api_get_session_id()) {
759
                    $view_as_student_link = '';
760
                }
761
            }
762
        }
763
    }
764
765
    if (!empty($final_navigation)) {
766
        $lis = '';
767
        $i = 0;
768
        $final_navigation_count = count($final_navigation);
769
        if (!empty($final_navigation)) {
770
            // $home_link.= '<span class="divider">/</span>';
771
            if (!empty($home_link)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $home_link seems to never exist and therefore empty should always be true.
Loading history...
772
                $lis .= Display::tag('li', $home_link);
773
            }
774
775
            foreach ($final_navigation as $bread) {
776
                $bread_check = trim(strip_tags($bread));
777
                if (!empty($bread_check)) {
778
                    if ($final_navigation_count - 1 > $i) {
779
                        $bread .= '';
780
                    }
781
                    $lis .= Display::tag('li', $bread, ['class' => 'active']);
782
                    $i++;
783
                }
784
            }
785
        } else {
786
            if (!empty($home_link)) {
787
                $lis .= Display::tag('li', $home_link);
788
            }
789
        }
790
791
        // View as student/teacher link
792
        if (!empty($view_as_student_link)) {
793
            $html .= Display::tag('div', $view_as_student_link, ['id' => 'view_as_link', 'class' => 'pull-right']);
794
        }
795
796
        if (!empty($navigation_right)) {
797
            foreach ($navigation_right as $item) {
798
                $extra_class = isset($item['class']) ? $item['class'] : null;
799
                $lis .= Display::tag(
800
                    'li',
801
                    $item['title'],
802
                    ['class' => $extra_class.' pull-right']
803
                );
804
            }
805
        }
806
807
        if (!empty($lis)) {
808
            $html .= Display::tag('ul', $lis, ['class' => 'breadcrumb']);
809
        }
810
    }
811
812
    return $html.$additionalBlocks;
813
}
814
815
/**
816
 * Helper function to get the number of users online, using cache if available
817
 * @return  int     The number of users currently online
818
 */
819
function getOnlineUsersCount()
820
{
821
    $number = 0;
822
    $cacheAvailable = api_get_configuration_value('apc');
823
    if ($cacheAvailable === true) {
824
        $apcVar = api_get_configuration_value('apc_prefix').'my_campus_whoisonline_count_simple';
0 ignored issues
show
Bug introduced by
Are you sure api_get_configuration_value('apc_prefix') of type mixed|false 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

824
        $apcVar = /** @scrutinizer ignore-type */ api_get_configuration_value('apc_prefix').'my_campus_whoisonline_count_simple';
Loading history...
825
        if (apcu_exists($apcVar)) {
826
            $number = apcu_fetch($apcVar);
827
        } else {
828
            $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
829
            apcu_store($apcVar, $number, 15);
830
        }
831
    } else {
832
        $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
833
    }
834
835
    return $number;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $number could also return false which is incompatible with the documented return type integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
836
}
837
838
/**
839
 * Helper function to get the number of users online in a course, using cache if available
840
 * @param   int $userId The user ID
841
 * @param   array $courseInfo The course details
842
 * @return  int     The number of users currently online
843
 */
844
function getOnlineUsersInCourseCount($userId, $courseInfo)
845
{
846
    $cacheAvailable = api_get_configuration_value('apc');
847
    $numberOnlineInCourse = 0;
848
    if (!empty($courseInfo['id'])) {
849
        if ($cacheAvailable === true) {
850
            $apcVar = api_get_configuration_value('apc_prefix').'my_campus_whoisonline_count_simple_'.$courseInfo['id'];
0 ignored issues
show
Bug introduced by
Are you sure api_get_configuration_value('apc_prefix') of type mixed|false 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

850
            $apcVar = /** @scrutinizer ignore-type */ api_get_configuration_value('apc_prefix').'my_campus_whoisonline_count_simple_'.$courseInfo['id'];
Loading history...
851
            if (apcu_exists($apcVar)) {
852
                $numberOnlineInCourse = apcu_fetch($apcVar);
853
            } else {
854
                $numberOnlineInCourse = who_is_online_in_this_course_count(
855
                    $userId,
856
                    api_get_setting('time_limit_whosonline'),
857
                    $courseInfo['id']
858
                );
859
                apcu_store(
860
                    $apcVar,
861
                    $numberOnlineInCourse,
862
                    15
863
                );
864
            }
865
        } else {
866
            $numberOnlineInCourse = who_is_online_in_this_course_count(
867
                $userId,
868
                api_get_setting('time_limit_whosonline'),
869
                $courseInfo['id']
870
            );
871
        }
872
    }
873
874
    return $numberOnlineInCourse;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $numberOnlineInCourse could also return false which is incompatible with the documented return type integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
875
}
876
877
/**
878
 * Helper function to get the number of users online in a session, using cache if available
879
 * @param int $sessionId The session ID
880
 * @return int The number of users currently online
881
 */
882
function getOnlineUsersInSessionCount($sessionId)
883
{
884
    $cacheAvailable = api_get_configuration_value('apc');
885
886
    if (!$sessionId) {
887
        return 0;
888
    }
889
890
    if ($cacheAvailable === true) {
891
        $apcVar = api_get_configuration_value('apc_prefix').'my_campus_whoisonline_session_count_simple_'.$sessionId;
0 ignored issues
show
Bug introduced by
Are you sure api_get_configuration_value('apc_prefix') of type mixed|false 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

891
        $apcVar = /** @scrutinizer ignore-type */ api_get_configuration_value('apc_prefix').'my_campus_whoisonline_session_count_simple_'.$sessionId;
Loading history...
892
893
        if (apcu_exists($apcVar)) {
894
            return apcu_fetch($apcVar);
895
        }
896
897
        $numberOnlineInCourse = whoIsOnlineInThisSessionCount(
898
            api_get_setting('time_limit_whosonline'),
899
            $sessionId
900
        );
901
        apcu_store($apcVar, $numberOnlineInCourse, 15);
902
903
        return $numberOnlineInCourse;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $numberOnlineInCourse returns the type boolean which is incompatible with the documented return type integer.
Loading history...
904
    }
905
906
    return whoIsOnlineInThisSessionCount(
0 ignored issues
show
Bug Best Practice introduced by
The expression return whoIsOnlineInThis...osonline'), $sessionId) returns the type boolean which is incompatible with the documented return type integer.
Loading history...
907
        api_get_setting('time_limit_whosonline'),
908
        $sessionId
909
    );
910
}
911