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

Link::moveLinkDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CLink;
5
6
/**
7
 * Function library for the links tool.
8
 *
9
 * This is a complete remake of the original link tool.
10
 * New features:
11
 * - Organize links into categories;
12
 * - favorites/bookmarks interface;
13
 * - move links up/down within a category;
14
 * - move categories up/down;
15
 * - expand/collapse all categories;
16
 * - add link to 'root' category => category-less link is always visible.
17
 *
18
 * @author Patrick Cool, complete remake (December 2003 - January 2004)
19
 * @author René Haentjens, CSV file import (October 2004)
20
 *
21
 * @package chamilo.link
22
 */
23
class Link extends Model
24
{
25
    public $table;
26
    public $is_course_model = true;
27
    public $columns = [
28
        'id',
29
        'c_id',
30
        'url',
31
        'title',
32
        'description',
33
        'category_id',
34
        'display_order',
35
        'on_homepage',
36
        'target',
37
        'session_id',
38
    ];
39
    public $required = ['url', 'title'];
40
    private $course;
41
42
    /**
43
     * Link constructor.
44
     */
45
    public function __construct()
46
    {
47
        $this->table = Database::get_course_table(TABLE_LINK);
48
    }
49
50
    /**
51
     * @param array $course
52
     */
53
    public function setCourse($course)
54
    {
55
        $this->course = $course;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getCourse()
62
    {
63
        return !empty($this->course) ? $this->course : api_get_course_info();
64
    }
65
66
    /**
67
     * Organize the saving of a link, using the parent's save method and
68
     * updating the item_property table.
69
     *
70
     * @param array $params
71
     * @param bool  $show_query Whether to show the query in logs when
72
     *                          calling parent's save method
73
     *
74
     * @return bool True if link could be saved, false otherwise
75
     */
76
    public function save($params, $show_query = null)
77
    {
78
        $course_info = $this->getCourse();
79
        $courseId = $course_info['real_id'];
80
81
        $params['session_id'] = api_get_session_id();
82
        $params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
83
84
        $sql = "SELECT MAX(display_order)
85
                FROM  ".$this->table."
86
                WHERE
87
                    c_id = $courseId AND
88
                    category_id = '".intval($params['category_id'])."'";
89
        $result = Database:: query($sql);
90
        list($orderMax) = Database:: fetch_row($result);
91
        $order = $orderMax + 1;
92
        $params['display_order'] = $order;
93
94
        $id = parent::save($params, $show_query);
95
96
        if (!empty($id)) {
97
            // iid
98
            $sql = "UPDATE ".$this->table." SET id = iid WHERE iid = $id";
99
            Database::query($sql);
100
101
            api_item_property_update(
102
                $course_info,
103
                TOOL_LINK,
104
                $id,
105
                'LinkAdded',
106
                api_get_user_id()
107
            );
108
109
            api_set_default_visibility($id, TOOL_LINK);
110
        }
111
112
        return $id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $id also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
113
    }
114
115
    /**
116
     * Update a link in the database.
117
     *
118
     * @param int    $linkId    The ID of the link to update
119
     * @param string $linkUrl   The new URL to be saved
120
     * @param int    $courseId
121
     * @param int    $sessionId
122
     *
123
     * @return bool
124
     */
125
    public function updateLink(
126
        $linkId,
127
        $linkUrl,
128
        $courseId = null,
129
        $sessionId = null
130
    ) {
131
        $tblLink = Database::get_course_table(TABLE_LINK);
132
        $linkUrl = Database::escape_string($linkUrl);
133
        $linkId = intval($linkId);
134
        if (is_null($courseId)) {
135
            $courseId = api_get_course_int_id();
136
        }
137
        $courseId = intval($courseId);
138
        if (is_null($sessionId)) {
139
            $sessionId = api_get_session_id();
140
        }
141
        $sessionId = intval($sessionId);
142
        if ($linkUrl != '') {
143
            $sql = "UPDATE $tblLink SET 
144
                    url = '$linkUrl'
145
                    WHERE id = $linkId AND c_id = $courseId AND session_id = $sessionId";
146
            $resLink = Database::query($sql);
147
148
            return $resLink;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $resLink returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * Used to add a link or a category.
156
     *
157
     * @param string $type , "link" or "category"
158
     *
159
     * @todo replace strings by constants
160
     *
161
     * @author Patrick Cool <[email protected]>, Ghent University
162
     *
163
     * @return bool True on success, false on failure
164
     */
165
    public static function addlinkcategory($type)
166
    {
167
        $ok = true;
168
        $_course = api_get_course_info();
169
        $course_id = $_course['real_id'];
170
        $session_id = api_get_session_id();
171
172
        if ($type == 'link') {
173
            $title = Security::remove_XSS(stripslashes($_POST['title']));
174
            $urllink = Security::remove_XSS($_POST['url']);
175
            $description = Security::remove_XSS($_POST['description']);
176
            $selectcategory = Security::remove_XSS($_POST['category_id']);
177
178
            $onhomepage = 0;
179
            if (isset($_POST['on_homepage'])) {
180
                $onhomepage = Security::remove_XSS($_POST['on_homepage']);
181
            }
182
183
            $target = '_self'; // Default target.
184
            if (!empty($_POST['target'])) {
185
                $target = Security::remove_XSS($_POST['target']);
186
            }
187
188
            $urllink = trim($urllink);
189
            $title = trim($title);
190
            $description = trim($description);
191
192
            // We ensure URL to be absolute.
193
            if (strpos($urllink, '://') === false) {
194
                $urllink = 'http://'.$urllink;
195
            }
196
197
            // If the title is empty, we use the URL as title.
198
            if ($title == '') {
199
                $title = $urllink;
200
            }
201
202
            // If the URL is invalid, an error occurs.
203
            if (!api_valid_url($urllink, true)) {
204
                // A check against an absolute URL
205
                Display::addFlash(Display::return_message(get_lang('GiveURL'), 'error'));
206
207
                return false;
208
            } else {
209
                // Looking for the largest order number for this category.
210
                $link = new Link();
211
                $params = [
212
                    'c_id' => $course_id,
213
                    'url' => $urllink,
214
                    'title' => $title,
215
                    'description' => $description,
216
                    'category_id' => $selectcategory,
217
                    'on_homepage' => $onhomepage,
218
                    'target' => $target,
219
                    'session_id' => $session_id,
220
                ];
221
                $link_id = $link->save($params);
222
223
                if ((api_get_setting('search_enabled') == 'true') &&
224
                    $link_id && extension_loaded('xapian')
225
                ) {
226
                    require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
227
228
                    $course_int_id = $_course['real_id'];
229
                    $courseCode = $_course['code'];
230
                    $specific_fields = get_specific_field_list();
231
                    $ic_slide = new IndexableChunk();
232
233
                    // Add all terms to db.
234
                    $all_specific_terms = '';
235
                    foreach ($specific_fields as $specific_field) {
236
                        if (isset($_REQUEST[$specific_field['code']])) {
237
                            $sterms = trim($_REQUEST[$specific_field['code']]);
238
                            if (!empty($sterms)) {
239
                                $all_specific_terms .= ' '.$sterms;
240
                                $sterms = explode(',', $sterms);
241
                                foreach ($sterms as $sterm) {
242
                                    $ic_slide->addTerm(
243
                                        trim($sterm),
244
                                        $specific_field['code']
245
                                    );
246
                                    add_specific_field_value(
247
                                        $specific_field['id'],
248
                                        $courseCode,
249
                                        TOOL_LINK,
250
                                        $link_id,
251
                                        $sterm
252
                                    );
253
                                }
254
                            }
255
                        }
256
                    }
257
258
                    // Build the chunk to index.
259
                    $ic_slide->addValue('title', $title);
260
                    $ic_slide->addCourseId($courseCode);
261
                    $ic_slide->addToolId(TOOL_LINK);
262
                    $xapian_data = [
263
                        SE_COURSE_ID => $courseCode,
264
                        SE_TOOL_ID => TOOL_LINK,
265
                        SE_DATA => [
266
                            'link_id' => (int) $link_id,
267
                        ],
268
                        SE_USER => (int) api_get_user_id(),
269
                    ];
270
                    $ic_slide->xapian_data = serialize($xapian_data);
271
                    $description = $all_specific_terms.' '.$description;
272
                    $ic_slide->addValue('content', $description);
273
274
                    // Add category name if set.
275
                    if (isset($selectcategory) && $selectcategory > 0) {
276
                        $table_link_category = Database::get_course_table(
277
                            TABLE_LINK_CATEGORY
278
                        );
279
                        $sql_cat = 'SELECT * FROM %s WHERE id=%d AND c_id = %d LIMIT 1';
280
                        $sql_cat = sprintf(
281
                            $sql_cat,
282
                            $table_link_category,
283
                            (int) $selectcategory,
284
                            $course_int_id
285
                        );
286
                        $result = Database:: query($sql_cat);
287
                        if (Database:: num_rows($result) == 1) {
288
                            $row = Database:: fetch_array($result);
289
                            $ic_slide->addValue(
290
                                'category',
291
                                $row['category_title']
292
                            );
293
                        }
294
                    }
295
296
                    $di = new ChamiloIndexer();
297
                    isset($_POST['language']) ? $lang = Database:: escape_string(
298
                        $_POST['language']
299
                    ) : $lang = 'english';
300
                    $di->connectDb(null, null, $lang);
301
                    $di->addChunk($ic_slide);
302
303
                    // Index and return search engine document id.
304
                    $did = $di->index();
305
                    if ($did) {
306
                        // Save it to db.
307
                        $tbl_se_ref = Database::get_main_table(
308
                            TABLE_MAIN_SEARCH_ENGINE_REF
309
                        );
310
                        $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
311
                                VALUES (NULL , \'%s\', \'%s\', %s, %s)';
312
                        $sql = sprintf(
313
                            $sql,
314
                            $tbl_se_ref,
315
                            $course_int_id,
316
                            $courseCode,
317
                            TOOL_LINK,
318
                            $link_id,
319
                            $did
320
                        );
321
                        Database:: query($sql);
322
                    }
323
                }
324
                Display::addFlash(Display::return_message(get_lang('LinkAdded')));
325
326
                return $link_id;
327
            }
328
        } elseif ($type == 'category') {
329
            $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
330
331
            $category_title = trim($_POST['category_title']);
332
            $description = trim($_POST['description']);
333
334
            if (empty($category_title)) {
335
                echo Display::return_message(get_lang('GiveCategoryName'), 'error');
336
                $ok = false;
337
            } else {
338
                // Looking for the largest order number for this category.
339
                $result = Database:: query(
340
                    "SELECT MAX(display_order) FROM  $tbl_categories
341
                    WHERE c_id = $course_id "
342
                );
343
                list($orderMax) = Database:: fetch_row($result);
344
                $order = $orderMax + 1;
345
                $order = intval($order);
346
                $session_id = api_get_session_id();
347
348
                $params = [
349
                    'c_id' => $course_id,
350
                    'category_title' => $category_title,
351
                    'description' => $description,
352
                    'display_order' => $order,
353
                    'session_id' => $session_id,
354
                ];
355
                $linkId = Database::insert($tbl_categories, $params);
356
357
                if ($linkId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linkId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
358
                    // iid
359
                    $sql = "UPDATE $tbl_categories SET id = iid WHERE iid = $linkId";
360
                    Database:: query($sql);
361
362
                    // add link_category visibility
363
                    // course ID is taken from context in api_set_default_visibility
364
                    //api_set_default_visibility($linkId, TOOL_LINK_CATEGORY);
365
                    api_item_property_update(
366
                        $_course,
367
                        TOOL_LINK_CATEGORY,
368
                        $linkId,
369
                        'LinkCategoryAdded',
370
                        api_get_user_id()
371
                    );
372
                    api_set_default_visibility($linkId, TOOL_LINK_CATEGORY);
373
                }
374
375
                Display::addFlash(Display::return_message(get_lang('CategoryAdded')));
376
377
                return $linkId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $linkId also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
378
            }
379
        }
380
381
        return $ok;
382
    }
383
384
    /**
385
     * Used to delete a link or a category.
386
     *
387
     * @author Patrick Cool <[email protected]>, Ghent University
388
     *
389
     * @param int    $id
390
     * @param string $type The type of item to delete
391
     *
392
     * @return bool
393
     */
394
    public static function deletelinkcategory($id, $type)
395
    {
396
        $courseInfo = api_get_course_info();
397
        $tbl_link = Database::get_course_table(TABLE_LINK);
398
        $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
399
400
        $course_id = $courseInfo['real_id'];
401
        $id = intval($id);
402
403
        if (empty($id)) {
404
            return false;
405
        }
406
407
        $result = false;
408
        switch ($type) {
409
            case 'link':
410
                // -> Items are no longer physically deleted,
411
                // but the visibility is set to 2 (in item_property).
412
                // This will make a restore function possible for the platform administrator.
413
                $sql = "UPDATE $tbl_link SET on_homepage='0'
414
                        WHERE c_id = $course_id AND id='".$id."'";
415
                Database:: query($sql);
416
417
                api_item_property_update(
418
                    $courseInfo,
419
                    TOOL_LINK,
420
                    $id,
421
                    'delete',
422
                    api_get_user_id()
423
                );
424
                self::delete_link_from_search_engine(api_get_course_id(), $id);
425
                Skill::deleteSkillsFromItem($id, ITEM_TYPE_LINK);
426
                Display::addFlash(Display::return_message(get_lang('LinkDeleted')));
427
                $result = true;
428
                break;
429
            case 'category':
430
                // First we delete the category itself and afterwards all the links of this category.
431
                $sql = "DELETE FROM ".$tbl_categories."
432
                        WHERE c_id = $course_id AND id='".$id."'";
433
                Database:: query($sql);
434
435
                $sql = "DELETE FROM ".$tbl_link."
436
                        WHERE c_id = $course_id AND category_id='".$id."'";
437
                Database:: query($sql);
438
439
                api_item_property_update(
440
                    $courseInfo,
441
                    TOOL_LINK_CATEGORY,
442
                    $id,
443
                    'delete',
444
                    api_get_user_id()
445
                );
446
447
                Display::addFlash(Display::return_message(get_lang('CategoryDeleted')));
448
                $result = true;
449
                break;
450
        }
451
452
        return $result;
453
    }
454
455
    /**
456
     * Removes a link from search engine database.
457
     *
458
     * @param string $course_id Course code
459
     * @param int    $link_id   Document id to delete
460
     */
461
    public static function delete_link_from_search_engine($course_id, $link_id)
462
    {
463
        // Remove from search engine if enabled.
464
        if (api_get_setting('search_enabled') === 'true') {
465
            $tbl_se_ref = Database::get_main_table(
466
                TABLE_MAIN_SEARCH_ENGINE_REF
467
            );
468
            $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
469
            $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
470
            $res = Database:: query($sql);
471
            if (Database:: num_rows($res) > 0) {
472
                $row = Database::fetch_array($res);
473
                $di = new ChamiloIndexer();
474
                $di->remove_document($row['search_did']);
475
            }
476
            $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
477
            $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
478
            Database:: query($sql);
479
480
            // Remove terms from db.
481
            require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
482
            delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
483
        }
484
    }
485
486
    /**
487
     * Get link info.
488
     *
489
     * @param int $id
490
     *
491
     * @return array link info
492
     */
493
    public static function getLinkInfo($id)
494
    {
495
        $tbl_link = Database::get_course_table(TABLE_LINK);
496
        $course_id = api_get_course_int_id();
497
498
        if (empty($id) || empty($course_id)) {
499
            return [];
500
        }
501
502
        $sql = "SELECT * FROM $tbl_link
503
                WHERE c_id = $course_id AND id='".intval($id)."' ";
504
        $result = Database::query($sql);
505
        $data = [];
506
        if (Database::num_rows($result)) {
507
            $data = Database::fetch_array($result);
508
        }
509
510
        return $data;
511
    }
512
513
    /**
514
     * @param int   $id
515
     * @param array $values
516
     */
517
    public static function editLink($id, $values = [])
518
    {
519
        $tbl_link = Database::get_course_table(TABLE_LINK);
520
        $_course = api_get_course_info();
521
        $course_id = $_course['real_id'];
522
        $id = intval($id);
523
524
        $values['url'] = trim($values['url']);
525
        $values['title'] = trim($values['title']);
526
        $values['description'] = trim($values['description']);
527
        $values['target'] = empty($values['target']) ? '_self' : $values['target'];
528
        $values['on_homepage'] = isset($values['on_homepage']) ? $values['on_homepage'] : '';
529
530
        $categoryId = intval($values['category_id']);
531
532
        // We ensure URL to be absolute.
533
        if (strpos($values['url'], '://') === false) {
534
            $values['url'] = 'http://'.$_POST['url'];
535
        }
536
537
        // If the title is empty, we use the URL as title.
538
        if ($values['title'] == '') {
539
            $values['title'] = $values['url'];
540
        }
541
542
        // If the URL is invalid, an error occurs.
543
        if (!api_valid_url($values['url'], true)) {
544
            Display::addFlash(
545
                Display::return_message(get_lang('GiveURL'), 'error')
546
            );
547
548
            return false;
549
        }
550
551
        if (empty($id) || empty($course_id)) {
552
            return false;
553
        }
554
555
        // Finding the old category_id.
556
        $sql = "SELECT * FROM $tbl_link
557
                WHERE c_id = $course_id AND id='".$id."'";
558
        $result = Database:: query($sql);
559
        $row = Database:: fetch_array($result);
560
        $category_id = $row['category_id'];
561
562
        if ($category_id != $values['category_id']) {
563
            $sql = "SELECT MAX(display_order)
564
                    FROM $tbl_link 
565
                    WHERE
566
                        c_id = $course_id AND
567
                        category_id='".intval($values['category_id'])."'";
568
            $result = Database:: query($sql);
569
            list($max_display_order) = Database:: fetch_row($result);
570
            $max_display_order++;
571
        } else {
572
            $max_display_order = $row['display_order'];
573
        }
574
        $params = [
575
            'url' => $values['url'],
576
            'title' => $values['title'],
577
            'description' => $values['description'],
578
            'category_id' => $values['category_id'],
579
            'display_order' => $max_display_order,
580
            'on_homepage' => $values['on_homepage'],
581
            'target' => $values['target'],
582
            'category_id' => $values['category_id'],
583
        ];
584
585
        Database::update(
586
            $tbl_link,
587
            $params,
588
            ['c_id = ? AND id = ?' => [$course_id, $id]]
589
        );
590
591
        // Update search enchine and its values table if enabled.
592
        if (api_get_setting('search_enabled') == 'true') {
593
            $course_int_id = api_get_course_int_id();
594
            $course_id = api_get_course_id();
595
            $link_title = Database:: escape_string($values['title']);
596
            $link_description = Database:: escape_string($values['description']);
597
598
            // Actually, it consists on delete terms from db,
599
            // insert new ones, create a new search engine document, and remove the old one.
600
            // Get search_did.
601
            $tbl_se_ref = Database::get_main_table(
602
                TABLE_MAIN_SEARCH_ENGINE_REF
603
            );
604
            $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
605
            $sql = sprintf(
606
                $sql,
607
                $tbl_se_ref,
608
                $course_id,
609
                TOOL_LINK,
610
                $id
611
            );
612
            $res = Database:: query($sql);
613
614
            if (Database:: num_rows($res) > 0) {
615
                require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
616
617
                $se_ref = Database:: fetch_array($res);
618
                $specific_fields = get_specific_field_list();
619
                $ic_slide = new IndexableChunk();
620
621
                $all_specific_terms = '';
622
                foreach ($specific_fields as $specific_field) {
623
                    delete_all_specific_field_value(
624
                        $course_id,
625
                        $specific_field['id'],
626
                        TOOL_LINK,
627
                        $id
628
                    );
629
                    if (isset($_REQUEST[$specific_field['code']])) {
630
                        $sterms = trim(
631
                            $_REQUEST[$specific_field['code']]
632
                        );
633
                        if (!empty($sterms)) {
634
                            $all_specific_terms .= ' '.$sterms;
635
                            $sterms = explode(',', $sterms);
636
                            foreach ($sterms as $sterm) {
637
                                $ic_slide->addTerm(
638
                                    trim($sterm),
639
                                    $specific_field['code']
640
                                );
641
                                add_specific_field_value(
642
                                    $specific_field['id'],
643
                                    $course_id,
644
                                    TOOL_LINK,
645
                                    $id,
646
                                    $sterm
647
                                );
648
                            }
649
                        }
650
                    }
651
                }
652
653
                // Build the chunk to index.
654
                $ic_slide->addValue("title", $link_title);
655
                $ic_slide->addCourseId($course_id);
656
                $ic_slide->addToolId(TOOL_LINK);
657
                $xapian_data = [
658
                    SE_COURSE_ID => $course_id,
659
                    SE_TOOL_ID => TOOL_LINK,
660
                    SE_DATA => [
661
                        'link_id' => (int) $id,
662
                    ],
663
                    SE_USER => (int) api_get_user_id(),
664
                ];
665
                $ic_slide->xapian_data = serialize($xapian_data);
666
                $link_description = $all_specific_terms.' '.$link_description;
667
                $ic_slide->addValue('content', $link_description);
668
669
                // Add category name if set.
670
                if (isset($categoryId) && $categoryId > 0) {
671
                    $table_link_category = Database::get_course_table(
672
                        TABLE_LINK_CATEGORY
673
                    );
674
                    $sql_cat = 'SELECT * FROM %s WHERE id=%d and c_id = %d LIMIT 1';
675
                    $sql_cat = sprintf(
676
                        $sql_cat,
677
                        $table_link_category,
678
                        $categoryId,
679
                        $course_int_id
680
                    );
681
                    $result = Database:: query($sql_cat);
682
                    if (Database:: num_rows($result) == 1) {
683
                        $row = Database:: fetch_array($result);
684
                        $ic_slide->addValue(
685
                            'category',
686
                            $row['category_title']
687
                        );
688
                    }
689
                }
690
691
                $di = new ChamiloIndexer();
692
                isset($_POST['language']) ? $lang = Database:: escape_string($_POST['language']) : $lang = 'english';
693
                $di->connectDb(null, null, $lang);
694
                $di->remove_document($se_ref['search_did']);
695
                $di->addChunk($ic_slide);
696
697
                // Index and return search engine document id.
698
                $did = $di->index();
699
                if ($did) {
700
                    // Save it to db.
701
                    $sql = 'DELETE FROM %s
702
                            WHERE course_code=\'%s\'
703
                            AND tool_id=\'%s\'
704
                            AND ref_id_high_level=\'%s\'';
705
                    $sql = sprintf(
706
                        $sql,
707
                        $tbl_se_ref,
708
                        $course_id,
709
                        TOOL_LINK,
710
                        $id
711
                    );
712
                    Database:: query($sql);
713
                    $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
714
                            VALUES (NULL , \'%s\', \'%s\', %s, %s)';
715
                    $sql = sprintf(
716
                        $sql,
717
                        $tbl_se_ref,
718
                        $course_int_id,
719
                        $course_id,
720
                        TOOL_LINK,
721
                        $id,
722
                        $did
723
                    );
724
                    Database:: query($sql);
725
                }
726
            }
727
        }
728
729
        // "WHAT'S NEW" notification: update table last_toolEdit.
730
        api_item_property_update(
731
            $_course,
732
            TOOL_LINK,
733
            $id,
734
            'LinkUpdated',
735
            api_get_user_id()
736
        );
737
        Display::addFlash(Display::return_message(get_lang('LinkModded')));
738
    }
739
740
    /**
741
     * @param int   $id
742
     * @param array $values
743
     *
744
     * @return bool
745
     */
746
    public static function editCategory($id, $values)
747
    {
748
        $table = Database::get_course_table(TABLE_LINK_CATEGORY);
749
        $course_id = api_get_course_int_id();
750
        $id = intval($id);
751
752
        // This is used to put the modified info of the category-form into the database.
753
        $params = [
754
            'category_title' => $values['category_title'],
755
            'description' => $values['description'],
756
        ];
757
        Database::update(
758
            $table,
759
            $params,
760
            ['c_id = ? AND id = ?' => [$course_id, $id]]
761
        );
762
        Display::addFlash(Display::return_message(get_lang('CategoryModded')));
763
764
        return true;
765
    }
766
767
    /**
768
     * Changes the visibility of a link.
769
     *
770
     * @todo add the changing of the visibility of a course
771
     *
772
     * @author Patrick Cool <[email protected]>, Ghent University
773
     */
774
    public static function change_visibility_link($id, $scope)
775
    {
776
        $_course = api_get_course_info();
777
        $_user = api_get_user_info();
778
        if ($scope == TOOL_LINK) {
779
            api_item_property_update(
780
                $_course,
781
                TOOL_LINK,
782
                $id,
783
                $_GET['action'],
784
                $_user['user_id']
785
            );
786
            Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
787
        } elseif ($scope == TOOL_LINK_CATEGORY) {
788
            api_item_property_update(
789
                $_course,
790
                TOOL_LINK_CATEGORY,
791
                $id,
792
                $_GET['action'],
793
                $_user['user_id']
794
            );
795
            Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
796
        }
797
    }
798
799
    /**
800
     * Generate SQL to select all the links categories in the current course and
801
     * session.
802
     *
803
     * @param int  $courseId
804
     * @param int  $sessionId
805
     * @param bool $withBaseContent
806
     *
807
     * @return array
808
     */
809
    public static function getLinkCategories($courseId, $sessionId, $withBaseContent = true)
810
    {
811
        $tblLinkCategory = Database::get_course_table(TABLE_LINK_CATEGORY);
812
        $tblItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
813
        $courseId = intval($courseId);
814
        $courseInfo = api_get_course_info_by_id($courseId);
815
816
        // Condition for the session.
817
        $sessionCondition = api_get_session_condition(
818
            $sessionId,
819
            true,
820
            $withBaseContent,
821
            'linkcat.session_id'
822
        );
823
824
        // Getting links
825
        $sql = "SELECT *, linkcat.id
826
                FROM $tblLinkCategory linkcat
827
                WHERE
828
                    linkcat.c_id = $courseId
829
                    $sessionCondition
830
                ORDER BY linkcat.display_order DESC";
831
832
        $result = Database::query($sql);
833
        $categories = Database::store_result($result);
834
835
        $sql = "SELECT *, linkcat.id
836
                FROM $tblLinkCategory linkcat
837
                INNER JOIN $tblItemProperty ip
838
                ON (linkcat.id = ip.ref AND linkcat.c_id = ip.c_id)
839
                WHERE
840
                    ip.tool = '".TOOL_LINK_CATEGORY."' AND
841
                    (ip.visibility = '0' OR ip.visibility = '1')
842
                    $sessionCondition AND
843
                    linkcat.c_id = ".$courseId."
844
                ORDER BY linkcat.display_order DESC";
845
846
        $result = Database::query($sql);
847
848
        $categoryInItemProperty = [];
849
        if (Database::num_rows($result)) {
850
            while ($row = Database::fetch_array($result, 'ASSOC')) {
851
                $categoryInItemProperty[$row['id']] = $row;
852
            }
853
        }
854
855
        foreach ($categories as &$category) {
856
            if (!isset($categoryInItemProperty[$category['id']])) {
857
                api_item_property_update(
858
                    $courseInfo,
859
                    TOOL_LINK_CATEGORY,
860
                    $category['id'],
861
                    'LinkCategoryAdded',
862
                    api_get_user_id()
863
                );
864
            }
865
        }
866
867
        $sql = "SELECT DISTINCT linkcat.*, visibility
868
                FROM $tblLinkCategory linkcat
869
                INNER JOIN $tblItemProperty ip
870
                ON (linkcat.id = ip.ref AND linkcat.c_id = ip.c_id)
871
                WHERE
872
                    ip.tool = '".TOOL_LINK_CATEGORY."' AND
873
                    (ip.visibility = '0' OR ip.visibility = '1')
874
                    $sessionCondition AND
875
                    linkcat.c_id = ".$courseId."
876
                ORDER BY linkcat.display_order DESC
877
                ";
878
        $result = Database::query($sql);
879
880
        return Database::store_result($result, 'ASSOC');
881
    }
882
883
    /**
884
     * @param int $categoryId
885
     * @param $courseId
886
     * @param $sessionId
887
     * @param bool $withBaseContent
888
     *
889
     * @return array
890
     */
891
    public static function getLinksPerCategory(
892
        $categoryId,
893
        $courseId,
894
        $sessionId,
895
        $withBaseContent = true
896
    ) {
897
        $tbl_link = Database::get_course_table(TABLE_LINK);
898
        $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
899
        $courseId = (int) $courseId;
900
        $sessionId = (int) $sessionId;
901
        $categoryId = (int) $categoryId;
902
903
        // Condition for the session.
904
        $condition_session = api_get_session_condition(
905
            $sessionId,
906
            true,
907
            false,
908
            'ip.session_id'
909
        );
910
911
        if (!empty($sessionId)) {
912
            $conditionBaseSession = api_get_session_condition(
913
                0,
914
                true,
915
                $withBaseContent,
916
                'ip.session_id'
917
            );
918
            $condition = " AND 
919
                (
920
                    (ip.visibility = '1' $conditionBaseSession) OR
921
                     
922
                    (
923
                        (ip.visibility = '0' OR ip.visibility = '1')
924
                        $condition_session
925
                    )
926
                )
927
            ";
928
        } else {
929
            $condition = api_get_session_condition(
930
                0,
931
                true,
932
                false,
933
                'ip.session_id'
934
            );
935
            $condition .= " AND (ip.visibility = '0' OR ip.visibility = '1') $condition ";
936
        }
937
938
        $sql = "SELECT 
939
                    link.id,
940
                    ip.session_id,
941
                    link.session_id link_session_id,
942
                    url,
943
                    category_id,
944
                    visibility,
945
                    description,
946
                    title,
947
                    target,
948
                    on_homepage
949
                FROM $tbl_link link
950
                INNER JOIN $TABLE_ITEM_PROPERTY ip
951
                ON (link.id = ip.ref AND link.c_id = ip.c_id)
952
                WHERE
953
                    ip.tool = '".TOOL_LINK."' AND
954
                    link.category_id = '".$categoryId."' AND
955
                    link.c_id = $courseId AND
956
                    ip.c_id = $courseId
957
                    $condition
958
                ORDER BY link.display_order ASC, ip.session_id DESC";
959
960
        $result = Database:: query($sql);
961
962
        return Database::store_result($result);
963
    }
964
965
    /**
966
     * Displays all the links of a given category.
967
     *
968
     * @param int  $catid
969
     * @param int  $courseId
970
     * @param int  $session_id
971
     * @param bool $showActionLinks
972
     *
973
     * @return string
974
     *
975
     * @author Julio Montoya
976
     * @author Patrick Cool <[email protected]>, Ghent University
977
     */
978
    public static function showLinksPerCategory($catid, $courseId, $session_id, $showActionLinks = true)
979
    {
980
        global $token;
981
        $_user = api_get_user_info();
982
        $catid = (int) $catid;
983
984
        $links = self::getLinksPerCategory($catid, $courseId, $session_id);
985
        $content = '';
986
        $numberOfLinks = count($links);
987
988
        if (!empty($links)) {
989
            $content .= '<div class="link list-group">';
990
            $i = 1;
991
            $linksAdded = [];
992
            foreach ($links as $myrow) {
993
                $linkId = $myrow['id'];
994
995
                if (in_array($linkId, $linksAdded)) {
996
                    continue;
997
                }
998
999
                $linksAdded[] = $linkId;
1000
                $categoryId = $myrow['category_id'];
1001
1002
                // Validation when belongs to a session.
1003
                $session_img = api_get_session_image(
1004
                    $myrow['link_session_id'],
1005
                    $_user['status']
1006
                );
1007
1008
                $toolbar = '';
1009
                $link_validator = '';
1010
                if (api_is_allowed_to_edit(null, true)) {
1011
                    $toolbar .= Display::toolbarButton(
1012
                        '',
1013
                        'javascript:void(0);',
1014
                        'check-circle',
1015
                        'secondary btn-sm',
1016
                        [
1017
                            'onclick' => "check_url('".$linkId."', '".addslashes($myrow['url'])."');",
1018
                            'title' => get_lang('CheckURL'),
1019
                        ]
1020
                    );
1021
1022
                    $link_validator .= Display::span(
1023
                        '',
1024
                        [
1025
                        'id' => 'url_id_'.$linkId,
1026
                        'class' => 'check-link',
1027
                        ]
1028
                    );
1029
1030
                    if ($session_id == $myrow['link_session_id']) {
1031
                        $url = api_get_self().'?'.api_get_cidreq().'&action=editlink&id='.$linkId;
1032
                        $title = get_lang('Edit');
1033
                        $toolbar .= Display::toolbarButton(
1034
                            '',
1035
                            $url,
1036
                            'pencil-alt',
1037
                            'secondary btn-sm',
1038
                            [
1039
                                'title' => $title,
1040
                            ]
1041
                        );
1042
                    }
1043
1044
                    $urlVisibility = api_get_self().'?'.api_get_cidreq().
1045
                            '&sec_token='.$token.
1046
                            '&id='.$linkId.
1047
                            '&scope=link&category_id='.$categoryId;
1048
1049
                    switch ($myrow['visibility']) {
1050
                        case '1':
1051
                            $urlVisibility .= '&action=invisible';
1052
                            $title = get_lang('MakeInvisible');
1053
                            $toolbar .= Display::toolbarButton(
1054
                                '',
1055
                                $urlVisibility,
1056
                                'eye',
1057
                                'secondary btn-sm',
1058
                                [
1059
                                    'title' => $title,
1060
                                ]
1061
                            );
1062
                            break;
1063
                        case '0':
1064
                            $urlVisibility .= '&action=visible';
1065
                            $title = get_lang('MakeVisible');
1066
                            $toolbar .= Display::toolbarButton(
1067
                                '',
1068
                                $urlVisibility,
1069
                                'eye-slash',
1070
                                'secondary btn-sm',
1071
                                [
1072
                                    'title' => $title,
1073
                                ]
1074
                            );
1075
                            break;
1076
                    }
1077
1078
                    if ($session_id == $myrow['link_session_id']) {
1079
                        $moveLinkParams = [
1080
                            'id' => $linkId,
1081
                            'scope' => 'category',
1082
                            'category_id' => $categoryId,
1083
                            'action' => 'move_link_up',
1084
                        ];
1085
1086
                        $toolbar .= Display::toolbarButton(
1087
                            get_lang('MoveUp'),
1088
                            api_get_self().'?'.api_get_cidreq().'&'.http_build_query($moveLinkParams),
1089
                            'level-up-alt',
1090
                            'secondary',
1091
                            ['class' => 'btn-sm '.($i === 1 ? 'disabled' : '')],
1092
                            false
1093
                        );
1094
1095
                        $moveLinkParams['action'] = 'move_link_down';
1096
                        $toolbar .= Display::toolbarButton(
1097
                            get_lang('MoveDown'),
1098
                            api_get_self().'?'.api_get_cidreq().'&'.http_build_query($moveLinkParams),
1099
                            'level-down-alt',
1100
                            'secondary',
1101
                            ['class' => 'btn-sm '.($i === $numberOfLinks ? 'disabled' : '')],
1102
                            false
1103
                        );
1104
1105
                        $url = api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=deletelink&id='.$linkId.'&category_id='.$categoryId;
1106
                        $event = "javascript: if(!confirm('".get_lang('LinkDelconfirm')."'))return false;";
1107
                        $title = get_lang('Delete');
1108
1109
                        $toolbar .= Display::toolbarButton(
1110
                            '',
1111
                            $url,
1112
                            'trash',
1113
                            'secondary btn-sm',
1114
                            [
1115
                                'onclick' => $event,
1116
                                'title' => $title,
1117
                            ]
1118
                        );
1119
                    }
1120
                }
1121
1122
                $showLink = false;
1123
                $titleClass = '';
1124
                if ($myrow['visibility'] == '1') {
1125
                    $showLink = true;
1126
                } else {
1127
                    if (api_is_allowed_to_edit(null, true)) {
1128
                        $showLink = true;
1129
                        $titleClass = 'text-muted';
1130
                    }
1131
                }
1132
1133
                if ($showLink) {
1134
                    $iconLink = Display::return_icon(
1135
                        'url.png',
1136
                        get_lang('Link'),
1137
                        null,
1138
                        ICON_SIZE_SMALL
1139
                    );
1140
                    $url = api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$linkId.'&link_url='.urlencode($myrow['url']);
1141
                    $content .= '<div class="list-group-item">';
1142
                    if ($showActionLinks) {
1143
                        $content .= '<div class="pull-right"><div class="btn-group">'.$toolbar.'</div></div>';
1144
                    }
1145
                    $content .= '<h4 class="list-group-item-heading">';
1146
                    $content .= $iconLink;
1147
                    $content .= Display::tag(
1148
                        'a',
1149
                        Security::remove_XSS($myrow['title']),
1150
                        [
1151
                            'href' => $url,
1152
                            'target' => $myrow['target'],
1153
                            'class' => $titleClass,
1154
                        ]
1155
                    );
1156
                    $content .= $link_validator;
1157
                    $content .= $session_img;
1158
                    $content .= '</h4>';
1159
                    $content .= '<p class="list-group-item-text">'.$myrow['description'].'</p>';
1160
                    $content .= '</div>';
1161
                }
1162
                $i++;
1163
            }
1164
            $content .= '</div>';
1165
        }
1166
1167
        return $content;
1168
    }
1169
1170
    /**
1171
     * Displays the edit, delete and move icons.
1172
     *
1173
     * @param int   Category ID
1174
     * @param int $currentCategory
1175
     * @param int $countCategories
1176
     *
1177
     * @return string
1178
     *
1179
     * @author Patrick Cool <[email protected]>, Ghent University
1180
     */
1181
    public static function showCategoryAdminTools($category, $currentCategory, $countCategories)
1182
    {
1183
        $categoryId = $category['id'];
1184
        $token = null;
1185
        $tools = '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=editcategory&id='.$categoryId.'&category_id='.$categoryId.'" title='.get_lang('Modify').'">'.
1186
            Display:: return_icon(
1187
                'edit.png',
1188
                get_lang('Modify'),
1189
                [],
1190
                ICON_SIZE_SMALL
1191
            ).'</a>';
1192
1193
        // DISPLAY MOVE UP COMMAND only if it is not the top link.
1194
        if ($currentCategory != 0) {
1195
            $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=up&up='.$categoryId.'&category_id='.$categoryId.'" title="'.get_lang('Up').'">'.
1196
                Display:: return_icon(
1197
                    'up.png',
1198
                    get_lang('Up'),
1199
                    [],
1200
                    ICON_SIZE_SMALL
1201
                ).'</a>';
1202
        } else {
1203
            $tools .= Display:: return_icon(
1204
                'up_na.png',
1205
                get_lang('Up'),
1206
                [],
1207
                ICON_SIZE_SMALL
1208
            ).'</a>';
1209
        }
1210
1211
        // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
1212
        if ($currentCategory < $countCategories - 1) {
1213
            $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=down&down='.$categoryId.'&category_id='.$categoryId.'">'.
1214
                Display:: return_icon(
1215
                    'down.png',
1216
                    get_lang('Down'),
1217
                    [],
1218
                    ICON_SIZE_SMALL
1219
                ).'</a>';
1220
        } else {
1221
            $tools .= Display:: return_icon(
1222
                'down_na.png',
1223
                get_lang('Down'),
1224
                [],
1225
                ICON_SIZE_SMALL
1226
            ).'</a>';
1227
        }
1228
1229
        $tools .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&sec_token='.$token.'&action=deletecategory&id='.$categoryId."&category_id=$categoryId\"
1230
            onclick=\"javascript: if(!confirm('".get_lang('CategoryDelconfirm')."')) return false;\">".
1231
            Display:: return_icon(
1232
                'delete.png',
1233
                get_lang('Delete'),
1234
                [],
1235
                ICON_SIZE_SMALL
1236
            ).'</a>';
1237
1238
        return $tools;
1239
    }
1240
1241
    /**
1242
     * move a link or a linkcategory up or down.
1243
     *
1244
     * @param   int Category ID
1245
     * @param   int Course ID
1246
     * @param   int Session ID
1247
     *
1248
     * @author Patrick Cool <[email protected]>, Ghent University
1249
     *
1250
     * @todo support sessions
1251
     */
1252
    public static function movecatlink($action, $catlinkid, $courseId = null, $sessionId = null)
1253
    {
1254
        $tbl_categories = Database::get_course_table(TABLE_LINK_CATEGORY);
1255
1256
        if (is_null($courseId)) {
1257
            $courseId = api_get_course_int_id();
1258
        }
1259
        $courseId = intval($courseId);
1260
        if (is_null($sessionId)) {
1261
            $sessionId = api_get_session_id();
1262
        }
1263
        $sessionId = intval($sessionId);
1264
        $thiscatlinkId = intval($catlinkid);
1265
1266
        if ($action == 'down') {
1267
            $sortDirection = 'DESC';
1268
        }
1269
1270
        if ($action == 'up') {
1271
            $sortDirection = 'ASC';
1272
        }
1273
1274
        $movetable = $tbl_categories;
1275
1276
        if (!empty($sortDirection)) {
1277
            if (!in_array(trim(strtoupper($sortDirection)), ['ASC', 'DESC'])) {
1278
                $sortDirection = 'ASC';
1279
            }
1280
1281
            $sql = "SELECT id, display_order FROM $movetable
1282
                    WHERE c_id = $courseId
1283
                    ORDER BY display_order $sortDirection";
1284
            $linkresult = Database:: query($sql);
1285
            $thislinkOrder = 1;
1286
            while ($sortrow = Database:: fetch_array($linkresult)) {
1287
                // STEP 2 : FOUND THE NEXT LINK ID AND ORDER, COMMIT SWAP
1288
                // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
1289
                // and we know the next iteration of the while loop is the next one. These should be swapped.
1290
                if (isset($thislinkFound) && $thislinkFound) {
1291
                    $nextlinkId = $sortrow['id'];
1292
                    $nextlinkOrder = $sortrow['display_order'];
1293
1294
                    Database:: query(
1295
                        "UPDATE ".$movetable."
1296
                        SET display_order = '$nextlinkOrder'
1297
                        WHERE c_id = $courseId  AND id =  '$thiscatlinkId'"
1298
                    );
1299
                    Database:: query(
1300
                        "UPDATE ".$movetable."
1301
                        SET display_order = '$thislinkOrder'
1302
                        WHERE c_id = $courseId  AND id =  '$nextlinkId'"
1303
                    );
1304
1305
                    break;
1306
                }
1307
                if ($sortrow['id'] == $thiscatlinkId) {
1308
                    $thislinkOrder = $sortrow['display_order'];
1309
                    $thislinkFound = true;
1310
                }
1311
            }
1312
        }
1313
1314
        Display::addFlash(Display::return_message(get_lang('LinkMoved')));
1315
    }
1316
1317
    /**
1318
     * This function checks if the url is a vimeo link.
1319
     *
1320
     * @author Julio Montoya
1321
     *
1322
     * @version 1.0
1323
     */
1324
    public static function isVimeoLink($url)
1325
    {
1326
        $isLink = strrpos($url, 'vimeo.com');
1327
1328
        return $isLink;
1329
    }
1330
1331
    /**
1332
     * Get vimeo id from URL.
1333
     *
1334
     * @param string $url
1335
     *
1336
     * @return bool|mixed
1337
     */
1338
    public static function getVimeoLinkId($url)
1339
    {
1340
        $possibleUrls = [
1341
            'http://www.vimeo.com/',
1342
            'http://vimeo.com/',
1343
            'https://www.vimeo.com/',
1344
            'https://vimeo.com/',
1345
        ];
1346
        $url = str_replace($possibleUrls, '', $url);
1347
1348
        if (is_numeric($url)) {
1349
            return $url;
1350
        }
1351
1352
        return false;
1353
    }
1354
1355
    /**
1356
     * This function checks if the url is a youtube link.
1357
     *
1358
     * @author Jorge Frisancho
1359
     * @author Julio Montoya - Fixing code
1360
     *
1361
     * @version 1.0
1362
     */
1363
    public static function is_youtube_link($url)
1364
    {
1365
        $is_youtube_link = strrpos($url, 'youtube') || strrpos(
1366
            $url,
1367
            'youtu.be'
1368
        );
1369
1370
        return $is_youtube_link;
1371
    }
1372
1373
    /**
1374
     * This function checks if the url is a PDF File link.
1375
     *
1376
     * @author Jorge Frisancho
1377
     * @author Alex Aragón - Fixing code
1378
     *
1379
     * @version 1.0
1380
     */
1381
    public static function isPdfLink($url)
1382
    {
1383
        $isPdfLink = strrpos(strtolower($url), '.pdf');
1384
1385
        return $isPdfLink;
1386
    }
1387
1388
    /**
1389
     * Get youtube id from an URL.
1390
     *
1391
     * @param string $url
1392
     *
1393
     * @return string
1394
     */
1395
    public static function get_youtube_video_id($url)
1396
    {
1397
        // This is the length of YouTube's video IDs
1398
        $len = 11;
1399
1400
        // The ID string starts after "v=", which is usually right after
1401
        // "youtube.com/watch?" in the URL
1402
        $pos = strpos($url, "v=");
1403
        $id = '';
1404
1405
        //If false try other options
1406
        if ($pos === false) {
1407
            $url_parsed = parse_url($url);
1408
1409
            //Youtube shortener
1410
            //http://youtu.be/ID
1411
            $pos = strpos($url, "youtu.be");
1412
1413
            if ($pos == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $pos of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
1414
                $id = '';
1415
            } else {
1416
                return substr($url_parsed['path'], 1);
1417
            }
1418
1419
            //if empty try the youtube.com/embed/ID
1420
            if (empty($id)) {
1421
                $pos = strpos($url, "embed");
1422
                if ($pos === false) {
1423
                    return '';
1424
                } else {
1425
                    return substr($url_parsed['path'], 7);
1426
                }
1427
            }
1428
        } else {
1429
            // Offset the start location to match the beginning of the ID string
1430
            $pos += 2;
1431
            // Get the ID string and return it
1432
            $id = substr($url, $pos, $len);
1433
1434
            return $id;
1435
        }
1436
    }
1437
1438
    /**
1439
     * @param int    $course_id
1440
     * @param int    $session_id
1441
     * @param int    $categoryId
1442
     * @param string $show
1443
     * @param null   $token
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $token is correct as it would always require null to be passed?
Loading history...
1444
     * @param bool   $showActionLinks
1445
     * @param bool   $forceOpenCategories
1446
     *
1447
     * @return string
1448
     */
1449
    public static function listLinksAndCategories(
1450
        $course_id,
1451
        $session_id,
1452
        $categoryId,
1453
        $show = 'none',
1454
        $token = null,
1455
        $showActionLinks = true,
1456
        $forceOpenCategories = false
1457
    ) {
1458
        $categoryId = (int) $categoryId;
1459
        $content = '';
1460
        $categories = self::getLinkCategories($course_id, $session_id);
1461
        $countCategories = count($categories);
1462
        $linksPerCategory = self::showLinksPerCategory(0, $course_id, $session_id, $showActionLinks);
1463
1464
        if ($showActionLinks) {
1465
            /*	Action Links */
1466
            $content = '<div class="actions">';
1467
            if (api_is_allowed_to_edit(null, true)) {
1468
                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq(
1469
                    ).'&action=addlink&category_id='.$categoryId.'">'.
1470
                    Display::return_icon('new_link.png', get_lang('LinkAdd'), '', ICON_SIZE_MEDIUM).'</a>';
1471
                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq(
1472
                    ).'&action=addcategory&category_id='.$categoryId.'">'.
1473
                    Display::return_icon('new_folder.png', get_lang('CategoryAdd'), '', ICON_SIZE_MEDIUM).'</a>';
1474
            }
1475
1476
            if (!empty($countCategories)) {
1477
                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=none">';
1478
                $content .= Display::return_icon(
1479
                        'forum_listview.png',
1480
                        get_lang('FlatView'),
1481
                        '',
1482
                        ICON_SIZE_MEDIUM
1483
                    ).' </a>';
1484
1485
                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=all">';
1486
                $content .= Display::return_icon(
1487
                        'forum_nestedview.png',
1488
                        get_lang('NestedView'),
1489
                        '',
1490
                        ICON_SIZE_MEDIUM
1491
                    ).'</a>';
1492
            }
1493
1494
            $content .= Display::url(
1495
                Display::return_icon('pdf.png', get_lang('ExportToPdf'), '', ICON_SIZE_MEDIUM),
1496
                api_get_self().'?'.api_get_cidreq().'&action=export'
1497
            );
1498
            $content .= '</div>';
1499
        }
1500
1501
        if (empty($countCategories)) {
1502
            $content .= $linksPerCategory;
1503
        } else {
1504
            if (!empty($linksPerCategory)) {
1505
                $content .= Display::panel($linksPerCategory, get_lang('NoCategory'));
1506
            }
1507
        }
1508
1509
        $counter = 0;
1510
        foreach ($categories as $myrow) {
1511
            // Student don't see invisible categories.
1512
            if (!api_is_allowed_to_edit(null, true)) {
1513
                if ($myrow['visibility'] == 0) {
1514
                    continue;
1515
                }
1516
            }
1517
1518
            // Validation when belongs to a session
1519
            $showChildren = $categoryId == $myrow['id'] || $show === 'all';
1520
            if ($forceOpenCategories) {
1521
                $showChildren = true;
1522
            }
1523
1524
            $strVisibility = '';
1525
            $visibilityClass = null;
1526
            if ($myrow['visibility'] == '1') {
1527
                $strVisibility = '<a href="link.php?'.api_get_cidreq().'&sec_token='.$token.'&action=invisible&id='.$myrow['id'].'&scope='.TOOL_LINK_CATEGORY.'" title="'.get_lang('Hide').'">'.
1528
                    Display::return_icon('visible.png', get_lang('Hide'), [], ICON_SIZE_SMALL).'</a>';
1529
            } elseif ($myrow['visibility'] == '0') {
1530
                $visibilityClass = 'text-muted';
1531
                $strVisibility = ' <a href="link.php?'.api_get_cidreq().'&sec_token='.$token.'&action=visible&id='.$myrow['id'].'&scope='.TOOL_LINK_CATEGORY.'" title="'.get_lang('Show').'">'.
1532
                    Display::return_icon('invisible.png', get_lang('Show'), [], ICON_SIZE_SMALL).'</a>';
1533
            }
1534
1535
            $header = '';
1536
            if ($showChildren) {
1537
                $header .= '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id=">';
1538
                $header .= Display::return_icon('forum_nestedview.png');
1539
            } else {
1540
                $header .= '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id='.$myrow['id'].'">';
1541
                $header .= Display::return_icon('forum_listview.png');
1542
            }
1543
            $header .= Security::remove_XSS($myrow['category_title']).'</a>';
1544
1545
            if ($showActionLinks) {
1546
                if (api_is_allowed_to_edit(null, true)) {
1547
                    if ($session_id == $myrow['session_id']) {
1548
                        $header .= $strVisibility;
1549
                        $header .= self::showCategoryAdminTools($myrow, $counter, count($categories));
1550
                    } else {
1551
                        $header .= get_lang('EditionNotAvailableFromSession');
1552
                    }
1553
                }
1554
            }
1555
1556
            $childrenContent = '';
1557
            if ($showChildren) {
1558
                $childrenContent = self::showLinksPerCategory(
1559
                    $myrow['id'],
1560
                    api_get_course_int_id(),
1561
                    api_get_session_id()
1562
                );
1563
            }
1564
1565
            $content .= Display::panel($myrow['description'].$childrenContent, $header);
1566
1567
            $counter++;
1568
        }
1569
1570
        return $content;
1571
    }
1572
1573
    /**
1574
     * @param int    $linkId
1575
     * @param string $action
1576
     * @param null   $token
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $token is correct as it would always require null to be passed?
Loading history...
1577
     *
1578
     * @return FormValidator
1579
     */
1580
    public static function getLinkForm($linkId, $action, $token = null)
1581
    {
1582
        $course_id = api_get_course_int_id();
1583
        $session_id = api_get_session_id();
1584
        $linkInfo = self::getLinkInfo($linkId);
1585
        $categoryId = isset($linkInfo['category_id']) ? $linkInfo['category_id'] : '';
1586
        $lpId = isset($_GET['lp_id']) ? Security::remove_XSS($_GET['lp_id']) : null;
1587
1588
        $form = new FormValidator(
1589
            'link',
1590
            'post',
1591
            api_get_self().'?action='.$action.
1592
            '&category_id='.$categoryId.
1593
            '&'.api_get_cidreq().
1594
            '&id='.$linkId.
1595
            '&sec_token='.$token
1596
        );
1597
1598
        if ($action == 'addlink') {
1599
            $form->addHeader(get_lang('LinkAdd'));
1600
        } else {
1601
            $form->addHeader(get_lang('LinkMod'));
1602
        }
1603
1604
        $target_link = '_blank';
1605
        $title = '';
1606
        $category = '';
1607
        $onhomepage = '';
1608
        $description = '';
1609
1610
        if (!empty($linkInfo)) {
1611
            $urllink = $linkInfo['url'];
1612
            $title = $linkInfo['title'];
1613
            $description = $linkInfo['description'];
1614
            $category = $linkInfo['category_id'];
1615
            if ($linkInfo['on_homepage'] != 0) {
1616
                $onhomepage = 1;
1617
            }
1618
            $target_link = $linkInfo['target'];
1619
        }
1620
1621
        $form->addHidden('id', $linkId);
1622
        $form->addText('url', 'URL');
1623
        $form->addRule('url', get_lang('GiveURL'), 'url');
1624
        $form->addText('title', get_lang('LinkName'));
1625
        $form->addHtmlEditor('description', get_lang('Description'), true, false, ['ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130']);
1626
1627
        $resultcategories = self::getLinkCategories($course_id, $session_id);
1628
        $options = ['0' => '--'];
1629
        if (!empty($resultcategories)) {
1630
            foreach ($resultcategories as $myrow) {
1631
                $options[$myrow['id']] = $myrow['category_title'];
1632
            }
1633
        }
1634
1635
        $form->addSelect('category_id', get_lang('Category'), $options);
1636
        $form->addCheckBox('on_homepage', null, get_lang('OnHomepage'));
1637
1638
        $targets = [
1639
            '_self' => get_lang('LinkOpenSelf'),
1640
            '_blank' => get_lang('LinkOpenBlank'),
1641
            '_parent' => get_lang('LinkOpenParent'),
1642
            '_top' => get_lang('LinkOpenTop'),
1643
        ];
1644
1645
        $form->addSelect(
1646
            'target',
1647
            [
1648
                get_lang('LinkTarget'),
1649
                get_lang('AddTargetOfLinkOnHomepage'),
1650
            ],
1651
            $targets
1652
        );
1653
1654
        $defaults = [
1655
            'url' => empty($urllink) ? 'http://' : Security::remove_XSS($urllink),
1656
            'title' => Security::remove_XSS($title),
1657
            'category_id' => $category,
1658
            'on_homepage' => $onhomepage,
1659
            'description' => $description,
1660
            'target' => $target_link,
1661
        ];
1662
1663
        if (api_get_setting('search_enabled') == 'true') {
1664
            require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
1665
            $specific_fields = get_specific_field_list();
1666
            $form->addCheckBox('index_document', get_lang('SearchFeatureDoIndexLink'), get_lang('Yes'));
1667
1668
            foreach ($specific_fields as $specific_field) {
1669
                $default_values = '';
1670
                if ($action == 'editlink') {
1671
                    $filter = [
1672
                        'field_id' => $specific_field['id'],
1673
                        'ref_id' => intval($_GET['id']),
1674
                        'tool_id' => '\''.TOOL_LINK.'\'',
1675
                    ];
1676
                    $values = get_specific_field_values_list($filter, ['value']);
1677
                    if (!empty($values)) {
1678
                        $arr_str_values = [];
1679
                        foreach ($values as $value) {
1680
                            $arr_str_values[] = $value['value'];
1681
                        }
1682
                        $default_values = implode(', ', $arr_str_values);
1683
                    }
1684
                }
1685
                $form->addText($specific_field['name'], $specific_field['code']);
1686
                $defaults[$specific_field['name']] = $default_values;
1687
            }
1688
        }
1689
1690
        $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_LINK, $linkId);
1691
        $form->addHidden('lp_id', $lpId);
1692
        $form->addButtonSave(get_lang('SaveLink'), 'submitLink');
1693
        $defaults['skills'] = array_keys($skillList);
1694
        $form->setDefaults($defaults);
1695
1696
        return $form;
1697
    }
1698
1699
    /**
1700
     * @param int    $id
1701
     * @param string $action
1702
     *
1703
     * @return FormValidator
1704
     */
1705
    public static function getCategoryForm($id, $action)
1706
    {
1707
        $id = (int) $id;
1708
        $action = Security::remove_XSS($action);
1709
1710
        $form = new FormValidator(
1711
            'category',
1712
            'post',
1713
            api_get_self().'?action='.$action.'&'.api_get_cidreq()
1714
        );
1715
1716
        $defaults = [];
1717
        if ($action == 'addcategory') {
1718
            $form->addHeader(get_lang('CategoryAdd'));
1719
            $my_cat_title = get_lang('CategoryAdd');
1720
        } else {
1721
            $form->addHeader(get_lang('CategoryMod'));
1722
            $my_cat_title = get_lang('CategoryMod');
1723
            $defaults = self::getCategory($id);
1724
        }
1725
        $form->addHidden('id', $id);
1726
        $form->addText('category_title', get_lang('CategoryName'));
1727
        $form->addHtmlEditor('description', get_lang('Description'), true, false, ['ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130']);
1728
        $form->addButtonSave($my_cat_title, 'submitCategory');
1729
        $form->setDefaults($defaults);
1730
1731
        return $form;
1732
    }
1733
1734
    /**
1735
     * @param int $id
1736
     *
1737
     * @return array
1738
     */
1739
    public static function getCategory($id)
1740
    {
1741
        $table = Database::get_course_table(TABLE_LINK_CATEGORY);
1742
        $id = (int) $id;
1743
        $courseId = api_get_course_int_id();
1744
1745
        if (empty($id) || empty($courseId)) {
1746
            return [];
1747
        }
1748
        $sql = "SELECT * FROM $table 
1749
                WHERE id = $id AND c_id = $courseId";
1750
        $result = Database::query($sql);
1751
        $category = Database::fetch_array($result, 'ASSOC');
1752
1753
        return $category;
1754
    }
1755
1756
    /**
1757
     * Move a link up in its category.
1758
     *
1759
     * @param int $id
1760
     *
1761
     * @return bool
1762
     */
1763
    public static function moveLinkUp($id)
1764
    {
1765
        return self::moveLinkDisplayOrder($id, 'ASC');
1766
    }
1767
1768
    /**
1769
     * Move a link down in its category.
1770
     *
1771
     * @param int $id
1772
     *
1773
     * @return bool
1774
     */
1775
    public static function moveLinkDown($id)
1776
    {
1777
        return self::moveLinkDisplayOrder($id, 'DESC');
1778
    }
1779
1780
    /**
1781
     * @param string $url
1782
     *
1783
     * @return bool
1784
     */
1785
    public static function checkUrl($url)
1786
    {
1787
        // Check if curl is available.
1788
        if (!in_array('curl', get_loaded_extensions())) {
1789
            return false;
1790
        }
1791
1792
        // set URL and other appropriate options
1793
        $defaults = [
1794
            CURLOPT_URL => $url,
1795
            CURLOPT_FOLLOWLOCATION => true, // follow redirects accept youtube.com
1796
            CURLOPT_HEADER => 0,
1797
            CURLOPT_RETURNTRANSFER => true,
1798
            CURLOPT_TIMEOUT => 4,
1799
        ];
1800
1801
        $proxySettings = api_get_configuration_value('proxy_settings');
1802
1803
        if (!empty($proxySettings) &&
1804
            isset($proxySettings['curl_setopt_array'])
1805
        ) {
1806
            $defaults[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY'];
1807
            $defaults[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT'];
1808
        }
1809
1810
        // Create a new cURL resource
1811
        $ch = curl_init();
1812
        curl_setopt_array($ch, $defaults);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1812
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $defaults);
Loading history...
1813
1814
        // grab URL and pass it to the browser
1815
        ob_start();
1816
        $result = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1816
        $result = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
1817
        ob_get_clean();
1818
1819
        // close cURL resource, and free up system resources
1820
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1820
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
1821
1822
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type string which is incompatible with the documented return type boolean.
Loading history...
1823
    }
1824
1825
    /**
1826
     * Move a link inside its category (display_order field).
1827
     *
1828
     * @param int    $id        The link ID
1829
     * @param string $direction The direction to sort the links
1830
     *
1831
     * @return bool
1832
     */
1833
    private static function moveLinkDisplayOrder($id, $direction)
1834
    {
1835
        $em = Database::getManager();
1836
        /** @var CLink $link */
1837
        $link = $em->find('ChamiloCourseBundle:CLink', $id);
1838
1839
        if (!$link) {
0 ignored issues
show
introduced by
$link is of type Chamilo\CourseBundle\Entity\CLink, thus it always evaluated to true.
Loading history...
1840
            return false;
1841
        }
1842
1843
        $compareLinks = $em
1844
            ->getRepository('ChamiloCourseBundle:CLink')
1845
            ->findBy(
1846
                [
1847
                    'cId' => $link->getCId(),
1848
                    'categoryId' => $link->getCategoryId(),
1849
                ],
1850
                ['displayOrder' => $direction]
1851
            );
1852
1853
        /** @var CLink $prevLink */
1854
        $prevLink = null;
1855
1856
        /** @var CLink $compareLink */
1857
        foreach ($compareLinks as $compareLink) {
1858
            if ($compareLink->getId() !== $link->getId()) {
1859
                $prevLink = $compareLink;
1860
1861
                continue;
1862
            }
1863
1864
            if (!$prevLink) {
1865
                return false;
1866
            }
1867
1868
            $newPrevLinkDisplayOrder = $link->getDisplayOrder();
1869
            $newLinkDisplayOrder = $prevLink->getDisplayOrder();
1870
1871
            $link->setDisplayOrder($newLinkDisplayOrder);
1872
            $prevLink->setDisplayOrder($newPrevLinkDisplayOrder);
1873
1874
            $em->merge($prevLink);
1875
            $em->merge($link);
1876
            break;
1877
        }
1878
1879
        $em->flush();
1880
1881
        return true;
1882
    }
1883
}
1884