Link   F
last analyzed

Complexity

Total Complexity 169

Size/Duplication

Total Lines 1900
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 169
eloc 1055
dl 0
loc 1900
rs 1.3799
c 0
b 0
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setCourse() 0 3 1
A getCourse() 0 3 2
A updateLink() 0 27 4
F addlinkcategory() 0 217 22
A save() 0 37 3
A change_visibility_link() 0 22 3
A getCategoryByName() 0 12 1
A getLinksPerCategory() 0 72 2
F editLink() 0 220 20
D getLinkForm() 0 115 14
A showCategoryAdminTools() 0 58 3
A editCategory() 0 19 1
A moveLinkDown() 0 3 1
F listLinksAndCategories() 0 122 18
B getLinkCategories() 0 72 5
A getCategoryForm() 0 27 2
A moveLinkUp() 0 3 1
A getLinkInfo() 0 18 4
A is_youtube_link() 0 8 2
B deletelinkcategory() 0 87 6
A get_youtube_video_id() 0 40 5
A getVimeoLinkId() 0 15 2
A isPdfLink() 0 5 1
D showLinksPerCategory() 0 188 15
A moveLinkDisplayOrder() 0 49 5
B movecatlink() 0 63 11
A isVimeoLink() 0 5 1
A getCategory() 0 15 3
A delete_link_from_search_engine() 0 22 3
B checkUrl() 0 47 7

How to fix   Complexity   

Complex Class

Complex classes like Link often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Link, and based on these observations, apply Extract Interface, too.

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