Completed
Push — master ( 3fdf06...e648ff )
by Julito
33:51
created

IndexManager::compareByCourse()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 2
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class IndexManager
6
 */
7
class IndexManager
8
{
9
    const VIEW_BY_DEFAULT = 0;
10
    const VIEW_BY_SESSION = 1;
11
12
    // An instance of the template engine
13
    public $tpl = false;
14
    public $name = '';
15
    public $home = '';
16
    public $default_home = 'home/';
17
18
    /**
19
     * Construct
20
     * @param string $title
21
     */
22
    public function __construct($title)
23
    {
24
        $this->tpl = new Template($title);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Template($title) of type object<Template> is incompatible with the declared type boolean of property $tpl.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
        $this->home = api_get_home_path();
26
        $this->user_id = api_get_user_id();
27
        $this->load_directories_preview = false;
28
29
        if (api_get_setting('show_documents_preview') === 'true') {
30
            $this->load_directories_preview = true;
31
        }
32
    }
33
34
    /**
35
     * @param array $personal_course_list
36
     */
37
    public function return_exercise_block($personal_course_list)
38
    {
39
        $exercise_list = array();
40
        if (!empty($personal_course_list)) {
41 View Code Duplication
            foreach ($personal_course_list as  $course_item) {
42
                $course_code = $course_item['c'];
43
                $session_id = $course_item['id_session'];
44
45
                $exercises = ExerciseLib::get_exercises_to_be_taken(
46
                    $course_code,
47
                    $session_id
48
                );
49
50
                foreach ($exercises as $exercise_item) {
51
                    $exercise_item['course_code']     = $course_code;
52
                    $exercise_item['session_id']     = $session_id;
53
                    $exercise_item['tms']     = api_strtotime($exercise_item['end_time'], 'UTC');
54
55
                    $exercise_list[] = $exercise_item;
56
                }
57
            }
58
            if (!empty($exercise_list)) {
59
                $exercise_list = msort($exercise_list, 'tms');
60
                $my_exercise = $exercise_list[0];
61
                $url = Display::url(
62
                    $my_exercise['title'],
63
                    api_get_path(WEB_CODE_PATH).'exercise/overview.php?exerciseId='.$my_exercise['id'].'&cidReq='.$my_exercise['course_code'].'&id_session='.$my_exercise['session_id']
64
                );
65
                $this->tpl->assign('exercise_url', $url);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66
                $this->tpl->assign('exercise_end_date', api_convert_and_format_date($my_exercise['end_time'], DATE_FORMAT_SHORT));
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
            }
68
        }
69
    }
70
71
    /**
72
     * This function checks if there are courses that are open to the world in the platform course categories (=faculties)
73
     *
74
     * @param string $category
75
     * @return boolean
76
     */
77
    public function category_has_open_courses($category)
78
    {
79
        $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
80
        $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
81
        $category = Database::escape_string($category);
82
        $sql_query = "SELECT * FROM $main_course_table WHERE category_code='$category'";
83
        $sql_result = Database::query($sql_query);
84
        while ($course = Database::fetch_array($sql_result)) {
85
            if (!$setting_show_also_closed_courses) {
86
                if ((api_get_user_id() > 0 && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
87
                    return true; //at least one open course
88
                }
89
            } else {
90
                if (isset($course['visibility'])) {
91
                    return true; // At least one course (it does not matter weither it's open or not because $setting_show_also_closed_courses = true).
92
                }
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Includes a created page
101
     * @return string
102
     */
103
    public function return_home_page()
104
    {
105
        $userId = api_get_user_id();
106
        // Including the page for the news
107
        $html = '';
108
        if (!empty($_GET['include']) && preg_match('/^[a-zA-Z0-9_-]*\.html$/', $_GET['include'])) {
109
            $open = @(string)file_get_contents($this->home.$_GET['include']);
110
            $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
111
        } else {
112
            // Hiding home top when user not connected.
113
            $hideTop = api_get_setting('hide_home_top_when_connected');
114
            if ($hideTop == 'true' && !empty($userId)) {
115
                return $html;
116
            }
117
118 View Code Duplication
            if (!empty($_SESSION['user_language_choice'])) {
119
                $user_selected_language = $_SESSION['user_language_choice'];
120
            } elseif (!empty($_SESSION['_user']['language'])) {
121
                $user_selected_language = $_SESSION['_user']['language'];
122
            } else {
123
                $user_selected_language = api_get_setting('platformLanguage');
124
            }
125
126
            $home_top_temp = '';
127
128
            // Try language specific home
129
            if (file_exists($this->home.'home_top_'.$user_selected_language.'.html')) {
130
                $home_top_temp = file_get_contents($this->home.'home_top_'.$user_selected_language.'.html');
131
            }
132
133
            // Try default language home
134
            if (empty($home_top_temp)) {
135
                if (file_exists($this->home.'home_top.html')) {
136
                    $home_top_temp = file_get_contents($this->home.'home_top.html');
137
                } else {
138
                    if (file_exists($this->default_home.'home_top.html')) {
139
                        $home_top_temp = file_get_contents($this->default_home . 'home_top.html');
140
                    }
141
                }
142
            }
143
144
			if (trim($home_top_temp) == '' && api_is_platform_admin()) {
145
				$home_top_temp = '<div class="welcome-mascot">' . get_lang('PortalHomepageDefaultIntroduction') . '</div>';
146
			} else {
147
				$home_top_temp = '<div class="welcome-home-top-temp">' . $home_top_temp . '</div>';
148
			}
149
			$open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
150
			$html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
151
		}
152
153
		return $html;
154
	}
155
156
    /**
157
     * @return string
158
     */
159
    public function return_notice()
160
    {
161
        $user_selected_language = api_get_interface_language();
162
163
        $html = '';
164
        // Notice
165
        $home_notice = @(string)file_get_contents($this->home.'home_notice_'.$user_selected_language.'.html');
166
        if (empty($home_notice)) {
167
            $home_notice = @(string)file_get_contents($this->home.'home_notice.html');
168
        }
169
170 View Code Duplication
        if (!empty($home_notice)) {
171
            $home_notice = api_to_system_encoding($home_notice, api_detect_encoding(strip_tags($home_notice)));
172
            $html = self::show_right_block(
173
                get_lang('Notice'),
174
                $home_notice,
175
                'notice_block',
176
                null,
177
                'notices',
178
                'noticesCollapse'
179
            );
180
        }
181
182
        return $html;
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function return_help()
189
    {
190
        $user_selected_language = api_get_interface_language();
191
        $platformLanguage       = api_get_setting('platformLanguage');
192
193
        // Help section.
194
        /* Hide right menu "general" and other parts on anonymous right menu. */
195
        if (!isset($user_selected_language)) {
196
            $user_selected_language = $platformLanguage;
197
        }
198
199
        $html = '';
200
        $home_menu = @(string)file_get_contents($this->home.'home_menu_'.$user_selected_language.'.html');
201
        if (!empty($home_menu)) {
202
            $home_menu_content = '<ul class="nav nav-pills nav-stacked">';
203
            $home_menu_content .= api_to_system_encoding($home_menu, api_detect_encoding(strip_tags($home_menu)));
204
            $home_menu_content .= '</ul>';
205
            $html .= self::show_right_block(
206
                get_lang('MenuGeneral'),
207
                $home_menu_content,
208
                'help_block',
209
                null,
210
                'helps',
211
                'helpsCollapse'
212
            );
213
        }
214
        return $html;
215
    }
216
217
218
    /**
219
     * Display list of courses in a category.
220
     * (for anonymous users)
221
     *
222
     * @version 1.1
223
     * @author Patrick Cool <[email protected]>, Ghent University - refactoring and code cleaning
224
     * @author Julio Montoya <[email protected]>, Beeznest template modifs
225
     */
226
    public function return_courses_in_categories()
227
    {
228
        $result = '';
229
        $stok = Security::get_token();
230
231
        // Initialization.
232
        $user_identified = (api_get_user_id() > 0 && !api_is_anonymous());
233
        $web_course_path = api_get_path(WEB_COURSE_PATH);
234
        $category = isset($_GET['category']) ? Database::escape_string($_GET['category']) : '';
235
        $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
236
237
        // Database table definitions.
238
        $main_course_table = Database:: get_main_table(TABLE_MAIN_COURSE);
239
        $main_category_table = Database:: get_main_table(TABLE_MAIN_CATEGORY);
240
241
        // Get list of courses in category $category.
242
        $sql = "SELECT * FROM $main_course_table cours
243
                WHERE category_code = '" . $category . "'
244
                ORDER BY title, UPPER(visual_code)";
245
246
        // Showing only the courses of the current access_url_id.
247
        if (api_is_multiple_url_enabled()) {
248
            $url_access_id = api_get_current_access_url_id();
249
            if ($url_access_id != -1) {
250
                $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
251
                $sql = "SELECT * FROM $main_course_table as course
252
                        INNER JOIN $tbl_url_rel_course as url_rel_course
253
                        ON (url_rel_course.c_id = course.id)
254
                        WHERE
255
                            access_url_id = $url_access_id AND
256
                            category_code = '".Database::escape_string($_GET['category'])."'
257
                        ORDER BY title, UPPER(visual_code)";
258
            }
259
        }
260
261
        // Removed: AND cours.visibility='".COURSE_VISIBILITY_OPEN_WORLD."'
262
        $sql_result_courses = Database::query($sql);
263
        while ($course_result = Database::fetch_array($sql_result_courses)) {
264
            $course_list[] = $course_result;
265
        }
266
267
        // $setting_show_also_closed_courses
268 View Code Duplication
        if ($user_identified) {
269
            if ($setting_show_also_closed_courses) {
270
                $platform_visible_courses = '';
271
            } else {
272
                $platform_visible_courses = "  AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' OR t3.visibility='".COURSE_VISIBILITY_OPEN_PLATFORM."' )";
273
            }
274
        } else {
275
            if ($setting_show_also_closed_courses) {
276
                $platform_visible_courses = '';
277
            } else {
278
                $platform_visible_courses = "  AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' )";
279
            }
280
        }
281
        $sqlGetSubCatList = "
282
                    SELECT  t1.name,
283
                            t1.code,
284
                            t1.parent_id,
285
                            t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
286
                    FROM $main_category_table t1
287
                    LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
288
                    LEFT JOIN $main_course_table t3 ON (t3.category_code = t1.code $platform_visible_courses)
289
                    WHERE t1.parent_id ". (empty($category) ? "IS NULL" : "='$category'")."
290
                    GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count 
291
                    ORDER BY t1.tree_pos, t1.name";
292
293
        // Showing only the category of courses of the current access_url_id
294
        if (api_is_multiple_url_enabled()) {
295
            $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
296
            $courseCategoryCondition = " INNER JOIN $table a ON (t1.id = a.course_category_id)";
297
298
            $url_access_id = api_get_current_access_url_id();
299 View Code Duplication
            if ($url_access_id != -1) {
300
                $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
301
                $sqlGetSubCatList = "
302
                    SELECT t1.name,
303
                            t1.code,
304
                            t1.parent_id,
305
                            t1.children_count,
306
                            COUNT(DISTINCT t3.code) AS nbCourse
307
                    FROM $main_category_table t1
308
                    $courseCategoryCondition
309
                    LEFT JOIN $main_category_table t2 ON t1.code = t2.parent_id
310
                    LEFT JOIN $main_course_table t3 ON (t3.category_code = t1.code $platform_visible_courses)
311
                    INNER JOIN $tbl_url_rel_course as url_rel_course
312
                    ON (url_rel_course.c_id = t3.id)
313
                    WHERE
314
                        url_rel_course.access_url_id = $url_access_id AND
315
                        t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
316
                    GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count
317
                    ORDER BY t1.tree_pos, t1.name";
318
            }
319
        }
320
321
        $resCats = Database::query($sqlGetSubCatList);
322
        $thereIsSubCat = false;
323
        if (Database::num_rows($resCats) > 0) {
324
            $htmlListCat = Display::page_header(get_lang('CatList'));
325
            $htmlListCat .= '<ul>';
326
            $htmlTitre = '';
327
            while ($catLine = Database::fetch_array($resCats)) {
328
                $category_has_open_courses = self::category_has_open_courses($catLine['code']);
329 View Code Duplication
                if ($category_has_open_courses) {
330
                    // The category contains courses accessible to anonymous visitors.
331
                    $htmlListCat .= '<li>';
332
                    $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
333
                    if (api_get_setting('show_number_of_courses') == 'true') {
334
                        $htmlListCat .= ' ('.$catLine['nbCourse'].' '.get_lang('Courses').')';
335
                    }
336
                    $htmlListCat .= "</li>";
337
                    $thereIsSubCat = true;
338
                } elseif ($catLine['children_count'] > 0) {
339
                    // The category has children, subcategories.
340
                    $htmlListCat .= '<li>';
341
                    $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
342
                    $htmlListCat .= "</li>";
343
                    $thereIsSubCat = true;
344
                } elseif (api_get_setting('show_empty_course_categories') == 'true') {
345
                    /* End changed code to eliminate the (0 courses) after empty categories. */
346
                      $htmlListCat .= '<li>';
347
                    $htmlListCat .= $catLine['name'];
348
                    $htmlListCat .= "</li>";
349
                    $thereIsSubCat = true;
350
                } // Else don't set thereIsSubCat to true to avoid printing things if not requested.
351
                // TODO: deprecate this useless feature - this includes removing system variable
352
                if (empty($htmlTitre)) {
353
                    $htmlTitre = '<p>';
354 View Code Duplication
                    if (api_get_setting('show_back_link_on_top_of_tree') == 'true') {
355
                        $htmlTitre .= '<a href="'.api_get_self().'">&lt;&lt; '.get_lang('BackToHomePage').'</a>';
356
                    }
357
                    $htmlTitre .= "</p>";
358
                }
359
            }
360
            $htmlListCat .= "</ul>";
361
        }
362
        $result .= $htmlTitre;
363
        if ($thereIsSubCat) {
364
            $result .=  $htmlListCat;
365
        }
366
        while ($categoryName = Database::fetch_array($resCats)) {
367
            $result .= '<h3>' . $categoryName['name'] . "</h3>\n";
368
        }
369
        $numrows = Database::num_rows($sql_result_courses);
370
        $courses_list_string = '';
371
        $courses_shown = 0;
372
        if ($numrows > 0) {
373
            $courses_list_string .= Display::page_header(get_lang('CourseList'));
374
            $courses_list_string .= "<ul>";
375
            if (api_get_user_id()) {
376
                $courses_of_user = self::get_courses_of_user(api_get_user_id());
377
            }
378
            foreach ($course_list as $course) {
379
                // $setting_show_also_closed_courses
380
                if ($course['visibility'] == COURSE_VISIBILITY_HIDDEN) { continue; }
381
                if (!$setting_show_also_closed_courses) {
382
                    // If we do not show the closed courses
383
                    // we only show the courses that are open to the world (to everybody)
384
                    // and the courses that are open to the platform (if the current user is a registered user.
385
                    if (($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
386
                        $courses_shown++;
387
                        $courses_list_string .= "<li>";
388
                        $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">'.$course['title'].'</a><br />';
389
                        $course_details = array();
390
                        if (api_get_setting('display_coursecode_in_courselist') === 'true') {
391
                            $course_details[] = $course['visual_code'];
392
                        }
393
                        if (api_get_setting('display_teacher_in_courselist') === 'true') {
394
                            $course_details[] = CourseManager::get_teacher_list_from_course_code_to_string($course['code']);
395
                        }
396 View Code Duplication
                        if (api_get_setting('show_different_course_language') === 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
397
                            $course_details[] = $course['course_language'];
398
                        }
399
                        $courses_list_string .= implode(' - ', $course_details);
400
                        $courses_list_string .= "</li>";
401
                    }
402
                } else {
403
                    // We DO show the closed courses.
404
                    // The course is accessible if (link to the course homepage):
405
                    // 1. the course is open to the world (doesn't matter if the user is logged in or not): $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD);
406
                    // 2. the user is logged in and the course is open to the world or open to the platform: ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM);
407
                    // 3. the user is logged in and the user is subscribed to the course and the course visibility is not COURSE_VISIBILITY_CLOSED;
408
                    // 4. the user is logged in and the user is course admin of te course (regardless of the course visibility setting);
409
                    // 5. the user is the platform admin api_is_platform_admin().
410
411
                    $courses_shown++;
412
                    $courses_list_string .= "<li>";
413 View Code Duplication
                    if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
414
                        || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
415
                        || ($user_identified && array_key_exists($course['code'], $courses_of_user)
416
                            && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
417
                        || $courses_of_user[$course['code']]['status'] == '1'
418
                        || api_is_platform_admin()) {
419
                            $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">';
420
                        }
421
                    $courses_list_string .= $course['title'];
422 View Code Duplication
                    if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
423
                        || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
424
                        || ($user_identified && array_key_exists($course['code'], $courses_of_user)
425
                            && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
426
                            || $courses_of_user[$course['code']]['status'] == '1'
427
                        || api_is_platform_admin()) {
428
                        $courses_list_string .= '</a><br />';
429
                    }
430
                    $course_details = array();
431
                    if (api_get_setting('display_coursecode_in_courselist') == 'true') {
432
                        $course_details[] = $course['visual_code'];
433
                    }
434
//                        if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
435
//                        $courses_list_string .= ' - ';
436
//                }
437
                    if (api_get_setting('display_teacher_in_courselist') === 'true') {
438
                        if (!empty($course['tutor_name'])) {
439
                            $course_details[] = $course['tutor_name'];
440
                        }
441
                    }
442 View Code Duplication
                    if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
443
                        $course_details[] = $course['course_language'];
444
                    }
445
446
                    $courses_list_string .= implode(' - ', $course_details);
447
                    // We display a subscription link if:
448
                    // 1. it is allowed to register for the course and if the course is not already in the courselist of the user and if the user is identiefied
449
                    // 2.
450
                    if ($user_identified && !array_key_exists($course['code'], $courses_of_user)) {
451
                        if ($course['subscribe'] == '1') {
452
                            $courses_list_string .= '&nbsp;<a class="btn btn-primary" href="main/auth/courses.php?action=subscribe_course&sec_token='.$stok.'&subscribe_course='.$course['code'].'&category_code='.Security::remove_XSS($_GET['category']).'">'.get_lang('Subscribe').'</a><br />';
453
                        } else {
454
                            $courses_list_string .= '<br />'.get_lang('SubscribingNotAllowed');
455
                        }
456
                    }
457
                    $courses_list_string .= "</li>";
458
                } //end else
459
            } // end foreach
460
            $courses_list_string .= "</ul>";
461
        }
462
        if ($courses_shown > 0) {
463
            // Only display the list of courses and categories if there was more than
464
                    // 0 courses visible to the world (we're in the anonymous list here).
465
            $result .=  $courses_list_string;
466
        }
467 View Code Duplication
        if ($category != '') {
468
            $result .=  '<p><a href="'.api_get_self().'"> ' .
469
                Display :: return_icon('back.png', get_lang('BackToHomePage')).
470
                get_lang('BackToHomePage') . '</a></p>';
471
        }
472
        return $result;
473
    }
474
475
    /**
476
    * retrieves all the courses that the user has already subscribed to
477
    * @author Patrick Cool <[email protected]>, Ghent University, Belgium
478
    * @param int $user_id: the id of the user
0 ignored issues
show
Documentation introduced by
There is no parameter named $user_id:. Did you maybe mean $user_id?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
479
    * @return array an array containing all the information of the courses of the given user
480
    */
481
    public function get_courses_of_user($user_id)
482
    {
483
        $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
484
        $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
485
        // Secondly we select the courses that are in a category (user_course_cat <> 0)
486
        // and sort these according to the sort of the category
487
        $user_id = intval($user_id);
488
        $sql = "SELECT
489
                course.code k,
490
                course.visual_code vc,
491
                course.subscribe subscr,
492
                course.unsubscribe unsubscr,
493
                course.title i,
494
                course.tutor_name t,
495
                course.directory dir,
496
                course_rel_user.status status,
497
                course_rel_user.sort sort,
498
                course_rel_user.user_course_cat user_course_cat
499
                FROM
500
                    $table_course course,
501
                    $table_course_user course_rel_user
502
                WHERE
503
                    course.id = course_rel_user.c_id AND
504
                    course_rel_user.user_id = '".$user_id."' AND
505
                    course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
506
                ORDER BY course_rel_user.sort ASC";
507
        $result = Database::query($sql);
508
        $courses = array();
509 View Code Duplication
        while ($row = Database::fetch_array($result)) {
510
            // We only need the database name of the course.
511
            $courses[$row['k']] = array(
512
                'code' => $row['k'],
513
                'visual_code' => $row['vc'],
514
                'title' => $row['i'],
515
                'directory' => $row['dir'],
516
                'status' => $row['status'],
517
                'tutor' => $row['t'],
518
                'subscribe' => $row['subscr'],
519
                'unsubscribe' => $row['unsubscr'],
520
                'sort' => $row['sort'],
521
                'user_course_category' => $row['user_course_cat']
522
            );
523
        }
524
        return $courses;
525
    }
526
527
    /**
528
     * @todo use the template system
529
     * @param $title
530
     * @param $content
531
     * @param string $id
532
     * @param array $params
533
     * @param string $idAccordion
534
     * @param string $idCollapse
535
     * @return string
536
     */
537
    public function show_right_block(
538
        $title,
539
        $content,
540
        $id = '',
541
        $params = [],
542
        $idAccordion = '',
543
        $idCollapse = ''
544
    ) {
545
        $html = '';
546
        if (!empty($idAccordion)) {
547
            $html .= '<div class="panel-group" id="'.$idAccordion.'" role="tablist" aria-multiselectable="true">';
548
            $html .= '<div class="panel panel-default" id="'.$id.'">';
549
            $html .= '<div class="panel-heading" role="tab"><h4 class="panel-title">';
550
            $html .= '<a role="button" data-toggle="collapse" data-parent="#'.$idAccordion.'" href="#'.$idCollapse.'" aria-expanded="true" aria-controls="'.$idCollapse.'">'.$title.'</a>';
551
            $html .= '</h4></div>';
552
            $html .= '<div id="'.$idCollapse.'" class="panel-collapse collapse in" role="tabpanel">';
553
            $html .= '<div class="panel-body">'.$content.'</div>';
554
            $html .= '</div></div></div>';
555
556
        } else {
557
            if (!empty($id)) {
558
                $params['id'] = $id;
559
            }
560
            $params['class'] = 'panel panel-default';
561
            $html = null;
562
            if (!empty($title)) {
563
                $html .= '<div class="panel-heading">'.$title.'</div>';
564
            }
565
            $html.= '<div class="panel-body">'.$content.'</div>';
566
            $html = Display::div($html, $params);
567
        }
568
569
        return $html;
570
    }
571
572
    /**
573
     * @todo use FormValidator
574
     * @return string
575
     */
576 View Code Duplication
    public function return_search_block()
577
    {
578
        $html = '';
579
        if (api_get_setting('search_enabled') == 'true') {
580
            $search_btn = get_lang('Search');
581
            $search_content = '<form action="main/search/" method="post">
582
                <div class="form-group">
583
                <input type="text" id="query" class="form-control" name="query" value="" />
584
                <button class="btn btn-default" type="submit" name="submit" value="'.$search_btn.'" />'.$search_btn.' </button>
585
                </div></form>';
586
            $html .= self::show_right_block(get_lang('Search'), $search_content, 'search_block');
587
        }
588
589
        return $html;
590
    }
591
592
    /**
593
     * @return string
594
     */
595 View Code Duplication
    public function return_classes_block()
596
    {
597
        $html = '';
598
        if (api_get_setting('show_groups_to_users') === 'true') {
599
            $usergroup = new UserGroup();
600
            $usergroup_list = $usergroup->get_usergroup_by_user(api_get_user_id());
601
            $classes = '';
602
            if (!empty($usergroup_list)) {
603
                foreach($usergroup_list as $group_id) {
604
                    $data = $usergroup->get($group_id);
605
                    $data['name'] = Display::url($data['name'], api_get_path(WEB_CODE_PATH).'user/classes.php?id='.$data['id']);
606
                    $classes .= Display::tag('li', $data['name']);
607
                }
608
            }
609
            if (api_is_platform_admin()) {
610
                $classes .= Display::tag(
611
                    'li',
612
                    Display::url(get_lang('AddClasses') ,api_get_path(WEB_CODE_PATH).'admin/usergroups.php?action=add')
613
                );
614
            }
615
            if (!empty($classes)) {
616
                $classes = Display::tag('ul', $classes, array('class'=>'nav nav-pills nav-stacked'));
617
                $html .= self::show_right_block(get_lang('Classes'), $classes, 'classes_block');
618
            }
619
        }
620
        return $html;
621
    }
622
623
    /**
624
     * @return null|string|void
625
     */
626
    public function return_profile_block()
627
    {
628
        global $_configuration;
629
        $user_id = api_get_user_id();
630
631
        if (empty($user_id)) {
632
            return;
633
        }
634
635
        $userGroup = new UserGroup();
636
637
        $profile_content = '<ul class="nav nav-pills nav-stacked">';
638
639
        //  @todo Add a platform setting to add the user image.
640
        if (api_get_setting('allow_message_tool') == 'true') {
641
            // New messages.
642
            $number_of_new_messages = MessageManager::getCountNewMessages();
643
            // New contact invitations.
644
            $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(api_get_user_id());
645
646
            // New group invitations sent by a moderator.
647
            $group_pending_invitations = $userGroup->get_groups_by_user(
648
                api_get_user_id(),
649
                GROUP_USER_PERMISSION_PENDING_INVITATION,
650
                false
651
            );
652
            $group_pending_invitations = count($group_pending_invitations);
653
654
            $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations;
655
            $cant_msg = Display::badge($number_of_new_messages);
656
657
            $link = '';
658
            if (api_get_setting('allow_social_tool') == 'true') {
659
                $link = '?f=social';
660
            }
661
            $profile_content .= '<li class="inbox-message-social"><a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php'.$link.'">'.
662
                Display::return_icon('inbox.png',get_lang('Inbox'),null,ICON_SIZE_SMALL).get_lang('Inbox').$cant_msg.' </a></li>';
663
            $profile_content .= '<li class="new-message-social"><a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php'.$link.'">'.
664
                Display::return_icon('new-message.png',get_lang('Compose'),null,ICON_SIZE_SMALL).get_lang('Compose').' </a></li>';
665
666
            if (api_get_setting('allow_social_tool') == 'true') {
667
                $total_invitations = Display::badge($total_invitations);
668
                $profile_content .= '<li class="invitations-social"><a href="'.api_get_path(WEB_PATH).'main/social/invitations.php">'.Display::return_icon('invitations.png',get_lang('PendingInvitations'),null,ICON_SIZE_SMALL).get_lang('PendingInvitations').$total_invitations.'</a></li>';
669
            }
670
671
            if (isset($_configuration['allow_my_files_link_in_homepage']) && $_configuration['allow_my_files_link_in_homepage']) {
672
                $myFiles = '<li class="myfiles-social"><a href="'.api_get_path(WEB_PATH).'main/social/myfiles.php">'.
673
                    Display::return_icon('sn-files.png',get_lang('Files'),null,ICON_SIZE_SMALL).get_lang('MyFiles').'</a></li>';
674
675
                if (api_get_setting('allow_my_files') === 'false') {
676
                    $myFiles = '';
677
                }
678
679
                $profile_content .= $myFiles;
680
            }
681
        }
682
683
        $editProfileUrl = Display::getProfileEditionLink($user_id);
684
685
        $profile_content .= '<li class="profile-social"><a href="' . $editProfileUrl . '">'.
686
            Display::return_icon(
687
                'edit-profile.png',
688
                get_lang('EditProfile'),
689
                null,
690
                ICON_SIZE_SMALL
691
            ).
692
            get_lang('EditProfile').'</a></li>';
693
        $profile_content .= '</ul>';
694
695
        $html = self::show_right_block(
696
            get_lang('Profile'),
697
            $profile_content,
698
            'profile_block',
699
            null,
700
            'profile',
701
            'profileCollapse'
702
        );
703
704
        $setting = api_get_plugin_setting('bbb', 'enable_global_conference');
705
        $settingLink = api_get_plugin_setting('bbb', 'enable_global_conference_link');
706
        if ($setting === 'true' && $settingLink === 'true') {
707
            $url = api_get_path(WEB_PLUGIN_PATH).'bbb/start.php?global=1';
708
            $content = Display::url(get_lang('LaunchVideoConferenceRoom'), $url);
709
            $html .= self::show_right_block(
710
                get_lang('VideoConference'),
711
                $content,
712
                'videoconference_block',
713
                null,
714
                'videoconference',
715
                'videoconferenceCollapse'
716
            );
717
        }
718
719
        return $html;
720
    }
721
722
    /**
723
     * @return null|string
724
     */
725
    public function return_navigation_links()
726
    {
727
        $html = '';
728
729
        // Deleting the myprofile link.
730
        if (api_get_setting('allow_social_tool') == 'true') {
731
            unset($this->tpl->menu_navigation['myprofile']);
732
        }
733
734
        // Main navigation section.
735
        // Tabs that are deactivated are added here.
736
        if (!empty($this->tpl->menu_navigation)) {
737
            $content = '<ul class="nav nav-pills nav-stacked">';
738
            foreach ($this->tpl->menu_navigation as $section => $navigation_info) {
739
                $current = $section == $GLOBALS['this_section'] ? ' id="current"' : '';
740
                $content .= '<li'.$current.'>';
741
                $content .= '<a href="'.$navigation_info['url'].'" target="_self">'.$navigation_info['title'].'</a>';
742
                $content .= '</li>';
743
            }
744
            $content .= '</ul>';
745
746
            $html = self::show_right_block(get_lang('MainNavigation'), $content, 'navigation_link_block');
747
        }
748
749
        return $html;
750
    }
751
752
    /**
753
     * Prints the session and course list (user_portal.php)
754
     * @param int $user_id
755
     * @return string
756
     */
757
    public function returnCoursesAndSessions($user_id)
758
    {
759
        $gameModeIsActive = api_get_setting('gamification_mode');
760
        $listCourse = '';
761
        $specialCourseList = '';
762
        $load_history = isset($_GET['history']) && intval($_GET['history']) == 1 ? true : false;
763
        $viewGridCourses = api_get_configuration_value('view_grid_courses');
764
        $showSimpleSessionInfo = api_get_configuration_value('show_simple_session_info');
765
766
        $coursesWithoutCategoryTemplate = '/user_portal/classic_courses_without_category.tpl';
767
        $coursesWithCategoryTemplate = '/user_portal/classic_courses_with_category.tpl';
768
769 View Code Duplication
        if ($load_history) {
770
            // Load sessions in category in *history*
771
            $session_categories = UserManager::get_sessions_by_category($user_id, true);
772
        } else {
773
            // Load sessions in category
774
            $session_categories = UserManager::get_sessions_by_category($user_id, false);
775
        }
776
777
        $sessionCount = 0;
778
        $courseCount = 0;
779
780
        // If we're not in the history view...
781
        if (!isset($_GET['history'])) {
782
            // Display special courses.
783
            $specialCourses = CourseManager::returnSpecialCourses(
784
                $user_id,
785
                $this->load_directories_preview
786
            );
787
788
            // Display courses.
789
            $courses = CourseManager::returnCourses(
790
                $user_id,
791
                $this->load_directories_preview
792
            );
793
794
            if ($viewGridCourses) {
795
                $coursesWithoutCategoryTemplate = '/user_portal/grid_courses_without_category.tpl';
796
                $coursesWithCategoryTemplate = '/user_portal/grid_courses_with_category.tpl';
797
            }
798
799 View Code Duplication
            if ($specialCourses) {
800
                $this->tpl->assign('courses', $specialCourses);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
801
802
                $specialCourseList = $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
803
                    $this->tpl->get_template($coursesWithoutCategoryTemplate)
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
804
                );
805
            }
806
807
            if ($courses['in_category'] || $courses['not_category']) {
808
                $this->tpl->assign('courses', $courses['not_category']);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
809
                $this->tpl->assign('categories', $courses['in_category']);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
810
811
                $listCourse = $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
812
                    $this->tpl->get_template($coursesWithCategoryTemplate)
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
813
                );
814
                $listCourse .= $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
815
                    $this->tpl->get_template($coursesWithoutCategoryTemplate)
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
816
                );
817
            }
818
819
            $courseCount = count($specialCourses) + count($courses['in_category']) + count($courses['not_category']);
820
        }
821
822
        $sessions_with_category = '';
823
        $coursesListSessionStyle = api_get_configuration_value('courses_list_session_title_link');
824
        $coursesListSessionStyle = $coursesListSessionStyle === false ? 1 : $coursesListSessionStyle;
825
        if (api_is_drh()) {
826
            $coursesListSessionStyle = 1;
827
        }
828
829
        // Declared listSession variable
830
        $listSession = [];
831
832
        $session_now = time();
833
        if (is_array($session_categories)) {
834
            foreach ($session_categories as $session_category) {
835
                $session_category_id = $session_category['session_category']['id'];
836
                // Sessions and courses that are not in a session category
837
                if (
838
                    empty($session_category_id) &&
839
                    isset($session_category['sessions'])
840
                ) {
841
                    // Independent sessions
842
                    foreach ($session_category['sessions'] as $session) {
843
                        $session_id = $session['session_id'];
844
845
                        // Don't show empty sessions.
846
                        if (count($session['courses']) < 1) {
847
                            continue;
848
                        }
849
850
                        // Courses inside the current session.
851
                        $date_session_start = $session['access_start_date'];
852
                        $date_session_end = $session['access_end_date'];
853
                        $coachAccessStartDate = $session['coach_access_start_date'];
854
                        $coachAccessEndDate = $session['coach_access_end_date'];
855
                        $count_courses_session = 0;
856
857
                        // Loop course content
858
                        $html_courses_session = [];
859
                        $atLeastOneCourseIsVisible = false;
860
861
                        foreach ($session['courses'] as $course) {
862
                            $is_coach_course = api_is_coach($session_id, $course['real_id']);
863
                            $allowed_time = 0;
864
                            $allowedEndTime = true;
865
866 View Code Duplication
                            if (!empty($date_session_start)) {
867
                                if ($is_coach_course) {
868
                                    $allowed_time = api_strtotime($coachAccessStartDate);
869
                                } else {
870
                                    $allowed_time = api_strtotime($date_session_start);
871
                                }
872
873
                                if (!isset($_GET['history'])) {
874
                                    if (!empty($date_session_end)) {
875
                                        if ($is_coach_course) {
876
                                            // if coach end date is empty we use the default end date
877
                                            if (empty($coachAccessEndDate)) {
878
                                                $endSessionToTms = api_strtotime($date_session_end);
879
                                                if ($session_now > $endSessionToTms) {
880
                                                    $allowedEndTime = false;
881
                                                }
882
                                            } else {
883
                                                $endSessionToTms = api_strtotime($coachAccessEndDate);
884
                                                if ($session_now > $endSessionToTms) {
885
                                                    $allowedEndTime = false;
886
                                                }
887
                                            }
888
                                        } else {
889
                                            $endSessionToTms = api_strtotime($date_session_end);
890
                                            if ($session_now > $endSessionToTms) {
891
                                                $allowedEndTime = false;
892
                                            }
893
                                        }
894
                                    }
895
                                }
896
                            }
897
898
                            if ($session_now >= $allowed_time && $allowedEndTime) {
899
                                // Read only and accessible.
900
                                $atLeastOneCourseIsVisible = true;
901
902 View Code Duplication
                                if (api_get_setting('hide_courses_in_sessions') == 'false') {
903
                                    $courseUserHtml = CourseManager::get_logged_user_course_html(
904
                                        $course,
905
                                        $session_id,
906
                                        'session_course_item',
907
                                        true,
908
                                        $this->load_directories_preview
909
                                    );
910
                                    if (isset($courseUserHtml[1])) {
911
                                        $course_session = $courseUserHtml[1];
912
                                        $course_session['skill'] = isset($courseUserHtml['skill']) ? $courseUserHtml['skill'] : '';
913
                                        $html_courses_session[] = $course_session;
914
                                    }
915
                                }
916
                                $count_courses_session++;
917
                            }
918
                        }
919
920
                        // No courses to show.
921
                        if ($atLeastOneCourseIsVisible == false) {
922
                            if (empty($html_courses_session)) {
923
                                continue;
924
                            }
925
                        }
926
927
                        if ($count_courses_session > 0) {
928
                            $params = array(
929
                                'id' => $session_id
930
                            );
931
                            $session_box = Display::get_session_title_box($session_id);
932
933
                            $actions = null;
934
                            if (api_is_platform_admin()) {
935
                                $actions = api_get_path(WEB_CODE_PATH) .'session/resume_session.php?id_session='.$session_id;
936
                            }
937
938
                            $coachId = $session_box['id_coach'];
939
                            $extraFieldValue = new ExtraFieldValue('session');
940
                            $imageField = $extraFieldValue->get_values_by_handler_and_field_variable($session_id, 'image');
941
942
                            $params['category_id'] = $session_box['category_id'];
943
                            $params['title'] = $session_box['title'];
944
                            $params['id_coach'] = $coachId;
945
                            $params['coach_url'] = api_get_path(WEB_AJAX_PATH) . 'user_manager.ajax.php?a=get_user_popup&user_id=' . $coachId;
946
                            $params['coach_name'] = !empty($session_box['coach']) ? $session_box['coach'] : null;
947
                            $params['coach_avatar'] = UserManager::getUserPicture($coachId, USER_IMAGE_SIZE_SMALL);
948
                            $params['date'] = $session_box['dates'];
949
                            $params['image'] = isset($imageField['value']) ? $imageField['value'] : null;
950
                            $params['duration'] = isset($session_box['duration']) ? ' ' . $session_box['duration'] : null;
951
                            $params['edit_actions'] = $actions;
952
                            $params['show_description'] = $session_box['show_description'];
953
                            $params['description'] = $session_box['description'];
954
                            $params['visibility'] = $session_box['visibility'];
955
                            $params['show_simple_session_info'] = false;
956
                            $params['course_list_session_style'] = $coursesListSessionStyle;
957
                            $params['num_users'] = $session_box['num_users'];
958
                            $params['num_courses'] = $session_box['num_courses'];
959
                            $params['courses'] = $html_courses_session;
960
961
                            if ($showSimpleSessionInfo) {
962
                                $params['show_simple_session_info'] = true;
963
                            }
964
965
                            if ($gameModeIsActive) {
966
                                $params['stars'] = GamificationUtils::getSessionStars($params['id'], $this->user_id);
967
                                $params['progress'] = GamificationUtils::getSessionProgress($params['id'], $this->user_id);
968
                                $params['points'] = GamificationUtils::getSessionPoints($params['id'], $this->user_id);
969
                            }
970
                            $listSession[] = $params;
971
                            $sessionCount++;
972
                        }
973
                    }
974
                } else {
975
                    // All sessions included in
976
                    $count_courses_session = 0;
977
                    $html_sessions = '';
978
                    if (isset($session_category['sessions'])) {
979
                        foreach ($session_category['sessions'] as $session) {
980
                            $session_id = $session['session_id'];
981
982
                            // Don't show empty sessions.
983
                            if (count($session['courses']) < 1) {
984
                                continue;
985
                            }
986
987
                            $date_session_start = $session['access_start_date'];
988
                            $date_session_end = $session['access_end_date'];
989
                            $coachAccessStartDate = $session['coach_access_start_date'];
990
                            $coachAccessEndDate = $session['coach_access_end_date'];
991
992
                            $html_courses_session = [];
993
                            $count = 0;
994
995
                            foreach ($session['courses'] as $course) {
996
                                $is_coach_course = api_is_coach($session_id, $course['real_id']);
997
                                $allowed_time = 0;
998
                                $allowedEndTime = true;
999
1000 View Code Duplication
                                if (!empty($date_session_start)) {
1001
                                    if ($is_coach_course) {
1002
                                        $allowed_time = api_strtotime($coachAccessStartDate);
1003
                                    } else {
1004
                                        $allowed_time = api_strtotime($date_session_start);
1005
                                    }
1006
1007
                                    if (!isset($_GET['history'])) {
1008
                                        if (!empty($date_session_end)) {
1009
                                            if ($is_coach_course) {
1010
                                                // if coach end date is empty we use the default end date
1011
                                                if (empty($coachAccessEndDate)) {
1012
                                                    $endSessionToTms = api_strtotime($date_session_end);
1013
                                                    if ($session_now > $endSessionToTms) {
1014
                                                        $allowedEndTime = false;
1015
                                                    }
1016
                                                } else {
1017
                                                    $endSessionToTms = api_strtotime($coachAccessEndDate);
1018
                                                    if ($session_now > $endSessionToTms) {
1019
                                                        $allowedEndTime = false;
1020
                                                    }
1021
                                                }
1022
                                            } else {
1023
                                                $endSessionToTms = api_strtotime($date_session_end);
1024
                                                if ($session_now > $endSessionToTms) {
1025
                                                    $allowedEndTime = false;
1026
                                                }
1027
                                            }
1028
                                        }
1029
                                    }
1030
                                }
1031
1032
                                if ($session_now >= $allowed_time && $allowedEndTime) {
1033
                                    if (api_get_setting('hide_courses_in_sessions') === 'false') {
1034
                                        $c = CourseManager::get_logged_user_course_html(
1035
                                            $course,
1036
                                            $session_id,
1037
                                            'session_course_item'
1038
                                        );
1039
                                        $html_courses_session[] = $c[1];
1040
                                    }
1041
                                    $count_courses_session++;
1042
                                    $count++;
1043
                                }
1044
                            }
1045
1046
                            $sessionParams = [];
1047
                            // Category
1048
                            if ($count > 0) {
1049
                                $session_box = Display::get_session_title_box($session_id);
1050
                                $sessionParams[0]['id'] = $session_id;
1051
                                $sessionParams[0]['date'] = $session_box['dates'];
1052
                                $sessionParams[0]['course_list_session_style'] = $coursesListSessionStyle;
1053
                                $sessionParams[0]['title'] = $session_box['title'];
1054
                                $sessionParams[0]['subtitle'] = (!empty($session_box['coach']) ? $session_box['coach'] . ' | ': '') . $session_box['dates'];
1055
                                $sessionParams[0]['show_actions'] = api_is_platform_admin();
1056
                                $sessionParams[0]['courses'] = $html_courses_session;
1057
                                $sessionParams[0]['show_simple_session_info'] = false;
1058
1059
                                if ($showSimpleSessionInfo) {
1060
                                    $sessionParams[0]['show_simple_session_info'] = true;
1061
                                }
1062
1063
                                $this->tpl->assign('session', $sessionParams);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1064
                                $html_sessions .= $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1065
                                    $this->tpl->get_template('user_portal/classic_session.tpl')
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1066
                                );
1067
1068
                                $sessionCount++;
1069
                            }
1070
                        }
1071
                    }
1072
1073
                    if ($count_courses_session > 0) {
1074
                        $categoryParams = array(
1075
                            'id' => $session_category['session_category']['id'],
1076
                            'title' => $session_category['session_category']['name'],
1077
                            'show_actions' => api_is_platform_admin(),
1078
                            'subtitle' => '',
1079
                            'sessions' => $html_sessions
1080
                        );
1081
1082
                        $session_category_start_date = $session_category['session_category']['date_start'];
1083
                        $session_category_end_date = $session_category['session_category']['date_end'];
1084
                        if ($session_category_start_date == '0000-00-00') {
1085
                            $session_category_start_date = '';
1086
                        }
1087
1088
                        if ($session_category_end_date == '0000-00-00') {
1089
                            $session_category_end_date = '';
1090
                        }
1091
1092
                        if (
1093
                            !empty($session_category_start_date) &&
1094
                            !empty($session_category_end_date)
1095
                        ) {
1096
                            $categoryParams['subtitle'] = sprintf(
1097
                                get_lang('FromDateXToDateY'),
1098
                                $session_category_start_date,
1099
                                $session_category_end_date
1100
                            );
1101
                        } else {
1102
                            if (
1103
                                !empty($session_category_start_date)
1104
                            ) {
1105
                                $categoryParams['subtitle'] = get_lang('From') . ' ' . $session_category_start_date;
1106
                            }
1107
1108
                            if (
1109
                                !empty($session_category_end_date)
1110
                            ) {
1111
                                $categoryParams['subtitle'] = get_lang('Until') . ' ' . $session_category_end_date;
1112
                            }
1113
                        }
1114
1115
                        $this->tpl->assign('session_category', $categoryParams);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1116
                        $sessions_with_category .= $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1117
                            "{$this->tpl->templateFolder}/user_portal/session_category.tpl"
1118
                        );
1119
                    }
1120
                }
1121
            }
1122
1123
            $allCoursesInSessions = [];
1124
1125
            foreach ($listSession as $currentSession) {
1126
                $coursesInSessions = $currentSession['courses'];
1127
                unset($currentSession['courses']);
1128
                foreach ($coursesInSessions as $coursesInSession) {
1129
                    $coursesInSession['session'] = $currentSession;
1130
                    $allCoursesInSessions[] = $coursesInSession;
1131
                }
1132
            }
1133
1134
            $this->tpl->assign('all_courses', $allCoursesInSessions);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1135
            $this->tpl->assign('session', $listSession);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1136
            $this->tpl->assign('show_tutor', (api_get_setting('show_session_coach')==='true' ? true : false));
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1137
            $this->tpl->assign('gamification_mode', $gameModeIsActive);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1138
1139
            if (api_get_configuration_value('view_grid_courses')) {
1140
                $sessions_with_no_category = $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1141
                    $this->tpl->get_template('/user_portal/grid_session.tpl')
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1142
                );
1143
            } else {
1144
                $sessions_with_no_category = $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1145
                    $this->tpl->get_template('user_portal/classic_session.tpl')
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1146
                );
1147
            }
1148
        }
1149
1150
        return [
1151
            'html' => $specialCourseList . $sessions_with_category.$sessions_with_no_category.$listCourse,
1152
            'session_count' => $sessionCount,
1153
            'course_count' => $courseCount
1154
        ];
1155
    }
1156
1157
    /**
1158
     * Shows a welcome message when the user doesn't have any content in the course list
1159
     */
1160
    public function return_welcome_to_course_block()
1161
    {
1162
        $count_courses = CourseManager::count_courses();
1163
        $tpl = $this->tpl->get_template('layout/welcome_to_course.tpl');
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1164
1165
        $course_catalog_url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
1166
        $course_list_url = api_get_path(WEB_PATH).'user_portal.php';
1167
1168
        $this->tpl->assign('course_catalog_url', $course_catalog_url);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1169
        $this->tpl->assign('course_list_url', $course_list_url);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1170
        $this->tpl->assign('course_catalog_link', Display::url(get_lang('Here'), $course_catalog_url));
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1171
        $this->tpl->assign('course_list_link', Display::url(get_lang('Here'), $course_list_url));
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1172
        $this->tpl->assign('count_courses', $count_courses);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1173
1174
        return $this->tpl->fetch($tpl);
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1175
    }
1176
1177
    /**
1178
     * @return array
1179
     */
1180
    public function return_hot_courses()
1181
    {
1182
        return CourseManager::return_hot_courses(30, 6);
1183
    }
1184
1185
    /**
1186
     * UserPortal view for session, return the HTML of the course list
1187
     * @param $user_id
1188
     * @return string
1189
     */
1190
    public function returnCoursesAndSessionsViewBySession($user_id)
1191
    {
1192
        $sessionCount = 0;
1193
        $courseCount = 0;
1194
        $load_history = (isset($_GET['history']) && intval($_GET['history']) == 1) ? true : false;
1195
1196 View Code Duplication
        if ($load_history) {
1197
            //Load sessions in category in *history*
1198
            $session_categories = UserManager::get_sessions_by_category($user_id, true);
1199
        } else {
1200
            //Load sessions in category
1201
            $session_categories = UserManager::get_sessions_by_category($user_id, false);
1202
        }
1203
1204
        $html = '';
1205
        $loadDirs = $this->load_directories_preview;
1206
1207
        // If we're not in the history view...
1208
        $listCoursesInfo = array();
1209
        if (!isset($_GET['history'])) {
1210
            // Display special courses
1211
            $specialCoursesResult = CourseManager::returnSpecialCourses(
1212
                $user_id,
1213
                $loadDirs
1214
            );
1215
            $specialCourses = $specialCoursesResult;
1216
1217 View Code Duplication
            if ($specialCourses) {
1218
                $this->tpl->assign('courses', $specialCourses);
0 ignored issues
show
Bug introduced by
The method assign cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1219
                $html = $this->tpl->fetch(
0 ignored issues
show
Bug introduced by
The method fetch cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1220
                    $this->tpl->get_template('/user_portal/classic_courses_without_category.tpl')
0 ignored issues
show
Bug introduced by
The method get_template cannot be called on $this->tpl (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1221
                );
1222
            }
1223
1224
            // Display courses
1225
            // [code=>xxx, real_id=>000]
1226
            $listCourses = CourseManager::get_courses_list_by_user_id($user_id, false);
1227
1228
            foreach ($listCourses as $i => $listCourseCodeId) {
1229
                if (isset($listCourseCodeId['special_course'])) {
1230
                    continue;
1231
                }
1232
                list($userCategoryId, $userCatTitle) = CourseManager::getUserCourseCategoryForCourse(
1233
                    $user_id,
1234
                    $listCourseCodeId['real_id']
1235
                );
1236
                $listCourse = api_get_course_info_by_id($listCourseCodeId['real_id']);
1237
                $listCoursesInfo[] = array(
1238
                    'course' => $listCourse,
1239
                    'code' => $listCourseCodeId['code'],
1240
                    'id' => $listCourseCodeId['real_id'],
1241
                    'title' => $listCourse['title'],
1242
                    'userCatId' => $userCategoryId,
1243
                    'userCatTitle' => $userCatTitle
1244
                );
1245
                $courseCount++;
1246
            }
1247
            usort($listCoursesInfo, 'self::compareByCourse');
1248
        }
1249
1250
        if (is_array($session_categories)) {
1251
            // all courses that are in a session
1252
            $listCoursesInSession = SessionManager::getNamedSessionCourseForCoach($user_id);
1253
        }
1254
1255
        // we got all courses
1256
        // for each user category, sorted alphabetically, display courses
1257
        $listUserCategories = CourseManager::get_user_course_categories($user_id);
1258
        $listCoursesAlreadyDisplayed = array();
1259
        uasort($listUserCategories, "self::compareListUserCategory");
1260
        $listUserCategories[0] = '';
1261
1262
        $html .= '<div class="session-view-block">';
1263
1264
        foreach ($listUserCategories as $userCategoryId => $userCatTitle) {
1265
            // add user category
1266
            $userCategoryHtml = '';
1267
            if ($userCategoryId != 0) {
1268
                $userCategoryHtml = '<div class="session-view-well ">';
1269
            }
1270
            $userCategoryHtml .= self::getHtmlForUserCategory($userCategoryId, $userCatTitle);
1271
1272
            // look for course in this userCat in session courses : $listCoursesInSession
1273
            $htmlCategory = '';
1274
            if (isset($listCoursesInSession[$userCategoryId])) {
1275
                // list of courses in this user cat
1276
                foreach ($listCoursesInSession[$userCategoryId]['courseInUserCatList'] as $i => $listCourse) {
1277
                    // add course
1278
                    $listCoursesAlreadyDisplayed[$listCourse['courseId']] = 1;
1279
                    if ($userCategoryId == 0) {
1280
                        $htmlCategory .= '<div class="panel panel-default">';
1281
                    } else {
1282
                        $htmlCategory .= '<div class="panel panel-default">';
1283
                    }
1284
                    $htmlCategory .= '<div class="panel-body">';
1285
                    $coursesInfo =  $listCourse['course'];
1286
1287
                    $htmlCategory .= self::getHtmlForCourse(
1288
                        $coursesInfo,
1289
                        $userCategoryId,
1290
                        1,
1291
                        $loadDirs
1292
                    );
1293
                    // list of session category
1294
                    $htmlSessionCategory = '<div class="session-view-row" style="display:none;" id="courseblock-'.$coursesInfo['real_id'].'">';
1295
                    foreach ($listCourse['sessionCatList'] as $j => $listCategorySession) {
1296
                        // add session category
1297
                        $htmlSessionCategory .= self::getHtmlSessionCategory(
1298
                            $listCategorySession['catSessionId'],
1299
                            $listCategorySession['catSessionName']
1300
                        );
1301
                        // list of session
1302
                        $htmlSession = '';    // start
1303
                        foreach ($listCategorySession['sessionList'] as $k => $listSession) {
1304
                            // add session
1305
                            $htmlSession .= '<div class="session-view-row">';
1306
                            $htmlSession .= self::getHtmlForSession(
1307
                                $listSession['sessionId'],
1308
                                $listSession['sessionName'],
1309
                                $listCategorySession['catSessionId'],
1310
                                $coursesInfo
1311
                            );
1312
                            $htmlSession .= '</div>';
1313
                            $sessionCount++;
1314
                        }
1315
                        $htmlSession .= ''; // end session block
1316
                        $htmlSessionCategory .= $htmlSession;
1317
                    }
1318
                    $htmlSessionCategory .= '</div>'; // end session cat block
1319
                    $htmlCategory .=  $htmlSessionCategory .'</div></div>' ;
1320
                    $htmlCategory .= '';   // end course block
1321
                }
1322
                $userCategoryHtml .= $htmlCategory;
1323
            }
1324
1325
            // look for courses in this userCat in not in session courses : $listCoursesInfo
1326
            // if course not already added
1327
            $htmlCategory = '';
1328
            foreach ($listCoursesInfo as $i => $listCourse) {
1329
                if ($listCourse['userCatId'] == $userCategoryId && !isset($listCoursesAlreadyDisplayed[$listCourse['id']])) {
1330
                    if ($userCategoryId != 0) {
1331
                        $htmlCategory .= '<div class="panel panel-default">';
1332
                    } else {
1333
                        $htmlCategory .= '<div class="panel panel-default">';
1334
                    }
1335
1336
                    $htmlCategory .= '<div class="panel-body">';
1337
                    $htmlCategory .= self::getHtmlForCourse(
1338
                        $listCourse['course'],
1339
                        $userCategoryId,
1340
                        0,
1341
                        $loadDirs
1342
                    );
1343
                    $htmlCategory .= '</div></div>';
1344
                }
1345
            }
1346
            $htmlCategory .= '';
1347
            $userCategoryHtml .= $htmlCategory;   // end user cat block
1348
            if ($userCategoryId != 0) {
1349
                $userCategoryHtml .= '</div>';
1350
            }
1351
            $html .= $userCategoryHtml;   //
1352
        }
1353
        $html .= '</div>';
1354
1355
        return [
1356
            'html' => $html,
1357
            'session_count' => $sessionCount,
1358
            'course_count' => $courseCount
1359
        ];
1360
    }
1361
1362
    /**
1363
     * Return HTML code for personal user course category
1364
     * @param $id
1365
     * @param $title
1366
     * @return string
1367
     */
1368 View Code Duplication
    private static function getHtmlForUserCategory($id, $title)
1369
    {
1370
        if ($id == 0) {
1371
            return '';
1372
        }
1373
        $icon = Display::return_icon(
1374
            'folder_yellow.png',
1375
            $title,
1376
            array('class' => 'sessionView'),
1377
            ICON_SIZE_LARGE
1378
        );
1379
1380
        return "<div class='session-view-user-category'>$icon<span>$title</span></div>";
1381
    }
1382
1383
    /**
1384
     * return HTML code for course display in session view
1385
     * @param array $courseInfo
1386
     * @param $userCategoryId
1387
     * @param bool $displayButton
1388
     * @param $loadDirs
1389
     * @return string
1390
     */
1391
    private static function getHtmlForCourse(
1392
        $courseInfo,
1393
        $userCategoryId,
1394
        $displayButton = false,
1395
        $loadDirs
1396
    ) {
1397
        if (empty($courseInfo)) {
1398
            return '';
1399
        }
1400
1401
        $id = $courseInfo['real_id'];
1402
        $title = $courseInfo['title'];
1403
        $code = $courseInfo['code'];
1404
1405
        $class = 'session-view-lvl-6';
1406
        if ($userCategoryId != 0 && !$displayButton) {
1407
            $class = 'session-view-lvl-7';
1408
        }
1409
1410
        $class2 = 'session-view-lvl-6';
1411
        if ($displayButton || $userCategoryId != 0) {
1412
            $class2 = 'session-view-lvl-7';
1413
        }
1414
1415
        $button = '';
1416
        if ($displayButton) {
1417
            $button = '<input id="session-view-button-'.intval($id).'" class="btn btn-default btn-sm" type="button" onclick="hideUnhide(\'courseblock-'.intval($id).'\', \'session-view-button-'.intval($id).'\', \'+\', \'-\')" value="+" />';
1418
        }
1419
1420
        $icon = Display::return_icon(
1421
            'blackboard.png',
1422
            $title,
1423
            array('class' => 'sessionView'),
1424
            ICON_SIZE_LARGE
1425
        );
1426
1427
        $courseLink = $courseInfo['course_public_url'].'?id_session=0';
1428
1429
        // get html course params
1430
        // ['right_actions'] ['teachers'] ['notifications']
1431
        $tabParams = CourseManager::getCourseParamsForDisplay($id, $loadDirs);
1432
        // teacher list
1433
        if (!empty($tabParams['teachers'])) {
1434
            $teachers = '<p class="'.$class2.' view-by-session-teachers">'.$tabParams['teachers'].'</p>';
1435
        }
1436
1437
        // notification
1438
        if (!empty($tabParams['right_actions'])) {
1439
            $rightActions = '<div class="pull-right">'.$tabParams['right_actions'].'</div>';
1440
        }
1441
1442
        return "<div>
1443
                    $button
1444
                    <span class='$class'>$icon
1445
                    <a class='sessionView' href='$courseLink'>$title</a>
1446
                    </span>".$tabParams['notifications']."$rightActions
1447
                </div>
1448
                $teachers";
1449
    }
1450
1451
    /**
1452
     * return HTML code for session category
1453
     * @param $id
1454
     * @param $title
1455
     * @return string
1456
     */
1457 View Code Duplication
    private static function getHtmlSessionCategory($id, $title)
1458
    {
1459
        if ($id == 0) {
1460
            return '';
1461
        }
1462
1463
        $icon = Display::return_icon(
1464
            'folder_blue.png',
1465
            $title,
1466
            array('class' => 'sessionView'),
1467
            ICON_SIZE_LARGE
1468
        );
1469
1470
        return "<div class='session-view-session-category'>
1471
                <span class='session-view-lvl-2'>
1472
                    $icon
1473
                    <span>$title</span>
1474
                </span>
1475
                </div>";
1476
    }
1477
1478
    /**
1479
     * return HTML code for session
1480
     * @param int $id session id
1481
     * @param string $title session title
1482
     * @param int $categorySessionId
1483
     * @param array $courseInfo
1484
     *
1485
     * @return string
1486
     */
1487
    private static function getHtmlForSession($id, $title, $categorySessionId, $courseInfo)
1488
    {
1489
        $html = '';
1490
1491
        if ($categorySessionId == 0) {
1492
            $class1 = 'session-view-lvl-2';    // session
1493
            $class2 = 'session-view-lvl-4';    // got to course in session link
1494
        } else {
1495
            $class1 = 'session-view-lvl-3';    // session
1496
            $class2 = 'session-view-lvl-5';    // got to course in session link
1497
        }
1498
1499
        $icon = Display::return_icon(
1500
            'blackboard_blue.png',
1501
            $title,
1502
            array('class' => 'sessionView'),
1503
            ICON_SIZE_LARGE
1504
        );
1505
        $courseLink = $courseInfo['course_public_url'].'?id_session='.intval($id);
1506
1507
        $html .= "<span class='$class1 session-view-session'>$icon$title</span>";
1508
        $html .= '<div class="'.$class2.' session-view-session-go-to-course-in-session">
1509
                  <a class="" href="'.$courseLink.'">'.get_lang('GoToCourseInsideSession').'</a></div>';
1510
1511
        return '<div>'.$html.'</div>';
1512
    }
1513
1514
    /**
1515
     * @param $listA
1516
     * @param $listB
1517
     * @return int
1518
     */
1519
    private static function compareByCourse($listA, $listB)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
1520
    {
1521
        if ($listA['userCatTitle'] == $listB['userCatTitle']) {
1522
            if ($listA['title'] == $listB['title']) {
1523
                return 0;
1524
            } else if($listA['title'] > $listB['title']) {
1525
                return 1;
1526
            } else {
1527
                return -1;
1528
            }
1529
        } else if ($listA['userCatTitle'] > $listB['userCatTitle']) {
1530
            return 1;
1531
        } else {
1532
            return -1;
1533
        }
1534
    }
1535
1536
    /**
1537
     * @param $listA
1538
     * @param $listB
1539
     * @return int
1540
     */
1541 View Code Duplication
    public static function compareListUserCategory($listA, $listB)
1542
    {
1543
        if ($listA['title'] == $listB['title']) {
1544
            return 0;
1545
        } else if($listA['title'] > $listB['title']) {
1546
            return 1;
1547
        } else {
1548
            return -1;
1549
        }
1550
    }
1551
1552
    /**
1553
     * @param $view
1554
     * @param $userId
1555
     */
1556
    public static function setDefaultMyCourseView($view, $userId)
1557
    {
1558
        setcookie('defaultMyCourseView'.$userId, $view);
1559
    }
1560
}
1561