| Total Complexity | 136 |
| Total Lines | 1050 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Plugin |
||
| 20 | { |
||
| 21 | const TAB_FILTER_NO_STUDENT = '::no-student'; |
||
| 22 | const TAB_FILTER_ONLY_STUDENT = '::only-student'; |
||
| 23 | public $isCoursePlugin = false; |
||
| 24 | public $isAdminPlugin = false; |
||
| 25 | public $isMailPlugin = false; |
||
| 26 | // Adds icon in the course home |
||
| 27 | public $addCourseTool = true; |
||
| 28 | public $hasPersonalEvents = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * When creating a new course, these settings are added to the course, in |
||
| 32 | * the course_info/infocours.php |
||
| 33 | * To show the plugin course icons you need to add these icons: |
||
| 34 | * main/img/icons/22/plugin_name.png |
||
| 35 | * main/img/icons/64/plugin_name.png |
||
| 36 | * main/img/icons/64/plugin_name_na.png. |
||
| 37 | * |
||
| 38 | * @example |
||
| 39 | * $course_settings = array( |
||
| 40 | array('name' => 'big_blue_button_welcome_message', 'type' => 'text'), |
||
| 41 | array('name' => 'big_blue_button_record_and_store', 'type' => 'checkbox') |
||
| 42 | ); |
||
| 43 | */ |
||
| 44 | public $course_settings = []; |
||
| 45 | /** |
||
| 46 | * This indicates whether changing the setting should execute the callback |
||
| 47 | * function. |
||
| 48 | */ |
||
| 49 | public $course_settings_callback = false; |
||
| 50 | |||
| 51 | protected $version = ''; |
||
| 52 | protected $author = ''; |
||
| 53 | protected $fields = []; |
||
| 54 | private $settings = []; |
||
| 55 | // Translation strings. |
||
| 56 | private $strings = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Default constructor for the plugin class. By default, it only sets |
||
| 60 | * a few attributes of the object. |
||
| 61 | * |
||
| 62 | * @param string $version of this plugin |
||
| 63 | * @param string $author of this plugin |
||
| 64 | * @param array $settings settings to be proposed to configure the plugin |
||
| 65 | */ |
||
| 66 | protected function __construct($version, $author, $settings = []) |
||
| 67 | { |
||
| 68 | $this->version = $version; |
||
| 69 | $this->author = $author; |
||
| 70 | $this->fields = $settings; |
||
| 71 | |||
| 72 | global $language_files; |
||
| 73 | $language_files[] = 'plugin_'.$this->get_name(); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Gets an array of information about this plugin (name, version, ...). |
||
| 78 | * |
||
| 79 | * @return array Array of information elements about this plugin |
||
| 80 | */ |
||
| 81 | public function get_info() |
||
| 82 | { |
||
| 83 | $result = []; |
||
| 84 | $result['obj'] = $this; |
||
| 85 | $result['title'] = $this->get_title(); |
||
| 86 | $result['comment'] = $this->get_comment(); |
||
| 87 | $result['version'] = $this->get_version(); |
||
| 88 | $result['author'] = $this->get_author(); |
||
| 89 | $result['plugin_class'] = get_class($this); |
||
| 90 | $result['is_course_plugin'] = $this->isCoursePlugin; |
||
| 91 | $result['is_admin_plugin'] = $this->isAdminPlugin; |
||
| 92 | $result['is_mail_plugin'] = $this->isMailPlugin; |
||
| 93 | |||
| 94 | if ($form = $this->getSettingsForm()) { |
||
| 95 | $result['settings_form'] = $form; |
||
| 96 | |||
| 97 | foreach ($this->fields as $name => $type) { |
||
| 98 | $value = $this->get($name); |
||
| 99 | |||
| 100 | if (is_array($type)) { |
||
| 101 | $value = $type['options']; |
||
| 102 | } |
||
| 103 | $result[$name] = $value; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return $result; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the "system" name of the plugin in lowercase letters. |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function get_name() |
||
| 116 | { |
||
| 117 | $result = get_class($this); |
||
| 118 | $result = str_replace('Plugin', '', $result); |
||
| 119 | $result = strtolower($result); |
||
| 120 | |||
| 121 | return $result; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getCamelCaseName() |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the title of the plugin. |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function get_title() |
||
| 140 | { |
||
| 141 | return $this->get_lang('plugin_title'); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns the description of the plugin. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function get_comment() |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the version of the plugin. |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function get_version() |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns the author of the plugin. |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function get_author() |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the contents of the CSS defined by the plugin. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function get_css() |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns an HTML form (generated by FormValidator) of the plugin settings. |
||
| 195 | * |
||
| 196 | * @return FormValidator FormValidator-generated form |
||
| 197 | */ |
||
| 198 | public function getSettingsForm() |
||
| 199 | { |
||
| 200 | $result = new FormValidator($this->get_name()); |
||
| 201 | |||
| 202 | $defaults = []; |
||
| 203 | $checkboxGroup = []; |
||
| 204 | $checkboxCollection = []; |
||
| 205 | |||
| 206 | if ($checkboxNames = array_keys($this->fields, 'checkbox')) { |
||
| 207 | $pluginInfoCollection = api_get_settings('Plugins'); |
||
| 208 | foreach ($pluginInfoCollection as $pluginInfo) { |
||
| 209 | if (false !== array_search($pluginInfo['title'], $checkboxNames)) { |
||
| 210 | $checkboxCollection[$pluginInfo['title']] = $pluginInfo; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | foreach ($this->fields as $name => $type) { |
||
| 216 | $options = null; |
||
| 217 | if (is_array($type) && isset($type['type']) && 'select' === $type['type']) { |
||
| 218 | $attributes = isset($type['attributes']) ? $type['attributes'] : []; |
||
| 219 | if (!empty($type['options']) && isset($type['translate_options']) && $type['translate_options']) { |
||
| 220 | foreach ($type['options'] as $key => &$optionName) { |
||
| 221 | $optionName = $this->get_lang($optionName); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | $options = $type['options']; |
||
| 225 | $type = $type['type']; |
||
| 226 | } |
||
| 227 | |||
| 228 | $value = $this->get($name); |
||
| 229 | $defaults[$name] = $value; |
||
| 230 | $type = isset($type) ? $type : 'text'; |
||
| 231 | |||
| 232 | $help = null; |
||
| 233 | if ($this->get_lang_plugin_exists($name.'_help')) { |
||
| 234 | $help = $this->get_lang($name.'_help'); |
||
| 235 | if ("show_main_menu_tab" === $name) { |
||
| 236 | $pluginName = strtolower(str_replace('Plugin', '', get_class($this))); |
||
| 237 | $pluginUrl = api_get_path(WEB_PATH)."plugin/$pluginName/index.php"; |
||
| 238 | $pluginUrl = "<a href=$pluginUrl>$pluginUrl</a>"; |
||
| 239 | $help = sprintf($help, $pluginUrl); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | switch ($type) { |
||
| 244 | case 'html': |
||
| 245 | $result->addHtml($this->get_lang($name)); |
||
| 246 | break; |
||
| 247 | case 'wysiwyg': |
||
| 248 | $result->addHtmlEditor($name, $this->get_lang($name), false); |
||
| 249 | break; |
||
| 250 | case 'text': |
||
| 251 | $result->addElement($type, $name, [$this->get_lang($name), $help]); |
||
| 252 | break; |
||
| 253 | case 'boolean': |
||
| 254 | $group = []; |
||
| 255 | $group[] = $result->createElement( |
||
| 256 | 'radio', |
||
| 257 | $name, |
||
| 258 | '', |
||
| 259 | get_lang('Yes'), |
||
| 260 | 'true' |
||
| 261 | ); |
||
| 262 | $group[] = $result->createElement( |
||
| 263 | 'radio', |
||
| 264 | $name, |
||
| 265 | '', |
||
| 266 | get_lang('No'), |
||
| 267 | 'false' |
||
| 268 | ); |
||
| 269 | $result->addGroup($group, null, [$this->get_lang($name), $help]); |
||
| 270 | break; |
||
| 271 | case 'checkbox': |
||
| 272 | $selectedValue = null; |
||
| 273 | if (isset($checkboxCollection[$name])) { |
||
| 274 | if ('true' === $checkboxCollection[$name]['selected_value']) { |
||
| 275 | $selectedValue = 'checked'; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | $element = $result->createElement( |
||
| 280 | $type, |
||
| 281 | $name, |
||
| 282 | '', |
||
| 283 | $this->get_lang($name), |
||
| 284 | $selectedValue |
||
| 285 | ); |
||
| 286 | $element->_attributes['value'] = 'true'; |
||
| 287 | $checkboxGroup[] = $element; |
||
| 288 | break; |
||
| 289 | case 'select': |
||
| 290 | $result->addElement( |
||
| 291 | $type, |
||
| 292 | $name, |
||
| 293 | [$this->get_lang($name), $help], |
||
| 294 | $options, |
||
| 295 | $attributes |
||
|
|
|||
| 296 | ); |
||
| 297 | break; |
||
| 298 | case 'user': |
||
| 299 | $options = []; |
||
| 300 | if (!empty($value)) { |
||
| 301 | $userInfo = api_get_user_info($value); |
||
| 302 | if ($userInfo) { |
||
| 303 | $options[$value] = $userInfo['complete_name']; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | $result->addSelectAjax( |
||
| 307 | $name, |
||
| 308 | [$this->get_lang($name), $help], |
||
| 309 | $options, |
||
| 310 | ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like'] |
||
| 311 | ); |
||
| 312 | break; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | if (!empty($checkboxGroup)) { |
||
| 317 | $result->addGroup( |
||
| 318 | $checkboxGroup, |
||
| 319 | null, |
||
| 320 | ['', $help] |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | $result->setDefaults($defaults); |
||
| 324 | $result->addButtonSave($this->get_lang('Save'), 'submit_button'); |
||
| 325 | |||
| 326 | return $result; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Returns the value of a given plugin global setting. |
||
| 331 | * |
||
| 332 | * @param string $name of the plugin |
||
| 333 | * |
||
| 334 | * @return string Value of the plugin |
||
| 335 | */ |
||
| 336 | public function get($name) |
||
| 337 | { |
||
| 338 | $settings = $this->get_settings(); |
||
| 339 | foreach ($settings as $setting) { |
||
| 340 | if ($setting['variable'] == $this->get_name().'_'.$name) { |
||
| 341 | $unserialized = UnserializeApi::unserialize('not_allowed_classes', $setting['selected_value'], true); |
||
| 342 | |||
| 343 | if (!empty($setting['selected_value']) && |
||
| 344 | false !== $unserialized |
||
| 345 | ) { |
||
| 346 | $setting['selected_value'] = $unserialized; |
||
| 347 | } |
||
| 348 | |||
| 349 | return $setting['selected_value']; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | return false; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Returns an array with the global settings for this plugin. |
||
| 358 | * |
||
| 359 | * @param bool $forceFromDB Optional. Force get settings from the database |
||
| 360 | * |
||
| 361 | * @return array Plugin settings as an array |
||
| 362 | */ |
||
| 363 | public function get_settings($forceFromDB = false) |
||
| 364 | { |
||
| 365 | if (empty($this->settings) || $forceFromDB) { |
||
| 366 | $settings = api_get_settings_params( |
||
| 367 | [ |
||
| 368 | "subkey = ? AND category = ? AND type = ? AND access_url = ?" => [ |
||
| 369 | $this->get_name(), |
||
| 370 | 'Plugins', |
||
| 371 | 'setting', |
||
| 372 | api_get_current_access_url_id(), |
||
| 373 | ], |
||
| 374 | ] |
||
| 375 | ); |
||
| 376 | $this->settings = $settings; |
||
| 377 | } |
||
| 378 | |||
| 379 | return $this->settings; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Tells whether language variables are defined for this plugin or not. |
||
| 384 | * |
||
| 385 | * @param string $name System name of the plugin |
||
| 386 | * |
||
| 387 | * @return bool True if the plugin has language variables defined, false otherwise |
||
| 388 | */ |
||
| 389 | public function get_lang_plugin_exists($name) |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Hook for the get_lang() function to check for plugin-defined language terms. |
||
| 396 | * |
||
| 397 | * @param string $name of the language variable we are looking for |
||
| 398 | * |
||
| 399 | * @return string The translated language term of the plugin |
||
| 400 | */ |
||
| 401 | public function get_lang($name) |
||
| 402 | { |
||
| 403 | // Check whether the language strings for the plugin have already been |
||
| 404 | // loaded. If so, no need to load them again. |
||
| 405 | if (is_null($this->strings)) { |
||
| 406 | $language_interface = api_get_interface_language(); |
||
| 407 | $root = api_get_path(SYS_PLUGIN_PATH); |
||
| 408 | $plugin_name = $this->get_name(); |
||
| 409 | |||
| 410 | $interfaceLanguageId = api_get_language_id($language_interface); |
||
| 411 | if (empty($interfaceLanguageId)) { |
||
| 412 | $language_interface = api_get_setting('platformLanguage'); |
||
| 413 | $interfaceLanguageId = api_get_language_id($language_interface); |
||
| 414 | } |
||
| 415 | $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId); |
||
| 416 | $languageParentId = !empty($interfaceLanguageInfo['parent_id']) ? (int) $interfaceLanguageInfo['parent_id'] : 0; |
||
| 417 | |||
| 418 | // 1. Loading english if exists |
||
| 419 | $english_path = $root.$plugin_name."/lang/english.php"; |
||
| 420 | |||
| 421 | if (is_readable($english_path)) { |
||
| 422 | $strings = []; |
||
| 423 | include $english_path; |
||
| 424 | $this->strings = $strings; |
||
| 425 | } |
||
| 426 | |||
| 427 | $path = $root.$plugin_name."/lang/$language_interface.php"; |
||
| 428 | // 2. Loading the system language |
||
| 429 | if (is_readable($path)) { |
||
| 430 | include $path; |
||
| 431 | if (!empty($strings)) { |
||
| 432 | foreach ($strings as $key => $string) { |
||
| 433 | $this->strings[$key] = $string; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | } elseif ($languageParentId > 0) { |
||
| 437 | $languageParentInfo = api_get_language_info($languageParentId); |
||
| 438 | $languageParentFolder = $languageParentInfo['dokeos_folder']; |
||
| 439 | |||
| 440 | $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php"; |
||
| 441 | if (is_readable($parentPath)) { |
||
| 442 | include $parentPath; |
||
| 443 | if (!empty($strings)) { |
||
| 444 | foreach ($strings as $key => $string) { |
||
| 445 | $this->strings[$key] = $string; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | } |
||
| 449 | } |
||
| 450 | } |
||
| 451 | if (isset($this->strings[$name])) { |
||
| 452 | return $this->strings[$name]; |
||
| 453 | } |
||
| 454 | |||
| 455 | return get_lang($name); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Caller for the install_course_fields() function. |
||
| 460 | * |
||
| 461 | * @param int $courseId |
||
| 462 | * @param bool $addToolLink Whether to add a tool link on the course homepage |
||
| 463 | */ |
||
| 464 | public function course_install($courseId, $addToolLink = true) |
||
| 465 | { |
||
| 466 | $this->install_course_fields($courseId, $addToolLink); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Add course settings and, if not asked otherwise, add a tool link on the course homepage. |
||
| 471 | * |
||
| 472 | * @param int $courseId Course integer ID |
||
| 473 | * @param bool $add_tool_link Whether to add a tool link or not |
||
| 474 | * (some tools might just offer a configuration section and act on the backend) |
||
| 475 | * |
||
| 476 | * @return bool|null False on error, null otherwise |
||
| 477 | */ |
||
| 478 | public function install_course_fields($courseId, $add_tool_link = true, $iconName = '') |
||
| 479 | { |
||
| 480 | $plugin_name = $this->get_name(); |
||
| 481 | $t_course = Database::get_course_table(TABLE_COURSE_SETTING); |
||
| 482 | $courseId = (int) $courseId; |
||
| 483 | |||
| 484 | if (empty($courseId)) { |
||
| 485 | return false; |
||
| 486 | } |
||
| 487 | |||
| 488 | // Adding course settings. |
||
| 489 | if (!empty($this->course_settings)) { |
||
| 490 | foreach ($this->course_settings as $setting) { |
||
| 491 | $variable = $setting['name']; |
||
| 492 | $value = ''; |
||
| 493 | if (isset($setting['init_value'])) { |
||
| 494 | $value = $setting['init_value']; |
||
| 495 | } |
||
| 496 | |||
| 497 | $pluginGlobalValue = api_get_plugin_setting($plugin_name, $variable); |
||
| 498 | if (null !== $pluginGlobalValue) { |
||
| 499 | $value = 1; |
||
| 500 | } |
||
| 501 | |||
| 502 | $type = 'textfield'; |
||
| 503 | if (isset($setting['type'])) { |
||
| 504 | $type = $setting['type']; |
||
| 505 | } |
||
| 506 | |||
| 507 | if (isset($setting['group'])) { |
||
| 508 | $group = $setting['group']; |
||
| 509 | $sql = "SELECT value |
||
| 510 | FROM $t_course |
||
| 511 | WHERE |
||
| 512 | c_id = $courseId AND |
||
| 513 | variable = '".Database::escape_string($group)."' AND |
||
| 514 | subkey = '".Database::escape_string($variable)."' |
||
| 515 | "; |
||
| 516 | $result = Database::query($sql); |
||
| 517 | if (!Database::num_rows($result)) { |
||
| 518 | $params = [ |
||
| 519 | 'c_id' => $courseId, |
||
| 520 | 'variable' => $group, |
||
| 521 | 'subkey' => $variable, |
||
| 522 | 'value' => $value, |
||
| 523 | 'category' => 'plugins', |
||
| 524 | 'type' => $type, |
||
| 525 | 'title' => '', |
||
| 526 | ]; |
||
| 527 | Database::insert($t_course, $params); |
||
| 528 | } |
||
| 529 | } else { |
||
| 530 | $sql = "SELECT value FROM $t_course |
||
| 531 | WHERE c_id = $courseId AND variable = '$variable' "; |
||
| 532 | $result = Database::query($sql); |
||
| 533 | if (!Database::num_rows($result)) { |
||
| 534 | $params = [ |
||
| 535 | 'c_id' => $courseId, |
||
| 536 | 'variable' => $variable, |
||
| 537 | 'subkey' => $plugin_name, |
||
| 538 | 'value' => $value, |
||
| 539 | 'category' => 'plugins', |
||
| 540 | 'type' => $type, |
||
| 541 | 'title' => '', |
||
| 542 | ]; |
||
| 543 | Database::insert($t_course, $params); |
||
| 544 | } |
||
| 545 | } |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | // Stop here if we don't want a tool link on the course homepage |
||
| 550 | if (!$add_tool_link || false == $this->addCourseTool) { |
||
| 551 | return true; |
||
| 552 | } |
||
| 553 | |||
| 554 | // Add an icon in the table tool list |
||
| 555 | $this->createLinkToCourseTool($plugin_name, $courseId, $iconName); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Delete the fields added to the course settings page and the link to the |
||
| 560 | * tool on the course's homepage. |
||
| 561 | * |
||
| 562 | * @param int $courseId |
||
| 563 | * |
||
| 564 | * @return false|null |
||
| 565 | */ |
||
| 566 | public function uninstall_course_fields($courseId) |
||
| 567 | { |
||
| 568 | $courseId = (int) $courseId; |
||
| 569 | |||
| 570 | if (empty($courseId)) { |
||
| 571 | return false; |
||
| 572 | } |
||
| 573 | $pluginName = $this->get_name(); |
||
| 574 | |||
| 575 | $t_course = Database::get_course_table(TABLE_COURSE_SETTING); |
||
| 576 | $t_tool = Database::get_course_table(TABLE_TOOL_LIST); |
||
| 577 | |||
| 578 | if (!empty($this->course_settings)) { |
||
| 579 | foreach ($this->course_settings as $setting) { |
||
| 580 | $variable = Database::escape_string($setting['name']); |
||
| 581 | if (!empty($setting['group'])) { |
||
| 582 | $variable = Database::escape_string($setting['group']); |
||
| 583 | } |
||
| 584 | if (empty($variable)) { |
||
| 585 | continue; |
||
| 586 | } |
||
| 587 | $sql = "DELETE FROM $t_course |
||
| 588 | WHERE c_id = $courseId AND variable = '$variable'"; |
||
| 589 | Database::query($sql); |
||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | $pluginName = Database::escape_string($pluginName); |
||
| 594 | $sql = "DELETE FROM $t_tool |
||
| 595 | WHERE c_id = $courseId AND |
||
| 596 | ( |
||
| 597 | name = '$pluginName' OR |
||
| 598 | name = '$pluginName:student' OR |
||
| 599 | name = '$pluginName:teacher' |
||
| 600 | )"; |
||
| 601 | Database::query($sql); |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Install the course fields and tool link of this plugin in all courses. |
||
| 606 | * |
||
| 607 | * @param bool $add_tool_link Whether we want to add a plugin link on the course homepage |
||
| 608 | */ |
||
| 609 | public function install_course_fields_in_all_courses($add_tool_link = true, $iconName = '') |
||
| 610 | { |
||
| 611 | // Update existing courses to add plugin settings |
||
| 612 | $table = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 613 | $sql = "SELECT id FROM $table ORDER BY id"; |
||
| 614 | $res = Database::query($sql); |
||
| 615 | while ($row = Database::fetch_assoc($res)) { |
||
| 616 | $this->install_course_fields($row['id'], $add_tool_link, $iconName); |
||
| 617 | } |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Uninstall the plugin settings fields from all courses. |
||
| 622 | */ |
||
| 623 | public function uninstall_course_fields_in_all_courses() |
||
| 624 | { |
||
| 625 | // Update existing courses to add conference settings |
||
| 626 | $table = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 627 | $sql = "SELECT id FROM $table |
||
| 628 | ORDER BY id"; |
||
| 629 | $res = Database::query($sql); |
||
| 630 | while ($row = Database::fetch_assoc($res)) { |
||
| 631 | $this->uninstall_course_fields($row['id']); |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | public function getCourseSettings() |
||
| 639 | { |
||
| 640 | $settings = []; |
||
| 641 | if (is_array($this->course_settings)) { |
||
| 642 | foreach ($this->course_settings as $item) { |
||
| 643 | // Skip html type |
||
| 644 | if ('html' === $item['type']) { |
||
| 645 | continue; |
||
| 646 | } |
||
| 647 | if (isset($item['group'])) { |
||
| 648 | if (!in_array($item['group'], $settings)) { |
||
| 649 | $settings[] = $item['group']; |
||
| 650 | } |
||
| 651 | } else { |
||
| 652 | $settings[] = $item['name']; |
||
| 653 | } |
||
| 654 | } |
||
| 655 | } |
||
| 656 | |||
| 657 | return $settings; |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Method to be extended when changing the setting in the course |
||
| 662 | * configuration should trigger the use of a callback method. |
||
| 663 | * |
||
| 664 | * @param array $values sent back from the course configuration script |
||
| 665 | */ |
||
| 666 | public function course_settings_updated($values = []) |
||
| 667 | { |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Add a tab to platform. |
||
| 672 | * |
||
| 673 | * @param string $tabName |
||
| 674 | * @param string $url |
||
| 675 | * @param string $userFilter Optional. Filter tab type |
||
| 676 | * |
||
| 677 | * @return false|string |
||
| 678 | */ |
||
| 679 | public function addTab($tabName, $url, $userFilter = null) |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Delete a tab to chamilo's platform. |
||
| 751 | * |
||
| 752 | * @param string $key |
||
| 753 | * |
||
| 754 | * @return bool $resp Transaction response |
||
| 755 | */ |
||
| 756 | public function deleteTab($key) |
||
| 757 | { |
||
| 758 | $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT); |
||
| 759 | $sql = "SELECT * |
||
| 760 | FROM $table |
||
| 761 | WHERE variable = 'show_tabs' |
||
| 762 | AND subkey <> '$key' |
||
| 763 | AND subkey like 'custom_tab_%' |
||
| 764 | "; |
||
| 765 | $resp = $result = Database::query($sql); |
||
| 766 | $customTabsNum = Database::num_rows($result); |
||
| 767 | |||
| 768 | if (!empty($key)) { |
||
| 769 | $whereCondition = [ |
||
| 770 | 'variable = ? AND subkey = ?' => ['show_tabs', $key], |
||
| 771 | ]; |
||
| 772 | $resp = Database::delete('settings_current', $whereCondition); |
||
| 773 | |||
| 774 | //if there is more than one tab |
||
| 775 | //re enumerate them |
||
| 776 | if (!empty($customTabsNum) && $customTabsNum > 0) { |
||
| 777 | $tabs = Database::store_result($result, 'ASSOC'); |
||
| 778 | $i = 1; |
||
| 779 | foreach ($tabs as $row) { |
||
| 780 | $newSubKey = "custom_tab_$i"; |
||
| 781 | |||
| 782 | if (false !== strpos($row['subkey'], self::TAB_FILTER_NO_STUDENT)) { |
||
| 783 | $newSubKey .= self::TAB_FILTER_NO_STUDENT; |
||
| 784 | } elseif (false !== strpos($row['subkey'], self::TAB_FILTER_ONLY_STUDENT)) { |
||
| 785 | $newSubKey .= self::TAB_FILTER_ONLY_STUDENT; |
||
| 786 | } |
||
| 787 | |||
| 788 | $attributes = ['subkey' => $newSubKey]; |
||
| 789 | $this->updateTab($row['subkey'], $attributes); |
||
| 790 | $i++; |
||
| 791 | } |
||
| 792 | } |
||
| 793 | } |
||
| 794 | |||
| 795 | return $resp; |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Update the tabs attributes. |
||
| 800 | * |
||
| 801 | * @param string $key |
||
| 802 | * @param array $attributes |
||
| 803 | * |
||
| 804 | * @return bool |
||
| 805 | */ |
||
| 806 | public function updateTab($key, $attributes) |
||
| 807 | { |
||
| 808 | $whereCondition = [ |
||
| 809 | 'variable = ? AND subkey = ?' => ['show_tabs', $key], |
||
| 810 | ]; |
||
| 811 | $resp = Database::update('settings_current', $attributes, $whereCondition); |
||
| 812 | |||
| 813 | return $resp; |
||
| 814 | } |
||
| 815 | |||
| 816 | /** |
||
| 817 | * This method shows or hides plugin's tab. |
||
| 818 | * |
||
| 819 | * @param bool $showTab Shows or hides the main menu plugin tab |
||
| 820 | * @param string $filePath Plugin starter file path |
||
| 821 | */ |
||
| 822 | public function manageTab($showTab, $filePath = 'index.php') |
||
| 823 | { |
||
| 824 | $langString = str_replace('Plugin', '', get_class($this)); |
||
| 825 | $pluginName = strtolower($langString); |
||
| 826 | $pluginUrl = 'plugin/'.$pluginName.'/'.$filePath; |
||
| 827 | |||
| 828 | if ('true' === $showTab) { |
||
| 829 | $tabAdded = $this->addTab($langString, $pluginUrl); |
||
| 830 | if ($tabAdded) { |
||
| 831 | // The page must be refreshed to show the recently created tab |
||
| 832 | echo "<script>location.href = '".Security::remove_XSS($_SERVER['REQUEST_URI'])."';</script>"; |
||
| 833 | } |
||
| 834 | } else { |
||
| 835 | $settingsCurrentTable = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT); |
||
| 836 | $conditions = [ |
||
| 837 | 'where' => [ |
||
| 838 | "variable = 'show_tabs' AND title = ? AND comment = ? " => [ |
||
| 839 | $langString, |
||
| 840 | $pluginUrl, |
||
| 841 | ], |
||
| 842 | ], |
||
| 843 | ]; |
||
| 844 | $result = Database::select('subkey', $settingsCurrentTable, $conditions); |
||
| 845 | if (!empty($result)) { |
||
| 846 | $this->deleteTab($result[0]['subkey']); |
||
| 847 | } |
||
| 848 | } |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * @param string $variable |
||
| 853 | * |
||
| 854 | * @return bool |
||
| 855 | */ |
||
| 856 | public function validateCourseSetting($variable) |
||
| 857 | { |
||
| 858 | return true; |
||
| 859 | } |
||
| 860 | |||
| 861 | /** |
||
| 862 | * @param string $region |
||
| 863 | * |
||
| 864 | * @return string |
||
| 865 | */ |
||
| 866 | public function renderRegion($region) |
||
| 867 | { |
||
| 868 | return ''; |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Returns true if the plugin is installed, false otherwise. |
||
| 873 | * |
||
| 874 | * @param bool $checkEnabled Also check if enabled (instead of only installed) |
||
| 875 | * |
||
| 876 | * @return bool True if plugin is installed/enabled, false otherwise |
||
| 877 | */ |
||
| 878 | public function isEnabled($checkEnabled = false) |
||
| 879 | { |
||
| 880 | $settings = api_get_settings_params_simple( |
||
| 881 | [ |
||
| 882 | "subkey = ? AND category = ? AND type = ? AND variable = 'status' " => [ |
||
| 883 | $this->get_name(), |
||
| 884 | 'Plugins', |
||
| 885 | 'setting', |
||
| 886 | ], |
||
| 887 | ] |
||
| 888 | ); |
||
| 889 | if (is_array($settings) && isset($settings['selected_value']) && $settings['selected_value'] == 'installed') { |
||
| 890 | // The plugin is installed |
||
| 891 | // If we need a check on whether it is enabled, also check for |
||
| 892 | // *plugin*_tool_enable and make sure it is *NOT* false |
||
| 893 | if ($checkEnabled) { |
||
| 894 | $enabled = api_get_settings_params_simple( |
||
| 895 | [ |
||
| 896 | "variable = ? AND subkey = ? AND category = 'Plugins' " => [ |
||
| 897 | $this->get_name().'_tool_enable', |
||
| 898 | $this->get_name(), |
||
| 899 | ], |
||
| 900 | ] |
||
| 901 | ); |
||
| 902 | if (is_array($enabled) && isset($enabled['selected_value']) && $enabled['selected_value'] == 'false') { |
||
| 903 | // Only return false if the setting exists and it is |
||
| 904 | // *specifically* set to false |
||
| 905 | return false; |
||
| 906 | } |
||
| 907 | } |
||
| 908 | |||
| 909 | return true; |
||
| 910 | } |
||
| 911 | |||
| 912 | return false; |
||
| 913 | } |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Allow make some actions after configure the plugin parameters |
||
| 917 | * This function is called from main/admin/configure_plugin.php page |
||
| 918 | * when saving the plugin parameters. |
||
| 919 | * |
||
| 920 | * @return \Plugin |
||
| 921 | */ |
||
| 922 | public function performActionsAfterConfigure() |
||
| 923 | { |
||
| 924 | return $this; |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * This function allows to change the visibility of the icon inside a course |
||
| 929 | * :student tool will be visible only for students |
||
| 930 | * :teacher tool will be visible only for teachers |
||
| 931 | * If nothing it's set then tool will be visible for both as a normal icon. |
||
| 932 | * |
||
| 933 | * @return string |
||
| 934 | */ |
||
| 935 | public function getToolIconVisibilityPerUserStatus() |
||
| 936 | { |
||
| 937 | return ''; |
||
| 938 | } |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Default tool icon visibility. |
||
| 942 | * |
||
| 943 | * @return bool |
||
| 944 | */ |
||
| 945 | public function isIconVisibleByDefault() |
||
| 946 | { |
||
| 947 | return true; |
||
| 948 | } |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Get the admin URL for the plugin if Plugin::isAdminPlugin is true. |
||
| 952 | * |
||
| 953 | * @return string |
||
| 954 | */ |
||
| 955 | public function getAdminUrl() |
||
| 956 | { |
||
| 957 | if (!$this->isAdminPlugin) { |
||
| 958 | return ''; |
||
| 959 | } |
||
| 960 | |||
| 961 | $name = $this->get_name(); |
||
| 962 | $sysPath = api_get_path(SYS_PLUGIN_PATH).$name; |
||
| 963 | $webPath = api_get_path(WEB_PLUGIN_PATH).$name; |
||
| 964 | |||
| 965 | if (file_exists("$sysPath/admin.php")) { |
||
| 966 | return "$webPath/admin.php"; |
||
| 967 | } |
||
| 968 | |||
| 969 | if (file_exists("$sysPath/start.php")) { |
||
| 970 | return "$webPath/start.php"; |
||
| 971 | } |
||
| 972 | |||
| 973 | return ''; |
||
| 974 | } |
||
| 975 | |||
| 976 | /** |
||
| 977 | * @param bool $value |
||
| 978 | */ |
||
| 979 | public function setHasPersonalEvents($value) |
||
| 980 | { |
||
| 981 | $this->hasPersonalEvents = $value; |
||
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Overwrite to perform some actions when deleting a user. |
||
| 986 | * |
||
| 987 | * @param int $userId |
||
| 988 | */ |
||
| 989 | public function doWhenDeletingUser($userId) |
||
| 991 | } |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Overwrite to perform some actions when deleting a course. |
||
| 995 | * |
||
| 996 | * @param int $courseId |
||
| 997 | */ |
||
| 998 | public function doWhenDeletingCourse($courseId) |
||
| 999 | { |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Overwrite to perform some actions when deleting a session. |
||
| 1004 | * |
||
| 1005 | * @param int $sessionId |
||
| 1006 | */ |
||
| 1007 | public function doWhenDeletingSession($sessionId) |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Add an link for a course tool. |
||
| 1013 | * |
||
| 1014 | * @param string $name The tool name |
||
| 1015 | * @param int $courseId The course ID |
||
| 1016 | * @param string $iconName Optional. Icon file name |
||
| 1017 | * @param string $link Optional. Link URL |
||
| 1018 | * |
||
| 1019 | * @return CTool|null |
||
| 1020 | */ |
||
| 1021 | protected function createLinkToCourseTool( |
||
| 1022 | $name, |
||
| 1023 | $courseId, |
||
| 1024 | $iconName = null, |
||
| 1069 | } |
||
| 1070 | } |
||
| 1071 |