Passed
Push — master ( 75577f...fa8cb2 )
by Julito
10:11
created

AppPlugin   F

Complexity

Total Complexity 110

Size/Duplication

Total Lines 729
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 298
dl 0
loc 729
rs 2
c 0
b 0
f 0
wmc 110

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A is_valid_plugin_location() 0 3 1
A uninstall() 0 18 4
A get_installed_plugins_by_region() 0 12 3
A getInstalledPluginListName() 0 7 2
A install() 0 28 4
A get_plugin_regions() 0 5 1
A get_areas_by_plugin() 0 11 3
A setInstalledPluginListObject() 0 13 4
A get_installed_plugins() 0 17 3
A getInstalledPluginListObject() 0 7 2
A read_plugins_from_path() 0 15 5
A is_valid_plugin() 0 9 3
A remove_all_regions() 0 11 2
A get_templates_list() 0 7 3
A add_to_region() 0 14 1
B getPluginInfo() 0 40 7
B load_plugin_lang_variables() 0 27 7
B setPluginRegion() 0 37 9
A getAllPluginCourseSettings() 0 13 3
B add_course_settings_form() 0 74 11
B get_all_plugin_contents_by_region() 0 52 11
B install_course_plugins() 0 14 7
A getSMSPluginName() 0 10 3
A getSMSPluginLibrary() 0 10 2
A saveCourseSettingsHook() 0 18 6
A loadRegion() 0 12 2

How to fix   Complexity   

Complex Class

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

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

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

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
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct()
39
    {
40
    }
41
42
    /**
43
     * Read plugin from path.
44
     *
45
     * @return array
46
     */
47
    public function read_plugins_from_path()
48
    {
49
        /* We scan the plugin directory. Each folder is a potential plugin. */
50
        $pluginPath = api_get_path(SYS_PLUGIN_PATH);
51
        $plugins = [];
52
        $handle = @opendir($pluginPath);
53
        while (false !== ($file = readdir($handle))) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

53
        while (false !== ($file = readdir(/** @scrutinizer ignore-type */ $handle))) {
Loading history...
54
            if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
55
                $plugins[] = $file;
56
            }
57
        }
58
        @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...
Bug introduced by
It seems like $handle can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        @closedir(/** @scrutinizer ignore-type */ $handle);
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

58
        /** @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...
59
        sort($plugins);
60
61
        return $plugins;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function get_installed_plugins_by_region()
68
    {
69
        $plugins = [];
70
        /* We retrieve all the active plugins. */
71
        $result = api_get_settings('Plugins');
72
        if (!empty($result)) {
73
            foreach ($result as $row) {
74
                $plugins[$row['variable']][] = $row['selected_value'];
75
            }
76
        }
77
78
        return $plugins;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getInstalledPluginListName()
85
    {
86
        if (empty($this->installedPluginListName)) {
87
            $this->installedPluginListName = $this->get_installed_plugins();
88
        }
89
90
        return $this->installedPluginListName;
91
    }
92
93
    /**
94
     * @return array List of Plugin
95
     */
96
    public function getInstalledPluginListObject()
97
    {
98
        if (empty($this->installedPluginListObject)) {
99
            $this->setInstalledPluginListObject();
100
        }
101
102
        return $this->installedPluginListObject;
103
    }
104
105
    public function setInstalledPluginListObject()
106
    {
107
        $pluginListName = $this->getInstalledPluginListName();
108
        $pluginList = [];
109
        if (!empty($pluginListName)) {
110
            foreach ($pluginListName as $pluginName) {
111
                $pluginInfo = $this->getPluginInfo($pluginName, true);
112
                if (isset($pluginInfo['plugin_class'])) {
113
                    $pluginList[] = $pluginInfo['plugin_class']::create();
114
                }
115
            }
116
        }
117
        $this->installedPluginListObject = $pluginList;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function get_installed_plugins()
124
    {
125
        $installedPlugins = [];
126
        $plugins = api_get_settings_params(
127
            [
128
                "variable = ? AND selected_value = ? AND category = ? " => ['status', 'installed', 'Plugins'],
129
            ]
130
        );
131
132
        if (!empty($plugins)) {
133
            foreach ($plugins as $row) {
134
                $installedPlugins[$row['subkey']] = true;
135
            }
136
            $installedPlugins = array_keys($installedPlugins);
137
        }
138
139
        return $installedPlugins;
140
    }
141
142
    /**
143
     * @param string $pluginName
144
     * @param int    $urlId
145
     */
146
    public function install($pluginName, $urlId = null)
147
    {
148
        if (empty($urlId)) {
149
            $urlId = api_get_current_access_url_id();
150
        } else {
151
            $urlId = intval($urlId);
152
        }
153
154
        api_add_setting(
155
            'installed',
156
            'status',
157
            $pluginName,
158
            'setting',
159
            'Plugins',
160
            $pluginName,
161
            '',
162
            '',
163
            '',
164
            $urlId,
165
            1
166
        );
167
168
        $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/install.php';
169
170
        if (is_file($pluginPath) && is_readable($pluginPath)) {
171
            // Execute the install procedure.
172
173
            require $pluginPath;
174
        }
175
    }
176
177
    /**
178
     * @param string $pluginName
179
     * @param int    $urlId
180
     */
181
    public function uninstall($pluginName, $urlId = null)
182
    {
183
        if (empty($urlId)) {
184
            $urlId = api_get_current_access_url_id();
185
        } else {
186
            $urlId = intval($urlId);
187
        }
188
189
        // First call the custom uninstall to allow full access to global settings
190
        $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/uninstall.php';
191
        if (is_file($pluginPath) && is_readable($pluginPath)) {
192
            // Execute the uninstall procedure.
193
194
            require $pluginPath;
195
        }
196
        // Second remove all remaining global settings
197
        api_delete_settings_params(
198
            ['category = ? AND access_url = ? AND subkey = ? ' => ['Plugins', $urlId, $pluginName]]
199
        );
200
    }
201
202
    /**
203
     * @param string $pluginName
204
     *
205
     * @return array
206
     */
207
    public function get_areas_by_plugin($pluginName)
208
    {
209
        $result = api_get_settings('Plugins');
210
        $areas = [];
211
        foreach ($result as $row) {
212
            if ($pluginName == $row['selected_value']) {
213
                $areas[] = $row['variable'];
214
            }
215
        }
216
217
        return $areas;
218
    }
219
220
    /**
221
     * @param string $location
222
     *
223
     * @return bool
224
     */
225
    public function is_valid_plugin_location($location)
226
    {
227
        return in_array($location, $this->plugin_list);
0 ignored issues
show
Bug introduced by
The property plugin_list does not exist on AppPlugin. Did you mean plugin_regions?
Loading history...
228
    }
229
230
    /**
231
     * @param string $pluginName
232
     *
233
     * @return bool
234
     */
235
    public function is_valid_plugin($pluginName)
236
    {
237
        if (is_dir(api_get_path(SYS_PLUGIN_PATH).$pluginName)) {
238
            if (is_file(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/index.php')) {
239
                return true;
240
            }
241
        }
242
243
        return false;
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function get_plugin_regions()
250
    {
251
        sort($this->plugin_regions);
252
253
        return $this->plugin_regions;
254
    }
255
256
    /**
257
     * @param string   $region
258
     * @param Template $template
259
     * @param bool     $forced
260
     *
261
     * @return null|string
262
     */
263
    public function loadRegion($pluginRegionList, $region, $template, $forced = false)
264
    {
265
        if ($region === 'course_tool_plugin') {
266
            return '';
267
        }
268
269
        ob_start();
270
        $this->get_all_plugin_contents_by_region($pluginRegionList, $region, $template, $forced);
271
        $content = ob_get_contents();
272
        ob_end_clean();
273
274
        return $content;
275
    }
276
277
    /**
278
     * Loads the translation files inside a plugin if exists.
279
     * It loads by default english see the hello world plugin.
280
     *
281
     * @param string $plugin_name
282
     *
283
     * @todo add caching
284
     */
285
    public function load_plugin_lang_variables($plugin_name)
286
    {
287
        $language_interface = api_get_interface_language();
288
        $root = api_get_path(SYS_PLUGIN_PATH);
289
        $strings = null;
290
291
        // 1. Loading english if exists
292
        $english_path = $root.$plugin_name.'/lang/english.php';
293
        if (is_readable($english_path)) {
294
            include $english_path;
295
296
            foreach ($strings as $key => $string) {
0 ignored issues
show
Bug introduced by
The expression $strings of type null is not traversable.
Loading history...
297
                $GLOBALS[$key] = $string;
298
            }
299
        }
300
301
        // 2. Loading the system language
302
        if ($language_interface != 'english') {
303
            $path = $root.$plugin_name."/lang/$language_interface.php";
304
            if (is_readable($path)) {
305
                include $path;
306
                if (!empty($strings)) {
307
                    foreach ($strings as $key => $string) {
308
                        $GLOBALS[$key] = $string;
309
                    }
310
                }
311
            } else {
312
                /*$interfaceLanguageId = api_get_language_id($language_interface);
313
                $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId);
314
                $languageParentId = intval($interfaceLanguageInfo['parent_id']);
315
316
                if ($languageParentId > 0) {
317
                    $languageParentInfo = api_get_language_info($languageParentId);
318
                    $languageParentFolder = $languageParentInfo['dokeos_folder'];
319
320
                    $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php";
321
                    if (is_readable($parentPath)) {
322
                        include $parentPath;
323
                        if (!empty($strings)) {
324
                            foreach ($strings as $key => $string) {
325
                                $this->strings[$key] = $string;
326
                            }
327
                        }
328
                    }
329
                }*/
330
            }
331
        }
332
    }
333
334
    /**
335
     * @param string   $region
336
     * @param Template $template
337
     * @param bool     $forced
338
     *
339
     * @return bool
340
     *
341
     * @todo improve this function
342
     */
343
    public function get_all_plugin_contents_by_region($_plugins, $region, $template, $forced = false)
344
    {
345
        if (isset($_plugins[$region]) && is_array($_plugins[$region])) {
346
            // Load the plugin information
347
            foreach ($_plugins[$region] as $plugin_name) {
348
                // The plugin_info variable is available inside the plugin index
349
                $plugin_info = $this->getPluginInfo($plugin_name, $forced);
350
351
352
                // We also know where the plugin is
353
                $plugin_info['current_region'] = $region;
354
355
                // Loading the plugin/XXX/index.php file
356
                $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php";
357
358
                if (file_exists($plugin_file)) {
359
                    //Loading the lang variables of the plugin if exists
360
                    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

360
                    self::/** @scrutinizer ignore-call */ 
361
                          load_plugin_lang_variables($plugin_name);
Loading history...
361
362
                    // Printing the plugin index.php file
363
                    require $plugin_file;
364
365
                    // If the variable $_template is set we assign those values to be accessible in Twig
366
                    if (isset($_template)) {
367
                        $_template['plugin_info'] = $plugin_info;
368
                    } else {
369
                        $_template = [];
370
                        $_template['plugin_info'] = $plugin_info;
371
                    }
372
373
                    // Setting the plugin info available in the template if exists.
374
                    $template->addGlobal($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...
Bug introduced by
The method addGlobal() does not exist on Template. ( Ignorable by Annotation )

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

374
                    $template->/** @scrutinizer ignore-call */ 
375
                               addGlobal($plugin_name, $_template);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
375
376
                    // Loading the Twig template plugin files if exists
377
                    $template_list = [];
378
                    if (isset($plugin_info) && isset($plugin_info['templates'])) {
379
                        $template_list = $plugin_info['templates'];
380
                    }
381
382
                    if (!empty($template_list)) {
383
                        foreach ($template_list as $plugin_tpl) {
384
                            if (!empty($plugin_tpl)) {
385
                                $template_plugin_file = "$plugin_name/$plugin_tpl"; // for twig
386
                                $template->display($template_plugin_file, false);
387
                            }
388
                        }
389
                    }
390
                }
391
            }
392
        }
393
394
        return true;
395
    }
396
397
    /**
398
     * Loads plugin info.
399
     *
400
     * @staticvar array $plugin_data
401
     *
402
     * @param string $plugin_name
403
     * @param bool   $forced      load from DB or from the static array
404
     *
405
     * @return array
406
     *
407
     * @todo filter setting_form
408
     */
409
    public function getPluginInfo($plugin_name, $forced = false)
410
    {
411
        $pluginData = Session::read('plugin_data');
412
        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...
413
            return $pluginData[$plugin_name];
414
        } else {
415
            $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/plugin.php";
416
417
            $plugin_info = [];
418
            if (file_exists($plugin_file)) {
419
                require $plugin_file;
420
            }
421
422
            // @todo check if settings are already added
423
            // Extra options
424
            $plugin_settings = api_get_settings_params(
425
                [
426
                    "subkey = ? AND category = ? AND type = ? AND access_url = ?" => [
427
                        $plugin_name,
428
                        'Plugins',
429
                        'setting',
430
                        api_get_current_access_url_id(),
431
                    ],
432
                ]
433
            );
434
435
            $settings_filtered = [];
436
            foreach ($plugin_settings as $item) {
437
                if (!empty($item['selected_value'])) {
438
                    if (@unserialize($item['selected_value']) !== false) {
439
                        $item['selected_value'] = unserialize($item['selected_value']);
440
                    }
441
                }
442
                $settings_filtered[$item['variable']] = $item['selected_value'];
443
            }
444
            $plugin_info['settings'] = $settings_filtered;
445
            $pluginData[$plugin_name] = $plugin_info;
446
            Session::write('plugin_data', $pluginData);
447
448
            return $plugin_info;
449
        }
450
    }
451
452
    /**
453
     * Get the template list.
454
     *
455
     * @param string $pluginName
456
     *
457
     * @return bool
458
     */
459
    public function get_templates_list($pluginName)
460
    {
461
        $plugin_info = $this->getPluginInfo($pluginName);
462
        if (isset($plugin_info) && isset($plugin_info['templates'])) {
463
            return $plugin_info['templates'];
464
        } else {
465
            return false;
466
        }
467
    }
468
469
    /**
470
     * Remove all regions of an specific plugin.
471
     *
472
     * @param string $plugin
473
     */
474
    public function remove_all_regions($plugin)
475
    {
476
        $access_url_id = api_get_current_access_url_id();
477
        if (!empty($plugin)) {
478
            api_delete_settings_params(
479
                [
480
                    'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => [
481
                        'Plugins',
482
                        'region',
483
                        $access_url_id,
484
                        $plugin,
485
                    ],
486
                ]
487
            );
488
        }
489
    }
490
491
    /**
492
     * Add a plugin to a region.
493
     *
494
     * @param string $plugin
495
     * @param string $region
496
     */
497
    public function add_to_region($plugin, $region)
498
    {
499
        api_add_setting(
500
            $plugin,
501
            $region,
502
            $plugin,
503
            'region',
504
            'Plugins',
505
            $plugin,
506
            '',
507
            '',
508
            '',
509
            api_get_current_access_url_id(),
510
            1
511
        );
512
    }
513
514
    /**
515
     * @param int $courseId
516
     */
517
    public function install_course_plugins($courseId)
518
    {
519
        $pluginList = $this->getInstalledPluginListObject();
520
521
        if (!empty($pluginList)) {
522
            /** @var Plugin $obj */
523
            foreach ($pluginList as $obj) {
524
                $pluginName = $obj->get_name();
525
                $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
526
527
                if (file_exists($plugin_path)) {
528
                    require $plugin_path;
529
                    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...
530
                        $obj->course_install($courseId);
531
                    }
532
                }
533
            }
534
        }
535
    }
536
537
    /**
538
     * Add the course settings to the course settings form.
539
     *
540
     * @param FormValidator $form
541
     */
542
    public function add_course_settings_form($form)
543
    {
544
        $pluginList = $this->getInstalledPluginListObject();
545
        /** @var Plugin $obj */
546
        foreach ($pluginList as $obj) {
547
            $plugin_name = $obj->get_name();
548
            $pluginTitle = $obj->get_title();
549
            if (!empty($obj->course_settings)) {
550
                if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$plugin_name.'.png')) {
551
                    $icon = Display::return_icon(
552
                        $plugin_name.'.png',
553
                        Security::remove_XSS($pluginTitle),
554
                        '',
555
                        ICON_SIZE_SMALL
556
                    );
557
                } else {
558
                    $icon = Display::return_icon(
559
                        'plugins.png',
560
                        Security::remove_XSS($pluginTitle),
561
                        '',
562
                        ICON_SIZE_SMALL
563
                    );
564
                }
565
566
                $form->addHtml('<div class="panel panel-default">');
567
                $form->addHtml('
568
                    <div class="panel-heading" role="tab" id="heading-'.$plugin_name.'-settings">
569
                        <h4 class="panel-title">
570
                            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-'.$plugin_name.'-settings" aria-expanded="false" aria-controls="collapse-'.$plugin_name.'-settings">
571
                ');
572
                $form->addHtml($icon.' '.$pluginTitle);
573
                $form->addHtml('
574
                            </a>
575
                        </h4>
576
                    </div>
577
                ');
578
                $form->addHtml('
579
                    <div id="collapse-'.$plugin_name.'-settings" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-'.$plugin_name.'-settings">
580
                        <div class="panel-body">
581
                ');
582
583
                $groups = [];
584
                foreach ($obj->course_settings as $setting) {
585
                    if ($obj->validateCourseSetting($setting['name']) === false) {
586
                        continue;
587
                    }
588
                    if ($setting['type'] != 'checkbox') {
589
                        $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name']));
590
                    } else {
591
                        $element = &$form->createElement(
592
                            $setting['type'],
593
                            $setting['name'],
594
                            '',
595
                            $obj->get_lang($setting['name'])
596
                        );
597
                        if (isset($setting['init_value']) && $setting['init_value'] == 1) {
598
                            $element->setChecked(true);
599
                        }
600
                        $form->addElement($element);
601
602
                        if (isset($setting['group'])) {
603
                            $groups[$setting['group']][] = $element;
604
                        }
605
                    }
606
                }
607
                foreach ($groups as $k => $v) {
608
                    $form->addGroup($groups[$k], $k, [$obj->get_lang($k)]);
609
                }
610
                $form->addButtonSave(get_lang('SaveSettings'));
611
                $form->addHtml('
612
                        </div>
613
                    </div>
614
                ');
615
                $form->addHtml('</div>');
616
            }
617
        }
618
    }
619
620
    /**
621
     * Get all course settings from all installed plugins.
622
     *
623
     * @return array
624
     */
625
    public function getAllPluginCourseSettings()
626
    {
627
        $pluginList = $this->getInstalledPluginListObject();
628
        /** @var Plugin $obj */
629
        $courseSettings = [];
630
        if (!empty($pluginList)) {
631
            foreach ($pluginList as $obj) {
632
                $pluginCourseSetting = $obj->getCourseSettings();
633
                $courseSettings = array_merge($courseSettings, $pluginCourseSetting);
634
            }
635
        }
636
637
        return $courseSettings;
638
    }
639
640
    /**
641
     * When saving the plugin values in the course settings, check whether
642
     * a callback method should be called and send it the updated settings.
643
     *
644
     * @param array $values The new settings the user just saved
645
     */
646
    public function saveCourseSettingsHook($values)
647
    {
648
        $pluginList = $this->getInstalledPluginListObject();
649
650
        /** @var Plugin $obj */
651
        foreach ($pluginList as $obj) {
652
            $settings = $obj->getCourseSettings();
653
            $subValues = [];
654
            if (!empty($settings)) {
655
                foreach ($settings as $v) {
656
                    if (isset($values[$v])) {
657
                        $subValues[$v] = $values[$v];
658
                    }
659
                }
660
            }
661
662
            if (!empty($subValues)) {
663
                $obj->course_settings_updated($subValues);
664
            }
665
        }
666
    }
667
668
    /**
669
     * Get first SMS plugin name.
670
     *
671
     * @return string|bool
672
     */
673
    public function getSMSPluginName()
674
    {
675
        $installedPluginsList = $this->getInstalledPluginListObject();
676
        foreach ($installedPluginsList as $installedPlugin) {
677
            if ($installedPlugin->isMailPlugin) {
678
                return get_class($installedPlugin);
679
            }
680
        }
681
682
        return false;
683
    }
684
685
    /**
686
     * @return SmsPluginLibraryInterface
687
     */
688
    public function getSMSPluginLibrary()
689
    {
690
        $className = $this->getSMSPluginName();
691
        $className = str_replace('Plugin', '', $className);
692
693
        if (class_exists($className)) {
694
            return new $className();
695
        }
696
697
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SmsPluginLibraryInterface.
Loading history...
698
    }
699
700
701
    public function setPluginRegion($pluginRegionList, $pluginRegion, $twig)
702
    {
703
        if (!empty($pluginRegion)) {
704
            $regionContent = $this->loadRegion(
705
                $pluginRegionList,
706
                $pluginRegion,
707
                $twig,
708
                true //$this->force_plugin_load
709
            );
710
711
            $pluginList = $this->get_installed_plugins();
712
            foreach ($pluginList as $plugin_name) {
713
                // The plugin_info variable is available inside the plugin index
714
                $pluginInfo = $this->getPluginInfo($plugin_name);
715
716
                if (isset($pluginInfo['is_course_plugin']) && $pluginInfo['is_course_plugin']) {
717
                    $courseInfo = api_get_course_info();
718
                    if (!empty($courseInfo)) {
719
                        if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof Plugin) {
720
                            /** @var Plugin $plugin */
721
                            $plugin = $pluginInfo['obj'];
722
                            $regionContent .= $plugin->renderRegion($pluginRegion);
723
                        }
724
                    }
725
                } else {
726
                    continue;
727
                }
728
            }
729
730
            if (!empty($regionContent)) {
731
                $twig->addGlobal('plugin_'.$pluginRegion, $regionContent);
732
            } else {
733
                $twig->addGlobal('plugin_'.$pluginRegion, null);
734
            }
735
        }
736
737
        return null;
738
    }
739
}
740