Passed
Push — master ( efeeba...65498c )
by Julito
10:04
created

AppPlugin::getSMSPluginName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 10
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
     * @deprecated
130
     */
131
    public function get_installed_plugins($fromDatabase = true)
132
    {
133
        return $this->getInstalledPlugins($fromDatabase);
134
    }
135
136
    /**
137
     * @param bool $fromDatabase
138
     *
139
     * @return array
140
     */
141
    public function getInstalledPlugins($fromDatabase = true)
142
    {
143
        // @todo restore plugin loading
144
        return [];
145
146
        static $installedPlugins = null;
0 ignored issues
show
Unused Code introduced by
StaticNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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

483
            self::/** @scrutinizer ignore-call */ 
484
                  load_plugin_lang_variables($plugin_name);
Loading history...
484
485
            // Printing the plugin index.php file
486
            require $plugin_file;
487
488
            // If the variable $_template is set we assign those values to be accessible in Twig
489
            if (isset($_template)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_template seems to never exist and therefore isset should always be false.
Loading history...
490
                $_template['plugin_info'] = $plugin_info;
491
            } else {
492
                $_template = [];
493
                $_template['plugin_info'] = $plugin_info;
494
            }
495
496
            // Setting the plugin info available in the template if exists.
497
            //$template->addGlobal($plugin_name, $_template);
498
499
            // Loading the Twig template plugin files if exists
500
            $templateList = [];
501
            if (isset($plugin_info) && isset($plugin_info['templates'])) {
502
                $templateList = $plugin_info['templates'];
503
            }
504
505
            if (!empty($templateList)) {
506
                foreach ($templateList as $pluginTemplate) {
507
                    if (!empty($pluginTemplate)) {
508
                        $templatePluginFile = "$plugin_name/$pluginTemplate"; // for twig
509
                        //$template->render($templatePluginFile, []);
510
                    }
511
                }
512
            }
513
        }
514
515
        return true;
516
    }
517
518
    /**
519
     * Loads plugin info.
520
     *
521
     * @staticvar array $plugin_data
522
     *
523
     * @param string $pluginName
524
     * @param bool   $forced     load from DB or from the static array
525
     *
526
     * @return array
527
     *
528
     * @todo filter setting_form
529
     */
530
    public function getPluginInfo($pluginName, $forced = false)
531
    {
532
        //$pluginData = Session::read('plugin_data');
533
        if (0) {
534
            //if (isset($pluginData[$pluginName]) && $forced == false) {
535
            return $pluginData[$pluginName];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pluginData seems to be never defined.
Loading history...
536
        } else {
537
            $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$pluginName/plugin.php";
538
539
            $plugin_info = [];
540
            if (file_exists($plugin_file)) {
541
                require $plugin_file;
542
            }
543
544
            // @todo check if settings are already added
545
            // Extra options
546
            $plugin_settings = api_get_settings_params(
547
                [
548
                    'subkey = ? AND category = ? AND type = ? AND access_url = ?' => [
549
                        $pluginName,
550
                        'Plugins',
551
                        'setting',
552
                        api_get_current_access_url_id(),
553
                    ],
554
                ]
555
            );
556
557
            $settings_filtered = [];
558
            foreach ($plugin_settings as $item) {
559
                if (!empty($item['selected_value'])) {
560
                    //if (unserialize($item['selected_value']) !== false) {
561
                        //$item['selected_value'] = unserialize($item['selected_value']);
562
                    //}
563
                }
564
                $settings_filtered[$item['variable']] = $item['selected_value'];
565
            }
566
567
            $plugin_info['settings'] = $settings_filtered;
568
            $pluginData[$pluginName] = $plugin_info;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$pluginData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pluginData = array(); before regardless.
Loading history...
569
            //Session::write('plugin_data', $pluginData);
570
571
            return $plugin_info;
572
        }
573
    }
574
575
    /**
576
     * Get the template list.
577
     *
578
     * @param string $pluginName
579
     *
580
     * @return bool
581
     */
582
    public function get_templates_list($pluginName)
583
    {
584
        $plugin_info = $this->getPluginInfo($pluginName);
585
        if (isset($plugin_info) && isset($plugin_info['templates'])) {
586
            return $plugin_info['templates'];
587
        }
588
589
        return false;
590
    }
591
592
    /**
593
     * Remove all regions of an specific plugin.
594
     *
595
     * @param string $plugin
596
     */
597
    public function removeAllRegions($plugin)
598
    {
599
        $access_url_id = api_get_current_access_url_id();
600
        if (!empty($plugin)) {
601
            api_delete_settings_params(
602
                [
603
                    'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => [
604
                        'Plugins',
605
                        'region',
606
                        $access_url_id,
607
                        $plugin,
608
                    ],
609
                ]
610
            );
611
        }
612
    }
613
614
    /**
615
     * Add a plugin to a region.
616
     *
617
     * @param string $plugin
618
     * @param string $region
619
     */
620
    public function add_to_region($plugin, $region)
621
    {
622
        api_add_setting(
623
            $plugin,
624
            $region,
625
            $plugin,
626
            'region',
627
            'Plugins',
628
            $plugin,
629
            '',
630
            '',
631
            '',
632
            api_get_current_access_url_id(),
633
            1
634
        );
635
    }
636
637
    /**
638
     * @param int $courseId
639
     */
640
    public function install_course_plugins($courseId)
641
    {
642
        $pluginList = $this->getInstalledPluginListObject();
643
644
        if (!empty($pluginList)) {
645
            /** @var Plugin $obj */
646
            foreach ($pluginList as $obj) {
647
                $pluginName = $obj->get_name();
648
                $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
649
650
                if (file_exists($plugin_path)) {
651
                    require $plugin_path;
652
                    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...
653
                        $obj->course_install($courseId);
654
                    }
655
                }
656
            }
657
        }
658
    }
659
660
    /**
661
     * Trigger for Plugin::doWhenDeleting[Item] functions.
662
     *
663
     * @param string $itemType
664
     * @param int    $itemId
665
     */
666
    public function performActionsWhenDeletingItem($itemType, $itemId)
667
    {
668
        $pluginList = $this->getInstalledPluginListObject();
669
670
        if (empty($pluginList)) {
671
            return;
672
        }
673
674
        /** @var Plugin $pluginObj */
675
        foreach ($pluginList as $pluginObj) {
676
            switch ($itemType) {
677
                case 'course':
678
                    $pluginObj->doWhenDeletingCourse($itemId);
679
                    break;
680
                case 'session':
681
                    $pluginObj->doWhenDeletingSession($itemId);
682
                    break;
683
                case 'user':
684
                    $pluginObj->doWhenDeletingUser($itemId);
685
                    break;
686
            }
687
        }
688
    }
689
690
    /**
691
     * Add the course settings to the course settings form.
692
     *
693
     * @param FormValidator $form
694
     */
695
    public function add_course_settings_form($form)
696
    {
697
        $pluginList = $this->getInstalledPluginListObject();
698
        /** @var Plugin $obj */
699
        foreach ($pluginList as $obj) {
700
            $pluginName = $obj->get_name();
701
            $pluginTitle = $obj->get_title();
702
            if (!empty($obj->course_settings)) {
703
                if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$pluginName.'.png')) {
704
                    $icon = Display::return_icon(
705
                        $pluginName.'.png',
706
                        Security::remove_XSS($pluginTitle),
707
                        '',
708
                        ICON_SIZE_SMALL
709
                    );
710
                } else {
711
                    $icon = Display::return_icon(
712
                        'plugins.png',
713
                        Security::remove_XSS($pluginTitle),
714
                        '',
715
                        ICON_SIZE_SMALL
716
                    );
717
                }
718
719
                $form->addHtml('<div class="panel panel-default">');
720
                $form->addHtml('
721
                    <div class="panel-heading" role="tab" id="heading-'.$pluginName.'-settings">
722
                        <h4 class="panel-title">
723
                            <a class="collapsed"
724
                                role="button" data-toggle="collapse" data-parent="#accordion"
725
                                href="#collapse-'.$pluginName.'-settings" aria-expanded="false"
726
                                aria-controls="collapse-'.$pluginName.'-settings">
727
                ');
728
                $form->addHtml($icon.' '.$pluginTitle);
729
                $form->addHtml('
730
                            </a>
731
                        </h4>
732
                    </div>
733
                ');
734
                $form->addHtml('
735
                    <div
736
                        id="collapse-'.$pluginName.'-settings"
737
                        class="panel-collapse collapse" role="tabpanel"
738
                        aria-labelledby="heading-'.$pluginName.'-settings">
739
                        <div class="panel-body">
740
                '
741
                );
742
743
                $groups = [];
744
                foreach ($obj->course_settings as $setting) {
745
                    if (false === $obj->validateCourseSetting($setting['name'])) {
746
                        continue;
747
                    }
748
                    if ('checkbox' !== $setting['type']) {
749
                        $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name']));
750
                    } else {
751
                        $element = &$form->createElement(
752
                            $setting['type'],
753
                            $setting['name'],
754
                            '',
755
                            $obj->get_lang($setting['name'])
756
                        );
757
                        $courseSetting = api_get_course_setting($setting['name']);
758
                        if (-1 === $courseSetting) {
759
                            $defaultValue = api_get_plugin_setting($plugin_name, $setting['name']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $plugin_name does not exist. Did you maybe mean $pluginName?
Loading history...
760
                            if (!empty($defaultValue)) {
761
                                if ('true' === $defaultValue) {
762
                                    $element->setChecked(true);
763
                                }
764
                            }
765
                        }
766
767
                        if (isset($setting['init_value']) && 1 == $setting['init_value']) {
768
                            $element->setChecked(true);
769
                        }
770
                        $form->addElement($element);
771
772
                        if (isset($setting['group'])) {
773
                            $groups[$setting['group']][] = $element;
774
                        }
775
                    }
776
                }
777
                foreach ($groups as $k => $v) {
778
                    $form->addGroup($groups[$k], $k, [$obj->get_lang($k)]);
779
                }
780
                $form->addButtonSave(get_lang('Save settings'));
781
                $form->addHtml(
782
            '
783
                        </div>
784
                    </div>
785
                '
786
        );
787
                $form->addHtml('</div>');
788
            }
789
        }
790
    }
791
792
    /**
793
     * Get all course settings from all installed plugins.
794
     *
795
     * @return array
796
     */
797
    public function getAllPluginCourseSettings()
798
    {
799
        $pluginList = $this->getInstalledPluginListObject();
800
        /** @var Plugin $obj */
801
        $courseSettings = [];
802
        if (!empty($pluginList)) {
803
            foreach ($pluginList as $obj) {
804
                $pluginCourseSetting = $obj->getCourseSettings();
805
                $courseSettings = array_merge($courseSettings, $pluginCourseSetting);
806
            }
807
        }
808
809
        return $courseSettings;
810
    }
811
812
    /**
813
     * When saving the plugin values in the course settings, check whether
814
     * a callback method should be called and send it the updated settings.
815
     *
816
     * @param array $values The new settings the user just saved
817
     */
818
    public function saveCourseSettingsHook($values)
819
    {
820
        $pluginList = $this->getInstalledPluginListObject();
821
822
        /** @var Plugin $obj */
823
        foreach ($pluginList as $obj) {
824
            $settings = $obj->getCourseSettings();
825
            $subValues = [];
826
            if (!empty($settings)) {
827
                foreach ($settings as $v) {
828
                    if (isset($values[$v])) {
829
                        $subValues[$v] = $values[$v];
830
                    }
831
                }
832
            }
833
834
            if (!empty($subValues)) {
835
                $obj->course_settings_updated($subValues);
836
            }
837
        }
838
    }
839
840
    /**
841
     * @param array            $pluginRegionList
842
     * @param string           $pluginRegion
843
     * @param Twig_Environment $twig
844
     */
845
    public function setPluginRegion($pluginRegionList, $pluginRegion, $twig)
846
    {
847
        $regionContent = $this->loadRegion(
848
            $pluginRegionList,
849
            $pluginRegion,
850
            $twig,
851
            true //$this->force_plugin_load
852
        );
853
854
        //$twig->addGlobal('plugin_'.$pluginRegion, $regionContent);
855
    }
856
}
857