Passed
Push — master ( 621302...15a517 )
by Angel Fernando Quiroz
16:26 queued 07:49
created

Plugin::uninstall_course_fields_in_all_courses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\ResourceLink;
5
use Chamilo\CoreBundle\Entity\Tool;
6
use Chamilo\CoreBundle\Framework\Container;
7
use Chamilo\CourseBundle\Entity\CTool;
8
use Doctrine\ORM\Exception\NotSupported;
9
use Doctrine\ORM\Exception\ORMException;
10
use Doctrine\ORM\OptimisticLockException;
11
12
/**
13
 * Class Plugin
14
 * Base class for plugins.
15
 *
16
 * This class has to be extended by every plugin. It defines basic methods
17
 * to install/uninstall and get information about a plugin
18
 *
19
 * @author    Julio Montoya <[email protected]>
20
 * @author    Yannick Warnier <[email protected]>
21
 * @author    Laurent Opprecht    <[email protected]>
22
 * @copyright 2012 University of Geneva
23
 * @license   GNU General Public License - http://www.gnu.org/copyleft/gpl.html
24
 */
25
class Plugin
26
{
27
    const TAB_FILTER_NO_STUDENT = '::no-student';
28
    const TAB_FILTER_ONLY_STUDENT = '::only-student';
29
    public $isCoursePlugin = false;
30
    public $isAdminPlugin = false;
31
    public $isMailPlugin = false;
32
    // Adds icon in the course home
33
    public $addCourseTool = true;
34
    public $hasPersonalEvents = false;
35
36
    /**
37
     * When creating a new course, these settings are added to the course, in
38
     * the course_info/infocours.php
39
     * To show the plugin course icons you need to add these icons:
40
     * main/img/icons/22/plugin_name.png
41
     * main/img/icons/64/plugin_name.png
42
     * main/img/icons/64/plugin_name_na.png.
43
     *
44
     * @example
45
     * $course_settings = array(
46
    array('name' => 'big_blue_button_welcome_message',  'type' => 'text'),
47
    array('name' => 'big_blue_button_record_and_store', 'type' => 'checkbox')
48
    );
49
     */
50
    public $course_settings = [];
51
    /**
52
     * This indicates whether changing the setting should execute the callback
53
     * function.
54
     */
55
    public $course_settings_callback = false;
56
57
    protected $version = '';
58
    protected $author = '';
59
    protected $fields = [];
60
    private $settings = [];
61
    // Translation strings.
62
    private $strings = null;
63
64
    /**
65
     * Default constructor for the plugin class. By default, it only sets
66
     * a few attributes of the object.
67
     *
68
     * @param string $version  of this plugin
69
     * @param string $author   of this plugin
70
     * @param array  $settings settings to be proposed to configure the plugin
71
     */
72
    protected function __construct($version, $author, $settings = [])
73
    {
74
        $this->version = $version;
75
        $this->author = $author;
76
        $this->fields = $settings;
77
78
        global $language_files;
79
        $language_files[] = 'plugin_'.$this->get_name();
80
81
        if ($this->isCoursePlugin) {
82
            if (!isset($this->fields['defaultVisibilityInCourseHomepage'])) {
83
                $this->fields['defaultVisibilityInCourseHomepage'] = [
84
                    'type' => 'select',
85
                    'options' => [
86
                        'visible' => 'Visible',
87
                        'hidden'  => 'Hidden',
88
                    ],
89
                ];
90
            }
91
        }
92
    }
93
94
    /**
95
     * Gets an array of information about this plugin (name, version, ...).
96
     *
97
     * @return array Array of information elements about this plugin
98
     */
99
    public function get_info()
100
    {
101
        $pluginRepo = Container::getPluginRepository();
102
103
        $result = [];
104
        $result['obj'] = $this;
105
        $result['title'] = $this->get_title();
106
        $result['comment'] = $this->get_comment();
107
        $result['version'] = $this->get_version();
108
        $result['author'] = $this->get_author();
109
        $result['plugin_class'] = static::class;
110
        $result['is_course_plugin'] = $this->isCoursePlugin;
111
        $result['is_admin_plugin'] = $this->isAdminPlugin;
112
        $result['is_mail_plugin'] = $this->isMailPlugin;
113
        $result['entity'] = $pluginRepo->findOneByTitle($this->get_title());
114
115
        if ($form = $this->getSettingsForm()) {
116
            $result['settings_form'] = $form;
117
118
            foreach ($this->fields as $name => $type) {
119
                $value = $this->get($name);
120
121
                if (is_array($type)) {
122
                    $value = $type['options'];
123
                }
124
                $result[$name] = $value;
125
            }
126
        }
127
128
        return $result;
129
    }
130
131
    /**
132
     * Returns the "system" name of the plugin in lowercase letters.
133
     */
134
    public function get_name(): string
135
    {
136
        return str_replace('Plugin', '', static::class);
137
    }
138
139
    /**
140
     * Returns the title of the plugin.
141
     *
142
     * @return string
143
     */
144
    public function get_title()
145
    {
146
        return $this->get_lang('plugin_title');
147
    }
148
149
    /**
150
     * Returns the description of the plugin.
151
     *
152
     * @return string
153
     */
154
    public function get_comment()
155
    {
156
        return $this->get_lang('plugin_comment');
157
    }
158
159
    /**
160
     * Returns the version of the plugin.
161
     *
162
     * @return string
163
     */
164
    public function get_version()
165
    {
166
        return $this->version;
167
    }
168
169
    /**
170
     * Returns the author of the plugin.
171
     *
172
     * @return string
173
     */
174
    public function get_author()
175
    {
176
        return $this->author;
177
    }
178
179
    /**
180
     * Returns the contents of the CSS defined by the plugin.
181
     *
182
     * @return string
183
     */
184
    public function get_css()
185
    {
186
        $name = $this->get_name();
187
        $path = api_get_path(SYS_PLUGIN_PATH)."$name/resources/$name.css";
188
        if (!is_readable($path)) {
189
            return '';
190
        }
191
        $css = [];
192
        $css[] = file_get_contents($path);
193
        $result = implode($css);
194
195
        return $result;
196
    }
197
198
    /**
199
     * Returns an HTML form (generated by FormValidator) of the plugin settings.
200
     *
201
     * @return FormValidator FormValidator-generated form
202
     */
203
    public function getSettingsForm()
204
    {
205
        $result = new FormValidator($this->get_name());
206
207
        $defaults = [];
208
        $checkboxGroup = [];
209
        $checkboxCollection = [];
210
211
        if ($checkboxNames = array_keys($this->fields, 'checkbox')) {
212
            $pluginInfoCollection = api_get_settings('Plugins');
213
            foreach ($pluginInfoCollection as $pluginInfo) {
214
                if (false !== array_search($pluginInfo['title'], $checkboxNames)) {
215
                    $checkboxCollection[$pluginInfo['title']] = $pluginInfo;
216
                }
217
            }
218
        }
219
220
        foreach ($this->fields as $name => $type) {
221
            $options = null;
222
            if (is_array($type) && isset($type['type']) && 'select' === $type['type']) {
223
                $attributes = isset($type['attributes']) ? $type['attributes'] : [];
224
                if (!empty($type['options']) && isset($type['translate_options']) && $type['translate_options']) {
225
                    foreach ($type['options'] as $key => &$optionName) {
226
                        $optionName = $this->get_lang($optionName);
227
                    }
228
                }
229
                $options = $type['options'];
230
                $type = $type['type'];
231
            }
232
233
            $value = $this->get($name);
234
            $defaults[$name] = $value;
235
            $type = isset($type) ? $type : 'text';
236
237
            $help = null;
238
            if ($this->get_lang_plugin_exists($name.'_help')) {
239
                $help = $this->get_lang($name.'_help');
240
                if ("show_main_menu_tab" === $name) {
241
                    $pluginName = $this->get_name();
242
                    $pluginUrl = api_get_path(WEB_PATH)."plugin/$pluginName/index.php";
243
                    $pluginUrl = "<a href=$pluginUrl>$pluginUrl</a>";
244
                    $help = sprintf($help, $pluginUrl);
245
                }
246
            }
247
248
            switch ($type) {
249
                case 'html':
250
                    $result->addHtml($this->get_lang($name));
251
                    break;
252
                case 'wysiwyg':
253
                    $result->addHtmlEditor($name, $this->get_lang($name), false);
254
                    break;
255
                case 'text':
256
                    $result->addElement($type, $name, [$this->get_lang($name), $help]);
257
                    break;
258
                case 'boolean':
259
                    $group = [];
260
                    $group[] = $result->createElement(
261
                        'radio',
262
                        $name,
263
                        '',
264
                        get_lang('Yes'),
265
                        'true'
266
                    );
267
                    $group[] = $result->createElement(
268
                        'radio',
269
                        $name,
270
                        '',
271
                        get_lang('No'),
272
                        'false'
273
                    );
274
                    $result->addGroup($group, null, [$this->get_lang($name), $help]);
275
                    break;
276
                case 'checkbox':
277
                    $selectedValue = null;
278
                    if (isset($checkboxCollection[$name])) {
279
                        if ('true' === $checkboxCollection[$name]['selected_value']) {
280
                            $selectedValue = 'checked';
281
                        }
282
                    }
283
284
                    $element = $result->createElement(
285
                        $type,
286
                        $name,
287
                        '',
288
                        $this->get_lang($name),
289
                        $selectedValue
290
                    );
291
                    $element->_attributes['value'] = 'true';
292
                    $checkboxGroup[] = $element;
293
                    break;
294
                case 'select':
295
                    $result->addElement(
296
                        $type,
297
                        $name,
298
                        [$this->get_lang($name), $help],
299
                        $options,
300
                        $attributes
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attributes does not seem to be defined for all execution paths leading up to this point.
Loading history...
301
                    );
302
                    break;
303
                case 'user':
304
                    $options = [];
305
                    if (!empty($value)) {
306
                        $userInfo = api_get_user_info($value);
307
                        if ($userInfo) {
308
                            $options[$value] = $userInfo['complete_name'];
309
                        }
310
                    }
311
                    $result->addSelectAjax(
312
                        $name,
313
                        [$this->get_lang($name), $help],
314
                        $options,
315
                        ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like']
316
                    );
317
                    break;
318
            }
319
        }
320
321
        if (!empty($checkboxGroup)) {
322
            $result->addGroup(
323
                $checkboxGroup,
324
                null,
325
                ['', $help]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $help seems to be defined by a foreach iteration on line 220. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
326
            );
327
        }
328
        $result->setDefaults($defaults);
329
        $result->addButtonSave($this->get_lang('Save'), 'submit_button');
330
331
        return $result;
332
    }
333
334
    /**
335
     * Returns the value of a given plugin global setting.
336
     */
337
    public function get(string $name): mixed
338
    {
339
        if ('tool_enable' === $name) {
340
            $isEnabled = Container::getPluginHelper()
341
                ->isPluginEnabled($this->get_name());
342
343
            return $isEnabled ? 'true' : 'false';
344
        }
345
346
        $settings = $this->get_settings();
347
348
        if (isset($settings[$name])) {
349
            return $settings[$name];
350
        }
351
352
        foreach ($settings as $setting) {
353
            if (is_array($setting) && isset($setting[$name])) {
354
                return $setting[$name];
355
            }
356
        }
357
358
        return null;
359
    }
360
361
    /**
362
     * Returns an array with the global settings for this plugin.
363
     *
364
     * @param bool $forceFromDB Optional. Force get settings from the database
365
     *
366
     * @return array Plugin settings as an array
367
     *
368
     * @throws Exception
369
     */
370
    public function get_settings(bool $forceFromDB = false): array
371
    {
372
        $plugin = Container::getPluginRepository()->findOneByTitle($this->get_name());
373
374
        if ($plugin && empty($this->settings) || $forceFromDB) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($plugin && empty($this-...tings)) || $forceFromDB, Probably Intended Meaning: $plugin && (empty($this-...tings) || $forceFromDB)
Loading history...
375
            $configByUrl = $plugin->getConfigurationsByAccessUrl(
376
                Container::getAccessUrlUtil()->getCurrent()
377
            );
378
379
            $this->settings = $configByUrl?->getConfiguration() ?? [];
380
        }
381
382
        return $this->settings;
383
    }
384
385
    /**
386
     * Tells whether language variables are defined for this plugin or not.
387
     *
388
     * @param string $name System name of the plugin
389
     *
390
     * @return bool True if the plugin has language variables defined, false otherwise
391
     */
392
    public function get_lang_plugin_exists($name)
393
    {
394
        return isset($this->strings[$name]);
395
    }
396
397
    /**
398
     * Hook for the get_lang() function to check for plugin-defined language terms.
399
     *
400
     * @param string $name of the language variable we are looking for
401
     *
402
     * @return string The translated language term of the plugin
403
     */
404
    public function get_lang($name)
405
    {
406
        // Check whether the language strings for the plugin have already been
407
        // loaded. If so, no need to load them again.
408
        if (is_null($this->strings)) {
409
            $language_interface = api_get_language_isocode();
410
            $root = api_get_path(SYS_PLUGIN_PATH);
411
            $plugin_name = $this->get_name();
412
413
            $interfaceLanguageId = api_get_language_id($language_interface);
414
            if (empty($interfaceLanguageId)) {
415
                $language_interface = api_get_setting('platformLanguage');
416
                $interfaceLanguageId = api_get_language_id($language_interface);
417
            }
418
            $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId);
419
            $languageParentId = !empty($interfaceLanguageInfo['parent_id']) ? (int) $interfaceLanguageInfo['parent_id'] : 0;
420
421
            // 1. Loading english if exists
422
            $english_path = $root.$plugin_name."/lang/english.php";
423
424
            if (is_readable($english_path)) {
425
                $strings = [];
426
                include $english_path;
427
                $this->strings = $strings;
428
            }
429
430
            $path = $root.$plugin_name."/lang/$language_interface.php";
431
            // 2. Loading the system language
432
            if (is_readable($path)) {
433
                include $path;
434
                if (!empty($strings)) {
435
                    foreach ($strings as $key => $string) {
436
                        $this->strings[$key] = $string;
437
                    }
438
                }
439
            } elseif ($languageParentId > 0) {
440
                $languageParentInfo = api_get_language_info($languageParentId);
441
                $languageParentFolder = $languageParentInfo['english_name'];
442
443
                $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php";
444
                if (is_readable($parentPath)) {
445
                    include $parentPath;
446
                    if (!empty($strings)) {
447
                        foreach ($strings as $key => $string) {
448
                            $this->strings[$key] = $string;
449
                        }
450
                    }
451
                }
452
            }
453
        }
454
        if (isset($this->strings[$name])) {
455
            return $this->strings[$name];
456
        }
457
458
        return get_lang($name);
459
    }
460
461
    /**
462
     * @param string $variable
463
     * @param string $language
464
     *
465
     * @return string
466
     */
467
    public function getLangFromFile($variable, $language)
468
    {
469
        static $langStrings = [];
470
471
        if (empty($langStrings[$language])) {
472
            $root = api_get_path(SYS_PLUGIN_PATH);
473
            $pluginName = $this->get_name();
474
475
            $englishPath = "$root$pluginName/lang/$language.php";
476
477
            if (is_readable($englishPath)) {
478
                $strings = [];
479
                include $englishPath;
480
481
                $langStrings[$language] = $strings;
482
            }
483
        }
484
485
        if (isset($langStrings[$language][$variable])) {
486
            return $langStrings[$language][$variable];
487
        }
488
489
        return $this->get_lang($variable);
490
    }
491
492
    /**
493
     * Caller for the install_course_fields() function.
494
     *
495
     * @param int  $courseId
496
     * @param bool $addToolLink Whether to add a tool link on the course homepage
497
     */
498
    public function course_install($courseId, $addToolLink = true)
499
    {
500
        $this->install_course_fields($courseId, $addToolLink);
501
    }
502
503
    /**
504
     * Add course settings and, if not asked otherwise, add a tool link on the course homepage.
505
     *
506
     * @param int  $courseId      Course integer ID
507
     * @param bool $add_tool_link Whether to add a tool link or not
508
     *                            (some tools might just offer a configuration section and act on the backend)
509
     *
510
     * @return bool|null False on error, null otherwise
511
     */
512
    public function install_course_fields($courseId, $add_tool_link = true)
513
    {
514
        $plugin_name = $this->get_name();
515
        $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
516
        $courseId = (int) $courseId;
517
518
        if (empty($courseId)) {
519
            return false;
520
        }
521
522
        // Adding course settings.
523
        if (!empty($this->course_settings)) {
524
            foreach ($this->course_settings as $setting) {
525
                $variable = $setting['name'];
526
                $value = '';
527
                if (isset($setting['init_value'])) {
528
                    $value = $setting['init_value'];
529
                }
530
531
                $pluginGlobalValue = api_get_plugin_setting($plugin_name, $variable);
532
                if (null !== $pluginGlobalValue) {
533
                    $value = 1;
534
                }
535
536
                $type = 'textfield';
537
                if (isset($setting['type'])) {
538
                    $type = $setting['type'];
539
                }
540
541
                if (isset($setting['group'])) {
542
                    $group = $setting['group'];
543
                    $sql = "SELECT value
544
                            FROM $t_course
545
                            WHERE
546
                                c_id = $courseId AND
547
                                variable = '".Database::escape_string($group)."' AND
548
                                subkey = '".Database::escape_string($variable)."'
549
                            ";
550
                    $result = Database::query($sql);
551
                    if (!Database::num_rows($result)) {
552
                        $params = [
553
                            'c_id' => $courseId,
554
                            'variable' => $group,
555
                            'subkey' => $variable,
556
                            'value' => $value,
557
                            'category' => 'plugins',
558
                            'type' => $type,
559
                            'title' => '',
560
                        ];
561
                        Database::insert($t_course, $params);
562
                    }
563
                } else {
564
                    $sql = "SELECT value FROM $t_course
565
                            WHERE c_id = $courseId AND variable = '$variable' ";
566
                    $result = Database::query($sql);
567
                    if (!Database::num_rows($result)) {
568
                        $params = [
569
                            'c_id' => $courseId,
570
                            'variable' => $variable,
571
                            'subkey' => $plugin_name,
572
                            'value' => $value,
573
                            'category' => 'plugins',
574
                            'type' => $type,
575
                            'title' => '',
576
                        ];
577
                        Database::insert($t_course, $params);
578
                    }
579
                }
580
            }
581
        }
582
583
        // Stop here if we don't want a tool link on the course homepage
584
        if (!$add_tool_link || false == $this->addCourseTool) {
585
            return true;
586
        }
587
588
        // Add an icon in the table tool list
589
        $this->createLinkToCourseTool($plugin_name, $courseId);
590
    }
591
592
    /**
593
     * Delete the fields added to the course settings page and the link to the
594
     * tool on the course's homepage.
595
     *
596
     * @param int $courseId
597
     *
598
     * @return false|null
599
     */
600
    public function uninstall_course_fields($courseId)
601
    {
602
        $courseId = (int) $courseId;
603
604
        if (empty($courseId)) {
605
            return false;
606
        }
607
        $pluginName = $this->get_name();
608
609
        $t_course = Database::get_course_table(TABLE_COURSE_SETTING);
610
        $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
611
612
        if (!empty($this->course_settings)) {
613
            foreach ($this->course_settings as $setting) {
614
                $variable = Database::escape_string($setting['name']);
615
                if (!empty($setting['group'])) {
616
                    $variable = Database::escape_string($setting['group']);
617
                }
618
                if (empty($variable)) {
619
                    continue;
620
                }
621
                $sql = "DELETE FROM $t_course
622
                        WHERE c_id = $courseId AND variable = '$variable'";
623
                Database::query($sql);
624
            }
625
        }
626
627
        $pluginName = Database::escape_string($pluginName);
628
        $sql = "DELETE FROM $t_tool
629
                WHERE c_id = $courseId AND
630
                (
631
                  title = '$pluginName' OR
632
                  title = '$pluginName:student' OR
633
                  title = '$pluginName:teacher'
634
                )";
635
        Database::query($sql);
636
    }
637
638
    /**
639
     * Install the course fields and tool link of this plugin in all courses.
640
     *
641
     * @param bool $add_tool_link Whether we want to add a plugin link on the course homepage
642
     */
643
    public function install_course_fields_in_all_courses($add_tool_link = true)
644
    {
645
        $accessUrlId = api_get_current_access_url_id();
646
647
        $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
648
        $tableRel    = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
649
650
        $sql = "SELECT c.id
651
            FROM $tableCourse c
652
            INNER JOIN $tableRel r ON r.c_id = c.id
653
            WHERE r.access_url_id = ".(int)$accessUrlId."
654
            ORDER BY c.id";
655
        $res = Database::query($sql);
656
        while ($row = Database::fetch_assoc($res)) {
657
            $this->install_course_fields((int)$row['id'], $add_tool_link);
658
        }
659
    }
660
661
    /**
662
     * Uninstall the plugin settings fields from all courses.
663
     */
664
    public function uninstall_course_fields_in_all_courses()
665
    {
666
        $accessUrlId = api_get_current_access_url_id();
667
668
        $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
669
        $tableRel    = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
670
671
        $sql = "SELECT c.id
672
            FROM $tableCourse c
673
            INNER JOIN $tableRel r ON r.c_id = c.id
674
            WHERE r.access_url_id = ".(int)$accessUrlId."
675
            ORDER BY c.id";
676
677
        $res = Database::query($sql);
678
        while ($row = Database::fetch_assoc($res)) {
679
            $this->uninstall_course_fields((int)$row['id']);
680
        }
681
    }
682
683
    /**
684
     * @return array
685
     */
686
    public function getCourseSettings()
687
    {
688
        $settings = [];
689
        if (is_array($this->course_settings)) {
690
            foreach ($this->course_settings as $item) {
691
                // Skip html type
692
                if ('html' === $item['type']) {
693
                    continue;
694
                }
695
                if (isset($item['group'])) {
696
                    if (!in_array($item['group'], $settings)) {
697
                        $settings[] = $item['group'];
698
                    }
699
                } else {
700
                    $settings[] = $item['name'];
701
                }
702
            }
703
        }
704
705
        return $settings;
706
    }
707
708
    /**
709
     * Method to be extended when changing the setting in the course
710
     * configuration should trigger the use of a callback method.
711
     *
712
     * @param array $values sent back from the course configuration script
713
     */
714
    public function course_settings_updated($values = [])
715
    {
716
    }
717
718
    /**
719
     * Add a tab to the platform.
720
     */
721
    public function addTab(string $tabName, string $url, ?string $userFilter = null): bool
722
    {
723
        $settingsManager = Container::getSettingsManager();
724
725
        $currentUrl = Container::getAccessUrlUtil()->getCurrent();
726
727
        $pluginInUrl = Container::getPluginRepository()
728
            ->findOneByTitle($this->get_name())
729
            ->getConfigurationsByAccessUrl($currentUrl)
730
        ;
731
        $pluginConf = $pluginInUrl->getConfiguration();
732
733
        $showTabs = $pluginConf['show_tabs'] ?? [];
734
735
        if (!isset($showTabs[$tabName])) {
736
            $showTabs[$tabName] = $url;
737
            $pluginConf['show_tabs'] = $showTabs;
738
739
            $pluginInUrl->setConfiguration($pluginConf);
740
        }
741
742
        $showTabsSetting = $settingsManager->getSetting('display.show_tabs', true);
743
744
//        $subkey = 'custom_tab_'.$tabNum;
745
//
746
//        if (!empty($userFilter)) {
747
//            switch ($userFilter) {
748
//                case self::TAB_FILTER_NO_STUDENT:
749
//                case self::TAB_FILTER_ONLY_STUDENT:
750
//                    $subkey .= $userFilter;
751
//                    break;
752
//            }
753
//        }
754
755
        if (!in_array($tabName, $showTabsSetting)) {
756
            $showTabsSetting[] = $tabName;
757
        }
758
759
        try {
760
            $settingsManager->updateSetting('display.show_tabs', $showTabsSetting);
761
762
            Container::getEntityManager()->flush();
763
        } catch (OptimisticLockException|ORMException) {
764
            return false;
765
        }
766
767
        return true;
768
    }
769
770
    /**
771
     * Delete a tab to chamilo's platform.
772
     *
773
     * @param string $key
774
     *
775
     * @return bool $resp Transaction response
776
     */
777
    public function deleteTab($tabName): bool
778
    {
779
        $settingsManager = Container::getSettingsManager();
780
781
        $showTabsSetting = $settingsManager->getSetting('display.show_tabs', true);
782
783
        try {
784
            if (in_array($tabName, $showTabsSetting)) {
785
                $key = array_search($tabName, $showTabsSetting, true);
786
787
                if ($key !== false) {
788
                    unset($showTabsSetting[$key]);
789
790
                    $showTabsSetting = array_values($showTabsSetting);
791
                }
792
            }
793
794
            $settingsManager->updateSetting('display.show_tabs', $showTabsSetting);
795
        } catch (OptimisticLockException|ORMException) {
796
            return false;
797
        }
798
799
        return true;
800
    }
801
802
    /**
803
     * Update the tabs attributes.
804
     *
805
     * @param string $key
806
     * @param array  $attributes
807
     *
808
     * @return bool
809
     */
810
    public function updateTab($key, $attributes)
811
    {
812
        $whereCondition = [
813
            'variable = ? AND subkey = ?' => ['show_tabs', $key],
814
        ];
815
        $resp = Database::update(TABLE_MAIN_SETTINGS, $attributes, $whereCondition);
816
817
        return $resp;
818
    }
819
820
    /**
821
     * This method shows or hides plugin's tab.
822
     *
823
     * @param bool   $showTab  Shows or hides the main menu plugin tab
824
     * @param string $filePath Plugin starter file path
825
     */
826
    public function manageTab($showTab, $filePath = 'index.php')
827
    {
828
        $langString = $this->get_name();
829
        $pluginUrl = 'plugin/'.$langString.'/'.$filePath;
830
831
        if ('true' === $showTab) {
832
            $tabAdded = $this->addTab($langString, $pluginUrl);
833
            if ($tabAdded) {
834
                // The page must be refreshed to show the recently created tab
835
                echo "<script>location.href = '".Security::remove_XSS($_SERVER['REQUEST_URI'])."';</script>";
836
            }
837
        } else {
838
            $this->deleteTab($langString);
839
        }
840
    }
841
842
    /**
843
     * @param string $variable
844
     *
845
     * @return bool
846
     */
847
    public function validateCourseSetting($variable)
848
    {
849
        return true;
850
    }
851
852
    /**
853
     * @param string $region
854
     *
855
     * @return string
856
     */
857
    public function renderRegion($region)
858
    {
859
        return '';
860
    }
861
862
    /**
863
     * Returns true if the plugin is installed, false otherwise.
864
     *
865
     * @param bool $checkEnabled Also check if enabled (instead of only installed)
866
     *
867
     * @return bool True if plugin is installed/enabled, false otherwise
868
     */
869
    public function isEnabled(bool $checkEnabled = false)
870
    {
871
        $settings = $this->get_settings();
872
873
        if (empty($settings)) {
874
            return false;
875
        }
876
877
        if ($checkEnabled) {
878
            if (
879
                isset($settings['tool_enable']) &&
880
                strtolower($settings['tool_enable']) === 'false'
881
            ) {
882
                return false;
883
            }
884
        }
885
886
        return true;
887
    }
888
889
    /**
890
     * Allow make some actions after configure the plugin parameters
891
     * This function is called from main/admin/configure_plugin.php page
892
     * when saving the plugin parameters.
893
     *
894
     * @return \Plugin
895
     */
896
    public function performActionsAfterConfigure()
897
    {
898
        return $this;
899
    }
900
901
    /**
902
     * This function allows to change the visibility of the icon inside a course
903
     * :student tool will be visible only for students
904
     * :teacher tool will be visible only for teachers
905
     * If nothing it's set then tool will be visible for both as a normal icon.
906
     *
907
     * @return string
908
     */
909
    public function getToolIconVisibilityPerUserStatus()
910
    {
911
        return '';
912
    }
913
914
    /**
915
     * Default tool icon visibility.
916
     *
917
     * @return bool
918
     */
919
    public function isIconVisibleByDefault()
920
    {
921
        $v = $this->get('defaultVisibilityInCourseHomepage');
922
923
        if (null === $v) {
924
            return true;
925
        }
926
927
        return in_array($v, ['visible', 'true', true, 1, '1'], true);
928
    }
929
930
    /**
931
     * Get the admin URL for the plugin if Plugin::isAdminPlugin is true.
932
     *
933
     * @return string
934
     */
935
    public function getAdminUrl()
936
    {
937
        if (!$this->isAdminPlugin) {
938
            return '';
939
        }
940
941
        $name = $this->get_name();
942
        $sysPath = api_get_path(SYS_PLUGIN_PATH).$name;
943
        $webPath = api_get_path(WEB_PLUGIN_PATH).$name;
944
945
        if (file_exists("$sysPath/admin.php")) {
946
            return "$webPath/admin.php";
947
        }
948
949
        if (file_exists("$sysPath/start.php")) {
950
            return "$webPath/start.php";
951
        }
952
953
        return '';
954
    }
955
956
    /**
957
     * @param bool $value
958
     */
959
    public function setHasPersonalEvents($value)
960
    {
961
        $this->hasPersonalEvents = $value;
962
    }
963
964
    /**
965
     * Overwrite to perform some actions when deleting a user.
966
     *
967
     * @param int $userId
968
     */
969
    public function doWhenDeletingUser($userId)
970
    {
971
    }
972
973
    /**
974
     * Overwrite to perform some actions when deleting a course.
975
     *
976
     * @param int $courseId
977
     */
978
    public function doWhenDeletingCourse($courseId)
979
    {
980
    }
981
982
    /**
983
     * Overwrite to perform some actions when deleting a session.
984
     *
985
     * @param int $sessionId
986
     */
987
    public function doWhenDeletingSession($sessionId)
988
    {
989
    }
990
991
    /**
992
     * Add a link for a course tool.
993
     *
994
     * @param string      $name     The tool name
995
     * @param int         $courseId The course ID
996
     *
997
     * @throws NotSupported
998
     * @throws ORMException
999
     * @throws OptimisticLockException
1000
     */
1001
    protected function createLinkToCourseTool(string $name, int $courseId): ?CTool
1002
    {
1003
        if (!$this->addCourseTool) {
1004
            return null;
1005
        }
1006
1007
        $visibilityPerStatus = $this->getToolIconVisibilityPerUserStatus();
1008
        $visibility = $this->isIconVisibleByDefault();
1009
1010
        $course = api_get_course_entity($courseId);
1011
        $user = api_get_user_entity();
1012
1013
        $em = Database::getManager();
1014
1015
        $toolRepo = $em->getRepository(Tool::class);
1016
        $cToolRepo = $em->getRepository(CTool::class);
1017
1018
        /** @var CTool $cTool */
1019
        $cTool = $cToolRepo->findOneBy([
1020
            'title' => $name.$visibilityPerStatus,
1021
            'course' => $course,
1022
        ]);
1023
1024
        if (!$cTool) {
0 ignored issues
show
introduced by
$cTool is of type Chamilo\CourseBundle\Entity\CTool, thus it always evaluated to true.
Loading history...
1025
            $tool = $toolRepo->findOneBy(['title' => $name]);
1026
1027
            if (!$tool) {
1028
                $tool = new Tool();
1029
                $tool->setTitle($name);
1030
1031
                $em->persist($tool);
1032
                $em->flush();
1033
            }
1034
1035
            $cTool = new CTool();
1036
            $cTool
1037
                ->setTool($tool)
1038
                ->setTitle($name.$visibilityPerStatus)
1039
                ->setVisibility($visibility)
1040
                ->setParent($course)
1041
                ->setCreator($user)
1042
                ->addCourseLink(
1043
                    $course,
1044
                    null,
1045
                    null,
1046
                    $visibility ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT
1047
                )
1048
            ;
1049
1050
            $course->addTool($cTool);
1051
1052
            $em->persist($cTool);
1053
            $em->flush();
1054
        }
1055
1056
        return $cTool;
1057
    }
1058
1059
    public function getFieldNames(): array
1060
    {
1061
        return array_keys($this->fields);
1062
    }
1063
}
1064