|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
use Chamilo\PluginBundle\UserRemoteService\UserRemoteService; |
|
5
|
|
|
use Doctrine\ORM\OptimisticLockException; |
|
6
|
|
|
|
|
7
|
|
|
class UserRemoteServicePlugin extends Plugin |
|
8
|
|
|
{ |
|
9
|
|
|
public const TABLE = 'plugin_user_remote_service'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* UserRemoteServicePlugin constructor. |
|
13
|
|
|
*/ |
|
14
|
|
|
protected function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::__construct( |
|
17
|
|
|
'1.0', |
|
18
|
|
|
'Sébastien Ducoulombier', |
|
19
|
|
|
[ |
|
20
|
|
|
'salt' => 'text', |
|
21
|
|
|
'hide_link_from_navigation_menu' => 'boolean', |
|
22
|
|
|
] |
|
23
|
|
|
); |
|
24
|
|
|
$this->isAdminPlugin = true; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Caches and returns a single instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @return UserRemoteServicePlugin |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function create() |
|
33
|
|
|
{ |
|
34
|
|
|
static $result = null; |
|
35
|
|
|
|
|
36
|
|
|
return $result ? $result : $result = new self(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates the plugin table. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function install() |
|
43
|
|
|
{ |
|
44
|
|
|
Database::query( |
|
45
|
|
|
sprintf( |
|
46
|
|
|
'create table if not exists %s ( |
|
47
|
|
|
id int unsigned not null auto_increment primary key, |
|
48
|
|
|
title varchar(255) not null, |
|
49
|
|
|
url varchar(255) not null |
|
50
|
|
|
)', |
|
51
|
|
|
Database::get_main_table(self::TABLE) |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Drops the plugin table. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function uninstall() |
|
60
|
|
|
{ |
|
61
|
|
|
Database::query('drop table if exists '.Database::get_main_table(self::TABLE)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string the salt setting |
|
66
|
|
|
*/ |
|
67
|
|
|
public function salt() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->get('salt'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return bool the value of hide_link_from_navigation_menu setting |
|
74
|
|
|
*/ |
|
75
|
|
|
public function get_hide_link_from_navigation_menu() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->get('hide_link_from_navigation_menu'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Retrieves the list of all services. |
|
82
|
|
|
* |
|
83
|
|
|
* @return UserRemoteService[] |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getServices() |
|
86
|
|
|
{ |
|
87
|
|
|
return Database::getManager()->getRepository( |
|
88
|
|
|
'Chamilo\PluginBundle\UserRemoteService\UserRemoteService' |
|
89
|
|
|
)->findAll(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Adds a new service. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $title |
|
96
|
|
|
* @param string $url |
|
97
|
|
|
* |
|
98
|
|
|
* @throws OptimisticLockException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function addService($title, $url) |
|
101
|
|
|
{ |
|
102
|
|
|
$service = new UserRemoteService(); |
|
103
|
|
|
$service->setTitle($title); |
|
104
|
|
|
$service->setURL($url); |
|
105
|
|
|
Database::getManager()->persist($service); |
|
106
|
|
|
Database::getManager()->flush(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Removes a service. |
|
111
|
|
|
* |
|
112
|
|
|
* @param UserRemoteService $service |
|
113
|
|
|
* |
|
114
|
|
|
* @throws OptimisticLockException |
|
115
|
|
|
*/ |
|
116
|
|
|
public function removeService($service) |
|
117
|
|
|
{ |
|
118
|
|
|
Database::getManager()->remove($service); |
|
119
|
|
|
Database::getManager()->flush(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Updates a service. |
|
124
|
|
|
* |
|
125
|
|
|
* @param UserRemoteService $service |
|
126
|
|
|
* |
|
127
|
|
|
* @throws OptimisticLockException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function updateService($service) |
|
130
|
|
|
{ |
|
131
|
|
|
Database::getManager()->persist($service); |
|
132
|
|
|
Database::getManager()->flush(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns the active service id. |
|
137
|
|
|
* |
|
138
|
|
|
* @return int|null |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getActiveServiceId() |
|
141
|
|
|
{ |
|
142
|
|
|
return array_key_exists('serviceId', $_REQUEST) ? intval($_REQUEST['serviceId']) : null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Generates the menu items to be appended to the navigation array. |
|
147
|
|
|
* |
|
148
|
|
|
* @see \return_navigation_array |
|
149
|
|
|
* |
|
150
|
|
|
* @return array menu items |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getNavigationMenu() |
|
153
|
|
|
{ |
|
154
|
|
|
$menu = []; |
|
155
|
|
|
$activeServiceId = $this->getActiveServiceId(); |
|
156
|
|
|
foreach ($this->getServices() as $service) { |
|
157
|
|
|
$key = 'service_'.$service->getId(); |
|
158
|
|
|
$current = $service->getId() == $activeServiceId; |
|
159
|
|
|
$menu[$key] = [ |
|
160
|
|
|
'key' => $key, |
|
161
|
|
|
'current' => $current ? 'active' : '', |
|
162
|
|
|
'url' => sprintf( |
|
163
|
|
|
'%s%s/iframe.php?serviceId=%d', |
|
164
|
|
|
api_get_path(WEB_PLUGIN_PATH), |
|
165
|
|
|
$this->get_name(), |
|
166
|
|
|
$service->getId() |
|
167
|
|
|
), |
|
168
|
|
|
'title' => $service->getTitle(), |
|
169
|
|
|
]; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $menu; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Generates and handles submission of the creation form. |
|
177
|
|
|
* |
|
178
|
|
|
* @throws OptimisticLockException |
|
179
|
|
|
* |
|
180
|
|
|
* @return FormValidator |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getCreationForm() |
|
183
|
|
|
{ |
|
184
|
|
|
$form = new FormValidator('creationForm'); |
|
185
|
|
|
$titleText = $form->addText('title', get_lang('ServiceTitle')); |
|
186
|
|
|
$urlText = $form->addText('url', get_lang('ServiceURL')); |
|
187
|
|
|
$form->addButtonCreate(get_lang('CreateService')); |
|
188
|
|
|
if ($form->validate()) { |
|
189
|
|
|
$this->addService($titleText->getValue(), $urlText->getValue()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $form; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Generates and handles submission of the service deletion form. |
|
197
|
|
|
* |
|
198
|
|
|
* @throws OptimisticLockException |
|
199
|
|
|
* |
|
200
|
|
|
* @return FormValidator |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getDeletionForm() |
|
203
|
|
|
{ |
|
204
|
|
|
$form = new FormValidator('deletionForm'); |
|
205
|
|
|
$services = $this->getServices(); |
|
206
|
|
|
$options = []; |
|
207
|
|
|
foreach ($services as $service) { |
|
208
|
|
|
$options[$service->getId()] = $service->getTitle(); |
|
209
|
|
|
} |
|
210
|
|
|
$serviceIdSelect = $form->addSelect('serviceId', get_lang('ServicesToDelete'), $options); |
|
211
|
|
|
$serviceIdSelect->setMultiple(true); |
|
212
|
|
|
$form->addButtonDelete(get_lang('DeleteServices')); |
|
213
|
|
|
if ($form->validate()) { |
|
214
|
|
|
foreach ($serviceIdSelect->getValue() as $serviceId) { |
|
215
|
|
|
foreach ($services as $service) { |
|
216
|
|
|
if ($service->getId() == $serviceId) { |
|
217
|
|
|
$this->removeService($service); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $form; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Generates the service HTML table. |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getServiceHTMLTable() |
|
232
|
|
|
{ |
|
233
|
|
|
$html = ''; |
|
234
|
|
|
$services = $this->getServices(); |
|
235
|
|
|
if (!empty($services)) { |
|
236
|
|
|
$table = new HTML_Table('class="table"'); |
|
237
|
|
|
$table->addRow( |
|
238
|
|
|
[ |
|
239
|
|
|
get_lang('ServiceTitle'), |
|
240
|
|
|
get_lang('ServiceURL'), |
|
241
|
|
|
get_lang('RedirectAccessURL'), |
|
242
|
|
|
], |
|
243
|
|
|
null, |
|
244
|
|
|
'th' |
|
245
|
|
|
); |
|
246
|
|
|
foreach ($services as $service) { |
|
247
|
|
|
$table->addRow([ |
|
248
|
|
|
$service->getTitle(), |
|
249
|
|
|
$service->getURL(), |
|
250
|
|
|
$service->getAccessURL($this->get_name()), |
|
251
|
|
|
]); |
|
252
|
|
|
} |
|
253
|
|
|
$html = $table->toHtml(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return $html; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Retrieves one service. |
|
261
|
|
|
* |
|
262
|
|
|
* @param int $id the service identifier |
|
263
|
|
|
* |
|
264
|
|
|
* @return UserRemoteService the service |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getService($id) |
|
267
|
|
|
{ |
|
268
|
|
|
return Database::getManager()->getRepository( |
|
269
|
|
|
'Chamilo\PluginBundle\UserRemoteService\UserRemoteService' |
|
270
|
|
|
)->find($id); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Generates the iframe HTML element to load a service URL. |
|
275
|
|
|
* |
|
276
|
|
|
* @throws Exception on hash generation failure |
|
277
|
|
|
* |
|
278
|
|
|
* @return string the iframe HTML element |
|
279
|
|
|
*/ |
|
280
|
|
|
public function getIFrame() |
|
281
|
|
|
{ |
|
282
|
|
|
$userInfo = api_get_user_info(); |
|
283
|
|
|
|
|
284
|
|
|
return sprintf( |
|
285
|
|
|
'<div class="embed-responsive embed-responsive-16by9"> |
|
286
|
|
|
<iframe class="embed-responsive-item" src="%s"></iframe> |
|
287
|
|
|
</div>', |
|
288
|
|
|
$this->getService( |
|
289
|
|
|
$this->getActiveServiceId() |
|
290
|
|
|
)->getCustomUserURL($userInfo['username'], $userInfo['id'], $this->salt()) |
|
291
|
|
|
); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Generates the redirect user specific URL for redirection. |
|
296
|
|
|
* |
|
297
|
|
|
* @throws Exception on hash generation failure |
|
298
|
|
|
* |
|
299
|
|
|
* @return string the specific user redirect URL |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getActiveServiceSpecificUserUrl() |
|
302
|
|
|
{ |
|
303
|
|
|
$userInfo = api_get_user_info(); |
|
304
|
|
|
|
|
305
|
|
|
return $this->getService( |
|
306
|
|
|
$this->getActiveServiceId() |
|
307
|
|
|
)->getCustomUserRedirectURL($userInfo['id'], $this->salt()); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|