Completed
Push — master ( 8b2aa0...562606 )
by Julito
09:06
created

returnThumbnail()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 8
nop 2
dl 0
loc 40
rs 9.504
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * View (MVC patter) for courses categories.
6
 *
7
 * @author Christian Fasanando <[email protected]> - Beeznest
8
 *
9
 * @package chamilo.auth
10
 */
11
if (isset($_REQUEST['action']) && Security::remove_XSS($_REQUEST['action']) !== 'subscribe') {
12
    $stok = Security::get_token();
13
} else {
14
    $stok = Security::getTokenFromSession();
15
}
16
17
$action = !empty($_REQUEST['action']) ? Security::remove_XSS($_REQUEST['action']) : 'display_courses';
18
global $actions;
19
$action = in_array($action, $actions) ? $action : 'display_courses';
20
21
$showCourses = CoursesAndSessionsCatalog::showCourses();
22
$showSessions = CoursesAndSessionsCatalog::showSessions();
23
$pageCurrent = isset($pageCurrent) ? $pageCurrent : isset($_GET['pageCurrent']) ? (int) $_GET['pageCurrent'] : 1;
24
$pageLength = isset($pageLength) ? $pageLength : isset($_GET['pageLength']) ? (int) $_GET['pageLength'] : CoursesAndSessionsCatalog::PAGE_LENGTH;
25
$pageTotal = (int) ceil((int) $countCoursesInCategory / $pageLength);
26
$cataloguePagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($pageCurrent, $pageLength, $pageTotal) : '';
27
$searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : '';
28
$codeType = isset($_REQUEST['category_code']) ? Security::remove_XSS($_REQUEST['category_code']) : '';
29
30
$date = date('Y-m-d');
31
if ($showSessions && isset($_POST['date'])) {
32
    $date = $_POST['date'];
33
}
34
$userInfo = api_get_user_info();
35
$code = isset($code) ? $code : null;
36
37
?>
38
<script>
39
    $(function() {
40
        $('.star-rating li a').on('click', function(event) {
41
            var id = $(this).parents('ul').attr('id');
42
            $('#vote_label2_' + id).html("<?php echo get_lang('Loading'); ?>");
43
            $.ajax({
44
                url: $(this).attr('data-link'),
45
                success: function(data) {
46
                    $("#rating_wrapper_"+id).html(data);
47
                    if (data == 'added') {
48
                        //$('#vote_label2_' + id).html("{'Saved'|get_lang}");
49
                    }
50
                    if (data == 'updated') {
51
                        //$('#vote_label2_' + id).html("{'Saved'|get_lang}");
52
                    }
53
                }
54
            });
55
        });
56
        var getSessionId = function (el) {
57
            var parts = el.id.split('_');
58
            return parseInt(parts[1], 10);
59
        };
60
61
        <?php if ($showSessions) {
62
    ?>
63
        $('#date').datepicker({
64
            dateFormat: 'yy-mm-dd'
65
        });
66
        <?php
67
} ?>
68
    });
69
</script>
70
<?php
71
72
echo '<div class="row">
73
    <div class="col-md-12">
74
        <h2 class="title-courses">'.get_lang('CourseManagement').'</h2>
75
        <div class="search-courses">
76
            <div class="row">';
77
78
if ($showCourses) {
79
    echo '<div class="col-md-'.($showSessions ? '4' : '6').'">';
80
    if (!isset($_GET['hidden_links']) || intval($_GET['hidden_links']) != 1) {
81
        ?>
82
        <form method="post"
83
              action="<?php echo CourseCategory::getCourseCategoryUrl(1, $pageLength, 'ALL', 0, 'subscribe'); ?>">
84
            <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
85
            <input type="hidden" name="search_course" value="1"/>
86
            <label><?php echo get_lang('Search'); ?></label>
87
            <div class="input-group">
88
                <input class="form-control" type="text" name="search_term"
89
                   value="<?php echo empty($_POST['search_term']) ? '' : api_htmlentities($searchTerm); ?>"/>
90
                <div class="input-group-btn">
91
                    <button class="btn btn-default" type="submit">
92
                        <em class="fa fa-search"></em> <?php echo get_lang('Search'); ?>
93
                    </button>
94
                </div>
95
            </div>
96
        </form>
97
        <?php
98
    }
99
    echo '</div>';
100
    echo '<div class="col-md-'.($showSessions ? '4' : '6').'">';
101
    $listCategories = CoursesAndSessionsCatalog::getCourseCategoriesTree();
102
    $categoriesSelect = getOptionSelect($listCategories, $codeType);
103
104
    $webAction = api_get_path(WEB_CODE_PATH).'auth/courses.php';
105
    $form = '<form action="'.$webAction.'" method="GET">';
106
    $form .= '<input type="hidden" name="action" value="'.$action.'">';
107
    $form .= '<input type="hidden" name="pageCurrent" value="'.$pageCurrent.'">';
108
    $form .= '<input type="hidden" name="pageLength" value="'.$pageLength.'">';
109
    $form .= '<div class="form-group">';
110
    $form .= '<label>'.get_lang('CourseCategories').'</label>';
111
    $form .= $categoriesSelect;
112
    $form .= '</div>';
113
    $form .= '</form>';
114
    echo $form;
115
    echo '</div>';
116
}
117
118
if ($showSessions) {
119
    $url = CourseCategory::getCourseCategoryUrl(1, $pageLength, null, 0, 'display_sessions');
120
    echo '<div class="col-md-4">
121
        <div class="return-catalog">
122
            <a class="btn btn-default btn-lg btn-block"
123
               href="'.$url.'">
124
                <em class="fa fa-arrow-right"></em>'.get_lang('SessionList').'
125
            </a>
126
        </div>
127
    </div>
128
    ';
129
}
130
131
echo '</div></div></div></div>';
132
133
if ($showCourses && $action != 'display_sessions') {
134
    if (!empty($message)) {
135
        echo Display::return_message($message, 'confirmation', false);
136
    }
137
    if (!empty($error)) {
138
        echo Display::return_message($error, 'error', false);
139
    }
140
141
    if (!empty($content)) {
142
        echo $content;
143
    }
144
145
    if (!empty($searchTerm)) {
146
        echo "<p><strong>".get_lang('SearchResultsFor')." ".$searchTerm."</strong><br />";
147
    }
148
149
    $showTeacher = api_get_setting('display_teacher_in_courselist') === 'true';
150
    $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
151
    $user_id = api_get_user_id();
152
    $categoryListFromDatabase = CourseCategory::getCategories();
153
154
    $categoryList = [];
155
    if (!empty($categoryListFromDatabase)) {
156
        foreach ($categoryListFromDatabase as $categoryItem) {
157
            $categoryList[$categoryItem['code']] = $categoryItem['name'];
158
        }
159
    }
160
161
    if (!empty($browse_courses_in_category)) {
162
        echo '<div class="grid-courses row">';
163
        foreach ($browse_courses_in_category as $course) {
164
            $course_hidden = $course['visibility'] == COURSE_VISIBILITY_HIDDEN;
165
166
            if ($course_hidden) {
167
                continue;
168
            }
169
170
            $userRegisteredInCourse = CourseManager::is_user_subscribed_in_course($user_id, $course['code']);
171
            $userRegisteredInCourseAsTeacher = CourseManager::is_course_teacher($user_id, $course['code']);
172
            $userRegistered = $userRegisteredInCourse && $userRegisteredInCourseAsTeacher;
173
174
            $course_public = $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD;
175
            $course_open = $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM;
176
            $course_private = $course['visibility'] == COURSE_VISIBILITY_REGISTERED;
177
            $course_closed = $course['visibility'] == COURSE_VISIBILITY_CLOSED;
178
179
            $course_subscribe_allowed = $course['subscribe'] == 1;
180
            $course_unsubscribe_allowed = $course['unsubscribe'] == 1;
181
            $count_connections = $course['count_connections'];
182
            $creation_date = substr($course['creation_date'], 0, 10);
183
184
            // display the course bloc
185
            $html = '<div class="col-xs-12 col-sm-6 col-md-4"><div class="items items-courses">';
186
187
            $course['category_title'] = '';
188
            if (isset($course['category'])) {
189
                $course['category_title'] = isset($categoryList[$course['category']]) ? $categoryList[$course['category']] : '';
190
            }
191
192
            // Display thumbnail
193
            $html .= returnThumbnail($course, $userRegistered);
194
195
            $separator = null;
196
            $subscribeButton = return_register_button($course, $stok, $code, $searchTerm);
197
            // Start buy course validation
198
            // display the course price and buy button if the buycourses plugin is enabled and this course is configured
199
            $plugin = BuyCoursesPlugin::create();
200
            $isThisCourseInSale = $plugin->buyCoursesForGridCatalogValidator(
201
                $course['real_id'],
202
                BuyCoursesPlugin::PRODUCT_TYPE_COURSE
203
            );
204
205
            if ($isThisCourseInSale) {
206
                // set the Price label
207
                $separator = $isThisCourseInSale['html'];
208
                // set the Buy button instead register.
209
                if ($isThisCourseInSale['verificator']) {
210
                    $subscribeButton = $plugin->returnBuyCourseButton(
211
                        $course['real_id'],
212
                        BuyCoursesPlugin::PRODUCT_TYPE_COURSE
213
                    );
214
                }
215
            }
216
            // end buy course validation
217
218
            // display course title and button bloc
219
            $html .= '<div class="description">';
220
            $html .= return_title($course, $userRegisteredInCourse);
221
222
            if ($showTeacher) {
223
                $html .= return_teacher($course);
224
            }
225
226
            // display button line
227
            $html .= '<div class="toolbar row">';
228
            $html .= $separator ? '<div class="col-sm-4">'.$separator.'</div>' : '';
229
            $html .= '<div class="col-sm-8">';
230
            // if user registered as student
231
            if ($userRegisteredInCourse) {
232
                $html .= return_already_registered_label('student');
233
                if (!$course_closed) {
234
                    if ($course_unsubscribe_allowed) {
235
                        $html .= return_unregister_button($course, $stok, $searchTerm, $code);
236
                    }
237
                }
238
            } elseif ($userRegisteredInCourseAsTeacher) {
239
                // if user registered as teacher
240
                if ($course_unsubscribe_allowed) {
241
                    $html .= return_unregister_button($course, $stok, $searchTerm, $code);
242
                }
243
            } else {
244
                // if user not registered in the course
245
                if (!$course_closed) {
246
                    if (!$course_private) {
247
                        if ($course_subscribe_allowed) {
248
                            $html .= $subscribeButton;
249
                        }
250
                    }
251
                }
252
            }
253
            $html .= '</div>';
254
            $html .= '</div>';
255
            $html .= '</div>';
256
            $html .= '</div>';
257
            $html .= '</div>';
258
            echo $html;
259
        }
260
        echo '</div>';
261
    } else {
262
        if (!isset($_REQUEST['subscribe_user_with_password']) &&
263
            !isset($_REQUEST['subscribe_course'])
264
        ) {
265
            echo Display::return_message(
266
                get_lang('ThereAreNoCoursesInThisCategory'),
267
                'warning'
268
            );
269
        }
270
    }
271
}
272
273
echo '<div class="col-md-12">';
274
echo $cataloguePagination;
275
echo '</div>';
276
277
function getOptionSelect($categories, $codeType)
278
{
279
    $html = '';
280
    $html .= '<select name="category_code" onchange="submit();" class="selectpicker form-control">';
281
    foreach ($categories as $category) {
282
        $categoryCode = Security::remove_XSS($category['code']);
283
        $categoryName = Security::remove_XSS($category['name']);
284
        $countCourse = (int) $category['number_courses'];
285
        $level = $category['level'];
286
        if (empty($countCourse)) {
287
            continue;
288
        }
289
        if ($level > 0) {
290
            $separate = str_repeat('--', $level);
291
        } else {
292
            $separate = '';
293
        }
294
        $html .= '<option '.($categoryCode == $codeType ? 'selected="selected" ' : '')
295
            .' value="'.$categoryCode.'">'.$separate.' '.$categoryName.' ('.$countCourse.') </option>';
296
    }
297
    $html .= '</select>';
298
299
    return $html;
300
}
301
/**
302
 * Display the course catalog image of a course.
303
 *
304
 * @param array $course
305
 * @param bool  $registeredUser
306
 *
307
 * @return string HTML string
308
 */
309
function returnThumbnail($course, $registeredUser)
0 ignored issues
show
Unused Code introduced by
The parameter $registeredUser is not used and could be removed. ( Ignorable by Annotation )

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

309
function returnThumbnail($course, /** @scrutinizer ignore-unused */ $registeredUser)

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

Loading history...
310
{
311
    $html = '';
312
    $title = cut($course['title'], 70);
313
    //$linkCourse = api_get_course_url($course['code']);
314
    $linkCourse = api_get_path(WEB_PATH).'course/'.$course['real_id'].'/about';
315
    // course path
316
    $course_path = api_get_path(SYS_COURSE_PATH).$course['directory'];
317
318
    if (file_exists($course_path.'/course-pic.png')) {
319
        // redimensioned image 85x85
320
        $courseMediumImage = api_get_path(WEB_COURSE_PATH).$course['directory'].'/course-pic.png';
321
    } else {
322
        // without picture
323
        $courseMediumImage = Display::return_icon(
324
            'session_default.png',
325
            null,
326
            null,
327
            null,
328
            null,
329
            true
330
        );
331
    }
332
333
    $html .= '<div class="image">';
334
    $html .= '<a href="'.$linkCourse.'" title="'.$course['title'].'">'
335
            .'<img class="img-responsive" src="'.$courseMediumImage.'" '
336
            .'alt="'.api_htmlentities($title).'"/></a>';
337
338
    $categoryTitle = isset($course['category_title']) ? $course['category_title'] : '';
339
    if (!empty($categoryTitle)) {
340
        $html .= '<span class="category">'.$categoryTitle.'</span>';
341
        $html .= '<div class="cribbon"></div>';
342
    }
343
344
    $html .= '<div class="user-actions">';
345
    $html .= CourseManager::returnDescriptionButton($course);
346
    $html .= '</div></div>';
347
348
    return $html;
349
}
350
351
/**
352
 * @param array $courseInfo
353
 *
354
 * @return string
355
 */
356
function return_teacher($courseInfo)
357
{
358
    $teachers = CourseManager::getTeachersFromCourse($courseInfo['real_id']);
359
    $length = count($teachers);
360
361
    if (!$length) {
362
        return '';
363
    }
364
365
    $html = '<div class="block-author">';
366
    if ($length > 6) {
367
        $html .= '<a 
368
            id="plist" 
369
            data-trigger="focus" 
370
            tabindex="0" role="button" 
371
            class="btn btn-default panel_popover" 
372
            data-toggle="popover" 
373
            title="'.addslashes(get_lang('CourseTeachers')).'" 
374
            data-html="true"
375
        >
376
            <i class="fa fa-graduation-cap" aria-hidden="true"></i>
377
        </a>';
378
        $html .= '<div id="popover-content-plist" class="hide">';
379
        foreach ($teachers as $value) {
380
            $name = $value['firstname'].' '.$value['lastname'];
381
            $html .= '<div class="popover-teacher">';
382
            $html .= '<a href="'.$value['url'].'" class="ajax" data-title="'.$name.'" title="'.$name.'">
383
                        <img src="'.$value['avatar'].'" title="'.$name.'" alt="'.get_lang('UserPicture').'"/></a>';
384
            $html .= '<div class="teachers-details"><h5>
385
                        <a href="'.$value['url'].'" class="ajax" data-title="'.$name.'" title="'.$name.'">'
386
                        .$name.'</a></h5></div>';
387
            $html .= '</div>';
388
        }
389
        $html .= '</div>';
390
    } else {
391
        foreach ($teachers as $value) {
392
            $name = $value['firstname'].' '.$value['lastname'];
393
            if ($length > 2) {
394
                $html .= '<a href="'.$value['url'].'" class="ajax" data-title="'.$name.'" title="'.$name.'">
395
                        <img src="'.$value['avatar'].'" title="'.$name.'" alt="'.get_lang('UserPicture').'"/></a>';
396
            } else {
397
                $html .= '<a href="'.$value['url'].'" class="ajax" data-title="'.$name.'" title="'.$name.'">
398
                        <img src="'.$value['avatar'].'" title="'.$name.'" alt="'.get_lang('UserPicture').'"/></a>';
399
                $html .= '<div class="teachers-details"><h5>
400
                        <a href="'.$value['url'].'" class="ajax" data-title="'.$name.'">'
401
                        .$name.'</a></h5><p>'.get_lang('Teacher').'</p></div>';
402
            }
403
        }
404
    }
405
    $html .= '</div>';
406
407
    return $html;
408
}
409
410
/**
411
 * Display the title of a course in course catalog.
412
 *
413
 * @param array $course
414
 * @param bool  $registeredUser
415
 *
416
 * @return string HTML string
417
 */
418
function return_title($course, $registeredUser)
0 ignored issues
show
Unused Code introduced by
The parameter $registeredUser is not used and could be removed. ( Ignorable by Annotation )

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

418
function return_title($course, /** @scrutinizer ignore-unused */ $registeredUser)

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

Loading history...
419
{
420
    //$linkCourse = api_get_course_url($course['code']);
421
    $linkCourse = api_get_path(WEB_PATH).'course/'.$course['real_id'].'/about';
422
    $html = '<div class="block-title"><h4 class="title">';
423
    $html .= '<a title="'.$course['title'].'" href="'.$linkCourse.'">'.$course['title'].'</a>';
424
    $html .= '</h4></div>';
425
426
    if (api_get_configuration_value('hide_course_rating') === false) {
427
        $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
428
        $rating = Display::return_rating_system(
429
            'star_'.$course['real_id'],
430
            $ajax_url.'&course_id='.$course['real_id'],
431
            $course['point_info']
432
        );
433
        $html .= '<div class="ranking">'.$rating.'</div>';
434
    }
435
436
    return $html;
437
}
438
439
/**
440
 * Display the goto course button of a course in the course catalog.
441
 *
442
 * @param $course
443
 *
444
 * @return string HTML string
445
 */
446
function return_goto_button($course)
447
{
448
    $title = get_lang('GoToCourse');
449
    $html = Display::url(
450
        Display::returnFontAwesomeIcon('share'),
451
        api_get_course_url($course['code']),
452
        [
453
            'class' => 'btn btn-default btn-sm',
454
            'title' => $title,
455
            'aria-label' => $title,
456
        ]
457
    );
458
459
    return $html.PHP_EOL;
460
}
461
462
/**
463
 * Display the already registerd text in a course in the course catalog.
464
 *
465
 * @param $in_status
466
 *
467
 * @return string HTML string
468
 */
469
function return_already_registered_label($in_status)
470
{
471
    $icon = '<em class="fa fa-check"></em>';
472
    $title = get_lang("YouAreATeacherOfThisCourse");
473
    if ($in_status == 'student') {
474
        $icon = '<em class="fa fa-check"></em>';
475
        $title = get_lang("AlreadySubscribed");
476
    }
477
478
    $html = Display::tag(
479
        'span',
480
        $icon.' '.$title,
481
        [
482
            'id' => 'register',
483
            'class' => 'label-subscribed text-success',
484
            'title' => $title,
485
            'aria-label' => $title,
486
        ]
487
    );
488
489
    return $html.PHP_EOL;
490
}
491
492
/**
493
 * Display the register button of a course in the course catalog.
494
 *
495
 * @param $course
496
 * @param $stok
497
 * @param $code
498
 * @param $search_term
499
 *
500
 * @return string
501
 */
502
function return_register_button($course, $stok, $code, $search_term)
503
{
504
    $title = get_lang('Subscribe');
505
    $action = 'subscribe_course';
506
    if (!empty($course['registration_code'])) {
507
        $action = 'subscribe_course_validation';
508
    }
509
510
    $html = Display::url(
511
        Display::returnFontAwesomeIcon('check').' '.$title,
512
        api_get_self().'?action='.$action.'&sec_token='.$stok.
513
        '&subscribe_course='.$course['code'].'&search_term='.$search_term.'&category_code='.$code,
514
        ['class' => 'btn btn-success btn-sm', 'title' => $title, 'aria-label' => $title]
515
    );
516
517
    return $html;
518
}
519
520
/**
521
 * Display the unregister button of a course in the course catalog.
522
 *
523
 * @param $course
524
 * @param $stok
525
 * @param $search_term
526
 * @param $code
527
 *
528
 * @return string
529
 */
530
function return_unregister_button($course, $stok, $search_term, $code)
531
{
532
    $title = get_lang('Unsubscription');
533
    $html = Display::url(
534
        Display::returnFontAwesomeIcon('sign-in').' '.$title,
535
        api_get_self().'?action=unsubscribe&sec_token='.$stok
536
        .'&unsubscribe='.$course['code'].'&search_term='.$search_term.'&category_code='.$code,
537
        ['class' => 'btn btn-danger btn-sm', 'title' => $title, 'aria-label' => $title]
538
    );
539
540
    return $html;
541
}
542