Test Setup Failed
Push — master ( f71949...6c6bd7 )
by Julito
55:21
created

CourseHome::deleteIcon()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 34
Code Lines 20

Duplication

Lines 10
Ratio 29.41 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 10
nop 1
dl 10
loc 34
rs 4.909
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CTool;
5
use Chamilo\CourseBundle\Entity\CLpCategory;
6
7
/**
8
 * Class CourseHome
9
 */
10
class CourseHome
11
{
12
    /**
13
     * Gets the html content to show in the 3 column view
14
     * @param string $cat
15
     * @param int $userId
16
     * @return string
17
     */
18
    public static function show_tool_3column($cat, $userId = null)
19
    {
20
        $_user = api_get_user_info($userId);
21
22
        $TBL_ACCUEIL = Database::get_course_table(TABLE_TOOL_LIST);
23
        $TABLE_TOOLS = Database::get_main_table(TABLE_MAIN_COURSE_MODULE);
24
25
        $numcols = 3;
26
        $table = new HTML_Table('width="100%"');
27
        $all_tools = array();
28
29
        $course_id = api_get_course_int_id();
30
31
        switch ($cat) {
32 View Code Duplication
            case 'Basic':
33
                $condition_display_tools = ' WHERE a.c_id = '.$course_id.' AND  a.link=t.link AND t.position="basic" ';
34
                if ((api_is_coach() || api_is_course_tutor()) && $_SESSION['studentview'] != 'studentview') {
35
                    $condition_display_tools = ' WHERE a.c_id = '.$course_id.' AND a.link=t.link AND (t.position="basic" OR a.name = "'.TOOL_TRACKING.'") ';
36
                }
37
38
                $sql = "SELECT a.*, t.image img, t.row, t.column  FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
39
                        $condition_display_tools ORDER BY t.row, t.column";
40
                break;
41
            case 'External':
42
                if (api_is_allowed_to_edit()) {
43
                    $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
44
                            WHERE a.c_id = $course_id AND ((a.link=t.link AND t.position='external')
45
                            OR (a.visibility <= 1 AND (a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND a.image=t.image))
46
                            ORDER BY a.id";
47
                } else {
48
                    $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
49
                            WHERE a.c_id = $course_id AND (a.visibility = 1 AND ((a.link=t.link AND t.position='external')
50
                            OR ((a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND a.image=t.image)))
51
                            ORDER BY a.id";
52
                }
53
                break;
54
            case 'courseAdmin':
55
                $sql = "SELECT a.*, t.image img, t.row, t.column  FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
56
                        WHERE a.c_id = $course_id AND admin=1 AND a.link=t.link ORDER BY t.row, t.column";
57
                break;
58
59
            case 'platformAdmin':
60
                $sql = "SELECT *, image img FROM $TBL_ACCUEIL WHERE c_id = $course_id AND visibility = 2 ORDER BY id";
61
        }
62
        $result = Database::query($sql);
63
64
        // Grabbing all the tools from $course_tool_table
65
        while ($tool = Database::fetch_array($result)) {
66
            $all_tools[] = $tool;
67
        }
68
69
        $course_id = api_get_course_int_id();
70
71
        // Grabbing all the links that have the property on_homepage set to 1
72
        if ($cat == 'External') {
73
            $tbl_link = Database::get_course_table(TABLE_LINK);
74
            $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
75
            if (api_is_allowed_to_edit(null, true)) {
76
                $sql_links = "SELECT tl.*, tip.visibility
77
								FROM $tbl_link tl
78
                                LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id
79
                                WHERE 	tl.c_id = $course_id AND
80
                                		tip.c_id = $course_id AND
81
                						tl.on_homepage='1' AND
82
                						tip.visibility != 2";
83
            } else {
84
                $sql_links = "SELECT tl.*, tip.visibility
85
                                    FROM $tbl_link tl
86
                                    LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id
87
                                    WHERE 	tl.c_id = $course_id AND
88
                                			tip.c_id = $course_id AND
89
                							tl.on_homepage='1' AND
90
                							tip.visibility = 1";
91
            }
92
            $result_links = Database::query($sql_links);
93 View Code Duplication
            while ($links_row = Database::fetch_array($result_links)) {
94
                $properties = array();
95
                $properties['name'] = $links_row['title'];
96
                $properties['link'] = $links_row['url'];
97
                $properties['visibility'] = $links_row['visibility'];
98
                $properties['img'] = 'external.gif';
99
                $properties['adminlink'] = api_get_path(WEB_CODE_PATH).'link/link.php?action=editlink&amp;id='.$links_row['id'];
100
                $all_tools[] = $properties;
101
            }
102
        }
103
104
        $cell_number = 0;
105
        // Draw line between basic and external, only if there are entries in External
106
        if ($cat == 'External' && count($all_tools)) {
107
            $table->setCellContents(0, 0, '<hr noshade="noshade" size="1"/>');
108
            $table->updateCellAttributes(0, 0, 'colspan="3"');
109
            $cell_number += $numcols;
110
        }
111
112
        foreach ($all_tools as & $tool) {
113 View Code Duplication
            if ($tool['image'] == 'scormbuilder.gif') {
114
                // check if the published learnpath is visible for student
115
                $published_lp_id = self::get_published_lp_id_from_link($tool['link']);
116
                if (!api_is_allowed_to_edit(null, true) &&
117
                    !learnpath::is_lp_visible_for_student(
118
                        $published_lp_id,
119
                        api_get_user_id(),
120
                        api_get_course_id(),
121
                        api_get_session_id()
122
                    )
123
                ) {
124
                    continue;
125
                }
126
            }
127
128 View Code Duplication
            if (api_get_session_id() != 0 &&
129
                in_array($tool['name'], array('course_maintenance', 'course_setting'))
130
            ) {
131
                continue;
132
            }
133
134
            $cell_content = '';
135
            // The name of the tool
136
            $tool_name = self::translate_tool_name($tool);
137
138
            $link_annex = '';
139
            // The url of the tool
140
            if ($tool['img'] != 'external.gif') {
141
                $tool['link'] = api_get_path(WEB_CODE_PATH).$tool['link'];
142
                $qm_or_amp = strpos($tool['link'], '?') === false ? '?' : '&amp;';
143
                $link_annex = $qm_or_amp.api_get_cidreq();
144
            } else {
145
                // If an external link ends with 'login=', add the actual login...
146
                $pos = strpos($tool['link'], '?login=');
147
                $pos2 = strpos($tool['link'], '&amp;login=');
148
                if ($pos !== false or $pos2 !== false) {
149
                    $link_annex = $_user['username'];
150
                }
151
            }
152
153
            // Setting the actual image url
154
            $tool['img'] = Display::returnIconPath($tool['img']);
155
156
            // VISIBLE
157
            if (($tool['visibility'] ||
158
                ((api_is_coach() || api_is_course_tutor()) && $tool['name'] == TOOL_TRACKING)) ||
159
                $cat == 'courseAdmin' || $cat == 'platformAdmin'
160
            ) {
161
                if (strpos($tool['name'], 'visio_') !== false) {
162
                    $cell_content .= '<a  href="javascript: void(0);" onclick="javascript: window.open(\''.$tool['link'].$link_annex.'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'"><img src="'.$tool['img'].'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
163 View Code Duplication
                } elseif (strpos($tool['name'], 'chat') !== false && api_get_course_setting('allow_open_chat_window')) {
164
                    $cell_content .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.$tool['link'].$link_annex.'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'"><img src="'.$tool['img'].'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
165
                    // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
166
                } else {
167
                    $cell_content .= '<a href="'.$tool['link'].$link_annex.'" target="'.$tool['target'].'"><img src="'.$tool['img'].'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
168
                    // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
169
                }
170
            } else {
171
                // INVISIBLE
172
                if (api_is_allowed_to_edit(null, true)) {
173
                    if (strpos($tool['name'], 'visio_') !== false) {
174
                        $cell_content .= '<a  href="javascript: void(0);" onclick="window.open(\''.$tool['link'].$link_annex.'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'"><img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
175 View Code Duplication
                    } elseif (strpos($tool['name'], 'chat') !== false && api_get_course_setting('allow_open_chat_window')) {
176
                        $cell_content .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.$tool['link'].$link_annex.'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'" class="text-muted"><img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
177
                        // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
178
                    } else {
179
                        $cell_content .= '<a href="'.$tool['link'].$link_annex.'" target="'.$tool['target'].'" class="text-muted">
180
                                            <img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">'.$tool_name.'</a>';
181
                        // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
182
                    }
183
                } else {
184
                    $cell_content .= '<img src="'.str_replace(".gif", "_na.gif", $tool['img']).'" title="'.$tool_name.'" alt="'.$tool_name.'" align="absmiddle" border="0">';
185
                    // don't replace img with display::return_icon because $tool['img'] = api_get_path(WEB_IMG_PATH).$tool['img']
186
                    $cell_content .= '<span class="text-muted">'.$tool_name.'</span>';
187
                }
188
            }
189
190
            $lnk = array();
191
            if (api_is_allowed_to_edit(null, true) &&
192
                $cat != "courseAdmin" &&
193
                !strpos($tool['link'], 'learnpath_handler.php?learnpath_id') &&
194
                !api_is_coach()
195
            ) {
196
                if ($tool['visibility']) {
197
                    $link['name'] = Display::return_icon('remove.gif', get_lang('Deactivate'), array('style' => 'vertical-align: middle;'));
198
                    $link['cmd'] = "hide=yes";
199
                    $lnk[] = $link;
200
                } else {
201
                    $link['name'] = Display::return_icon('add.gif', get_lang('Activate'), array('style' => 'vertical-align: middle;'));
202
                    $link['cmd'] = "restore=yes";
203
                    $lnk[] = $link;
204
                }
205
                if (is_array($lnk)) {
206
                    foreach ($lnk as & $this_lnk) {
207
                        if ($tool['adminlink']) {
208
                            $cell_content .= '<a href="'.$properties['adminlink'].'">'.
209
                                Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
210
                        } else {
211
                            $cell_content .= '<a href="'.api_get_self().'?id='.$tool['id'].'&amp;'.$this_lnk['cmd'].'">'.$this_lnk['name'].'</a>';
212
                        }
213
                    }
214
                }
215
            }
216
            $table->setCellContents($cell_number / $numcols, ($cell_number) % $numcols, $cell_content);
217
            $table->updateCellAttributes($cell_number / $numcols, ($cell_number) % $numcols, 'width="32%" height="42"');
218
            $cell_number++;
219
        }
220
221
        return $table->toHtml();
222
    }
223
224
    /**
225
     * Displays the tools of a certain category.
226
     *
227
     * @return void
228
     * @param string $course_tool_category	contains the category of tools to display:
229
     * "Public", "PublicButHide", "courseAdmin", "claroAdmin"
230
     */
231
    public static function show_tool_2column($course_tool_category)
232
    {
233
        $html = '';
234
        $web_code_path = api_get_path(WEB_CODE_PATH);
235
        $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
236
237
        $course_id = api_get_course_int_id();
238
239
        switch ($course_tool_category) {
240 View Code Duplication
            case TOOL_PUBLIC:
241
                $condition_display_tools = ' WHERE c_id = '.$course_id.' AND visibility = 1 ';
242
                if ((api_is_coach() || api_is_course_tutor()) && $_SESSION['studentview'] != 'studentview') {
243
                    $condition_display_tools = ' WHERE c_id = '.$course_id.' AND (visibility = 1 OR (visibility = 0 AND name = "'.TOOL_TRACKING.'")) ';
244
                }
245
                $result = Database::query("SELECT * FROM $course_tool_table $condition_display_tools ORDER BY id");
246
                $col_link = "##003399";
247
                break;
248
            case TOOL_PUBLIC_BUT_HIDDEN:
249
                $result = Database::query("SELECT * FROM $course_tool_table WHERE c_id = $course_id AND visibility=0 AND admin=0 ORDER BY id");
250
                $col_link = "##808080";
251
                break;
252
            case TOOL_COURSE_ADMIN:
253
                $result = Database::query("SELECT * FROM $course_tool_table WHERE c_id = $course_id AND admin=1 AND visibility != 2 ORDER BY id");
254
                $col_link = "##003399";
255
                break;
256
            case TOOL_PLATFORM_ADMIN:
257
                $result = Database::query("SELECT * FROM $course_tool_table WHERE c_id = $course_id AND visibility = 2  ORDER BY id");
258
                $col_link = "##003399";
259
        }
260
        $i = 0;
261
262
        // Grabbing all the tools from $course_tool_table
263
        while ($temp_row = Database::fetch_array($result)) {
264
            if ($course_tool_category == TOOL_PUBLIC_BUT_HIDDEN && $temp_row['image'] != 'scormbuilder.gif') {
265
                $temp_row['image'] = str_replace('.gif', '_na.gif', $temp_row['image']);
266
            }
267
            $all_tools_list[] = $temp_row;
268
        }
269
270
        // Grabbing all the links that have the property on_homepage set to 1
271
        $course_link_table = Database::get_course_table(TABLE_LINK);
272
        $course_item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
273
274
        switch ($course_tool_category) {
275
            case TOOL_PUBLIC:
276
                $sql_links = "SELECT tl.*, tip.visibility
277
                        FROM $course_link_table tl
278
                        LEFT JOIN $course_item_property_table tip ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id
279
                        WHERE tl.on_homepage='1' AND tip.visibility = 1";
280
                break;
281
            case TOOL_PUBLIC_BUT_HIDDEN:
282
                $sql_links = "SELECT tl.*, tip.visibility
283
                    FROM $course_link_table tl
284
                    LEFT JOIN $course_item_property_table tip ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id
285
                    WHERE tl.on_homepage='1' AND tip.visibility = 0";
286
287
                break;
288
            default:
289
                $sql_links = null;
290
                break;
291
        }
292
        if ($sql_links != null) {
293
            $properties = array();
294
            $result_links = Database::query($sql_links);
295 View Code Duplication
            while ($links_row = Database::fetch_array($result_links)) {
296
                unset($properties);
297
                $properties['name'] = $links_row['title'];
298
                $properties['link'] = $links_row['url'];
299
                $properties['visibility'] = $links_row['visibility'];
300
                $properties['image'] = $course_tool_category == TOOL_PUBLIC_BUT_HIDDEN ? 'external_na.gif' : 'external.gif';
301
                $properties['adminlink'] = api_get_path(WEB_CODE_PATH).'link/link.php?action=editlink&id='.$links_row['id'];
302
                $all_tools_list[] = $properties;
303
            }
304
        }
305
        if (isset($all_tools_list)) {
306
            $lnk = array();
307
            foreach ($all_tools_list as & $tool) {
308 View Code Duplication
                if ($tool['image'] == 'scormbuilder.gif') {
309
                    // check if the published learnpath is visible for student
310
                    $published_lp_id = self::get_published_lp_id_from_link($tool['link']);
311
312
                    if (!api_is_allowed_to_edit(null, true) &&
313
                        !learnpath::is_lp_visible_for_student(
314
                            $published_lp_id,
315
                            api_get_user_id(),
316
                            api_get_course_id(),
317
                            api_get_session_id()
318
                        )
319
                    ) {
320
                        continue;
321
                    }
322
                }
323
324 View Code Duplication
                if (api_get_session_id() != 0 &&
325
                    in_array($tool['name'], array('course_maintenance', 'course_setting'))
326
                ) {
327
                    continue;
328
                }
329
330
                if (!($i % 2)) {
331
                    $html .= "<tr valign=\"top\">";
332
                }
333
334
                // NOTE : Table contains only the image file name, not full path
335 View Code Duplication
                if (stripos($tool['link'], 'http://') === false &&
336
                    stripos($tool['link'], 'https://') === false &&
337
                    stripos($tool['link'], 'ftp://') === false
338
                ) {
339
                    $tool['link'] = $web_code_path.$tool['link'];
340
                }
341
                $class = '';
342
                if ($course_tool_category == TOOL_PUBLIC_BUT_HIDDEN) {
343
                    $class = 'class="text-muted"';
344
                }
345
                $qm_or_amp = strpos($tool['link'], '?') === false ? '?' : '&amp;';
346
347
                $tool['link'] = $tool['link'];
348
                $html .= '<td width="50%" height="30">';
349
350
                if (strpos($tool['name'], 'visio_') !== false) {
351
                    $html .= '<a  '.$class.' href="javascript: void(0);" onclick="javascript: window.open(\''.htmlspecialchars($tool['link']).(($tool['image'] == 'external.gif' || $tool['image'] == 'external_na.gif') ? '' : $qm_or_amp.api_get_cidreq()).'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'">';
352
                } elseif (strpos($tool['name'], 'chat') !== false && api_get_course_setting('allow_open_chat_window')) {
353
                    $html .= '<a href="javascript: void(0);" onclick="javascript: window.open(\''.htmlspecialchars($tool['link']).$qm_or_amp.api_get_cidreq().'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$tool['target'].'" '.$class.'>';
354
                } else {
355
                    $html .= '<a href="'.htmlspecialchars($tool['link']).(($tool['image'] == 'external.gif' || $tool['image'] == 'external_na.gif') ? '' : $qm_or_amp.api_get_cidreq()).'" target="'.$tool['target'].'" '.$class.'>';
356
                }
357
358
                $tool_name = self::translate_tool_name($tool);
359
                $html .= Display::return_icon(
360
                        $tool['image'],
361
                        $tool_name,
362
                        array(),
363
                        null,
364
                        ICON_SIZE_MEDIUM
365
                    ).'&nbsp;'.$tool_name.'</a>';
366
367
                // This part displays the links to hide or remove a tool.
368
                // These links are only visible by the course manager.
369
                unset($lnk);
370
                if (api_is_allowed_to_edit(null, true) && !api_is_coach()) {
371 View Code Duplication
                    if ($tool['visibility'] == '1' || $tool['name'] == TOOL_TRACKING) {
372
                        $link['name'] = Display::returnFontAwesomeIcon('minus');
373
                        $link['title'] = get_lang('Deactivate');
374
                        $link['cmd'] = 'hide=yes';
375
                        $lnk[] = $link;
376
                    }
377
378 View Code Duplication
                    if ($course_tool_category == TOOL_PUBLIC_BUT_HIDDEN) {
379
                        //$link['name'] = Display::return_icon('add.gif', get_lang('Activate'));
380
                        $link['name'] = Display::returnFontAwesomeIcon('plus');
381
                        $link['title'] = get_lang('Activate');
382
                        $link['cmd'] = 'restore=yes';
383
                        $lnk[] = $link;
384
385
                        if ($tool['added_tool'] == 1) {
386
                            //$link['name'] = Display::return_icon('delete.gif', get_lang('Remove'));
387
                            $link['name'] = Display::returnFontAwesomeIcon('trash');
388
                            $link['title'] = get_lang('Remove');
389
                            $link['cmd'] = 'remove=yes';
390
                            $lnk[] = $link;
391
                        }
392
                    }
393
                    if (isset($tool['adminlink'])) {
394
                        $html .= '<a href="'.$tool['adminlink'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
395
                    }
396
                }
397
                if (api_is_platform_admin() && !api_is_coach()) {
398 View Code Duplication
                    if ($tool['visibility'] == 2) {
399
                        $link['name'] = Display::returnFontAwesomeIcon('undo');
400
                        $link['title'] = get_lang('Activate');
401
                        $link['cmd'] = 'hide=yes';
402
                        $lnk[] = $link;
403
404
                        if ($tool['added_tool'] == 1) {
405
                            $link['name'] = get_lang('Delete');
406
                            $link['cmd'] = 'askDelete=yes';
407
                            $lnk[] = $link;
408
                        }
409
                    }
410 View Code Duplication
                    if ($tool['visibility'] == 0 && $tool['added_tool'] == 0) {
411
                        $link['name'] = Display::returnFontAwesomeIcon('trash');
412
                        $link['title'] = get_lang('Remove');
413
                        $link['cmd'] = 'remove=yes';
414
                        $lnk[] = $link;
415
                    }
416
                }
417
                if (is_array($lnk)) {
418
                    $html .= '<div class="pull-right">';
419
                    $html .= '<div class="btn-options">';
420
                    $html .= '<div class="btn-group btn-group-sm" role="group">';
421 View Code Duplication
                    foreach ($lnk as & $this_link) {
422
                        if (!isset($tool['adminlink'])) {
423
                            $html .= '<a class="btn btn-default" title='.$this_link['title'].' href="'.api_get_self().'?'.api_get_cidreq().'&amp;id='.$tool['id'].'&amp;'.$this_link['cmd'].'">'.$this_link['name'].'</a>';
424
                        }
425
                    }
426
                    $html .= '</div>';
427
                    $html .= '</div>';
428
                    $html .= '</div>';
429
                }
430
                $html .= "</td>";
431
432
                if ($i % 2) {
433
                    $html .= "</tr>";
434
                }
435
436
                $i++;
437
            }
438
        }
439
440
        if ($i % 2) {
441
            $html .= "<td width=\"50%\">&nbsp;</td></tr>";
442
        }
443
444
        return $html;
445
    }
446
447
    /**
448
     * Gets the tools of a certain category. Returns an array expected
449
     * by show_tools_category()
450
     * @param string $course_tool_category contains the category of tools to
451
     * display: "toolauthoring", "toolinteraction", "tooladmin", "tooladminplatform", "toolplugin"
452
     * @param int $courseId Optional
453
     * @param int $sessionId Optional
454
     * @return array
455
     */
456
    public static function get_tools_category($course_tool_category, $courseId = 0, $sessionId = 0)
457
    {
458
        $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
459
        $is_platform_admin = api_is_platform_admin();
460
        $all_tools_list = array();
461
462
        // Condition for the session
463
        $session_id = $sessionId ?: api_get_session_id();
464
        $course_id = $courseId ?: api_get_course_int_id();
465
        $userId = api_get_user_id();
466
        $user = api_get_user_entity($userId);
467
        $condition_session = api_get_session_condition(
468
            $session_id,
469
            true,
470
            true,
471
            't.session_id'
472
        );
473
474
        switch ($course_tool_category) {
475
            case TOOL_STUDENT_VIEW:
476
                $conditions = ' WHERE visibility = 1 AND (category = "authoring" OR category = "interaction" OR category = "plugin") ';
477
                if ((api_is_coach() || api_is_course_tutor()) && $_SESSION['studentview'] != 'studentview') {
478
                    $conditions = ' WHERE (visibility = 1 AND (category = "authoring" OR category = "interaction" OR category = "plugin") OR (name = "'.TOOL_TRACKING.'") )   ';
479
                }
480
                $sql = "SELECT *
481
                        FROM $course_tool_table t
482
                        $conditions AND
483
                        c_id = $course_id $condition_session
484
                        ORDER BY id";
485
                $result = Database::query($sql);
486
                break;
487
            case TOOL_AUTHORING:
488
                $sql = "SELECT * FROM $course_tool_table t
489
                        WHERE category = 'authoring' AND c_id = $course_id $condition_session
490
                        ORDER BY id";
491
                $result = Database::query($sql);
492
                break;
493
            case TOOL_INTERACTION:
494
                $sql = "SELECT * FROM $course_tool_table t
495
                        WHERE category = 'interaction' AND c_id = $course_id $condition_session
496
                        ORDER BY id";
497
                $result = Database::query($sql);
498
                break;
499
            case TOOL_ADMIN_VISIBLE:
500
                $sql = "SELECT * FROM $course_tool_table t
501
                        WHERE category = 'admin' AND visibility ='1' AND c_id = $course_id $condition_session
502
                        ORDER BY id";
503
                $result = Database::query($sql);
504
                break;
505
            case TOOL_ADMIN_PLATFORM:
506
                $sql = "SELECT * FROM $course_tool_table t
507
                        WHERE category = 'admin' AND c_id = $course_id $condition_session
508
                        ORDER BY id";
509
                $result = Database::query($sql);
510
                break;
511
            case TOOL_DRH:
512
                $sql = "SELECT * FROM $course_tool_table t
513
                        WHERE name IN ('tracking') AND c_id = $course_id $condition_session
514
                        ORDER BY id";
515
                $result = Database::query($sql);
516
                break;
517
            case TOOL_COURSE_PLUGIN:
518
                //Other queries recover id, name, link, image, visibility, admin, address, added_tool, target, category and session_id
519
                // but plugins are not present in the tool table, only globally and inside the course_settings table once configured
520
                $sql = "SELECT * FROM $course_tool_table t
521
                        WHERE category = 'plugin' AND name <> 'courseblock' AND c_id = $course_id $condition_session
522
                        ORDER BY id";
523
                $result = Database::query($sql);
524
                break;
525
        }
526
527
        //Get the list of hidden tools - this might imply performance slowdowns
528
        // if the course homepage is loaded many times, so the list of hidden
529
        // tools might benefit from a shared memory storage later on
530
        $list = api_get_settings('Tools', 'list', api_get_current_access_url_id());
531
        $hide_list = array();
532
        $check = false;
533
534
        foreach ($list as $line) {
535
            // Admin can see all tools even if the course_hide_tools configuration is set
536
            if ($is_platform_admin) {
537
                continue;
538
            }
539
            if ($line['variable'] == 'course_hide_tools' and $line['selected_value'] == 'true') {
540
                $hide_list[] = $line['subkey'];
541
                $check = true;
542
            }
543
        }
544
545
        $allowEditionInSession = api_get_configuration_value('allow_edit_tool_visibility_in_session');
546
547
        while ($temp_row = Database::fetch_assoc($result)) {
548
            $add = false;
549
            if ($check) {
550
                if (!in_array($temp_row['name'], $hide_list)) {
551
                    $add = true;
552
                }
553
            } else {
554
                $add = true;
555
            }
556
557
            if ($allowEditionInSession && !empty($session_id)) {
558
                // Checking if exist row in session
559
                $criteria = [
560
                    'cId' => $course_id,
561
                    'name' => $temp_row['name'],
562
                    'sessionId' => $session_id,
563
                ];
564
                /** @var CTool $tool */
565
                $toolObj = Database::getManager()->getRepository('ChamiloCourseBundle:CTool')->findOneBy($criteria);
566
                if ($toolObj) {
567
                    if ($toolObj->getVisibility() == 0) {
568
                        continue;
569
                    }
570
                }
571
            }
572
573
            if ($temp_row['image'] == 'scormbuilder.gif') {
574
                $lp_id = self::get_published_lp_id_from_link($temp_row['link']);
575
                $lp = new learnpath(
576
                    api_get_course_id(),
577
                    $lp_id,
578
                    $userId
579
                );
580
                $path = $lp->get_preview_image_path(ICON_SIZE_BIG);
581
                $add = learnpath::is_lp_visible_for_student(
582
                    $lp_id,
583
                    $userId,
584
                    api_get_course_id(),
585
                    api_get_session_id()
586
                );
587
                if ($path) {
588
                    $temp_row['custom_image'] = $path;
589
                }
590
            }
591
592
            if ($temp_row['image'] === 'lp_category.gif') {
593
                $lpCategory = self::getPublishedLpCategoryFromLink(
594
                    $temp_row['link']
595
                );
596
                $add = learnpath::categoryIsVisibleForStudent(
597
                    $lpCategory,
598
                    $user
599
                );
600
            }
601
602
            if ($add) {
603
                $all_tools_list[] = $temp_row;
604
            }
605
        }
606
607
        // Grabbing all the links that have the property on_homepage set to 1
608
        $course_link_table = Database::get_course_table(TABLE_LINK);
609
        $course_item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
610
        $condition_session = api_get_session_condition($session_id, true, true, 'tip.session_id');
611
612
        switch ($course_tool_category) {
613
            case TOOL_AUTHORING:
614
                $sql_links = "SELECT tl.*, tip.visibility
615
                    FROM $course_link_table tl
616
                    LEFT JOIN $course_item_property_table tip
617
                    ON tip.tool='link' AND tip.ref=tl.id
618
                    WHERE
619
                        tl.c_id = $course_id AND
620
                        tip.c_id = $course_id AND
621
                        tl.on_homepage='1' $condition_session";
622
                break;
623
            case TOOL_INTERACTION:
624
                $sql_links = null;
625
                /*
626
                  $sql_links = "SELECT tl.*, tip.visibility
627
                  FROM $course_link_table tl
628
                  LEFT JOIN $course_item_property_table tip ON tip.tool='link' AND tip.ref=tl.id
629
                  WHERE tl.on_homepage='1' ";
630
                 */
631
                break;
632
            case TOOL_STUDENT_VIEW:
633
                $sql_links = "SELECT tl.*, tip.visibility
634
                    FROM $course_link_table tl
635
                    LEFT JOIN $course_item_property_table tip
636
                    ON tip.tool='link' AND tip.ref=tl.id
637
                    WHERE
638
                        tl.c_id 		= $course_id AND
639
                        tip.c_id 		= $course_id AND
640
                        tl.on_homepage	='1' $condition_session";
641
                break;
642
            case TOOL_ADMIN:
643
                $sql_links = "SELECT tl.*, tip.visibility
644
                    FROM $course_link_table tl
645
                    LEFT JOIN $course_item_property_table tip
646
                    ON tip.tool='link' AND tip.ref=tl.id
647
                    WHERE
648
                        tl.c_id = $course_id AND
649
                        tip.c_id = $course_id AND
650
                        tl.on_homepage='1' $condition_session";
651
                break;
652
            default:
653
                $sql_links = null;
654
                break;
655
        }
656
657
        // Edited by Kevin Van Den Haute ([email protected]) for integrating Smartblogs
658
        if ($sql_links != null) {
659
            $result_links = Database::query($sql_links);
660
661
            if (Database::num_rows($result_links) > 0) {
662
                while ($links_row = Database::fetch_array($result_links, 'ASSOC')) {
663
                    $properties = array();
664
                    $properties['name'] = $links_row['title'];
665
                    $properties['session_id'] = $links_row['session_id'];
666
                    $properties['link'] = $links_row['url'];
667
                    $properties['visibility'] = $links_row['visibility'];
668
                    $properties['image'] = $links_row['visibility'] == '0' ? 'file_html.png' : 'file_html.png';
669
                    $properties['adminlink'] = api_get_path(WEB_CODE_PATH).'link/link.php?action=editlink&id='.$links_row['id'];
670
                    $properties['target'] = $links_row['target'];
671
                    $tmp_all_tools_list[] = $properties;
672
                }
673
            }
674
        }
675
676
        if (isset($tmp_all_tools_list)) {
677
            foreach ($tmp_all_tools_list as $tool) {
678
                if ($tool['image'] == 'blog.gif') {
679
                    // Init
680
                    $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
681
682
                    // Get blog id
683
                    $blog_id = substr($tool['link'], strrpos($tool['link'], '=') + 1, strlen($tool['link']));
684
685
                    // Get blog members
686
                    if ($is_platform_admin) {
687
                        $sql_blogs = "SELECT * FROM $tbl_blogs_rel_user blogs_rel_user
688
                                      WHERE blog_id =".$blog_id;
689
                    } else {
690
                        $sql_blogs = "SELECT * FROM $tbl_blogs_rel_user blogs_rel_user
691
                                      WHERE blog_id =".$blog_id." AND user_id = ".$userId;
692
                    }
693
                    $result_blogs = Database::query($sql_blogs);
694
695
                    if (Database::num_rows($result_blogs) > 0) {
696
                        $all_tools_list[] = $tool;
697
                    }
698
                } else {
699
                    $all_tools_list[] = $tool;
700
                }
701
            }
702
        }
703
704
        return $all_tools_list;
705
    }
706
707
    /**
708
     * Displays the tools of a certain category.
709
     * @param $urlGenerator
710
     * @param array $all_tools_list List of tools as returned by get_tools_category()
711
     * @param bool  $rows
712
     *
713
     * @return string
714
     */
715
    public static function show_tools_category($urlGenerator, $all_tools_list, $rows = false)
716
    {
717
        $_user = api_get_user_info();
718
        $web_code_path = api_get_path(WEB_CODE_PATH);
719
        $session_id = api_get_session_id();
720
        $is_platform_admin = api_is_platform_admin();
721
        $theme = api_get_setting('homepage_view');
722
723
        if ($theme === 'vertical_activity') {
724
            //ordering by get_lang name
725
            $order_tool_list = array();
726
            if (is_array($all_tools_list) && count($all_tools_list) > 0) {
727
                foreach ($all_tools_list as $key => $new_tool) {
728
                    $tool_name = self::translate_tool_name($new_tool);
729
                    $order_tool_list [$key] = $tool_name;
730
                }
731
                natsort($order_tool_list);
732
                $my_temp_tool_array = array();
733
                foreach ($order_tool_list as $key => $new_tool) {
734
                    $my_temp_tool_array[] = $all_tools_list[$key];
735
                }
736
                $all_tools_list = $my_temp_tool_array;
737
            } else {
738
                $all_tools_list = array();
739
            }
740
        }
741
        $web_code_path = api_get_path(WEB_CODE_PATH);
742
        $session_id = api_get_session_id();
743
        $is_platform_admin = api_is_platform_admin();
744
        $allowEditionInSession = api_get_configuration_value('allow_edit_tool_visibility_in_session');
745
746
        if ($session_id == 0) {
747
            $is_allowed_to_edit = api_is_allowed_to_edit(null, true) && api_is_course_admin();
748
        } else {
749
            $is_allowed_to_edit = api_is_allowed_to_edit(null, true) && !api_is_coach();
750
            if ($allowEditionInSession) {
751
                $is_allowed_to_edit = api_is_allowed_to_edit(null, true) && api_is_coach($session_id, api_get_course_int_id());
752
            }
753
        }
754
755
        $i = 0;
756
        $items = array();
757
        $app_plugin = new AppPlugin();
758
759
        $toolChain = \Chamilo\CoreBundle\Framework\Container::getToolChain();
760
761
        if (isset($all_tools_list)) {
762
            $lnk = '';
763
764
            foreach ($all_tools_list as & $tool) {
765
                $item = array();
766
                $studentview = false;
767
768
                // Using tool chain to load links instead of loading link from the database.
769
                $toolObject = $toolChain->getToolFromName($tool['name']);
770
                if ($toolObject) {
771
                    $tool['link'] = $toolObject->getLink();
772
                }
773
774
                $tool['original_link'] = $tool['link'];
775
776
                if ($tool['image'] == 'scormbuilder.gif') {
777
                    // check if the published learnpath is visible for student
778
                    $published_lp_id = self::get_published_lp_id_from_link($tool['link']);
779
                    if (api_is_allowed_to_edit(null, true)) {
780
                        $studentview = true;
781
                    }
782
                    if (!api_is_allowed_to_edit(null, true) &&
783
                        !learnpath::is_lp_visible_for_student(
784
                            $published_lp_id,
785
                            api_get_user_id(),
786
                            api_get_course_id(),
787
                            api_get_session_id()
788
                        )
789
                    ) {
790
                        continue;
791
                    }
792
                }
793
794
                if ($session_id != 0 && in_array($tool['name'], array('course_setting'))) {
795
                    continue;
796
                }
797
798
                // This part displays the links to hide or remove a tool.
799
                // These links are only visible by the course manager.
800
                unset($lnk);
801
802
                $item['extra'] = null;
803
                $toolAdmin = isset($tool['admin']) ? $tool['admin'] : '';
804
805
                if ($is_allowed_to_edit) {
806
                    if (empty($session_id)) {
807
                        if (isset($tool['id'])) {
808 View Code Duplication
                            if ($tool['visibility'] == '1' && $toolAdmin != '1') {
809
                                $link['name'] = Display::return_icon(
810
                                    'visible.png',
811
                                    get_lang('Deactivate'),
812
                                    array('id' => 'linktool_'.$tool['id']),
813
                                    ICON_SIZE_SMALL,
814
                                    false
815
                                );
816
                                $link['cmd'] = 'hide=yes';
817
                                $lnk[] = $link;
818
                            }
819 View Code Duplication
                            if ($tool['visibility'] == '0' && $toolAdmin != '1') {
820
                                $link['name'] = Display::return_icon(
821
                                    'invisible.png',
822
                                    get_lang('Activate'),
823
                                    array('id' => 'linktool_'.$tool['id']),
824
                                    ICON_SIZE_SMALL,
825
                                    false
826
                                );
827
                                $link['cmd'] = 'restore=yes';
828
                                $lnk[] = $link;
829
                            }
830
                        }
831
                    } elseif ($allowEditionInSession) {
832
                        $criteria = [
833
                            'cId' => api_get_course_int_id(),
834
                            'name' => $tool['name'],
835
                            'sessionId' => $session_id
836
                        ];
837
                        /** @var CTool $tool */
838
                        $toolObj = Database::getManager()->getRepository('ChamiloCourseBundle:CTool')->findOneBy($criteria);
839
                        if ($toolObj) {
840
                            $visibility = $toolObj->getVisibility();
841
                            switch ($visibility) {
842 View Code Duplication
                                case '0':
843
                                    $link['name'] = Display::return_icon(
844
                                        'invisible.png',
845
                                        get_lang('Activate'),
846
                                        array('id' => 'linktool_'.$tool['id']),
847
                                        ICON_SIZE_SMALL,
848
                                        false
849
                                    );
850
                                    $link['cmd'] = 'restore=yes';
851
                                    $lnk[] = $link;
852
                                    break;
853 View Code Duplication
                                case '1':
854
                                    $link['name'] = Display::return_icon(
855
                                        'visible.png',
856
                                        get_lang('Deactivate'),
857
                                        array('id' => 'linktool_'.$tool['id']),
858
                                        ICON_SIZE_SMALL,
859
                                        false
860
                                    );
861
                                    $link['cmd'] = 'hide=yes';
862
                                    $lnk[] = $link;
863
                                    break;
864
                            }
865 View Code Duplication
                        } else {
866
                            $link['name'] = Display::return_icon(
867
                                'visible.png',
868
                                get_lang('Deactivate'),
869
                                array('id' => 'linktool_'.$tool['id']),
870
                                ICON_SIZE_SMALL,
871
                                false
872
                            );
873
                            $link['cmd'] = 'hide=yes';
874
                            $lnk[] = $link;
875
                        }
876
                    }
877
                    if (!empty($tool['adminlink'])) {
878
                        $item['extra'] = '<a href="'.$tool['adminlink'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
879
                    }
880
                }
881
882
                // Both checks are necessary as is_platform_admin doesn't take student view into account
883
                if ($is_platform_admin && $is_allowed_to_edit) {
884
                    if ($toolAdmin != '1') {
885
                        $link['cmd'] = 'hide=yes';
886
                    }
887
                }
888
889
                $item['visibility'] = null;
890
                if (isset($lnk) && is_array($lnk)) {
891 View Code Duplication
                    foreach ($lnk as $this_link) {
892
                        if (empty($tool['adminlink'])) {
893
                            $item['visibility'] .= '<a class="make_visible_and_invisible" href="'.api_get_self().'?'.api_get_cidreq().'&id='.$tool['id'].'&'.$this_link['cmd'].'">'.
894
                                $this_link['name'].'</a>';
895
                        }
896
                    }
897
                } else {
898
                    $item['visibility'] .= '';
899
                }
900
901
                // NOTE : Table contains only the image file name, not full path
902 View Code Duplication
                if (stripos($tool['link'], 'http://') === false &&
903
                    stripos($tool['link'], 'https://') === false &&
904
                    stripos($tool['link'], 'ftp://') === false
905
                ) {
906
                    $tool['link'] = $web_code_path.$tool['link'];
907
                }
908
909
                if ($tool['visibility'] == '0' && $toolAdmin != '1') {
910
                    $class = 'text-muted';
911
                    $info = pathinfo($tool['image']);
912
                    $basename = basename($tool['image'], '.'.$info['extension']); // $file is set to "index"
913
                    $tool['image'] = $basename.'_na.'.$info['extension'];
914
                } else {
915
                    $class = '';
916
                }
917
918
                $qm_or_amp = strpos($tool['link'], '?') === false ? '?' : '&';
919
                // If it's a link, we don't add the cidReq
920
921
                if ($tool['image'] == 'file_html.png' || $tool['image'] == 'file_html_na.png') {
922
                    $tool['link'] = $tool['link'].$qm_or_amp;
923
                } else {
924
                    $tool['link'] = $tool['link'].$qm_or_amp.api_get_cidreq();
925
                }
926
927
                $tool_link_params = array();
928
                $toolId = isset($tool["id"]) ? $tool["id"] : null;
929
930
                //@todo this visio stuff should be removed
931
                if (strpos($tool['name'], 'visio_') !== false) {
932
                    $tool_link_params = array(
933
                        'id' => 'tooldesc_'.$toolId,
934
                        'href' => '"javascript: void(0);"',
935
                        'class' => $class,
936
                        'onclick' => 'javascript: window.open(\''.$tool['link'].'\',\'window_visio'.api_get_course_id().'\',config=\'height=\'+730+\', width=\'+1020+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')',
937
                        'target' => $tool['target']
938
                    );
939
                } elseif (strpos($tool['name'], 'chat') !== false && api_get_course_setting('allow_open_chat_window')) {
940
                    $tool_link_params = array(
941
                        'id' => 'tooldesc_'.$toolId,
942
                        'class' => $class,
943
                        'href' => 'javascript: void(0);',
944
                        'onclick' => 'javascript: window.open(\''.$tool['link'].'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')', //Chat Open Windows
945
                        'target' => $tool['target']
946
                    );
947
                } else {
948
                    $tool_link_params = array(
949
                        'id' => 'tooldesc_'.$toolId,
950
                        'href' => $tool['link'],
951
                        'class' => $class,
952
                        'target' => $tool['target']
953
                    );
954
                }
955
956
                $tool_name = self::translate_tool_name($tool);
0 ignored issues
show
Bug introduced by
It seems like $tool can also be of type object<Chamilo\CourseBundle\Entity\CTool>; however, CourseHome::translate_tool_name() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
957
958
                // Including Courses Plugins
959
                // Creating title and the link
960
                if (isset($tool['category']) && $tool['category'] == 'plugin') {
961
                    $plugin_info = $app_plugin->getPluginInfo($tool['name']);
962
                    if (isset($plugin_info) && isset($plugin_info['title'])) {
963
                        $tool_name = $plugin_info['title'];
964
                    }
965
966 View Code Duplication
                    if (!file_exists(api_get_path(SYS_CODE_PATH).'img/'.$tool['image']) &&
967
                        !file_exists(api_get_path(SYS_CODE_PATH).'img/icons/64/'.$tool['image'])) {
968
                        $tool['image'] = 'plugins.png';
969
                    }
970
                    $tool_link_params['href'] = api_get_path(WEB_PLUGIN_PATH).$tool['original_link'].'?'.api_get_cidreq();
971
                }
972
973
                $icon = Display::return_icon(
974
                    $tool['image'],
975
                    $tool_name,
976
                    array('class' => 'tool-icon', 'id' => 'toolimage_'.$toolId),
977
                    ICON_SIZE_BIG,
978
                    false
979
                );
980
981
                /*if (!empty($tool['custom_icon'])) {
982
                    $image = self::getCustomWebIconPath().$tool['custom_icon'];
983
                    $icon = Display::img(
984
                        $image,
985
                        $tool['description'],
986
                        array(
987
                            'class' => 'tool-icon',
988
                            'id' => 'toolimage_'.$tool['id']
989
                        )
990
                    );
991
                }*/
992
993
                // Validation when belongs to a session
994
                $session_img = api_get_session_image($tool['session_id'], (!empty($_user['status']) ? $_user['status'] : ''));
995
                if ($studentview) {
996
                    $tool_link_params['href'] .= '&isStudentView=true';
997
                }
998
                $item['url_params'] = $tool_link_params;
999
                $item['icon'] = Display::url($icon, $tool_link_params['href'], $tool_link_params);
1000
                $item['tool'] = $tool;
1001
                $item['name'] = $tool_name;
1002
                $tool_link_params['id'] = 'is'.$tool_link_params['id'];
1003
                $item['link'] = Display::url(
1004
                    $tool_name.$session_img,
1005
                    $tool_link_params['href'],
1006
                    $tool_link_params
1007
                );
1008
1009
                $items[] = $item;
1010
1011
                $i++;
1012
            } // end of foreach
1013
        }
1014
1015
        $i = 0;
1016
        $html = '';
1017
1018
        if (!empty($items)) {
1019
            foreach ($items as $item) {
1020
                switch ($theme) {
1021
                    case 'activity_big':
1022
                        $data = '';
1023
                        $html .= '<div class="col-xs-6 col-md-3 course-tool">';
1024
                        $image = (substr($item['tool']['image'], 0, strpos($item['tool']['image'], '.'))).'.png';
1025
                        $toolId = isset($item['tool']['id']) ? $item['tool']['id'] : null;
1026
1027
                        if (isset($item['tool']['custom_image'])) {
1028
                            $original_image = Display::img(
1029
                                $item['tool']['custom_image'],
1030
                                $item['name'],
1031
                                array('id' => 'toolimage_'.$toolId)
1032
                            );
1033
                        } elseif (isset($item['tool']['custom_icon']) &&
1034
                            !empty($item['tool']['custom_icon'])
1035
                        ) {
1036
                            $customIcon = $item['tool']['custom_icon'];
1037
                            if ($item['tool']['visibility'] == '0') {
1038
                                $fileInfo = pathinfo($item['tool']['custom_icon']);
1039
                                $customIcon = self::getDisableIcon($item['tool']['custom_icon']);
1040
                            }
1041
                            $original_image = Display::img(
1042
                                self::getCustomWebIconPath().$customIcon,
1043
                                $item['name'],
1044
                                array('id' => 'toolimage_'.$toolId)
1045
                            );
1046
                        } else {
1047
                            $original_image = Display::return_icon(
1048
                                $image,
1049
                                $item['name'],
1050
                                array('id' => 'toolimage_'.$toolId),
1051
                                ICON_SIZE_BIG,
1052
                                false
1053
                            );
1054
                        }
1055
1056
                        $data .= Display::url($original_image, $item['url_params']['href'], $item['url_params']);
1057
                        $html .= Display::div($data, array('class' => 'big_icon')); //box-image reflection
1058
                        $html .= Display::div('<h4>'.$item['visibility'].$item['extra'].$item['link'].'</h4>', array('class' => 'content'));
1059
                        $html .= '</div>';
1060
1061
                        break;
1062
                    case 'activity':
1063
                        $html .= '<div class="offset2 col-md-4 course-tool">';
1064
                        $html .= $item['extra'];
1065
                        $html .= $item['visibility'];
1066
                        $html .= $item['icon'];
1067
                        $html .= $item['link'];
1068
                        $html .= '</div>';
1069
                        break;
1070
                    case 'vertical_activity':
1071
                        if ($i == 0) {
1072
                            $html .= '<ul>';
1073
                        }
1074
                        $image = (substr($item['tool']['image'], 0, strpos($item['tool']['image'], '.'))).'.png';
1075
                        $original_image = Display::return_icon(
1076
                                $image,
1077
                                $item['name'],
1078
                                array('id' => 'toolimage_'.$item['tool']['id']),
1079
                                ICON_SIZE_SMALL,
1080
                                false
1081
                            );
1082
                        $html .= '<li class="course-tool">';
1083
                        $html .= $item['extra'];
1084
                        $html .= $item['visibility'];
1085
                        $url = Display::url($original_image, $item['url_params']['href'], $item['url_params']);
1086
                        $html .= $url;
1087
                        $html .= $item['link'];
1088
                        $html .= '</li>';
1089
1090
                        if ($i == count($items) - 1) {
1091
                            $html .= '</ul>';
1092
                        }
1093
                        break;
1094
                }
1095
                $i++;
1096
            }
1097
        }
1098
1099
         return array(
1100
            'content' => $html,
1101
            'tool_list' => $items
1102
        );
1103
    }
1104
1105
    /**
1106
     * Shows the general data for a particular meeting
1107
     *
1108
     * @param id	session id
1109
     * @return string	session data
1110
     */
1111
    public static function show_session_data($id_session)
1112
    {
1113
        $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
1114
1115
        $sessionInfo = api_get_session_info($id_session);
1116
1117
        if (empty($sessionInfo)) {
1118
            return '';
1119
        }
1120
1121
        $sql = 'SELECT name FROM '.$session_category_table.'
1122
                WHERE id = "'.intval($sessionInfo['session_category_id']).'"';
1123
        $rs_category = Database::query($sql);
1124
        $session_category = '';
1125 View Code Duplication
        if (Database::num_rows($rs_category) > 0) {
1126
            $rows_session_category = Database::store_result($rs_category);
1127
            $rows_session_category = $rows_session_category[0];
1128
            $session_category = $rows_session_category['name'];
1129
        }
1130
1131
        $coachInfo = api_get_user_info($sessionInfo['id_coach']);
1132
1133
        $output = '';
1134
        if (!empty($session_category)) {
1135
            $output .= '<tr><td>'.get_lang('SessionCategory').': '.'<b>'.$session_category.'</b></td></tr>';
1136
        }
1137
        $dateInfo = SessionManager::parseSessionDates($sessionInfo);
1138
1139
        $msgDate = $dateInfo['access'];
1140
        $output .= '<tr>
1141
                    <td style="width:50%">'.get_lang('SessionName').': '.'<b>'.$sessionInfo['name'].'</b></td>
1142
                    <td>'.get_lang('GeneralCoach').': '.'<b>'.$coachInfo['complete_name'].'</b></td></tr>';
1143
        $output .= '<tr>
1144
                        <td>'.get_lang('SessionIdentifier').': '.
1145
                            Display::return_icon('star.png', ' ', array('align' => 'absmiddle')).'
1146
                        </td>
1147
                        <td>'.get_lang('Date').': '.'<b>'.$msgDate.'</b>
1148
                        </td>
1149
                    </tr>';
1150
1151
        return $output;
1152
    }
1153
1154
    /**
1155
     * Retrieves the name-field within a tool-record and translates it on necessity.
1156
     * @param array $tool		The input record.
1157
     * @return string			Returns the name of the corresponding tool.
1158
     */
1159
    public static function translate_tool_name(& $tool)
1160
    {
1161
        static $already_translated_icons = array(
1162
            'file_html.gif',
1163
            'file_html_na.gif',
1164
            'file_html.png',
1165
            'file_html_na.png',
1166
            'scormbuilder.gif',
1167
            'scormbuilder_na.gif',
1168
            'blog.gif',
1169
            'blog_na.gif',
1170
            'external.gif',
1171
            'external_na.gif'
1172
        );
1173
1174
        $toolName = Security::remove_XSS(stripslashes($tool['name']));
1175
1176
        if (in_array($tool['image'], $already_translated_icons)) {
1177
            return $toolName;
1178
        }
1179
1180
        $toolName = api_underscore_to_camel_case($toolName);
1181
1182
        if (isset($GLOBALS['Tool' . $toolName])) {
1183
            return get_lang('Tool' . $toolName);
1184
        }
1185
1186
        return $toolName;
1187
    }
1188
1189
    /**
1190
     * Get published learning path id from link inside course home
1191
     * @param 	string	Link to published lp
1192
     * @return	int		Learning path id
1193
     */
1194
    public static function get_published_lp_id_from_link($published_lp_link)
1195
    {
1196
        $lp_id = 0;
1197
        $param_lp_id = strstr($published_lp_link, 'lp_id=');
1198
        if (!empty($param_lp_id)) {
1199
            $a_param_lp_id = explode('=', $param_lp_id);
1200
            if (isset($a_param_lp_id[1])) {
1201
                $lp_id = intval($a_param_lp_id[1]);
1202
            }
1203
        }
1204
1205
        return $lp_id;
1206
    }
1207
1208
    /**
1209
     * Get published learning path category from link inside course home
1210
     * @param string $link
1211
     * @return CLpCategory
1212
     */
1213
    public static function getPublishedLpCategoryFromLink($link)
1214
    {
1215
        $query = parse_url($link, PHP_URL_QUERY);
1216
        parse_str($query, $params);
1217
1218
        $id = isset($params['id']) ? (int) $params['id'] : 0;
1219
1220
        $em = Database::getManager();
1221
        /** @var CLpCategory $category */
1222
        $category = $em->find('ChamiloCourseBundle:CLpCategory', $id);
1223
1224
        return $category;
1225
    }
1226
1227
    /**
1228
     * @param bool $include_admin_tools
1229
     * @return array
1230
     */
1231
    public static function get_navigation_items($include_admin_tools = false)
1232
    {
1233
        $navigation_items = array();
1234
        $course_id = api_get_course_int_id();
1235
        $courseInfo = api_get_course_info();
1236
        $sessionId = api_get_session_id();
1237
1238
        if (!empty($course_id)) {
1239
1240
            $course_tools_table = Database::get_course_table(TABLE_TOOL_LIST);
1241
1242
            /* 	Link to the Course homepage */
1243
            $navigation_items['home']['image'] = 'home.gif';
1244
            $navigation_items['home']['link'] = $courseInfo['course_public_url'];
1245
            $navigation_items['home']['name'] = get_lang('CourseHomepageLink');
1246
1247
            $sql = "SELECT * FROM $course_tools_table
1248
                    WHERE c_id = $course_id AND visibility='1' and admin='0'
1249
                    ORDER BY id ASC";
1250
            $sql_result = Database::query($sql);
1251
            while ($row = Database::fetch_array($sql_result)) {
1252
                $navigation_items[$row['id']] = $row;
1253
                if (stripos($row['link'], 'http://') === false && stripos($row['link'], 'https://') === false) {
1254
                    $navigation_items[$row['id']]['link'] = api_get_path(WEB_CODE_PATH);
1255
1256
                    if ($row['category'] == 'plugin') {
1257
                        $plugin = new AppPlugin();
1258
                        $pluginInfo = $plugin->getPluginInfo($row['name']);
1259
1260
                        $navigation_items[$row['id']]['link'] = api_get_path(WEB_PLUGIN_PATH);
1261
                        $navigation_items[$row['id']]['name'] = $pluginInfo['title'];
1262
                    } else {
1263
                        $navigation_items[$row['id']]['name'] = self::translate_tool_name($row);
1264
                    }
1265
1266
                    $navigation_items[$row['id']]['link'] .= $row['link'];
1267
                }
1268
            }
1269
1270
            /* 	Admin (edit rights) only links
1271
              - Course settings (course admin only)
1272
              - Course rights (roles & rights overview) */
1273
1274
            if ($include_admin_tools) {
1275
                $sql = "SELECT name, image FROM $course_tools_table
1276
                        WHERE c_id = $course_id  AND link='course_info/infocours.php'";
1277
                $sql_result = Database::query($sql);
1278
                $course_setting_info = Database::fetch_array($sql_result);
1279
                $course_setting_visual_name = self::translate_tool_name($course_setting_info);
1280
                if ($sessionId == 0) {
1281
                    // course settings item
1282
                    $navigation_items['course_settings']['image'] = $course_setting_info['image'];
1283
                    $navigation_items['course_settings']['link'] = api_get_path(WEB_CODE_PATH).'course_info/infocours.php';
1284
                    $navigation_items['course_settings']['name'] = $course_setting_visual_name;
1285
                }
1286
            }
1287
        }
1288
1289
        foreach ($navigation_items as $key => $navigation_item) {
1290
            if (strstr($navigation_item['link'], '?')) {
1291
                //link already contains a parameter, add course id parameter with &
1292
                $parameter_separator = '&amp;';
1293
            } else {
1294
                //link doesn't contain a parameter yet, add course id parameter with ?
1295
                $parameter_separator = '?';
1296
            }
1297
            //$navigation_items[$key]['link'] .= $parameter_separator.api_get_cidreq();
1298
            $navigation_items[$key]['link'] .= $parameter_separator.'cidReq='.api_get_course_id().'&gidReq=0&id_session='.$sessionId;
1299
        }
1300
1301
        return $navigation_items;
1302
    }
1303
1304
    /**
1305
     * Show a navigation menu
1306
     */
1307
    public static function show_navigation_menu()
1308
    {
1309
        $navigation_items = self::get_navigation_items(true);
1310
        $course_id = api_get_course_id();
1311
1312
        $class = null;
1313
        $idLearn = null;
1314
        $item = null;
1315
        $marginLeft = 160;
1316
1317
        $html = '<div id="toolnav">';
1318
        $html .= '<ul id="toolnavbox">';
1319
        $count = 0;
1320
        foreach ($navigation_items as $key => $navigation_item) {
1321
            //students can't see the course settings option
1322
            $count++;
1323
            if (!api_is_allowed_to_edit() && $key == 'course_settings') {
1324
                continue;
1325
            }
1326
            $html .= '<li>';
1327
            $url_item = parse_url($navigation_item['link']);
1328
            $url_current = parse_url($_SERVER['REQUEST_URI']);
1329
1330
            if (api_get_setting('show_navigation_menu') == 'text') {
1331
                $class = 'text';
1332
                $marginLeft = 170;
1333
                $item = $navigation_item['name'];
1334
            } else if (api_get_setting('show_navigation_menu') == 'icons') {
1335
                $class = 'icons';
1336
                $marginLeft = 25;
1337
                $item = Display::return_icon(substr($navigation_item['image'], 0, -3)."png", $navigation_item['name'], array('class'=>'tool-img'), ICON_SIZE_SMALL);
1338 View Code Duplication
            } else {
1339
                $class = 'icons-text';
1340
                $item = $navigation_item['name'].Display::return_icon(substr($navigation_item['image'], 0, -3)."png", $navigation_item['name'], array('class'=>'tool-img'), ICON_SIZE_SMALL);
1341
            }
1342
1343
            if (stristr($url_item['path'], $url_current['path'])) {
1344
                if (!isset($_GET['learnpath_id']) || strpos($url_item['query'], 'learnpath_id='.intval($_GET['learnpath_id'])) === 0) {
1345
                    $idLearn = ' id="here"';
1346
                }
1347
            }
1348
1349
            if (strpos($navigation_item['link'], 'chat') !== false &&
1350
                api_get_course_setting('allow_open_chat_window', $course_id)
1351
            ) {
1352
                $html .= '<a '.$idLearn.' class="btn btn-default text-left '.$class.' " href="javascript: void(0);" onclick="javascript: window.open(\''.$navigation_item['link'].'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$navigation_item['target'].'"';
1353
                $html .= ' title="'.$navigation_item['name'].'">';
1354
                $html .= $item;
1355
                $html .= '</a>';
1356
            } else {
1357
                $html .= '<a '.$idLearn.' class="btn btn-default text-left '.$class.'" href="'.$navigation_item['link'].'" target="_top" title="'.$navigation_item['name'].'">';
1358
                $html .= $item;
1359
                $html .= '</a>';
1360
            }
1361
1362
            $html .= '</li>';
1363
        }
1364
        $html .= '</ul>';
1365
        $html .= '<script>$(function() {
1366
                $("#toolnavbox a").stop().animate({"margin-left":"-' . $marginLeft.'px"},1000);
1367
                $("#toolnavbox > li").hover(
1368
                    function () {
1369
                        $("a",$(this)).stop().animate({"margin-left":"-2px"},200);
1370
                        $("span",$(this)).css("display","block");
1371
                    },
1372
                    function () {
1373
                        $("a",$(this)).stop().animate({"margin-left":"-' . $marginLeft.'px"},200);
1374
                        $("span",$(this)).css("display","initial");
1375
                    }
1376
                );
1377
            });</script>';
1378
        $html .= '</div>';
1379
1380
        return $html;
1381
    }
1382
1383
    /**
1384
     * Show a toolbar with shortcuts to the course tool
1385
     * @param int $orientation
1386
     *
1387
     * @return string
1388
     */
1389
    public static function show_navigation_tool_shortcuts($orientation = SHORTCUTS_HORIZONTAL)
1390
    {
1391
        $origin = api_get_origin();
1392
        if ($origin === 'learnpath') {
1393
            return '';
1394
        }
1395
1396
        $navigation_items = self::get_navigation_items(false);
1397
        $html = '';
1398
        if (!empty($navigation_items)) {
1399
            if ($orientation == SHORTCUTS_HORIZONTAL) {
1400
                $style_id = "toolshortcuts_horizontal";
1401
            } else {
1402
                $style_id = "toolshortcuts_vertical";
1403
            }
1404
            $html .= '<div id="'.$style_id.'">';
1405
            foreach ($navigation_items as $key => $navigation_item) {
1406
                if (strpos($navigation_item['link'], 'chat') !== false &&
1407
                    api_get_course_setting('allow_open_chat_window')
1408
                ) {
1409
                    $html .= '<a class="items-icon" href="javascript: void(0);" onclick="javascript: window.open(\''.$navigation_item['link'].'\',\'window_chat'.api_get_course_id().'\',config=\'height=\'+600+\', width=\'+825+\', left=2, top=2, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no\')" target="'.$navigation_item['target'].'"';
1410
                } else {
1411
                    $html .= '<a class="items-icon" href="'.$navigation_item['link'].'"';
1412
                }
1413
                if (strpos(api_get_self(), $navigation_item['link']) !== false) {
1414
                    $html .= ' id="here"';
1415
                }
1416
                $html .= ' target="_top" title="'.$navigation_item['name'].'">';
1417
1418
                if (isset($navigation_item['category']) && $navigation_item['category'] == 'plugin') {
1419
                    /*$plugin_info = $app_plugin->getPluginInfo($navigation_item['name']);
1420
                    if (isset($plugin_info) && isset($plugin_info['title'])) {
1421
                        $tool_name = $plugin_info['title'];
1422
                    }*/
1423
1424 View Code Duplication
                    if (!file_exists(api_get_path(SYS_CODE_PATH).'img/'.$navigation_item['image']) &&
1425
                        !file_exists(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_MEDIUM.'/'.$navigation_item['image'])
1426
                    ) {
1427
                        $navigation_item['image'] = 'plugins.png';
1428
                    }
1429
                    //$tool_link_params['href'] = api_get_path(WEB_PLUGIN_PATH).$navigation_item['link'].'?'.api_get_cidreq();
1430
                }
1431
1432
                $html .= Display::return_icon(
1433
                    substr($navigation_item['image'], 0, -3).'png',
1434
                    $navigation_item['name'],
1435
                    [],
1436
                    ICON_SIZE_MEDIUM
1437
                );
1438
                $html .= '</a> ';
1439
                if ($orientation == SHORTCUTS_VERTICAL) {
1440
                    $html .= '<br />';
1441
                }
1442
            }
1443
            $html .= '</div>';
1444
        }
1445
1446
        return $html;
1447
    }
1448
1449
    /**
1450
     * List course homepage tools from authoring and interaction sections
1451
     * @param   int $courseId The course ID (guessed from context if not provided)
1452
     * @param   int $sessionId The session ID (guessed from context if not provided)
1453
     * @return  array List of all tools data from the c_tools table
1454
     */
1455
    public static function toolsIconsAction($courseId = null, $sessionId = null)
1456
    {
1457
        if (empty($courseId)) {
1458
            $courseId = api_get_course_int_id();
1459
        } else {
1460
            $courseId = intval($courseId);
1461
        }
1462
        if (empty($sessionId)) {
1463
            $sessionId = api_get_session_id();
1464
        } else {
1465
            $sessionId = intval($sessionId);
1466
        }
1467
1468
        if (empty($courseId)) {
1469
            // We shouldn't get here, but for some reason api_get_course_int_id()
1470
            // doesn't seem to get the course from the context, sometimes
1471
            return array();
1472
        }
1473
1474
        $table = Database::get_course_table(TABLE_TOOL_LIST);
1475
        $sql = "SELECT * FROM $table
1476
                WHERE category in ('authoring','interaction')
1477
                AND c_id = $courseId
1478
                AND session_id = $sessionId
1479
                ORDER BY id";
1480
1481
        $result = Database::query($sql);
1482
        $data = Database::store_result($result, 'ASSOC');
1483
1484
        return $data;
1485
    }
1486
1487
    /**
1488
     * @param int $editIcon
1489
     * @return array
1490
     */
1491 View Code Duplication
    public static function getTool($editIcon)
1492
    {
1493
        $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
1494
        $editIcon = intval($editIcon);
1495
1496
        $sql = "SELECT * FROM $course_tool_table
1497
                WHERE iid = $editIcon";
1498
        $result = Database::query($sql);
1499
        $tool = Database::fetch_assoc($result, 'ASSOC');
1500
1501
        return $tool;
1502
    }
1503
1504
    /**
1505
     * @return string
1506
     */
1507
    public static function getCustomSysIconPath()
1508
    {
1509
        // Check if directory exists or create it if it doesn't
1510
        $dir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/';
1511
        if (!is_dir($dir)) {
1512
            mkdir($dir, api_get_permissions_for_new_directories(), true);
1513
        }
1514
1515
        return $dir;
1516
    }
1517
1518
    /**
1519
     * @return string
1520
     */
1521
    public static function getCustomWebIconPath()
1522
    {
1523
        // Check if directory exists or create it if it doesn't
1524
        $dir = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/course_home_icons/';
1525
1526
        return $dir;
1527
    }
1528
1529
    /**
1530
     * @param string $icon
1531
     * @return string
1532
     */
1533
    public static function getDisableIcon($icon)
1534
    {
1535
        $fileInfo = pathinfo($icon);
1536
1537
        return $fileInfo['filename'].'_na.'.$fileInfo['extension'];
1538
    }
1539
1540
    /**
1541
     * @param int $id
1542
     * @param array $values
1543
     */
1544
    public static function updateTool($id, $values)
1545
    {
1546
        $table = Database::get_course_table(TABLE_TOOL_LIST);
1547
        $params = [
1548
            'name' => $values['name'],
1549
            'link' => $values['link'],
1550
            'target' => $values['target'],
1551
            'visibility' => $values['visibility'],
1552
            'description' => $values['description'],
1553
        ];
1554
1555
        if (isset($_FILES['icon']['size']) && $_FILES['icon']['size'] !== 0) {
1556
            $dir = self::getCustomSysIconPath();
1557
1558
            // Resize image if it is larger than 64px
1559
            $temp = new Image($_FILES['icon']['tmp_name']);
1560
            $picture_infos = $temp->get_image_info();
1561
            if ($picture_infos['width'] > 64) {
1562
                $thumbwidth = 64;
1563
            } else {
1564
                $thumbwidth = $picture_infos['width'];
1565
            }
1566
            if ($picture_infos['height'] > 64) {
1567
                $new_height = 64;
1568
            } else {
1569
                $new_height = $picture_infos['height'];
1570
            }
1571
            $temp->resize($thumbwidth, $new_height, 0);
1572
1573
            //copy the image to the course upload folder
1574
            $path = $dir.$_FILES['icon']['name'];
1575
            $result = $temp->send_image($path);
1576
1577
            $temp = new Image($path);
1578
            $r = $temp->convert2bw();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $r is correct as $temp->convert2bw() (which targets Image::convert2bw()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1579
            $ext = pathinfo($path, PATHINFO_EXTENSION);
1580
            $bwPath = substr($path, 0, -(strlen($ext) + 1)).'_na.'.$ext;
1581
1582
            if ($r === false) {
1583
                error_log('Conversion to B&W of '.$path.' failed in '.__FILE__.' at line '.__LINE__);
1584
            } else {
1585
                $temp->send_image($bwPath);
1586
                $iconName = $_FILES['icon']['name'];
1587
                $params['custom_icon'] = $iconName;
1588
            }
1589
        }
1590
1591
        Database::update(
1592
            $table,
1593
            $params,
1594
            [' iid = ?' => [$id]]
1595
        );
1596
    }
1597
1598
    /**
1599
     * @param int $id
1600
     */
1601
    public static function deleteIcon($id)
1602
    {
1603
        $table = Database::get_course_table(TABLE_TOOL_LIST);
1604
        $tool = self::getTool($id);
1605
1606
        if ($tool && !empty($tool['custom_icon'])) {
1607
            $file = self::getCustomSysIconPath().$tool['custom_icon'];
1608
            $fileInfo = pathinfo($file);
1609
            $fileGray = $fileInfo['filename'].'_na.'.$fileInfo['extension'];
1610
            $fileGray = self::getCustomSysIconPath().$fileGray;
1611
1612 View Code Duplication
            if (file_exists($file) && is_file($file)) {
1613
                if (Security::check_abs_path($file, self::getCustomSysIconPath())) {
1614
                    unlink($file);
1615
                }
1616
            }
1617
1618 View Code Duplication
            if (file_exists($fileGray) && is_file($fileGray)) {
1619
                if (Security::check_abs_path($fileGray, self::getCustomSysIconPath())) {
1620
                    unlink($fileGray);
1621
                }
1622
            }
1623
1624
            $params = [
1625
                'custom_icon' => ''
1626
            ];
1627
1628
            Database::update(
1629
                $table,
1630
                $params,
1631
                [' iid = ?' => [$id]]
1632
            );
1633
        }
1634
    }
1635
1636
    /**
1637
     * @param string $text
1638
     * @param array $toolList
1639
     * @return string
1640
     */
1641
    public static function replaceTextWithToolUrls($text, $toolList)
1642
    {
1643
        if (empty($toolList)) {
1644
            return $text;
1645
        }
1646
1647
        foreach ($toolList as $tool) {
1648
            if (!isset($tool['icon'])) {
1649
                continue;
1650
            }
1651
            $toolName = $tool['tool']['name'];
1652
            $search = array("{{ ".$toolName." }}", "{{".$toolName."}}", "((".$toolName."))", "(( ".$toolName." ))");
1653
            $text = str_replace($search, $tool['icon'], $text);
1654
        }
1655
1656
        // Cleaning tags that are not used.
1657
        $tools = self::availableTools();
1658
        foreach ($tools as $toolName) {
1659
            $search = array("{{ ".$toolName." }}", "{{".$toolName."}}", "((".$toolName."))", "(( ".$toolName." ))");
1660
            $text = str_replace($search, null, $text);
1661
        }
1662
1663
        return $text;
1664
    }
1665
1666
    /**
1667
     * Available tools
1668
     * @return array
1669
     */
1670
    public static function availableTools()
1671
    {
1672
        return array(
1673
            'course_description',
1674
            'quiz',
1675
            'announcement',
1676
            'forum',
1677
            'dropbox',
1678
            'user',
1679
            'group',
1680
            'chat',
1681
            'student_publication',
1682
            'survey',
1683
            'wiki',
1684
            'gradebook',
1685
            'glossary',
1686
            'notebook',
1687
            'attendance',
1688
            'course_progress',
1689
            'curriculum',
1690
            'blog_management',
1691
            'tracking',
1692
            'course_setting',
1693
            'course_maintenance'
1694
        );
1695
    }
1696
}
1697