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