Passed
Push — 1.11.x ( 1a2284...d20441 )
by Angel Fernando Quiroz
11:23
created

AppPlugin::getAllPluginCourseSettings()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/* See license terms in /license.txt */
3
4
use ChamiloSession as Session;
5
6
/**
7
 * Class AppPlugin.
8
 */
9
class AppPlugin
10
{
11
    public $plugin_regions = [
12
        'main_top',
13
        'main_bottom',
14
        'login_top',
15
        'login_bottom',
16
        'menu_top',
17
        'menu_bottom',
18
        'content_top',
19
        'content_bottom',
20
        'header_main',
21
        'header_center',
22
        'header_left',
23
        'header_right',
24
        'pre_footer',
25
        'footer_left',
26
        'footer_center',
27
        'footer_right',
28
        'menu_administrator',
29
        'course_tool_plugin',
30
    ];
31
32
    public $installedPluginListName = [];
33
    public $installedPluginListObject = [];
34
    private static $instance;
35
36
    /**
37
     * Constructor.
38
     */
39
    public function __construct()
40
    {
41
    }
42
43
    /**
44
     * @return AppPlugin
45
     */
46
    public static function getInstance()
47
    {
48
        if (!isset(self::$instance)) {
49
            self::$instance = new self();
50
        }
51
52
        return self::$instance;
53
    }
54
55
    /**
56
     * Read plugin from path.
57
     *
58
     * @return array
59
     */
60
    public function read_plugins_from_path()
61
    {
62
        /* We scan the plugin directory. Each folder is a potential plugin. */
63
        $pluginPath = api_get_path(SYS_PLUGIN_PATH);
64
        $plugins = [];
65
        $handle = @opendir($pluginPath);
66
        while (false !== ($file = readdir($handle))) {
67
            if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
68
                $plugins[] = $file;
69
            }
70
        }
71
        @closedir($handle);
0 ignored issues
show
Bug introduced by
Are you sure the usage of closedir($handle) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for closedir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

71
        /** @scrutinizer ignore-unhandled */ @closedir($handle);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
72
        sort($plugins);
73
74
        return $plugins;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function getInstalledPluginListName()
81
    {
82
        if (empty($this->installedPluginListName)) {
83
            $this->installedPluginListName = $this->getInstalledPlugins();
84
        }
85
86
        return $this->installedPluginListName;
87
    }
88
89
    /**
90
     * @return array List of Plugin
91
     */
92
    public function getInstalledPluginListObject()
93
    {
94
        if (empty($this->installedPluginListObject)) {
95
            $this->setInstalledPluginListObject();
96
        }
97
98
        return $this->installedPluginListObject;
99
    }
100
101
    public function setInstalledPluginListObject()
102
    {
103
        $pluginListName = $this->getInstalledPluginListName();
104
        $pluginList = [];
105
        if (!empty($pluginListName)) {
106
            foreach ($pluginListName as $pluginName) {
107
                $pluginInfo = $this->getPluginInfo($pluginName, true);
108
                if (isset($pluginInfo['plugin_class'])) {
109
                    $pluginList[] = $pluginInfo['plugin_class']::create();
110
                }
111
            }
112
        }
113
        $this->installedPluginListObject = $pluginList;
114
    }
115
116
    /**
117
     * @param string $plugin
118
     *
119
     * @return bool
120
     */
121
    public function isInstalled($plugin)
122
    {
123
        $list = self::getInstalledPlugins(false);
0 ignored issues
show
Bug Best Practice introduced by
The method AppPlugin::getInstalledPlugins() is not static, but was called statically. ( Ignorable by Annotation )

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

123
        /** @scrutinizer ignore-call */ 
124
        $list = self::getInstalledPlugins(false);
Loading history...
124
125
        return in_array($plugin, $list);
126
    }
127
128
    /**
129
     * Returns a list of all installed plugins.
130
     *
131
     * @param bool $fromDatabase
132
     *
133
     * @return array
134
     */
135
    public function getInstalledPlugins($fromDatabase = true)
136
    {
137
        static $installedPlugins = null;
138
139
        if ($fromDatabase === false) {
140
            if (is_array($installedPlugins)) {
141
                return $installedPlugins;
142
            }
143
        }
144
145
        if ($fromDatabase || $installedPlugins === null) {
146
            $installedPlugins = [];
147
148
            /*if (api_is_multiple_url_enabled()) {
149
                $urlId = api_get_current_access_url_id();
150
                $plugins = api_get_settings_params(
151
                    [
152
                        'variable = ? AND selected_value = ? AND category = ? AND access_url = ? ' => [
153
                            'status',
154
                            'installed',
155
                            'Plugins',
156
                            $urlId,
157
                        ],
158
                    ]
159
                );
160
            } else {*/
161
            $plugins = api_get_settings_params(
162
                    [
163
                        'variable = ? AND selected_value = ? AND category = ? ' => ['status', 'installed', 'Plugins'],
164
                    ]
165
                );
166
            //}
167
168
            if (!empty($plugins)) {
169
                foreach ($plugins as $row) {
170
                    $installedPlugins[$row['subkey']] = true;
171
                }
172
                $installedPlugins = array_keys($installedPlugins);
173
            }
174
        }
175
176
        return $installedPlugins;
177
    }
178
179
    public function getInstalledPluginsInCurrentUrl()
180
    {
181
        $installedPlugins = [];
182
        $urlId = api_get_current_access_url_id();
183
        $plugins = api_get_settings_params(
184
            [
185
                'variable = ? AND selected_value = ? AND category = ? AND access_url = ?' => ['status', 'installed', 'Plugins', $urlId],
186
            ]
187
        );
188
189
        if (!empty($plugins)) {
190
            foreach ($plugins as $row) {
191
                $installedPlugins[$row['subkey']] = true;
192
            }
193
            $installedPlugins = array_keys($installedPlugins);
194
        }
195
196
        return $installedPlugins;
197
    }
198
199
    /**
200
     * Returns a list of all official (delivered with the Chamilo package)
201
     * plugins. This list is maintained manually and updated with every new
202
     * release to avoid hacking.
203
     *
204
     * @return array
205
     */
206
    public function getOfficialPlugins()
207
    {
208
        static $officialPlugins = null;
209
        // Please keep this list alphabetically sorted
210
        $officialPlugins = [
211
            'add_cas_login_button',
212
            'add_cas_logout_button',
213
            'add_facebook_login_button',
214
            'add_shibboleth_login_button',
215
            'advanced_subscription',
216
            'azure_active_directory',
217
            'bbb',
218
            'before_login',
219
            'buycourses',
220
            'card_game',
221
            'check_extra_field_author_company',
222
            'cleandeletedfiles',
223
            'clockworksms',
224
            'courseblock',
225
            'coursehomenotify',
226
            'courselegal',
227
            'createdrupaluser',
228
            'customcertificate',
229
            'customfooter',
230
            'dashboard',
231
            'date',
232
            'dictionary',
233
            'embedregistry',
234
            'exercise_signature',
235
            'ext_auth_chamilo_logout_button_behaviour',
236
            'follow_buttons',
237
            'formLogin_hide_unhide',
238
            'google_maps',
239
            'google_meet',
240
            'grading_electronic',
241
            'h5p',
242
            'hello_world',
243
            'ims_lti',
244
            'justification',
245
            'kannelsms',
246
            'keycloak',
247
            'learning_calendar',
248
            'lti_provider',
249
            'maintenancemode',
250
            'migrationmoodle',
251
            'mindmap',
252
            'nosearchindex',
253
            'notebookteacher',
254
            'oauth2',
255
            'olpc_peru_filter',
256
            'onlyoffice',
257
            'openmeetings',
258
            'pausetraining',
259
            'pens',
260
            'positioning',
261
            'questionoptionsevaluation',
262
            'redirection',
263
            'remedial_course',
264
            'reports',
265
            'resubscription',
266
            'rss',
267
            'search_course',
268
            'send_notification_new_lp',
269
            'sepe',
270
            'share_buttons',
271
            'show_regions',
272
            'show_user_info',
273
            'static',
274
            'studentfollowup',
275
            'surveyexportcsv',
276
            'surveyexporttxt',
277
            'test2pdf',
278
            'toplinks',
279
            'tour',
280
            'userremoteservice',
281
            'vchamilo',
282
            'whispeakauth',
283
            'zoom',
284
            'xapi',
285
        ];
286
287
        return $officialPlugins;
288
    }
289
290
    /**
291
     * @param string $pluginName
292
     * @param int    $urlId
293
     */
294
    public function install($pluginName, $urlId = null)
295
    {
296
        $urlId = (int) $urlId;
297
        if (empty($urlId)) {
298
            $urlId = api_get_current_access_url_id();
299
        }
300
301
        api_add_setting(
302
            'installed',
303
            'status',
304
            $pluginName,
305
            'setting',
306
            'Plugins',
307
            $pluginName,
308
            '',
309
            '',
310
            '',
311
            $urlId,
312
            1
313
        );
314
315
        $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/install.php';
316
317
        if (is_file($pluginPath) && is_readable($pluginPath)) {
318
            // Execute the install procedure.
319
320
            require $pluginPath;
321
        }
322
    }
323
324
    /**
325
     * @param string $pluginName
326
     * @param int    $urlId
327
     */
328
    public function uninstall($pluginName, $urlId = null)
329
    {
330
        $urlId = (int) $urlId;
331
        if (empty($urlId)) {
332
            $urlId = api_get_current_access_url_id();
333
        }
334
335
        // First call the custom uninstall to allow full access to global settings
336
        $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/uninstall.php';
337
        if (is_file($pluginPath) && is_readable($pluginPath)) {
338
            // Execute the uninstall procedure.
339
340
            require $pluginPath;
341
        }
342
343
        // Second remove all remaining global settings
344
        api_delete_settings_params(
345
            ['category = ? AND access_url = ? AND subkey = ? ' => ['Plugins', $urlId, $pluginName]]
346
        );
347
    }
348
349
    /**
350
     * @param string $pluginName
351
     *
352
     * @return array
353
     */
354
    public function get_areas_by_plugin($pluginName)
355
    {
356
        $result = api_get_settings('Plugins');
357
        $areas = [];
358
        foreach ($result as $row) {
359
            if ($pluginName == $row['selected_value']) {
360
                $areas[] = $row['variable'];
361
            }
362
        }
363
364
        return $areas;
365
    }
366
367
    /**
368
     * @param string $pluginName
369
     *
370
     * @return bool
371
     */
372
    public function is_valid_plugin($pluginName)
373
    {
374
        if (is_dir(api_get_path(SYS_PLUGIN_PATH).$pluginName)) {
375
            if (is_file(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/index.php')) {
376
                return true;
377
            }
378
        }
379
380
        return false;
381
    }
382
383
    /**
384
     * @return array
385
     */
386
    public function get_plugin_regions()
387
    {
388
        sort($this->plugin_regions);
389
390
        return $this->plugin_regions;
391
    }
392
393
    /**
394
     * @param string   $region
395
     * @param Template $template
396
     * @param bool     $forced
397
     *
398
     * @return string|null
399
     */
400
    public function load_region($region, $template, $forced = false)
401
    {
402
        if ($region == 'course_tool_plugin') {
403
            return '';
404
        }
405
406
        ob_start();
407
        $this->get_all_plugin_contents_by_region($region, $template, $forced);
408
        $content = ob_get_contents();
409
        ob_end_clean();
410
411
        return $content;
412
    }
413
414
    /**
415
     * Loads the translation files inside a plugin if exists.
416
     * It loads by default english see the hello world plugin.
417
     *
418
     * @param string $plugin_name
419
     *
420
     * @todo add caching
421
     */
422
    public function load_plugin_lang_variables($plugin_name)
423
    {
424
        global $language_interface;
425
        $root = api_get_path(SYS_PLUGIN_PATH);
426
        $strings = null;
427
428
        // 1. Loading english if exists
429
        $english_path = $root.$plugin_name."/lang/english.php";
430
431
        if (is_readable($english_path)) {
432
            include $english_path;
433
434
            foreach ($strings as $key => $string) {
0 ignored issues
show
Bug introduced by
The expression $strings of type null is not traversable.
Loading history...
435
                $GLOBALS[$key] = $string;
436
            }
437
        }
438
439
        // 2. Loading the system language
440
        if ($language_interface != 'english') {
441
            $path = $root.$plugin_name."/lang/$language_interface.php";
442
443
            if (is_readable($path)) {
444
                include $path;
445
                if (!empty($strings)) {
446
                    foreach ($strings as $key => $string) {
447
                        $GLOBALS[$key] = $string;
448
                    }
449
                }
450
            } else {
451
                $interfaceLanguageId = api_get_language_id($language_interface);
452
                $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId);
453
                $languageParentId = intval($interfaceLanguageInfo['parent_id']);
454
455
                if ($languageParentId > 0) {
456
                    $languageParentInfo = api_get_language_info($languageParentId);
457
                    $languageParentFolder = $languageParentInfo['dokeos_folder'];
458
459
                    $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php";
460
                    if (is_readable($parentPath)) {
461
                        include $parentPath;
462
                        if (!empty($strings)) {
463
                            foreach ($strings as $key => $string) {
464
                                $this->strings[$key] = $string;
465
                            }
466
                        }
467
                    }
468
                }
469
            }
470
        }
471
    }
472
473
    /**
474
     * @param string   $region
475
     * @param Template $template
476
     * @param bool     $forced
477
     *
478
     * @return bool
479
     *
480
     * @todo improve this function
481
     */
482
    public function get_all_plugin_contents_by_region($region, $template, $forced = false)
483
    {
484
        global $_plugins;
485
        if (isset($_plugins[$region]) && is_array($_plugins[$region])) {
486
            // Load the plugin information
487
            foreach ($_plugins[$region] as $plugin_name) {
488
                // The plugin_info variable is available inside the plugin index
489
                $plugin_info = $this->getPluginInfo($plugin_name, $forced);
490
491
                // We also know where the plugin is
492
                $plugin_info['current_region'] = $region;
493
494
                // Loading the plugin/XXX/index.php file
495
                $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php";
496
497
                if (file_exists($plugin_file)) {
498
                    //Loading the lang variables of the plugin if exists
499
                    self::load_plugin_lang_variables($plugin_name);
0 ignored issues
show
Bug Best Practice introduced by
The method AppPlugin::load_plugin_lang_variables() is not static, but was called statically. ( Ignorable by Annotation )

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

499
                    self::/** @scrutinizer ignore-call */ 
500
                          load_plugin_lang_variables($plugin_name);
Loading history...
500
501
                    // Printing the plugin index.php file
502
                    require $plugin_file;
503
504
                    // If the variable $_template is set we assign those values to be accessible in Twig
505
                    if (isset($_template)) {
506
                        $_template['plugin_info'] = $plugin_info;
507
                    } else {
508
                        $_template = [];
509
                        $_template['plugin_info'] = $plugin_info;
510
                    }
511
512
                    // Setting the plugin info available in the template if exists.
513
                    $template->assign($plugin_name, $_template);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_template does not seem to be defined for all execution paths leading up to this point.
Loading history...
514
515
                    // Loading the Twig template plugin files if exists
516
                    $template_list = [];
517
                    if (isset($plugin_info) && isset($plugin_info['templates'])) {
518
                        $template_list = $plugin_info['templates'];
519
                    }
520
521
                    if (!empty($template_list)) {
522
                        foreach ($template_list as $plugin_tpl) {
523
                            if (!empty($plugin_tpl)) {
524
                                $template_plugin_file = "$plugin_name/$plugin_tpl"; // for twig
525
                                $template->display($template_plugin_file, false);
526
                            }
527
                        }
528
                    }
529
                }
530
            }
531
        }
532
533
        return true;
534
    }
535
536
    /**
537
     * Loads plugin info.
538
     *
539
     * @staticvar array $plugin_data
540
     *
541
     * @param string $plugin_name
542
     * @param bool   $forced      load from DB or from the static array
543
     *
544
     * @return array
545
     *
546
     * @todo filter setting_form
547
     */
548
    public function getPluginInfo($plugin_name, $forced = false)
549
    {
550
        $pluginData = Session::read('plugin_data');
551
        if (isset($pluginData[$plugin_name]) && $forced == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
552
            return $pluginData[$plugin_name];
553
        } else {
554
            $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/plugin.php";
555
556
            $plugin_info = [];
557
            if (file_exists($plugin_file)) {
558
                require $plugin_file;
559
            }
560
561
            // @todo check if settings are already added
562
            // Extra options
563
            $plugin_settings = api_get_settings_params(
564
                [
565
                    "subkey = ? AND category = ? AND type = ? AND access_url = ?" => [
566
                        $plugin_name,
567
                        'Plugins',
568
                        'setting',
569
                        api_get_current_access_url_id(),
570
                    ],
571
                ]
572
            );
573
574
            $settings_filtered = [];
575
            foreach ($plugin_settings as $item) {
576
                if (!empty($item['selected_value'])) {
577
                    $unserialized = UnserializeApi::unserialize('not_allowed_classes', $item['selected_value'], true);
578
                    if (false !== $unserialized) {
579
                        $item['selected_value'] = $unserialized;
580
                    }
581
                }
582
                $settings_filtered[$item['variable']] = $item['selected_value'];
583
            }
584
            $plugin_info['settings'] = $settings_filtered;
585
            $pluginData[$plugin_name] = $plugin_info;
586
            Session::write('plugin_data', $pluginData);
587
588
            return $plugin_info;
589
        }
590
    }
591
592
    /**
593
     * Get the template list.
594
     *
595
     * @param string $pluginName
596
     *
597
     * @return bool
598
     */
599
    public function get_templates_list($pluginName)
600
    {
601
        $plugin_info = $this->getPluginInfo($pluginName);
602
        if (isset($plugin_info) && isset($plugin_info['templates'])) {
603
            return $plugin_info['templates'];
604
        }
605
606
        return false;
607
    }
608
609
    /**
610
     * Remove all regions of an specific plugin.
611
     *
612
     * @param string $plugin
613
     */
614
    public function remove_all_regions($plugin)
615
    {
616
        $access_url_id = api_get_current_access_url_id();
617
        if (!empty($plugin)) {
618
            api_delete_settings_params(
619
                [
620
                    'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => [
621
                        'Plugins',
622
                        'region',
623
                        $access_url_id,
624
                        $plugin,
625
                    ],
626
                ]
627
            );
628
        }
629
    }
630
631
    /**
632
     * Add a plugin to a region.
633
     *
634
     * @param string $plugin
635
     * @param string $region
636
     */
637
    public function add_to_region($plugin, $region)
638
    {
639
        api_add_setting(
640
            $plugin,
641
            $region,
642
            $plugin,
643
            'region',
644
            'Plugins',
645
            $plugin,
646
            '',
647
            '',
648
            '',
649
            api_get_current_access_url_id(),
650
            1
651
        );
652
    }
653
654
    /**
655
     * @param int $courseId
656
     */
657
    public function install_course_plugins($courseId)
658
    {
659
        $pluginList = $this->getInstalledPluginListObject();
660
661
        if (!empty($pluginList)) {
662
            /** @var Plugin $obj */
663
            foreach ($pluginList as $obj) {
664
                $pluginName = $obj->get_name();
665
                $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
666
667
                if (file_exists($plugin_path)) {
668
                    require $plugin_path;
669
                    if (isset($plugin_info) && isset($plugin_info['plugin_class']) && $obj->isCoursePlugin) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $plugin_info does not exist. Did you maybe mean $plugin_path?
Loading history...
670
                        $obj->course_install($courseId);
671
                    }
672
                }
673
            }
674
        }
675
    }
676
677
    /**
678
     * Trigger for Plugin::doWhenDeleting[Item] functions.
679
     *
680
     * @param string $itemType
681
     * @param int    $itemId
682
     */
683
    public function performActionsWhenDeletingItem($itemType, $itemId)
684
    {
685
        $pluginList = $this->getInstalledPluginListObject();
686
687
        if (empty($pluginList)) {
688
            return;
689
        }
690
691
        /** @var Plugin $pluginObj */
692
        foreach ($pluginList as $pluginObj) {
693
            switch ($itemType) {
694
                case 'course':
695
                    $pluginObj->doWhenDeletingCourse($itemId);
696
                    break;
697
                case 'session':
698
                    $pluginObj->doWhenDeletingSession($itemId);
699
                    break;
700
                case 'user':
701
                    $pluginObj->doWhenDeletingUser($itemId);
702
                    break;
703
            }
704
        }
705
    }
706
707
    /**
708
     * Add the course settings to the course settings form.
709
     *
710
     * @param FormValidator $form
711
     */
712
    public function add_course_settings_form($form)
713
    {
714
        $pluginList = $this->getInstalledPluginListObject();
715
        /** @var Plugin $obj */
716
        foreach ($pluginList as $obj) {
717
            $plugin_name = $obj->get_name();
718
            $pluginTitle = $obj->get_title();
719
            if (!empty($obj->course_settings)) {
720
                if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$plugin_name.'.png')) {
721
                    $icon = Display::return_icon(
722
                        $plugin_name.'.png',
723
                        Security::remove_XSS($pluginTitle),
724
                        '',
725
                        ICON_SIZE_SMALL
726
                    );
727
                } else {
728
                    $icon = Display::return_icon(
729
                        'plugins.png',
730
                        Security::remove_XSS($pluginTitle),
731
                        '',
732
                        ICON_SIZE_SMALL
733
                    );
734
                }
735
736
                $form->addHtml('<div class="panel panel-default">');
737
                $form->addHtml('
738
                    <div class="panel-heading" role="tab" id="heading-'.$plugin_name.'-settings">
739
                        <h4 class="panel-title">
740
                            <a class="collapsed"
741
                                role="button" data-toggle="collapse" data-parent="#accordion"
742
                                href="#collapse-'.$plugin_name.'-settings" aria-expanded="false"
743
                                aria-controls="collapse-'.$plugin_name.'-settings">
744
                ');
745
                $form->addHtml($icon.' '.$pluginTitle);
746
                $form->addHtml('
747
                            </a>
748
                        </h4>
749
                    </div>
750
                ');
751
                $form->addHtml('
752
                    <div
753
                        id="collapse-'.$plugin_name.'-settings"
754
                        class="panel-collapse collapse" role="tabpanel"
755
                        aria-labelledby="heading-'.$plugin_name.'-settings">
756
                        <div class="panel-body">
757
                ');
758
759
                $groups = [];
760
                foreach ($obj->course_settings as $setting) {
761
                    if ($obj->validateCourseSetting($setting['name']) === false) {
762
                        continue;
763
                    }
764
                    if ($setting['type'] !== 'checkbox') {
765
                        $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name']));
766
                    } else {
767
                        $element = &$form->createElement(
768
                            $setting['type'],
769
                            $setting['name'],
770
                            '',
771
                            $obj->get_lang($setting['name'])
772
                        );
773
774
                        // Check global settings
775
                        $courseSetting = api_get_course_setting($setting['name']);
776
                        if (-1 === $courseSetting) {
777
                            $defaultValue = api_get_plugin_setting($plugin_name, $setting['name']);
778
                            if (!empty($defaultValue)) {
779
                                if ('true' === $defaultValue) {
780
                                    $element->setChecked(true);
781
                                }
782
                            }
783
                        }
784
785
                        if (isset($setting['init_value']) && $setting['init_value'] == 1) {
786
                            $element->setChecked(true);
787
                        }
788
789
                        $form->addElement($element);
790
                        if (isset($setting['group'])) {
791
                            $groups[$setting['group']][] = $element;
792
                        }
793
                    }
794
                }
795
                foreach ($groups as $k => $v) {
796
                    $form->addGroup($groups[$k], $k, [$obj->get_lang($k)]);
797
                }
798
                $form->addButtonSave(get_lang('SaveSettings'));
799
                $form->addHtml('
800
                        </div>
801
                    </div>
802
                ');
803
                $form->addHtml('</div>');
804
            }
805
        }
806
    }
807
808
    /**
809
     * Get all course settings from all installed plugins.
810
     *
811
     * @return array
812
     */
813
    public function getAllPluginCourseSettings()
814
    {
815
        $pluginList = $this->getInstalledPluginListObject();
816
        /** @var Plugin $obj */
817
        $courseSettings = [];
818
        if (!empty($pluginList)) {
819
            foreach ($pluginList as $obj) {
820
                $pluginCourseSetting = $obj->getCourseSettings();
821
                $courseSettings = array_merge($courseSettings, $pluginCourseSetting);
822
            }
823
        }
824
825
        return $courseSettings;
826
    }
827
828
    /**
829
     * When saving the plugin values in the course settings, check whether
830
     * a callback method should be called and send it the updated settings.
831
     *
832
     * @param array $values The new settings the user just saved
833
     */
834
    public function saveCourseSettingsHook($values)
835
    {
836
        $pluginList = $this->getInstalledPluginListObject();
837
838
        /** @var Plugin $obj */
839
        foreach ($pluginList as $obj) {
840
            $settings = $obj->getCourseSettings();
841
            $subValues = [];
842
            if (!empty($settings)) {
843
                foreach ($settings as $v) {
844
                    if (isset($values[$v])) {
845
                        $subValues[$v] = $values[$v];
846
                    }
847
                }
848
            }
849
850
            if (!empty($subValues)) {
851
                $obj->course_settings_updated($subValues);
852
            }
853
        }
854
    }
855
856
    /**
857
     * Get first SMS plugin name.
858
     *
859
     * @return string|bool
860
     */
861
    public function getSMSPluginName()
862
    {
863
        $installedPluginsList = $this->getInstalledPluginListObject();
864
        foreach ($installedPluginsList as $installedPlugin) {
865
            if ($installedPlugin->isMailPlugin) {
866
                return get_class($installedPlugin);
867
            }
868
        }
869
870
        return false;
871
    }
872
873
    /**
874
     * @return SmsPluginLibraryInterface
875
     */
876
    public function getSMSPluginLibrary()
877
    {
878
        $className = $this->getSMSPluginName();
879
        $className = str_replace('Plugin', '', $className);
880
881
        if (class_exists($className)) {
882
            return new $className();
883
        }
884
885
        return false;
886
    }
887
888
    public static function cleanEntitiesInBundle()
889
    {
890
        $pluginList = [
891
            'CourseHomeNotify',
892
            'EmbedRegistry',
893
            'ImsLti',
894
            'LtiProvider',
895
            'StudentFollowUp',
896
            'WhispeakAuth',
897
        ];
898
899
        foreach ($pluginList as $pluginName) {
900
            $entityPath = api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$pluginName;
901
902
            if (!is_dir($entityPath)) {
903
                continue;
904
            }
905
906
            if (!is_writable($entityPath)) {
907
                continue;
908
            }
909
910
            rmdirr($entityPath);
911
        }
912
    }
913
}
914