Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/inc/lib/banner.lib.php (1 issue)

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
 *
10
 * @todo use globals or parameters or add this file in the template
11
 *
12
 * @package chamilo.include
13
 */
14
15
/**
16
 * Determines the possible tabs (=sections) that are available.
17
 * This function is used when creating the tabs in the third header line and
18
 * all the sections that do not appear there (as determined by the
19
 * platform admin on the Dokeos configuration settings page)
20
 * will appear in the right hand menu that appears on several other pages.
21
 *
22
 * @return array containing all the possible tabs
23
 *
24
 * @author Patrick Cool <[email protected]>, Ghent University
25
 */
26
function get_tabs($courseId = null)
27
{
28
    $courseInfo = api_get_course_info($courseId);
29
30
    $navigation = [];
31
32
    // Campus Homepage
33
    $navigation[SECTION_CAMPUS]['url'] = api_get_path(WEB_PATH).'index.php';
34
    $navigation[SECTION_CAMPUS]['title'] = get_lang('CampusHomepage');
35
    $navigation[SECTION_CAMPUS]['key'] = 'homepage';
36
    $navigation[SECTION_CAMPUS]['icon'] = 'homepage.png';
37
38
    $navigation[SECTION_CATALOG]['url'] = api_get_path(WEB_PATH).'main/auth/courses.php';
39
    $navigation[SECTION_CATALOG]['title'] = get_lang('Courses');
40
    $navigation[SECTION_CATALOG]['key'] = 'catalog';
41
    $navigation[SECTION_CATALOG]['icon'] = 'catalog.png';
42
43
    // My Courses
44
    if (api_is_allowed_to_create_course()) {
45
        // Link to my courses for teachers
46
        $navigation['mycourses']['url'] = api_get_path(WEB_PATH).'user_portal.php?nosession=true';
47
    } else {
48
        // Link to my courses for students
49
        $navigation['mycourses']['url'] = api_get_path(WEB_PATH).'user_portal.php';
50
    }
51
    $navigation['mycourses']['title'] = get_lang('MyCourses');
52
    $navigation['mycourses']['key'] = 'my-course';
53
    $navigation['mycourses']['icon'] = 'my-course.png';
54
55
    // My Profile
56
    $navigation['myprofile']['url'] = api_get_path(WEB_CODE_PATH).'auth/profile.php'
57
        .(!empty($courseInfo['path']) ? '?coursePath='.$courseInfo['path'].'&amp;courseCode='.$courseInfo['official_code'] : '');
58
    $navigation['myprofile']['title'] = get_lang('ModifyProfile');
59
    $navigation['myprofile']['key'] = 'profile';
60
    $navigation['myprofile']['icon'] = 'profile.png';
61
    // Link to my agenda
62
    $navigation['myagenda']['url'] = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?type=personal';
63
    $navigation['myagenda']['title'] = get_lang('MyAgenda');
64
    $navigation['myagenda']['key'] = 'agenda';
65
    $navigation['myagenda']['icon'] = 'agenda.png';
66
67
    // Gradebook
68
    if (api_get_setting('gradebook_enable') == 'true') {
69
        $navigation['mygradebook']['url'] = api_get_path(WEB_CODE_PATH)
70
            .'gradebook/gradebook.php'
71
            .(!empty($courseInfo['path']) ? '?coursePath='.$courseInfo['path'].'&amp;courseCode='.$courseInfo['official_code'] : '');
72
        $navigation['mygradebook']['title'] = get_lang('MyGradebook');
73
        $navigation['mygradebook']['key'] = 'gradebook';
74
        $navigation['mygradebook']['icon'] = 'gradebook.png';
75
    }
76
77
    // Reporting
78
    if (api_is_teacher() || api_is_drh() || api_is_session_admin()) {
79
        // Link to my space
80
        $navigation['session_my_space']['url'] = api_get_path(WEB_CODE_PATH).'mySpace/'
81
            .(api_is_drh() ? 'session.php' : '');
82
        $navigation['session_my_space']['title'] = get_lang('MySpace');
83
        $navigation['session_my_space']['key'] = 'my-space';
84
        $navigation['session_my_space']['icon'] = 'my-space.png';
85
    } else {
86
        if (api_is_student_boss()) {
87
            $navigation['session_my_space']['url'] = api_get_path(WEB_CODE_PATH).'mySpace/student.php';
88
            $navigation['session_my_space']['title'] = get_lang('MySpace');
89
            $navigation['session_my_space']['key'] = 'my-space';
90
            $navigation['session_my_space']['icon'] = 'my-space.png';
91
        } else {
92
            $navigation['session_my_progress']['url'] = api_get_path(WEB_CODE_PATH);
93
            // Link to my progress
94
            switch (api_get_setting('gamification_mode')) {
95
                case 1:
96
                    $navigation['session_my_progress']['url'] .= 'gamification/my_progress.php';
97
                    break;
98
                default:
99
                    $navigation['session_my_progress']['url'] .= 'auth/my_progress.php';
100
            }
101
102
            $navigation['session_my_progress']['title'] = get_lang('MyProgress');
103
            $navigation['session_my_progress']['key'] = 'my-progress';
104
            $navigation['session_my_progress']['icon'] = 'my-progress.png';
105
        }
106
    }
107
108
    // Social
109
    if (api_get_setting('allow_social_tool') == 'true') {
110
        $navigation['social']['url'] = api_get_path(WEB_CODE_PATH).'social/home.php';
111
        $navigation['social']['title'] = get_lang('SocialNetwork');
112
        $navigation['social']['key'] = 'social-network';
113
        $navigation['social']['icon'] = 'social-network.png';
114
    }
115
116
    // Dashboard
117
    if (api_is_platform_admin() || api_is_drh() || api_is_session_admin()) {
118
        $navigation['dashboard']['url'] = api_get_path(WEB_CODE_PATH).'dashboard/index.php';
119
        $navigation['dashboard']['title'] = get_lang('Dashboard');
120
        $navigation['dashboard']['key'] = 'dashboard';
121
        $navigation['dashboard']['icon'] = 'dashboard.png';
122
    }
123
124
    // Custom Tabs See BT#7180
125
    $customTabs = getCustomTabs();
126
    if (!empty($customTabs)) {
127
        foreach ($customTabs as $tab) {
128
            if (api_get_setting($tab['variable'], $tab['subkey']) == 'true') {
129
                if (!empty($tab['comment']) && $tab['comment'] !== 'ShowTabsComment') {
130
                    $navigation[$tab['subkey']]['url'] = $tab['comment'];
131
                    // $tab['title'] value must be included in trad4all.inc.php
132
                    $navigation[$tab['subkey']]['title'] = get_lang($tab['title']);
133
                    $navigation[$tab['subkey']]['key'] = $tab['subkey'];
134
                }
135
            }
136
        }
137
    }
138
    // End Custom Tabs
139
140
    // Platform administration
141
    if (api_is_platform_admin(true)) {
142
        $navigation['platform_admin']['url'] = api_get_path(WEB_CODE_PATH).'admin/';
143
        $navigation['platform_admin']['title'] = get_lang('PlatformAdmin');
144
        $navigation['platform_admin']['key'] = 'admin';
145
        $navigation['platform_admin']['icon'] = 'admin.png';
146
    }
147
148
    return $navigation;
149
}
150
151
/**
152
 * This function returns the custom tabs.
153
 *
154
 * @return array
155
 */
156
function getCustomTabs()
157
{
158
    static $customTabs = null;
159
160
    if ($customTabs !== null) {
161
        return $customTabs;
162
    }
163
164
    $urlId = api_get_current_access_url_id();
165
    $isStudent = api_is_student();
166
    $cacheAvailable = api_get_configuration_value('apc');
167
    if ($cacheAvailable === true) {
168
        $apcVar = api_get_configuration_value('apc_prefix').'custom_tabs_url_'.$urlId.'_student_'.($isStudent ? '1' : '0');
169
        if (apcu_exists($apcVar)) {
170
            return apcu_fetch($apcVar);
171
        }
172
    }
173
    $tableSettingsCurrent = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
174
    $sql = "SELECT * FROM $tableSettingsCurrent
175
            WHERE
176
                variable = 'show_tabs' AND
177
                subkey LIKE 'custom_tab_%' AND access_url = $urlId ";
178
    $result = Database::query($sql);
179
    $customTabs = [];
180
    while ($row = Database::fetch_assoc($result)) {
181
        $shouldAdd = true;
182
        if (strpos($row['subkey'], Plugin::TAB_FILTER_NO_STUDENT) !== false && $isStudent) {
183
            $shouldAdd = false;
184
        } elseif (strpos($row['subkey'], Plugin::TAB_FILTER_ONLY_STUDENT) !== false && !$isStudent) {
185
            $shouldAdd = false;
186
        }
187
188
        if ($shouldAdd) {
189
            $customTabs[] = $row;
190
        }
191
    }
192
    if ($cacheAvailable === true) {
193
        $apcVar = api_get_configuration_value('apc_prefix').'custom_tabs_url_'.$urlId.'_student_'.($isStudent ? '1' : '0');
194
        apcu_store($apcVar, $customTabs, 15);
195
    }
196
197
    return $customTabs;
198
}
199
200
/**
201
 * Return the active logo of the portal, based on a series of settings.
202
 *
203
 * @param string $theme      The name of the theme folder from web/css/themes/
204
 * @param bool   $responsive add class img-responsive
205
 *
206
 * @return string HTML string with logo as an HTML element
207
 */
208
function return_logo($theme = '', $responsive = true)
209
{
210
    $siteName = api_get_setting('siteName');
211
    $class = 'img-responsive';
212
    if (!$responsive) {
213
        $class = '';
214
    }
215
216
    return ChamiloApi::getPlatformLogo(
217
        $theme,
218
        [
219
            'title' => $siteName,
220
            'class' => $class,
221
            'id' => 'header-logo',
222
        ]
223
    );
224
}
225
226
/**
227
 * Check if user have access to "who is online" page.
228
 *
229
 * @param int $userId   The user for whom we want to check
230
 * @param int $courseId The course ID for if we want the number of users in the course. Set to 0 for "out of a course context". Leave empty if you want the PHP session info to be used.
231
 *
232
 * @return bool
233
 */
234
function accessToWhoIsOnline($userId = null, $courseId = null)
235
{
236
    if (empty($userId)) {
237
        $userId = api_get_user_id();
238
    }
239
    // If we received 0, treat it as "no course" instead of searching again
240
    if ($courseId === null) {
241
        $courseId = api_get_course_int_id();
242
    }
243
    $access = false;
244
245
    if (true === api_get_configuration_value('whoisonline_only_for_admin') && !api_is_platform_admin()) {
246
        return false;
247
    }
248
249
    if ((api_get_setting('showonline', 'world') == 'true' && !$userId) ||
250
        (api_get_setting('showonline', 'users') == 'true' && $userId) ||
251
        (api_get_setting('showonline', 'course') == 'true' && $userId && $courseId)
252
    ) {
253
        $access = true;
254
        $profileList = api_get_configuration_value('allow_online_users_by_status');
255
        if (!empty($profileList) && isset($profileList['status'])) {
256
            $userInfo = api_get_user_info($userId);
257
            if ($userInfo['is_admin']) {
258
                $userInfo['status'] = PLATFORM_ADMIN;
259
            }
260
            $profileList = $profileList['status'];
261
            $access = false;
262
            if (in_array($userInfo['status'], $profileList)) {
263
                $access = true;
264
            }
265
        }
266
    }
267
268
    return $access;
269
}
270
271
/**
272
 * Return HTML string of a list as <li> items.
273
 *
274
 * @return string
275
 */
276
function returnNotificationMenu()
277
{
278
    $html = '';
279
280
    $user_id = api_get_user_id();
281
    $courseInfo = api_get_course_info();
282
    if (accessToWhoIsOnline($user_id, (!empty($courseInfo['real_id']) ?: 0))) {
283
        // Display the who's online of the platform
284
        if ((api_get_setting('showonline', 'world') == 'true' && !$user_id) ||
285
            (api_get_setting('showonline', 'users') == 'true' && $user_id)
286
        ) {
287
            $number = getOnlineUsersCount();
288
            if ($number) {
289
                $html .= '<li class="user-online"><a href="'.api_get_path(WEB_PATH).'whoisonline.php" target="_self" title="'
290
                    .get_lang('UsersOnline').'" >'
291
                    .Display::return_icon('user.png', get_lang('UsersOnline'), [], ICON_SIZE_TINY)
292
                    .' '.$number.'</a></li>';
293
            }
294
        }
295
296
        // Display the who's online for the course
297
        if (
298
            is_array($courseInfo) &&
299
            api_get_setting('showonline', 'course') == 'true' && isset($courseInfo['sysCode'])
300
        ) {
301
            $number_online_in_course = getOnlineUsersInCourseCount($user_id, $courseInfo);
302
            if ($number_online_in_course) {
303
                $html .= '<li class="user-online-course"><a href="'.api_get_path(WEB_PATH).'whoisonline.php?cidReq='.$courseInfo['sysCode']
304
                    .'" target="_self">'
305
                    .Display::return_icon('course.png', get_lang('UsersOnline').' '.get_lang('InThisCourse'), [], ICON_SIZE_TINY)
306
                    .' '.$number_online_in_course.' </a></li>';
307
            }
308
        }
309
310
        $sessionId = api_get_session_id();
311
        if (!empty($sessionId)) {
312
            $allow = api_is_platform_admin(true) ||
313
                api_is_coach($sessionId, null, false) ||
314
                SessionManager::isUserSubscribedAsStudent($sessionId, api_get_user_id());
315
            if ($allow) {
316
                $numberOnlineInSession = getOnlineUsersInSessionCount($sessionId);
317
                $html .= '<li class="user-online-session">
318
                            <a href="'.api_get_path(WEB_PATH).'whoisonlinesession.php" target="_self">'
319
                            .Display::return_icon('session.png', get_lang('UsersConnectedToMySessions'), [], ICON_SIZE_TINY)
320
                            .' '.$numberOnlineInSession.'</a></li>';
321
            }
322
        }
323
    }
324
325
    return $html;
326
}
327
328
/**
329
 * Return an array with different navigation mennu elements.
330
 *
331
 * @return array [menu_navigation[], navigation[], possible_tabs[]]
332
 */
333
function return_navigation_array()
334
{
335
    $navigation = [];
336
    $menu_navigation = [];
337
    $possible_tabs = get_tabs();
338
339
    // Campus Homepage
340
    if (api_get_setting('show_tabs', 'campus_homepage') == 'true') {
341
        $navigation[SECTION_CAMPUS] = $possible_tabs[SECTION_CAMPUS];
342
    } else {
343
        $menu_navigation[SECTION_CAMPUS] = $possible_tabs[SECTION_CAMPUS];
344
    }
345
346
    if (api_get_setting('course_catalog_published') == 'true' && api_is_anonymous()) {
347
        $navigation[SECTION_CATALOG] = $possible_tabs[SECTION_CATALOG];
348
    }
349
350
    if (api_get_user_id() && !api_is_anonymous()) {
351
        // My Courses
352
        if (api_get_setting('show_tabs', 'my_courses') == 'true') {
353
            $navigation['mycourses'] = $possible_tabs['mycourses'];
354
        } else {
355
            $menu_navigation['mycourses'] = $possible_tabs['mycourses'];
356
        }
357
358
        // My Profile
359
        if (api_get_setting('show_tabs', 'my_profile') == 'true' &&
360
            api_get_setting('allow_social_tool') != 'true'
361
        ) {
362
            $navigation['myprofile'] = $possible_tabs['myprofile'];
363
        } else {
364
            $menu_navigation['myprofile'] = $possible_tabs['myprofile'];
365
        }
366
367
        // My Agenda
368
        if (api_get_setting('show_tabs', 'my_agenda') == 'true') {
369
            $navigation['myagenda'] = $possible_tabs['myagenda'];
370
        } else {
371
            $menu_navigation['myagenda'] = $possible_tabs['myagenda'];
372
        }
373
374
        // Gradebook
375
        if (api_get_setting('gradebook_enable') == 'true') {
376
            if (api_get_setting('show_tabs', 'my_gradebook') == 'true') {
377
                $navigation['mygradebook'] = $possible_tabs['mygradebook'];
378
            } else {
379
                $menu_navigation['mygradebook'] = $possible_tabs['mygradebook'];
380
            }
381
        }
382
383
        // Reporting
384
        if (api_get_setting('show_tabs', 'reporting') == 'true') {
385
            if (api_is_teacher() || api_is_drh() || api_is_session_admin() || api_is_student_boss()) {
386
                $navigation['session_my_space'] = $possible_tabs['session_my_space'];
387
            } else {
388
                $navigation['session_my_space'] = $possible_tabs['session_my_progress'];
389
            }
390
        } else {
391
            if (api_is_teacher() || api_is_drh() || api_is_session_admin() || api_is_student_boss()) {
392
                $menu_navigation['session_my_space'] = $possible_tabs['session_my_space'];
393
            } else {
394
                $menu_navigation['session_my_space'] = $possible_tabs['session_my_progress'];
395
            }
396
        }
397
398
        // Social Networking
399
        if (api_get_setting('show_tabs', 'social') == 'true') {
400
            if (api_get_setting('allow_social_tool') == 'true') {
401
                $navigation['social'] = isset($possible_tabs['social']) ? $possible_tabs['social'] : null;
402
            }
403
        } else {
404
            $menu_navigation['social'] = isset($possible_tabs['social']) ? $possible_tabs['social'] : null;
405
        }
406
407
        // Dashboard
408
        if (api_get_setting('show_tabs', 'dashboard') == 'true') {
409
            if (api_is_platform_admin() || api_is_drh() || api_is_session_admin()) {
410
                $navigation['dashboard'] = isset($possible_tabs['dashboard']) ? $possible_tabs['dashboard'] : null;
411
            }
412
        } else {
413
            $menu_navigation['dashboard'] = isset($possible_tabs['dashboard']) ? $possible_tabs['dashboard'] : null;
414
        }
415
416
        $installed = AppPlugin::getInstance()->isInstalled('studentfollowup');
417
        if ($installed) {
418
            $plugin = StudentFollowUpPlugin::create();
419
            // Students
420
            $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/posts.php';
421
            if (api_is_platform_admin() || api_is_drh() || api_is_teacher()) {
422
                $url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/my_students.php';
423
            }
424
            $navigation['follow_up']['url'] = $url;
425
            $navigation['follow_up']['title'] = $plugin->get_lang('CareDetailView');
426
            $navigation['follow_up']['key'] = 'homepage';
427
            $navigation['follow_up']['icon'] = 'homepage.png';
428
        }
429
430
        // Administration
431
        if (api_is_platform_admin(true)) {
432
            if (api_get_setting('show_tabs', 'platform_administration') == 'true') {
433
                $navigation['platform_admin'] = $possible_tabs['platform_admin'];
434
            } else {
435
                $menu_navigation['platform_admin'] = $possible_tabs['platform_admin'];
436
            }
437
        }
438
439
        // Custom tabs
440
        $customTabs = getCustomTabs();
441
        if (!empty($customTabs)) {
442
            foreach ($customTabs as $tab) {
443
                if (api_get_setting($tab['variable'], $tab['subkey']) == 'true' &&
444
                    isset($possible_tabs[$tab['subkey']])
445
                ) {
446
                    $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
447
                    $navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
448
                } else {
449
                    if (isset($possible_tabs[$tab['subkey']])) {
450
                        $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
451
                        $menu_navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
452
                    }
453
                }
454
            }
455
        }
456
    } else {
457
        // Show custom tabs that are specifically marked as public
458
        $customTabs = getCustomTabs();
459
        if (!empty($customTabs)) {
460
            foreach ($customTabs as $tab) {
461
                if (api_get_setting($tab['variable'], $tab['subkey']) == 'true' &&
462
                    isset($possible_tabs[$tab['subkey']]) &&
463
                    api_get_plugin_setting(strtolower(str_replace('Tabs', '', $tab['subkeytext'])), 'public_main_menu_tab') == 'true'
464
                ) {
465
                    $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
466
                    $navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
467
                } else {
468
                    if (isset($possible_tabs[$tab['subkey']])) {
469
                        $possible_tabs[$tab['subkey']]['url'] = api_get_path(WEB_PATH).$possible_tabs[$tab['subkey']]['url'];
470
                        $menu_navigation[$tab['subkey']] = $possible_tabs[$tab['subkey']];
471
                    }
472
                }
473
            }
474
        }
475
    }
476
477
    return [
478
        'menu_navigation' => $menu_navigation,
479
        'navigation' => $navigation,
480
        'possible_tabs' => $possible_tabs,
481
    ];
482
}
483
484
/**
485
 * Return the navigation menu elements as a flat array.
486
 *
487
 * @return array
488
 */
489
function menuArray()
490
{
491
    $mainNavigation = return_navigation_array();
492
    unset($mainNavigation['possible_tabs']);
493
    unset($mainNavigation['menu_navigation']);
494
    //$navigation = $navigation['navigation'];
495
    // Get active language
496
    $lang = api_get_setting('platformLanguage');
497
    if (!empty($_SESSION['user_language_choice'])) {
498
        $lang = $_SESSION['user_language_choice'];
499
    } elseif (!empty($_SESSION['_user']['language'])) {
500
        $lang = $_SESSION['_user']['language'];
501
    }
502
503
    // Preparing home folder for multiple urls
504
    if (api_get_multiple_access_url()) {
505
        $access_url_id = api_get_current_access_url_id();
506
        if ($access_url_id != -1) {
507
            // If not a dead URL
508
            $urlInfo = api_get_access_url($access_url_id);
509
            $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $urlInfo['url']));
510
            $cleanUrl = api_replace_dangerous_char($url);
511
            $cleanUrl = str_replace('/', '-', $cleanUrl);
512
            $cleanUrl .= '/';
513
            $homepath = api_get_path(SYS_HOME_PATH).$cleanUrl; //homep for Home Path
514
515
            //we create the new dir for the new sites
516
            if (!is_dir($homepath)) {
517
                mkdir($homepath, api_get_permissions_for_new_directories());
518
            }
519
        }
520
    } else {
521
        $homepath = api_get_path(SYS_HOME_PATH);
522
    }
523
    $ext = '.html';
524
    $menuTabs = 'home_tabs';
525
    $menuTabsLoggedIn = 'home_tabs_logged_in';
526
    $pageContent = '';
527
    // Get the extra page content, containing the links to add to the tabs
528
    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...
529
        $pageContent = @(string) file_get_contents($homepath.$menuTabs.'_'.$lang.$ext);
530
    } elseif (is_file($homepath.$menuTabs.$lang.$ext) && is_readable($homepath.$menuTabs.$lang.$ext)) {
531
        $pageContent = @(string) file_get_contents($homepath.$menuTabs.$lang.$ext);
532
    }
533
    // Sanitize page content
534
    $pageContent = api_to_system_encoding($pageContent, api_detect_encoding(strip_tags($pageContent)));
535
    $open = str_replace('{rel_path}', api_get_path(REL_PATH), $pageContent);
536
    $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
537
    // Get the extra page content, containing the links to add to the tabs
538
    //  that are only for users already logged in
539
    $openMenuTabsLoggedIn = '';
540
    if (api_get_user_id() && !api_is_anonymous()) {
541
        if (is_file($homepath.$menuTabsLoggedIn.'_'.$lang.$ext) && is_readable(
542
                $homepath.$menuTabsLoggedIn.'_'.$lang.$ext
543
            )
544
        ) {
545
            $pageContent = @(string) file_get_contents($homepath.$menuTabsLoggedIn.'_'.$lang.$ext);
546
            $pageContent = str_replace('::private', '', $pageContent);
547
        } elseif (is_file($homepath.$menuTabsLoggedIn.$lang.$ext) && is_readable(
548
                $homepath.$menuTabsLoggedIn.$lang.$ext
549
            )
550
        ) {
551
            $pageContent = @(string) file_get_contents($homepath.$menuTabsLoggedIn.$lang.$ext);
552
            $pageContent = str_replace('::private', '', $pageContent);
553
        }
554
555
        $pageContent = api_to_system_encoding($pageContent, api_detect_encoding(strip_tags($pageContent)));
556
        $openMenuTabsLoggedIn = str_replace('{rel_path}', api_get_path(REL_PATH), $pageContent);
557
        $openMenuTabsLoggedIn = api_to_system_encoding(
558
            $openMenuTabsLoggedIn,
559
            api_detect_encoding(strip_tags($openMenuTabsLoggedIn))
560
        );
561
    }
562
    if (!empty($open) || !empty($openMenuTabsLoggedIn)) {
563
        if (strpos($open.$openMenuTabsLoggedIn, 'show_menu') !== false) {
564
            $list = explode("\n", api_get_user_id() && !api_is_anonymous() ? $openMenuTabsLoggedIn : $open);
565
566
            foreach ($list as $link) {
567
                if (strpos($link, 'class="hide_menu"') !== false) {
568
                    continue;
569
                }
570
571
                $matches = [];
572
                $match = preg_match('$href="([^"]*)" target="([^"]*)">([^<]*)</a>$', $link, $matches);
573
574
                if (!$match) {
575
                    continue;
576
                }
577
578
                $mainNavigation['navigation'][$matches[3]] = [
579
                    'url' => $matches[1],
580
                    'target' => $matches[2],
581
                    'title' => $matches[3],
582
                    'key' => 'page-'.str_replace(' ', '-', strtolower($matches[3])),
583
                ];
584
            }
585
        }
586
    }
587
588
    if (count($mainNavigation['navigation']) > 0) {
589
        //$pre_lis = '';
590
        $activeSection = '';
591
        foreach ($mainNavigation['navigation'] as $section => $navigation_info) {
592
            $key = (!empty($navigation_info['key']) ? 'tab-'.$navigation_info['key'] : '');
593
594
            if (isset($GLOBALS['this_section'])) {
595
                $tempSection = $section;
596
                if ($section == 'social') {
597
                    $tempSection = 'social-network';
598
                }
599
                if ($tempSection == $GLOBALS['this_section']) {
600
                    $activeSection = $section;
601
                }
602
                // If we're on the index page and a specific extra link has been
603
                // loaded
604
                if ($GLOBALS['this_section'] == SECTION_CAMPUS) {
605
                    if (!empty($_GET['include'])) {
606
                        $name = str_replace(' ', '-', strtolower($navigation_info['title'])).'_'.$lang.$ext;
607
                        if (strtolower($_GET['include']) == $name) {
608
                            $activeSection = $section;
609
                        }
610
                    }
611
                }
612
            } else {
613
                $current = '';
614
            }
615
            $mainNavigation['navigation'][$section]['current'] = '';
616
        }
617
        if (!empty($activeSection)) {
618
            $mainNavigation['navigation'][$activeSection]['current'] = 'active';
619
        }
620
    }
621
    unset($mainNavigation['navigation']['myprofile']);
622
623
    return $mainNavigation['navigation'];
624
}
625
626
/**
627
 * Return the breadcrumb menu elements as an array of <li> items.
628
 *
629
 * @param array  $interbreadcrumb The elements to add to the breadcrumb
630
 * @param string $language_file   Deprecated
631
 * @param string $nameTools       The name of the current tool (not linked)
632
 *
633
 * @return string HTML string of <li> items
634
 */
635
function return_breadcrumb($interbreadcrumb, $language_file, $nameTools)
636
{
637
    // This configuration option allows you to completely hide the breadcrumb
638
    if (api_get_configuration_value('breadcrumb_hide') == true) {
639
        return '';
640
    }
641
    $courseInfo = api_get_course_info();
642
    $user_id = api_get_user_id();
643
    $additionalBlocks = '';
644
645
    /*  Plugins for banner section */
646
    $web_course_path = api_get_path(WEB_COURSE_PATH);
647
648
    /* If the user is a coach he can see the users who are logged in its session */
649
    $navigation = [];
650
651
    $sessionId = api_get_session_id();
652
    // part 1: Course Homepage. If we are in a course then the first breadcrumb
653
    // is a link to the course homepage
654
    if (!empty($courseInfo) && !isset($_GET['hide_course_breadcrumb'])) {
655
        $sessionName = '';
656
        if (!empty($sessionId)) {
657
            /** @var \Chamilo\CoreBundle\Entity\Session $session */
658
            $session = Database::getManager()->find('ChamiloCoreBundle:Session', $sessionId);
659
            $sessionName = $session ? ' ('.cut(Security::remove_XSS($session->getName()), MAX_LENGTH_BREADCRUMB).')' : '';
660
        }
661
662
        $courseInfo['name'] = api_htmlentities($courseInfo['name']);
663
        $course_title = cut($courseInfo['name'], MAX_LENGTH_BREADCRUMB);
664
665
        switch (api_get_setting('breadcrumbs_course_homepage')) {
666
            case 'get_lang':
667
                $itemTitle = Display::return_icon(
668
                    'home.png',
669
                    get_lang('CourseHomepageLink'),
670
                    [],
671
                    ICON_SIZE_TINY
672
                );
673
                break;
674
            case 'course_code':
675
                $itemTitle = Display::return_icon(
676
                    'home.png',
677
                    $courseInfo['official_code'],
678
                    [],
679
                    ICON_SIZE_TINY
680
                )
681
                .' '.$courseInfo['official_code'];
682
                break;
683
            case 'session_name_and_course_title':
684
            default:
685
                $itemTitle = Display::return_icon(
686
                    'home.png',
687
                    $courseInfo['name'].$sessionName,
688
                    [],
689
                    ICON_SIZE_TINY
690
                )
691
                .' '.$course_title.$sessionName;
692
693
                if (!empty($sessionId) && ($session->getDuration() && !api_is_allowed_to_edit())) {
694
                    $daysLeft = SessionManager::getDayLeftInSession(
695
                        ['id' => $session->getId(), 'duration' => $session->getDuration()],
696
                        $user_id
697
                    );
698
699
                    if ($daysLeft >= 0) {
700
                        $additionalBlocks .= Display::return_message(
701
                            sprintf(get_lang('SessionDurationXDaysLeft'), $daysLeft),
702
                            'information'
703
                        );
704
                    } else {
705
                        $additionalBlocks .= Display::return_message(
706
                            get_lang('YourSessionTimeHasExpired'),
707
                            'warning'
708
                        );
709
                    }
710
                }
711
                break;
712
        }
713
714
        /**
715
         * @todo could be useful adding the My courses in the breadcrumb
716
         * $navigation_item_my_courses['title'] = get_lang('MyCourses');
717
         * $navigation_item_my_courses['url'] = api_get_path(WEB_PATH).'user_portal.php';
718
         * $navigation[] = $navigation_item_my_courses;
719
         */
720
        $navigation[] = [
721
            'url' => $web_course_path.$courseInfo['path'].'/index.php?id_session='.$sessionId,
722
            'title' => $itemTitle,
723
        ];
724
    }
725
726
    /* part 2: Interbreadcrumbs. If there is an array $interbreadcrumb
727
    defined then these have to appear before the last breadcrumb
728
    (which is the tool itself)*/
729
    if (isset($interbreadcrumb) && is_array($interbreadcrumb)) {
730
        foreach ($interbreadcrumb as $breadcrumb_step) {
731
            if (isset($breadcrumb_step['type']) && $breadcrumb_step['type'] == 'right') {
732
                continue;
733
            }
734
            if ($breadcrumb_step['url'] != '#') {
735
                $sep = strrchr($breadcrumb_step['url'], '?') ? '&' : '?';
736
                $courseParams = strpos($breadcrumb_step['url'], 'cidReq') === false ? api_get_cidreq() : '';
737
                $navigation_item['url'] = $breadcrumb_step['url'].$sep.$courseParams;
738
            } else {
739
                $navigation_item['url'] = '#';
740
            }
741
            $navigation_item['title'] = $breadcrumb_step['name'];
742
            // titles for shared folders
743
            if ($breadcrumb_step['name'] == 'shared_folder') {
744
                $navigation_item['title'] = get_lang('UserFolders');
745
            } elseif (strstr($breadcrumb_step['name'], 'shared_folder_session_')) {
746
                $navigation_item['title'] = get_lang('UserFolders');
747
            } elseif (strstr($breadcrumb_step['name'], 'sf_user_')) {
748
                $userinfo = api_get_user_info(substr($breadcrumb_step['name'], 8));
749
                $navigation_item['title'] = $userinfo['complete_name'];
750
            } elseif ($breadcrumb_step['name'] == 'chat_files') {
751
                $navigation_item['title'] = get_lang('ChatFiles');
752
            } elseif ($breadcrumb_step['name'] == 'images') {
753
                $navigation_item['title'] = get_lang('Images');
754
            } elseif ($breadcrumb_step['name'] == 'video') {
755
                $navigation_item['title'] = get_lang('Video');
756
            } elseif ($breadcrumb_step['name'] == 'audio') {
757
                $navigation_item['title'] = get_lang('Audio');
758
            } elseif ($breadcrumb_step['name'] == 'flash') {
759
                $navigation_item['title'] = get_lang('Flash');
760
            } elseif ($breadcrumb_step['name'] == 'gallery') {
761
                $navigation_item['title'] = get_lang('Gallery');
762
            }
763
            // Fixes breadcrumb title now we applied the Security::remove_XSS and
764
            // we cut the string depending of the MAX_LENGTH_BREADCRUMB value
765
            $navigation_item['title'] = cut($navigation_item['title'], MAX_LENGTH_BREADCRUMB);
766
            $navigation_item['title'] = Security::remove_XSS($navigation_item['title']);
767
768
            $navigation[] = $navigation_item;
769
        }
770
    }
771
772
    $navigation_right = [];
773
    if (isset($interbreadcrumb) && is_array($interbreadcrumb)) {
774
        foreach ($interbreadcrumb as $breadcrumb_step) {
775
            if (isset($breadcrumb_step['type']) && $breadcrumb_step['type'] == 'right') {
776
                if ($breadcrumb_step['url'] != '#') {
777
                    $sep = (strrchr($breadcrumb_step['url'], '?') ? '&amp;' : '?');
778
                    $navigation_item['url'] = $breadcrumb_step['url'].$sep.api_get_cidreq();
779
                } else {
780
                    $navigation_item['url'] = '#';
781
                }
782
                $breadcrumb_step['title'] = cut($navigation_item['title'], MAX_LENGTH_BREADCRUMB);
783
                $breadcrumb_step['title'] = Security::remove_XSS($navigation_item['title']);
784
                $navigation_right[] = $breadcrumb_step;
785
            }
786
        }
787
    }
788
789
    // part 3: The tool itself. If we are on the course homepage we do not want
790
    // to display the title of the course because this
791
    // is the same as the first part of the breadcrumbs (see part 1)
792
    if (isset($nameTools)) {
793
        $navigation_item['url'] = '#';
794
        $navigation_item['title'] = $nameTools;
795
        $navigation[] = $navigation_item;
796
    }
797
798
    $final_navigation = [];
799
    $counter = 0;
800
    foreach ($navigation as $index => $navigation_info) {
801
        if (!empty($navigation_info['title'])) {
802
            if ($navigation_info['url'] == '#') {
803
                $final_navigation[$index] = $navigation_info['title'];
804
            } else {
805
                $final_navigation[$index] = '<a href="'.$navigation_info['url'].'" target="_self">'.$navigation_info['title'].'</a>';
806
            }
807
            $counter++;
808
        }
809
    }
810
811
    $html = '';
812
813
    /* Part 4 . Show the teacher view/student view button at the right of the breadcrumb */
814
    $view_as_student_link = null;
815
    if ($user_id && !empty($courseInfo)) {
816
        if ((
817
                api_is_course_admin() ||
818
                api_is_platform_admin() ||
819
                api_is_coach(null, null, false)
820
            ) &&
821
            api_get_setting('student_view_enabled') === 'true' && api_get_course_info()
822
        ) {
823
            $view_as_student_link = api_display_tool_view_option();
824
825
            // Only show link if LP can be editable
826
            /** @var learnpath $learnPath */
827
            $learnPath = Session::read('oLP');
828
            if (!empty($learnPath) && !empty($view_as_student_link)) {
829
                if ((int) $learnPath->get_lp_session_id() != (int) api_get_session_id()) {
830
                    $view_as_student_link = '';
831
                }
832
            }
833
        }
834
    }
835
836
    if (!empty($final_navigation)) {
837
        $lis = '';
838
        $i = 0;
839
        $final_navigation_count = count($final_navigation);
840
        if (!empty($final_navigation)) {
841
            if (!empty($home_link)) {
842
                $lis .= Display::tag('li', $home_link);
843
            }
844
845
            foreach ($final_navigation as $bread) {
846
                $bread_check = trim(strip_tags($bread));
847
                if (!empty($bread_check)) {
848
                    if ($final_navigation_count - 1 > $i) {
849
                        $bread .= '';
850
                    }
851
                    $lis .= Display::tag('li', $bread, ['class' => 'active']);
852
                    $i++;
853
                }
854
            }
855
        } else {
856
            if (!empty($home_link)) {
857
                $lis .= Display::tag('li', $home_link);
858
            }
859
        }
860
861
        // View as student/teacher link
862
        if (!empty($view_as_student_link)) {
863
            $html .= Display::tag('div', $view_as_student_link, ['id' => 'view_as_link', 'class' => 'pull-right']);
864
        }
865
866
        if ($sessionId &&
867
            (
868
                api_is_platform_admin()
869
                || ($courseInfo && CourseManager::is_course_teacher($user_id, $courseInfo['code']))
870
            )
871
        ) {
872
            $url = Display::url(
873
                Display::return_icon('course.png', get_lang('Course')),
874
                $courseInfo['course_public_url'].'?id_session=0',
875
                ['class' => 'btn btn-default btn-sm', 'target' => '_blank']
876
            );
877
            $button = Display::tag('div', $url, ['class' => 'view-options']);
878
            $html .= Display::tag('div', $button, ['id' => 'view_as_link', 'class' => 'pull-right']);
879
        }
880
881
        if (!empty($navigation_right)) {
882
            foreach ($navigation_right as $item) {
883
                $extra_class = isset($item['class']) ? $item['class'] : null;
884
                $lis .= Display::tag(
885
                    'li',
886
                    $item['title'],
887
                    ['class' => $extra_class.' pull-right']
888
                );
889
            }
890
        }
891
892
        if (!empty($lis)) {
893
            $html .= Display::tag('ul', $lis, ['class' => 'breadcrumb']);
894
        }
895
    }
896
897
    return $html.$additionalBlocks;
898
}
899
900
/**
901
 * Helper function to get the number of users online, using cache if available.
902
 *
903
 * @param int $minutes Number of minutes (how many users were active in those last X minutes?)
904
 *
905
 * @return int The number of users currently online
906
 */
907
function getOnlineUsersCount($minutes = null)
908
{
909
    $number = 0;
910
    $limit = !empty($minutes) ? intval($minutes) : api_get_setting('time_limit_whosonline');
911
    $cacheAvailable = api_get_configuration_value('apc');
912
    if ($cacheAvailable === true) {
913
        $apcVar = api_get_configuration_value('apc_prefix').'my_campus_whoisonline_count_simple_'.$minutes;
914
        if (apcu_exists($apcVar)) {
915
            $number = apcu_fetch($apcVar);
916
        } else {
917
            $number = who_is_online_count($limit);
918
            apcu_store($apcVar, $number, 15);
919
        }
920
    } else {
921
        $number = who_is_online_count($limit);
922
    }
923
924
    return $number;
925
}
926
927
/**
928
 * Helper function to get the number of users online in a course, using cache if available.
929
 *
930
 * @param int   $userId     The user ID
931
 * @param array $courseInfo The course details
932
 *
933
 * @return int The number of users currently online
934
 */
935
function getOnlineUsersInCourseCount($userId, $courseInfo)
936
{
937
    $cacheAvailable = api_get_configuration_value('apc');
938
    $numberOnlineInCourse = 0;
939
    if (!empty($courseInfo['id'])) {
940
        if ($cacheAvailable === true) {
941
            $apcVar = api_get_configuration_value('apc_prefix').'my_campus_whoisonline_count_simple_'.$courseInfo['id'];
942
            if (apcu_exists($apcVar)) {
943
                $numberOnlineInCourse = apcu_fetch($apcVar);
944
            } else {
945
                $numberOnlineInCourse = who_is_online_in_this_course_count(
946
                    $userId,
947
                    api_get_setting('time_limit_whosonline'),
948
                    $courseInfo['id']
949
                );
950
                apcu_store(
951
                    $apcVar,
952
                    $numberOnlineInCourse,
953
                    15
954
                );
955
            }
956
        } else {
957
            $numberOnlineInCourse = who_is_online_in_this_course_count(
958
                $userId,
959
                api_get_setting('time_limit_whosonline'),
960
                $courseInfo['id']
961
            );
962
        }
963
    }
964
965
    return $numberOnlineInCourse;
966
}
967
968
/**
969
 * Helper function to get the number of users online in a session, using cache if available.
970
 *
971
 * @param int $sessionId The session ID
972
 *
973
 * @return int The number of users currently online
974
 */
975
function getOnlineUsersInSessionCount($sessionId)
976
{
977
    $cacheAvailable = api_get_configuration_value('apc');
978
979
    if (!$sessionId) {
980
        return 0;
981
    }
982
983
    if ($cacheAvailable === true) {
984
        $apcVar = api_get_configuration_value('apc_prefix').'my_campus_whoisonline_session_count_simple_'.$sessionId;
985
986
        if (apcu_exists($apcVar)) {
987
            return apcu_fetch($apcVar);
988
        }
989
990
        $numberOnlineInCourse = whoIsOnlineInThisSessionCount(
991
            api_get_setting('time_limit_whosonline'),
992
            $sessionId
993
        );
994
        apcu_store($apcVar, $numberOnlineInCourse, 15);
995
996
        return $numberOnlineInCourse;
997
    }
998
999
    return whoIsOnlineInThisSessionCount(
1000
        api_get_setting('time_limit_whosonline'),
1001
        $sessionId
1002
    );
1003
}
1004