| Total Complexity | 104 |
| Total Lines | 774 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 11 | class AppPlugin |
||
| 12 | { |
||
| 13 | public $plugin_regions = [ |
||
| 14 | 'main_top', |
||
| 15 | 'main_bottom', |
||
| 16 | 'login_top', |
||
| 17 | 'login_bottom', |
||
| 18 | 'menu_top', |
||
| 19 | 'menu_bottom', |
||
| 20 | 'content_top', |
||
| 21 | 'content_bottom', |
||
| 22 | 'header_main', |
||
| 23 | 'header_center', |
||
| 24 | 'header_left', |
||
| 25 | 'header_right', |
||
| 26 | 'pre_footer', |
||
| 27 | 'footer_left', |
||
| 28 | 'footer_center', |
||
| 29 | 'footer_right', |
||
| 30 | 'menu_administrator', |
||
| 31 | 'course_tool_plugin', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | public $installedPluginListName = []; |
||
| 35 | public $installedPluginListObject = []; |
||
| 36 | private static $instance; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructor. |
||
| 40 | */ |
||
| 41 | public function __construct() |
||
| 42 | { |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return AppPlugin |
||
| 47 | */ |
||
| 48 | public static function getInstance() |
||
| 49 | { |
||
| 50 | if (!isset(self::$instance)) { |
||
| 51 | self::$instance = new self(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return self::$instance; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Read plugin from path. |
||
| 59 | */ |
||
| 60 | public function read_plugins_from_path(): array |
||
| 61 | { |
||
| 62 | /* We scan the plugin directory. Each folder is a potential plugin. */ |
||
| 63 | $pluginPath = api_get_path(SYS_PLUGIN_PATH); |
||
| 64 | $finder = (new Finder())->directories()->depth('== 0')->sortByName()->in($pluginPath); |
||
| 65 | |||
| 66 | $plugins = []; |
||
| 67 | |||
| 68 | foreach ($finder as $file) { |
||
| 69 | $plugins[] = $file->getFilename(); |
||
| 70 | } |
||
| 71 | |||
| 72 | return $plugins; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getInstalledPluginListName() |
||
| 79 | { |
||
| 80 | if (empty($this->installedPluginListName)) { |
||
| 81 | $this->installedPluginListName = $this->getInstalledPlugins(); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this->installedPluginListName; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return array List of Plugin |
||
| 89 | */ |
||
| 90 | public function getInstalledPluginListObject() |
||
| 91 | { |
||
| 92 | if (empty($this->installedPluginListObject)) { |
||
| 93 | $this->setInstalledPluginListObject(); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $this->installedPluginListObject; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function setInstalledPluginListObject() |
||
| 100 | { |
||
| 101 | $pluginListName = $this->getInstalledPluginListName(); |
||
| 102 | $pluginList = []; |
||
| 103 | if (!empty($pluginListName)) { |
||
| 104 | foreach ($pluginListName as $pluginName) { |
||
| 105 | $pluginInfo = $this->getPluginInfo($pluginName, true); |
||
| 106 | if (isset($pluginInfo['plugin_class'])) { |
||
| 107 | $pluginList[] = $pluginInfo['plugin_class']::create(); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $this->installedPluginListObject = $pluginList; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $plugin |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function isInstalled($plugin) |
||
| 120 | { |
||
| 121 | $list = self::getInstalledPlugins(false); |
||
|
|
|||
| 122 | |||
| 123 | return in_array($plugin, $list); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @deprecated |
||
| 128 | */ |
||
| 129 | public function get_installed_plugins($fromDatabase = true) |
||
| 130 | { |
||
| 131 | return $this->getInstalledPlugins($fromDatabase); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param bool $fromDatabase |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function getInstalledPlugins($fromDatabase = true) |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getInstalledPluginsInCurrentUrl() |
||
| 169 | { |
||
| 170 | $installedPlugins = []; |
||
| 171 | $urlId = api_get_current_access_url_id(); |
||
| 172 | $plugins = api_get_settings_params( |
||
| 173 | [ |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns a list of all official (delivered with the Chamilo package) |
||
| 190 | * plugins. This list is maintained manually and updated with every new |
||
| 191 | * release to avoid hacking. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public static function getOfficialPlugins(): array |
||
| 196 | { |
||
| 197 | // Please keep this list alphabetically sorted |
||
| 198 | return [ |
||
| 199 | 'ai_helper', |
||
| 200 | 'azure_active_directory', |
||
| 201 | 'bbb', |
||
| 202 | 'before_login', |
||
| 203 | 'buycourses', |
||
| 204 | 'card_game', |
||
| 205 | 'check_extra_field_author_company', |
||
| 206 | 'cleandeletedfiles', |
||
| 207 | 'courseblock', |
||
| 208 | 'coursehomenotify', |
||
| 209 | 'courselegal', |
||
| 210 | 'customcertificate', |
||
| 211 | 'customfooter', |
||
| 212 | 'dashboard', |
||
| 213 | 'dictionary', |
||
| 214 | 'embedregistry', |
||
| 215 | 'exercise_signature', |
||
| 216 | 'ext_auth_chamilo_logout_button_behaviour', |
||
| 217 | 'externalnotificationconnect', |
||
| 218 | 'extramenufromwebservice', |
||
| 219 | 'google_maps', |
||
| 220 | 'grading_electronic', |
||
| 221 | 'h5pimport', |
||
| 222 | 'hello_world', |
||
| 223 | 'ims_lti', |
||
| 224 | 'justification', |
||
| 225 | 'learning_calendar', |
||
| 226 | 'lti_provider', |
||
| 227 | 'maintenancemode', |
||
| 228 | 'migrationmoodle', |
||
| 229 | 'mobidico', |
||
| 230 | 'nosearchindex', |
||
| 231 | 'notebookteacher', |
||
| 232 | 'pausetraining', |
||
| 233 | 'pens', |
||
| 234 | 'positioning', |
||
| 235 | 'questionoptionsevaluation', |
||
| 236 | 'redirection', |
||
| 237 | 'resubscription', |
||
| 238 | 'rss', |
||
| 239 | 'search_course', |
||
| 240 | 'show_regions', |
||
| 241 | 'show_user_info', |
||
| 242 | 'static', |
||
| 243 | 'studentfollowup', |
||
| 244 | 'surveyexportcsv', |
||
| 245 | 'surveyexporttxt', |
||
| 246 | 'test2pdf', |
||
| 247 | 'toplinks', |
||
| 248 | 'tour', |
||
| 249 | 'userremoteservice', |
||
| 250 | 'zoom', |
||
| 251 | ]; |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function isOfficial(string $title): bool |
||
| 255 | { |
||
| 256 | return in_array($title, self::getOfficialPlugins()); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function install(string $pluginName): void |
||
| 260 | { |
||
| 261 | $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/install.php'; |
||
| 262 | |||
| 263 | if (is_file($pluginPath) && is_readable($pluginPath)) { |
||
| 264 | // Execute the install procedure. |
||
| 265 | |||
| 266 | require $pluginPath; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | public function uninstall(string $pluginName): void |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $pluginName |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function get_areas_by_plugin($pluginName) |
||
| 287 | { |
||
| 288 | $result = api_get_settings('Plugins'); |
||
| 289 | $areas = []; |
||
| 290 | foreach ($result as $row) { |
||
| 291 | if ($pluginName == $row['selected_value']) { |
||
| 292 | $areas[] = $row['variable']; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | return $areas; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $pluginName |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function is_valid_plugin($pluginName) |
||
| 305 | { |
||
| 306 | if (is_dir(api_get_path(SYS_PLUGIN_PATH).$pluginName)) { |
||
| 307 | if (is_file(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/index.php')) { |
||
| 308 | return true; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | return false; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getPluginRegions() |
||
| 319 | { |
||
| 320 | sort($this->plugin_regions); |
||
| 321 | |||
| 322 | return $this->plugin_regions; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $region |
||
| 327 | * @param Twig_Environment $template |
||
| 328 | * @param bool $forced |
||
| 329 | * |
||
| 330 | * @return string|null |
||
| 331 | */ |
||
| 332 | public function loadRegion($pluginName, $region, $template, $forced = false) |
||
| 333 | { |
||
| 334 | if ('course_tool_plugin' == $region) { |
||
| 335 | return ''; |
||
| 336 | } |
||
| 337 | |||
| 338 | ob_start(); |
||
| 339 | $this->getAllPluginContentsByRegion($pluginName, $region, $template, $forced); |
||
| 340 | $content = ob_get_contents(); |
||
| 341 | ob_end_clean(); |
||
| 342 | |||
| 343 | return $content; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Loads the translation files inside a plugin if exists. |
||
| 348 | * It loads by default english see the hello world plugin. |
||
| 349 | * |
||
| 350 | * @param string $plugin_name |
||
| 351 | * |
||
| 352 | * @todo add caching |
||
| 353 | */ |
||
| 354 | public function load_plugin_lang_variables($plugin_name) |
||
| 355 | { |
||
| 356 | $language_interface = api_get_language_isocode(); |
||
| 357 | $root = api_get_path(SYS_PLUGIN_PATH); |
||
| 358 | $strings = null; |
||
| 359 | |||
| 360 | // 1. Loading english if exists |
||
| 361 | $english_path = $root.$plugin_name.'/lang/english.php'; |
||
| 362 | if (is_readable($english_path)) { |
||
| 363 | include $english_path; |
||
| 364 | |||
| 365 | foreach ($strings as $key => $string) { |
||
| 366 | $GLOBALS[$key] = $string; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | // 2. Loading the system language |
||
| 371 | if ('english' != $language_interface) { |
||
| 372 | $path = $root.$plugin_name."/lang/$language_interface.php"; |
||
| 373 | if (is_readable($path)) { |
||
| 374 | include $path; |
||
| 375 | if (!empty($strings)) { |
||
| 376 | foreach ($strings as $key => $string) { |
||
| 377 | $GLOBALS[$key] = $string; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | } else { |
||
| 381 | /*$interfaceLanguageId = api_get_language_id($language_interface); |
||
| 382 | $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId); |
||
| 383 | $languageParentId = intval($interfaceLanguageInfo['parent_id']); |
||
| 384 | |||
| 385 | if ($languageParentId > 0) { |
||
| 386 | $languageParentInfo = api_get_language_info($languageParentId); |
||
| 387 | $languageParentFolder = $languageParentInfo['dokeos_folder']; |
||
| 388 | |||
| 389 | $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php"; |
||
| 390 | if (is_readable($parentPath)) { |
||
| 391 | include $parentPath; |
||
| 392 | if (!empty($strings)) { |
||
| 393 | foreach ($strings as $key => $string) { |
||
| 394 | $this->strings[$key] = $string; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | } |
||
| 398 | }*/ |
||
| 399 | } |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $region |
||
| 405 | * @param Twig_Environment $template |
||
| 406 | * @param bool $forced |
||
| 407 | * |
||
| 408 | * @return bool |
||
| 409 | * |
||
| 410 | * @todo improve this function |
||
| 411 | */ |
||
| 412 | public function getAllPluginContentsByRegion($plugin_name, $region, $template, $forced = false) |
||
| 413 | { |
||
| 414 | // The plugin_info variable is available inside the plugin index |
||
| 415 | $plugin_info = $this->getPluginInfo($plugin_name, $forced); |
||
| 416 | |||
| 417 | // We also know where the plugin is |
||
| 418 | $plugin_info['current_region'] = $region; |
||
| 419 | |||
| 420 | // Loading the plugin/XXX/index.php file |
||
| 421 | $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php"; |
||
| 422 | |||
| 423 | if (file_exists($plugin_file)) { |
||
| 424 | //Loading the lang variables of the plugin if exists |
||
| 425 | self::load_plugin_lang_variables($plugin_name); |
||
| 426 | |||
| 427 | // Printing the plugin index.php file |
||
| 428 | require $plugin_file; |
||
| 429 | |||
| 430 | // If the variable $_template is set we assign those values to be accessible in Twig |
||
| 431 | if (isset($_template)) { |
||
| 432 | $_template['plugin_info'] = $plugin_info; |
||
| 433 | } else { |
||
| 434 | $_template = []; |
||
| 435 | $_template['plugin_info'] = $plugin_info; |
||
| 436 | } |
||
| 437 | |||
| 438 | // Setting the plugin info available in the template if exists. |
||
| 439 | //$template->addGlobal($plugin_name, $_template); |
||
| 440 | |||
| 441 | // Loading the Twig template plugin files if exists |
||
| 442 | $templateList = []; |
||
| 443 | if (isset($plugin_info) && isset($plugin_info['templates'])) { |
||
| 444 | $templateList = $plugin_info['templates']; |
||
| 445 | } |
||
| 446 | |||
| 447 | if (!empty($templateList)) { |
||
| 448 | foreach ($templateList as $pluginTemplate) { |
||
| 449 | if (!empty($pluginTemplate)) { |
||
| 450 | $templatePluginFile = "$plugin_name/$pluginTemplate"; // for twig |
||
| 451 | //$template->render($templatePluginFile, []); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | return true; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Loads plugin info. |
||
| 462 | * |
||
| 463 | * @staticvar array $plugin_data |
||
| 464 | * |
||
| 465 | * @param string $pluginName |
||
| 466 | * @param bool $forced load from DB or from the static array |
||
| 467 | * |
||
| 468 | * @return array |
||
| 469 | * |
||
| 470 | * @todo filter setting_form |
||
| 471 | */ |
||
| 472 | public function getPluginInfo($pluginName, $forced = false) |
||
| 473 | { |
||
| 474 | //$pluginData = Session::read('plugin_data'); |
||
| 475 | if (0) { |
||
| 476 | //if (isset($pluginData[$pluginName]) && $forced == false) { |
||
| 477 | return $pluginData[$pluginName]; |
||
| 478 | } else { |
||
| 479 | $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$pluginName/plugin.php"; |
||
| 480 | |||
| 481 | $plugin_info = []; |
||
| 482 | if (file_exists($plugin_file)) { |
||
| 483 | require $plugin_file; |
||
| 484 | } |
||
| 485 | |||
| 486 | // @todo check if settings are already added |
||
| 487 | // Extra options |
||
| 488 | $plugin_settings = api_get_settings_params( |
||
| 489 | [ |
||
| 490 | 'subkey = ? AND category = ? AND type = ? AND access_url = ?' => [ |
||
| 491 | $pluginName, |
||
| 492 | 'Plugins', |
||
| 493 | 'setting', |
||
| 494 | api_get_current_access_url_id(), |
||
| 495 | ], |
||
| 496 | ] |
||
| 497 | ); |
||
| 498 | |||
| 499 | $settings_filtered = []; |
||
| 500 | foreach ($plugin_settings as $item) { |
||
| 501 | if (!empty($item['selected_value'])) { |
||
| 502 | //if (unserialize($item['selected_value']) !== false) { |
||
| 503 | //$item['selected_value'] = unserialize($item['selected_value']); |
||
| 504 | //} |
||
| 505 | } |
||
| 506 | $settings_filtered[$item['variable']] = $item['selected_value']; |
||
| 507 | } |
||
| 508 | |||
| 509 | $plugin_info['settings'] = $settings_filtered; |
||
| 510 | $pluginData[$pluginName] = $plugin_info; |
||
| 511 | //Session::write('plugin_data', $pluginData); |
||
| 512 | |||
| 513 | return $plugin_info; |
||
| 514 | } |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Get the template list. |
||
| 519 | * |
||
| 520 | * @param string $pluginName |
||
| 521 | * |
||
| 522 | * @return bool |
||
| 523 | */ |
||
| 524 | public function get_templates_list($pluginName) |
||
| 525 | { |
||
| 526 | $plugin_info = $this->getPluginInfo($pluginName); |
||
| 527 | if (isset($plugin_info) && isset($plugin_info['templates'])) { |
||
| 528 | return $plugin_info['templates']; |
||
| 529 | } |
||
| 530 | |||
| 531 | return false; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Remove all regions of an specific plugin. |
||
| 536 | * |
||
| 537 | * @param string $plugin |
||
| 538 | */ |
||
| 539 | public function removeAllRegions($plugin) |
||
| 550 | ], |
||
| 551 | ] |
||
| 552 | ); |
||
| 553 | } |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Add a plugin to a region. |
||
| 558 | * |
||
| 559 | * @param string $plugin |
||
| 560 | * @param string $region |
||
| 561 | */ |
||
| 562 | public function add_to_region($plugin, $region) |
||
| 563 | { |
||
| 564 | api_add_setting( |
||
| 565 | $plugin, |
||
| 566 | $region, |
||
| 567 | $plugin, |
||
| 568 | 'region', |
||
| 569 | 'Plugins', |
||
| 570 | $plugin, |
||
| 571 | '', |
||
| 572 | '', |
||
| 573 | '', |
||
| 574 | api_get_current_access_url_id(), |
||
| 575 | 1 |
||
| 576 | ); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param int $courseId |
||
| 581 | */ |
||
| 582 | public function install_course_plugins($courseId) |
||
| 583 | { |
||
| 584 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 585 | |||
| 586 | if (!empty($pluginList)) { |
||
| 587 | /** @var Plugin $obj */ |
||
| 588 | foreach ($pluginList as $obj) { |
||
| 589 | $pluginName = $obj->get_name(); |
||
| 590 | $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php'; |
||
| 591 | |||
| 592 | if (file_exists($plugin_path)) { |
||
| 593 | require $plugin_path; |
||
| 594 | if (isset($plugin_info) && isset($plugin_info['plugin_class']) && $obj->isCoursePlugin) { |
||
| 595 | $obj->course_install($courseId); |
||
| 596 | } |
||
| 597 | } |
||
| 598 | } |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Trigger for Plugin::doWhenDeleting[Item] functions. |
||
| 604 | * |
||
| 605 | * @param string $itemType |
||
| 606 | * @param int $itemId |
||
| 607 | */ |
||
| 608 | public function performActionsWhenDeletingItem($itemType, $itemId) |
||
| 628 | } |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Add the course settings to the course settings form. |
||
| 634 | * |
||
| 635 | * @param FormValidator $form |
||
| 636 | */ |
||
| 637 | public function add_course_settings_form($form) |
||
| 638 | { |
||
| 639 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 640 | /** @var Plugin $obj */ |
||
| 641 | foreach ($pluginList as $obj) { |
||
| 642 | $pluginName = $obj->get_name(); |
||
| 643 | $pluginTitle = $obj->get_title(); |
||
| 644 | if (!empty($obj->course_settings)) { |
||
| 645 | $icon = Display::getMdiIcon( |
||
| 646 | ToolIcon::PLUGIN, |
||
| 647 | 'ch-tool-icon', |
||
| 648 | null, |
||
| 649 | ICON_SIZE_SMALL, |
||
| 650 | Security::remove_XSS($pluginTitle) |
||
| 651 | ); |
||
| 652 | $form->addHtml('<div class="panel panel-default">'); |
||
| 653 | $form->addHtml(' |
||
| 654 | <div class="panel-heading" role="tab" id="heading-'.$pluginName.'-settings"> |
||
| 655 | <h4 class="panel-title"> |
||
| 656 | <a class="collapsed" |
||
| 657 | role="button" data-toggle="collapse" data-parent="#accordion" |
||
| 658 | href="#collapse-'.$pluginName.'-settings" aria-expanded="false" |
||
| 659 | aria-controls="collapse-'.$pluginName.'-settings"> |
||
| 660 | '); |
||
| 661 | $form->addHtml($icon.' '.$pluginTitle); |
||
| 662 | $form->addHtml(' |
||
| 663 | </a> |
||
| 664 | </h4> |
||
| 665 | </div> |
||
| 666 | '); |
||
| 667 | $form->addHtml(' |
||
| 668 | <div |
||
| 669 | id="collapse-'.$pluginName.'-settings" |
||
| 670 | class="panel-collapse collapse" role="tabpanel" |
||
| 671 | aria-labelledby="heading-'.$pluginName.'-settings"> |
||
| 672 | <div class="panel-body"> |
||
| 673 | ' |
||
| 674 | ); |
||
| 675 | |||
| 676 | $groups = []; |
||
| 677 | foreach ($obj->course_settings as $setting) { |
||
| 678 | if (false === $obj->validateCourseSetting($setting['name'])) { |
||
| 679 | continue; |
||
| 680 | } |
||
| 681 | if ('checkbox' !== $setting['type']) { |
||
| 682 | $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name'])); |
||
| 683 | } else { |
||
| 684 | $element = &$form->createElement( |
||
| 685 | $setting['type'], |
||
| 686 | $setting['name'], |
||
| 687 | '', |
||
| 688 | $obj->get_lang($setting['name']) |
||
| 689 | ); |
||
| 690 | $courseSetting = api_get_course_setting($setting['name']); |
||
| 691 | if (-1 === $courseSetting) { |
||
| 692 | $defaultValue = api_get_plugin_setting($pluginName, $setting['name']); |
||
| 693 | if (!empty($defaultValue)) { |
||
| 694 | if ('true' === $defaultValue) { |
||
| 695 | $element->setChecked(true); |
||
| 696 | } |
||
| 697 | } |
||
| 698 | } |
||
| 699 | |||
| 700 | if (isset($setting['init_value']) && 1 == $setting['init_value']) { |
||
| 701 | $element->setChecked(true); |
||
| 702 | } |
||
| 703 | $form->addElement($element); |
||
| 704 | |||
| 705 | if (isset($setting['group'])) { |
||
| 706 | $groups[$setting['group']][] = $element; |
||
| 707 | } |
||
| 708 | } |
||
| 709 | } |
||
| 710 | foreach ($groups as $k => $v) { |
||
| 711 | $form->addGroup($groups[$k], $k, [$obj->get_lang($k)]); |
||
| 712 | } |
||
| 713 | $form->addButtonSave(get_lang('Save settings')); |
||
| 714 | $form->addHtml( |
||
| 715 | ' |
||
| 716 | </div> |
||
| 717 | </div> |
||
| 718 | ' |
||
| 719 | ); |
||
| 720 | $form->addHtml('</div>'); |
||
| 721 | } |
||
| 722 | } |
||
| 723 | } |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Get all course settings from all installed plugins. |
||
| 727 | * |
||
| 728 | * @return array |
||
| 729 | */ |
||
| 730 | public function getAllPluginCourseSettings() |
||
| 731 | { |
||
| 732 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 733 | /** @var Plugin $obj */ |
||
| 734 | $courseSettings = []; |
||
| 735 | if (!empty($pluginList)) { |
||
| 736 | foreach ($pluginList as $obj) { |
||
| 737 | $pluginCourseSetting = $obj->getCourseSettings(); |
||
| 738 | $courseSettings = array_merge($courseSettings, $pluginCourseSetting); |
||
| 739 | } |
||
| 740 | } |
||
| 741 | |||
| 742 | return $courseSettings; |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * When saving the plugin values in the course settings, check whether |
||
| 747 | * a callback method should be called and send it the updated settings. |
||
| 748 | * |
||
| 749 | * @param array $values The new settings the user just saved |
||
| 750 | */ |
||
| 751 | public function saveCourseSettingsHook($values) |
||
| 752 | { |
||
| 753 | $pluginList = $this->getInstalledPluginListObject(); |
||
| 754 | |||
| 755 | /** @var Plugin $obj */ |
||
| 756 | foreach ($pluginList as $obj) { |
||
| 757 | $settings = $obj->getCourseSettings(); |
||
| 758 | $subValues = []; |
||
| 759 | if (!empty($settings)) { |
||
| 760 | foreach ($settings as $v) { |
||
| 761 | if (isset($values[$v])) { |
||
| 762 | $subValues[$v] = $values[$v]; |
||
| 763 | } |
||
| 764 | } |
||
| 765 | } |
||
| 766 | |||
| 767 | if (!empty($subValues)) { |
||
| 768 | $obj->course_settings_updated($subValues); |
||
| 769 | } |
||
| 770 | } |
||
| 771 | } |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @param array $pluginRegionList |
||
| 775 | * @param string $pluginRegion |
||
| 776 | * @param Twig_Environment $twig |
||
| 777 | */ |
||
| 778 | public function setPluginRegion($pluginRegionList, $pluginRegion, $twig) |
||
| 785 | ); |
||
| 786 | |||
| 787 | //$twig->addGlobal('plugin_'.$pluginRegion, $regionContent); |
||
| 788 | } |
||
| 789 | } |
||
| 790 |