RedirectionPlugin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Redirection plugin. Allows the configuration of redirection of specific users right after they login.
  5. *
  6. * @author Enrique Alcaraz Lopez
  7. *
  8. * @package chamilo.plugin.redirection
  9. */
  10. class RedirectionPlugin extends Plugin
  11. {
  12. public $isAdminPlugin = true;
  13. /**
  14. * Class constructor.
  15. */
  16. protected function __construct()
  17. {
  18. $version = '1.0';
  19. $author = 'Enrique Alcaraz, Julio Montoya';
  20. parent::__construct($version, $author, ['enabled' => 'boolean']);
  21. $this->isAdminPlugin = true;
  22. }
  23. /**
  24. * @return RedirectionPlugin
  25. */
  26. public static function create()
  27. {
  28. static $result = null;
  29. return $result ? $result : $result = new self();
  30. }
  31. /**
  32. * Inser a new redirection (and delete the previous one for this user, if any).
  33. *
  34. * @param int $userId
  35. * @param string $url
  36. *
  37. * @return false|string
  38. */
  39. public static function insert($userId, $url)
  40. {
  41. $userId = (int) $userId;
  42. if (empty($userId)) {
  43. return false;
  44. }
  45. $sql = "DELETE FROM plugin_redirection WHERE user_id = $userId";
  46. Database::query($sql);
  47. $userInfo = api_get_user_info($userId);
  48. if (empty($userInfo)) {
  49. return false;
  50. }
  51. return Database::insert(
  52. 'plugin_redirection',
  53. [
  54. 'user_id' => $userId,
  55. 'url' => $url,
  56. ]
  57. );
  58. }
  59. /**
  60. * Get the current redirection for a given user (if any).
  61. *
  62. * @param int $userId
  63. *
  64. * @return array
  65. */
  66. public static function getUrlFromUser($userId)
  67. {
  68. $userId = (int) $userId;
  69. $userInfo = api_get_user_info($userId);
  70. if (empty($userInfo)) {
  71. return false;
  72. }
  73. $sql = "SELECT * FROM plugin_redirection WHERE user_id = $userId LIMIT 1";
  74. $result = Database::query($sql);
  75. return Database::fetch_array($result, 'ASSOC');
  76. }
  77. /**
  78. * Deletes an existing redirection.
  79. *
  80. * @param int $id
  81. */
  82. public static function delete($id)
  83. {
  84. $table = Database::get_main_table('plugin_redirection');
  85. Database::delete(
  86. $table,
  87. ['id = ?' => [$id]]
  88. );
  89. }
  90. /**
  91. * Get all current redirection records.
  92. *
  93. * @return array
  94. */
  95. public static function getAll()
  96. {
  97. $table = Database::get_main_table('plugin_redirection');
  98. return Database::select('*', $table);
  99. }
  100. /**
  101. * Install the required DB structure to store the plugin data.
  102. */
  103. public static function install()
  104. {
  105. $table = Database::get_main_table('plugin_redirection');
  106. $sql = "CREATE TABLE IF NOT EXISTS $table (
  107. id INT unsigned NOT NULL auto_increment PRIMARY KEY,
  108. user_id INT unsigned NOT NULL DEFAULT 0,
  109. url VARCHAR(255) NOT NULL DEFAULT ''
  110. )";
  111. Database::query($sql);
  112. }
  113. /**
  114. * Uninstall the plugin, cleaning up the database structure created on install.
  115. */
  116. public static function uninstall()
  117. {
  118. $table = Database::get_main_table('plugin_redirection');
  119. $sql = "DROP TABLE IF EXISTS $table";
  120. Database::query($sql);
  121. }
  122. /**
  123. * Redirect user if plugin is installed.
  124. *
  125. * @param int $userId
  126. */
  127. public static function redirectUser($userId)
  128. {
  129. // Check redirection plugin
  130. $plugin = new AppPlugin();
  131. $pluginList = $plugin->get_installed_plugins();
  132. $redirectionInstalled = in_array('redirection', $pluginList);
  133. if ($redirectionInstalled) {
  134. $pluginInfo = $plugin->getPluginInfo('redirection');
  135. if (!empty($pluginInfo) && isset($pluginInfo['obj'])) {
  136. /** @var RedirectionPlugin $redirectionPlugin */
  137. $redirectionPlugin = $pluginInfo['obj'];
  138. $record = $redirectionPlugin->getUrlFromUser($userId);
  139. if (!empty($record) && !empty($record['url'])) {
  140. header('Location: '.$record['url']);
  141. exit;
  142. }
  143. }
  144. }
  145. }
  146. }