RedirectionPlugin.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 redirection from user.
  79. *
  80. * @param int $userId
  81. */
  82. public static function deleteUserRedirection($userId)
  83. {
  84. $table = Database::get_main_table('plugin_redirection');
  85. Database::delete(
  86. $table,
  87. ['user_id = ?' => [$userId]]
  88. );
  89. }
  90. /**
  91. * Deletes an existing redirection.
  92. *
  93. * @param int $id
  94. */
  95. public static function delete($id)
  96. {
  97. $table = Database::get_main_table('plugin_redirection');
  98. Database::delete(
  99. $table,
  100. ['id = ?' => [$id]]
  101. );
  102. }
  103. /**
  104. * Get all current redirection records.
  105. *
  106. * @return array
  107. */
  108. public static function getAll()
  109. {
  110. $table = Database::get_main_table('plugin_redirection');
  111. return Database::select('*', $table);
  112. }
  113. /**
  114. * Install the required DB structure to store the plugin data.
  115. */
  116. public static function install()
  117. {
  118. $table = Database::get_main_table('plugin_redirection');
  119. $sql = "CREATE TABLE IF NOT EXISTS $table (
  120. id INT unsigned NOT NULL auto_increment PRIMARY KEY,
  121. user_id INT unsigned NOT NULL DEFAULT 0,
  122. url VARCHAR(255) NOT NULL DEFAULT ''
  123. )";
  124. Database::query($sql);
  125. }
  126. /**
  127. * Uninstall the plugin, cleaning up the database structure created on install.
  128. */
  129. public static function uninstall()
  130. {
  131. $table = Database::get_main_table('plugin_redirection');
  132. $sql = "DROP TABLE IF EXISTS $table";
  133. Database::query($sql);
  134. }
  135. /**
  136. * Redirect user if plugin is installed.
  137. *
  138. * @param int $userId
  139. */
  140. public static function redirectUser($userId)
  141. {
  142. // Check redirection plugin
  143. $plugin = new AppPlugin();
  144. $pluginList = $plugin->getInstalledPlugins();
  145. $redirectionInstalled = in_array('redirection', $pluginList);
  146. if ($redirectionInstalled) {
  147. $pluginInfo = $plugin->getPluginInfo('redirection');
  148. if (!empty($pluginInfo) && isset($pluginInfo['obj'])) {
  149. /** @var RedirectionPlugin $redirectionPlugin */
  150. $redirectionPlugin = $pluginInfo['obj'];
  151. $record = $redirectionPlugin->getUrlFromUser($userId);
  152. if (!empty($record) && !empty($record['url'])) {
  153. header('Location: '.$record['url']);
  154. exit;
  155. }
  156. }
  157. }
  158. }
  159. }