| Total Complexity | 107 |
| Total Lines | 747 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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() |
||
| 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); |
||
| 72 | sort($plugins); |
||
| 73 | |||
| 74 | return $plugins; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function getInstalledPluginListName() |
||
| 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() |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $plugin |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function isInstalled($plugin) |
||
| 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 | static $installedPlugins = null; |
||
| 144 | |||
| 145 | if ($fromDatabase === false) { |
||
| 146 | if (is_array($installedPlugins)) { |
||
| 147 | return $installedPlugins; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($fromDatabase || $installedPlugins === null) { |
||
| 152 | $installedPlugins = []; |
||
| 153 | $plugins = api_get_settings_params( |
||
| 154 | [ |
||
| 155 | 'variable = ? AND selected_value = ? AND category = ? ' => ['status', 'installed', 'Plugins'], |
||
| 156 | ] |
||
| 157 | ); |
||
| 158 | |||
| 159 | if (!empty($plugins)) { |
||
| 160 | foreach ($plugins as $row) { |
||
| 161 | $installedPlugins[$row['subkey']] = true; |
||
| 162 | } |
||
| 163 | $installedPlugins = array_keys($installedPlugins); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | return $installedPlugins; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $pluginName |
||
| 172 | * @param int $urlId |
||
| 173 | */ |
||
| 174 | public function install($pluginName, $urlId = null) |
||
| 175 | { |
||
| 176 | $urlId = (int) $urlId; |
||
| 177 | if (empty($urlId)) { |
||
| 178 | $urlId = api_get_current_access_url_id(); |
||
| 179 | } |
||
| 180 | |||
| 181 | api_add_setting( |
||
| 182 | 'installed', |
||
| 183 | 'status', |
||
| 184 | $pluginName, |
||
| 185 | 'setting', |
||
| 186 | 'Plugins', |
||
| 187 | $pluginName, |
||
| 188 | '', |
||
| 189 | '', |
||
| 190 | '', |
||
| 191 | $urlId, |
||
| 192 | 1 |
||
| 193 | ); |
||
| 194 | |||
| 195 | $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/install.php'; |
||
| 196 | |||
| 197 | if (is_file($pluginPath) && is_readable($pluginPath)) { |
||
| 198 | // Execute the install procedure. |
||
| 199 | |||
| 200 | require $pluginPath; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $pluginName |
||
| 206 | * @param int $urlId |
||
| 207 | */ |
||
| 208 | public function uninstall($pluginName, $urlId = null) |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $pluginName |
||
| 231 | * |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public function get_areas_by_plugin($pluginName) |
||
| 235 | { |
||
| 236 | $result = api_get_settings('Plugins'); |
||
| 237 | $areas = []; |
||
| 238 | foreach ($result as $row) { |
||
| 239 | if ($pluginName == $row['selected_value']) { |
||
| 240 | $areas[] = $row['variable']; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $areas; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $pluginName |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function is_valid_plugin($pluginName) |
||
| 253 | { |
||
| 254 | if (is_dir(api_get_path(SYS_PLUGIN_PATH).$pluginName)) { |
||
| 255 | if (is_file(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/index.php')) { |
||
| 256 | return true; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | return false; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function getPluginRegions() |
||
| 267 | { |
||
| 268 | sort($this->plugin_regions); |
||
| 269 | |||
| 270 | return $this->plugin_regions; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param array $pluginRegionList |
||
| 275 | * @param string $region |
||
| 276 | * @param Twig_Environment $template |
||
| 277 | * @param bool $forced |
||
| 278 | * |
||
| 279 | * @return string|null |
||
| 280 | */ |
||
| 281 | public function loadRegion($pluginName, $region, $template, $forced = false) |
||
| 282 | { |
||
| 283 | if ($region == 'course_tool_plugin') { |
||
| 284 | return ''; |
||
| 285 | } |
||
| 286 | |||
| 287 | ob_start(); |
||
| 288 | $this->getAllPluginContentsByRegion($pluginName, $region, $template, $forced); |
||
| 289 | $content = ob_get_contents(); |
||
| 290 | ob_end_clean(); |
||
| 291 | |||
| 292 | return $content; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Loads the translation files inside a plugin if exists. |
||
| 297 | * It loads by default english see the hello world plugin. |
||
| 298 | * |
||
| 299 | * @param string $plugin_name |
||
| 300 | * |
||
| 301 | * @todo add caching |
||
| 302 | */ |
||
| 303 | public function load_plugin_lang_variables($plugin_name) |
||
| 304 | { |
||
| 305 | $language_interface = api_get_interface_language(); |
||
| 306 | $root = api_get_path(SYS_PLUGIN_PATH); |
||
| 307 | $strings = null; |
||
| 308 | |||
| 309 | // 1. Loading english if exists |
||
| 310 | $english_path = $root.$plugin_name.'/lang/english.php'; |
||
| 311 | if (is_readable($english_path)) { |
||
| 312 | include $english_path; |
||
| 313 | |||
| 314 | foreach ($strings as $key => $string) { |
||
| 315 | $GLOBALS[$key] = $string; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | // 2. Loading the system language |
||
| 320 | if ($language_interface != 'english') { |
||
| 321 | $path = $root.$plugin_name."/lang/$language_interface.php"; |
||
| 322 | if (is_readable($path)) { |
||
| 323 | include $path; |
||
| 324 | if (!empty($strings)) { |
||
| 325 | foreach ($strings as $key => $string) { |
||
| 326 | $GLOBALS[$key] = $string; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | } else { |
||
| 330 | /*$interfaceLanguageId = api_get_language_id($language_interface); |
||
| 331 | $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId); |
||
| 332 | $languageParentId = intval($interfaceLanguageInfo['parent_id']); |
||
| 333 | |||
| 334 | if ($languageParentId > 0) { |
||
| 335 | $languageParentInfo = api_get_language_info($languageParentId); |
||
| 336 | $languageParentFolder = $languageParentInfo['dokeos_folder']; |
||
| 337 | |||
| 338 | $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php"; |
||
| 339 | if (is_readable($parentPath)) { |
||
| 340 | include $parentPath; |
||
| 341 | if (!empty($strings)) { |
||
| 342 | foreach ($strings as $key => $string) { |
||
| 343 | $this->strings[$key] = $string; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | }*/ |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param array $_plugins |
||
| 354 | * @param string $region |
||
| 355 | * @param Twig_Environment $template |
||
| 356 | * @param bool $forced |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | * |
||
| 360 | * @todo improve this function |
||
| 361 | */ |
||
| 362 | public function getAllPluginContentsByRegion($plugin_name, $region, $template, $forced = false) |
||
| 363 | { |
||
| 364 | // The plugin_info variable is available inside the plugin index |
||
| 365 | $plugin_info = $this->getPluginInfo($plugin_name, $forced); |
||
| 366 | |||
| 367 | // We also know where the plugin is |
||
| 368 | $plugin_info['current_region'] = $region; |
||
| 369 | |||
| 370 | // Loading the plugin/XXX/index.php file |
||
| 371 | $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php"; |
||
| 372 | |||
| 373 | if (file_exists($plugin_file)) { |
||
| 374 | //Loading the lang variables of the plugin if exists |
||
| 375 | self::load_plugin_lang_variables($plugin_name); |
||
| 376 | |||
| 377 | // Printing the plugin index.php file |
||
| 378 | require $plugin_file; |
||
| 379 | |||
| 380 | // If the variable $_template is set we assign those values to be accessible in Twig |
||
| 381 | if (isset($_template)) { |
||
| 382 | $_template['plugin_info'] = $plugin_info; |
||
| 383 | } else { |
||
| 384 | $_template = []; |
||
| 385 | $_template['plugin_info'] = $plugin_info; |
||
| 386 | } |
||
| 387 | |||
| 388 | // Setting the plugin info available in the template if exists. |
||
| 389 | //$template->addGlobal($plugin_name, $_template); |
||
| 390 | |||
| 391 | // Loading the Twig template plugin files if exists |
||
| 392 | $templateList = []; |
||
| 393 | if (isset($plugin_info) && isset($plugin_info['templates'])) { |
||
| 394 | $templateList = $plugin_info['templates']; |
||
| 395 | } |
||
| 396 | |||
| 397 | if (!empty($templateList)) { |
||
| 398 | foreach ($templateList as $pluginTemplate) { |
||
| 399 | if (!empty($pluginTemplate)) { |
||
| 400 | $templatePluginFile = "$plugin_name/$pluginTemplate"; // for twig |
||
| 401 | //$template->render($templatePluginFile, []); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | return true; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Loads plugin info. |
||
| 412 | * |
||
| 413 | * @staticvar array $plugin_data |
||
| 414 | * |
||
| 415 | * @param string $pluginName |
||
| 416 | * @param bool $forced load from DB or from the static array |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | * |
||
| 420 | * @todo filter setting_form |
||
| 421 | */ |
||
| 422 | public function getPluginInfo($pluginName, $forced = false) |
||
| 423 | { |
||
| 424 | //$pluginData = Session::read('plugin_data'); |
||
| 425 | if (0) { |
||
| 426 | //if (isset($pluginData[$pluginName]) && $forced == false) { |
||
| 427 | return $pluginData[$pluginName]; |
||
| 428 | } else { |
||
| 429 | $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$pluginName/plugin.php"; |
||
| 430 | |||
| 431 | $plugin_info = []; |
||
| 432 | if (file_exists($plugin_file)) { |
||
| 433 | require $plugin_file; |
||
| 434 | } |
||
| 435 | |||
| 436 | // @todo check if settings are already added |
||
| 437 | // Extra options |
||
| 438 | $plugin_settings = api_get_settings_params( |
||
| 439 | [ |
||
| 440 | 'subkey = ? AND category = ? AND type = ? AND access_url = ?' => [ |
||
| 441 | $pluginName, |
||
| 442 | 'Plugins', |
||
| 443 | 'setting', |
||
| 444 | api_get_current_access_url_id(), |
||
| 445 | ], |
||
| 446 | ] |
||
| 447 | ); |
||
| 448 | |||
| 449 | $settings_filtered = []; |
||
| 450 | foreach ($plugin_settings as $item) { |
||
| 451 | if (!empty($item['selected_value'])) { |
||
| 452 | //if (unserialize($item['selected_value']) !== false) { |
||
| 453 | //$item['selected_value'] = unserialize($item['selected_value']); |
||
| 454 | //} |
||
| 455 | } |
||
| 456 | $settings_filtered[$item['variable']] = $item['selected_value']; |
||
| 457 | } |
||
| 458 | |||
| 459 | $plugin_info['settings'] = $settings_filtered; |
||
| 460 | $pluginData[$pluginName] = $plugin_info; |
||
| 461 | //Session::write('plugin_data', $pluginData); |
||
| 462 | |||
| 463 | return $plugin_info; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get the template list. |
||
| 469 | * |
||
| 470 | * @param string $pluginName |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function get_templates_list($pluginName) |
||
| 475 | { |
||
| 476 | $plugin_info = $this->getPluginInfo($pluginName); |
||
| 477 | if (isset($plugin_info) && isset($plugin_info['templates'])) { |
||
| 478 | return $plugin_info['templates']; |
||
| 479 | } |
||
| 480 | |||
| 481 | return false; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Remove all regions of an specific plugin. |
||
| 486 | * |
||
| 487 | * @param string $plugin |
||
| 488 | */ |
||
| 489 | public function removeAllRegions($plugin) |
||
| 499 | ], |
||
| 500 | ] |
||
| 501 | ); |
||
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Add a plugin to a region. |
||
| 507 | * |
||
| 508 | * @param string $plugin |
||
| 509 | * @param string $region |
||
| 510 | */ |
||
| 511 | public function add_to_region($plugin, $region) |
||
| 512 | { |
||
| 513 | api_add_setting( |
||
| 514 | $plugin, |
||
| 515 | $region, |
||
| 516 | $plugin, |
||
| 517 | 'region', |
||
| 518 | 'Plugins', |
||
| 519 | $plugin, |
||
| 520 | '', |
||
| 521 | '', |
||
| 522 | '', |
||
| 523 | api_get_current_access_url_id(), |
||
| 524 | 1 |
||
| 525 | ); |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param int $courseId |
||
| 530 | */ |
||
| 531 | public function install_course_plugins($courseId) |
||
| 532 | { |
||
| 533 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 534 | |||
| 535 | if (!empty($pluginList)) { |
||
| 536 | /** @var Plugin $obj */ |
||
| 537 | foreach ($pluginList as $obj) { |
||
| 538 | $pluginName = $obj->get_name(); |
||
| 539 | $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php'; |
||
| 540 | |||
| 541 | if (file_exists($plugin_path)) { |
||
| 542 | require $plugin_path; |
||
| 543 | if (isset($plugin_info) && isset($plugin_info['plugin_class']) && $obj->isCoursePlugin) { |
||
| 544 | $obj->course_install($courseId); |
||
| 545 | } |
||
| 546 | } |
||
| 547 | } |
||
| 548 | } |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Trigger for Plugin::doWhenDeleting[Item] functions. |
||
| 553 | * |
||
| 554 | * @param string $itemType |
||
| 555 | * @param int $itemId |
||
| 556 | */ |
||
| 557 | public function performActionsWhenDeletingItem($itemType, $itemId) |
||
| 577 | } |
||
| 578 | } |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Add the course settings to the course settings form. |
||
| 583 | * |
||
| 584 | * @param FormValidator $form |
||
| 585 | */ |
||
| 586 | public function add_course_settings_form($form) |
||
| 587 | { |
||
| 588 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 589 | /** @var Plugin $obj */ |
||
| 590 | foreach ($pluginList as $obj) { |
||
| 591 | $pluginName = $obj->get_name(); |
||
| 592 | $pluginTitle = $obj->get_title(); |
||
| 593 | if (!empty($obj->course_settings)) { |
||
| 594 | if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$pluginName.'.png')) { |
||
| 595 | $icon = Display::return_icon( |
||
| 596 | $pluginName.'.png', |
||
| 597 | Security::remove_XSS($pluginTitle), |
||
| 598 | '', |
||
| 599 | ICON_SIZE_SMALL |
||
| 600 | ); |
||
| 601 | } else { |
||
| 602 | $icon = Display::return_icon( |
||
| 603 | 'plugins.png', |
||
| 604 | Security::remove_XSS($pluginTitle), |
||
| 605 | '', |
||
| 606 | ICON_SIZE_SMALL |
||
| 607 | ); |
||
| 608 | } |
||
| 609 | |||
| 610 | $form->addHtml('<div class="panel panel-default">'); |
||
| 611 | $form->addHtml(' |
||
| 612 | <div class="panel-heading" role="tab" id="heading-'.$pluginName.'-settings"> |
||
| 613 | <h4 class="panel-title"> |
||
| 614 | <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-'.$pluginName.'-settings" aria-expanded="false" aria-controls="collapse-'.$pluginName.'-settings"> |
||
| 615 | '); |
||
| 616 | $form->addHtml($icon.' '.$pluginTitle); |
||
| 617 | $form->addHtml(' |
||
| 618 | </a> |
||
| 619 | </h4> |
||
| 620 | </div> |
||
| 621 | '); |
||
| 622 | $form->addHtml(' |
||
| 623 | <div id="collapse-'.$pluginName.'-settings" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-'.$pluginName.'-settings"> |
||
| 624 | <div class="panel-body"> |
||
| 625 | '); |
||
| 626 | |||
| 627 | $groups = []; |
||
| 628 | foreach ($obj->course_settings as $setting) { |
||
| 629 | if ($obj->validateCourseSetting($setting['name']) === false) { |
||
| 630 | continue; |
||
| 631 | } |
||
| 632 | if ($setting['type'] != 'checkbox') { |
||
| 633 | $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name'])); |
||
| 634 | } else { |
||
| 635 | $element = &$form->createElement( |
||
| 636 | $setting['type'], |
||
| 637 | $setting['name'], |
||
| 638 | '', |
||
| 639 | $obj->get_lang($setting['name']) |
||
| 640 | ); |
||
| 641 | if (isset($setting['init_value']) && $setting['init_value'] == 1) { |
||
| 642 | $element->setChecked(true); |
||
| 643 | } |
||
| 644 | $form->addElement($element); |
||
| 645 | |||
| 646 | if (isset($setting['group'])) { |
||
| 647 | $groups[$setting['group']][] = $element; |
||
| 648 | } |
||
| 649 | } |
||
| 650 | } |
||
| 651 | foreach ($groups as $k => $v) { |
||
| 652 | $form->addGroup($groups[$k], $k, [$obj->get_lang($k)]); |
||
| 653 | } |
||
| 654 | $form->addButtonSave(get_lang('SaveSettings')); |
||
| 655 | $form->addHtml(' |
||
| 656 | </div> |
||
| 657 | </div> |
||
| 658 | '); |
||
| 659 | $form->addHtml('</div>'); |
||
| 660 | } |
||
| 661 | } |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Get all course settings from all installed plugins. |
||
| 666 | * |
||
| 667 | * @return array |
||
| 668 | */ |
||
| 669 | public function getAllPluginCourseSettings() |
||
| 670 | { |
||
| 671 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 672 | /** @var Plugin $obj */ |
||
| 673 | $courseSettings = []; |
||
| 674 | if (!empty($pluginList)) { |
||
| 675 | foreach ($pluginList as $obj) { |
||
| 676 | $pluginCourseSetting = $obj->getCourseSettings(); |
||
| 677 | $courseSettings = array_merge($courseSettings, $pluginCourseSetting); |
||
| 678 | } |
||
| 679 | } |
||
| 680 | |||
| 681 | return $courseSettings; |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * When saving the plugin values in the course settings, check whether |
||
| 686 | * a callback method should be called and send it the updated settings. |
||
| 687 | * |
||
| 688 | * @param array $values The new settings the user just saved |
||
| 689 | */ |
||
| 690 | public function saveCourseSettingsHook($values) |
||
| 691 | { |
||
| 692 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 693 | |||
| 694 | /** @var Plugin $obj */ |
||
| 695 | foreach ($pluginList as $obj) { |
||
| 696 | $settings = $obj->getCourseSettings(); |
||
| 697 | $subValues = []; |
||
| 698 | if (!empty($settings)) { |
||
| 699 | foreach ($settings as $v) { |
||
| 700 | if (isset($values[$v])) { |
||
| 701 | $subValues[$v] = $values[$v]; |
||
| 702 | } |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | if (!empty($subValues)) { |
||
| 707 | $obj->course_settings_updated($subValues); |
||
| 708 | } |
||
| 709 | } |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Get first SMS plugin name. |
||
| 714 | * |
||
| 715 | * @return string|bool |
||
| 716 | */ |
||
| 717 | public function getSMSPluginName() |
||
| 718 | { |
||
| 719 | $installedPluginsList = $this->getInstalledPluginListObject(); |
||
| 720 | foreach ($installedPluginsList as $installedPlugin) { |
||
| 721 | if ($installedPlugin->isMailPlugin) { |
||
| 722 | return get_class($installedPlugin); |
||
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | return false; |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * @return SmsPluginLibraryInterface |
||
| 731 | */ |
||
| 732 | public function getSMSPluginLibrary() |
||
| 733 | { |
||
| 734 | $className = $this->getSMSPluginName(); |
||
| 735 | $className = str_replace('Plugin', '', $className); |
||
| 736 | |||
| 737 | if (class_exists($className)) { |
||
| 738 | return new $className(); |
||
| 739 | } |
||
| 740 | |||
| 741 | return false; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @param array $pluginRegionList |
||
| 746 | * @param string $pluginRegion |
||
| 747 | * @param Twig_Environment $twig |
||
| 748 | */ |
||
| 749 | public function setPluginRegion($pluginRegionList, $pluginRegion, $twig) |
||
| 756 | ); |
||
| 757 | |||
| 758 | //$twig->addGlobal('plugin_'.$pluginRegion, $regionContent); |
||
| 759 | } |
||
| 760 | } |
||
| 761 |