/
CourseDriver.php
445 lines (388 loc) · 12.8 KB
/
CourseDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Editor\Driver;
/**
* Class CourseDriver.
*
* @package Chamilo\CoreBundle\Component\Editor\Driver
*/
class CourseDriver extends Driver implements DriverInterface
{
public $name = 'CourseDriver';
public $visibleFiles = [];
private $coursePath;
/**
* Setups the folder.
*/
public function setup()
{
$userId = api_get_user_id();
$userInfo = api_get_user_info();
$sessionId = api_get_session_id();
$courseInfo = $this->connector->course;
if (!empty($courseInfo)) {
$coursePath = api_get_path(SYS_COURSE_PATH);
$courseDir = $courseInfo['directory'].'/document';
$baseDir = $coursePath.$courseDir;
$this->coursePath = $baseDir;
// Creates shared folder
if (!file_exists($baseDir.'/shared_folder')) {
$title = get_lang('UserFolders');
$folderName = '/shared_folder';
//$groupId = 0;
$visibility = 0;
create_unexisting_directory(
$courseInfo,
$userId,
$sessionId,
0,
null,
$baseDir,
$folderName,
$title,
$visibility
);
}
// Creates user-course folder
if (!file_exists($baseDir.'/shared_folder/sf_user_'.$userId)) {
$title = $userInfo['complete_name'];
$folderName = '/shared_folder/sf_user_'.$userId;
$visibility = 1;
create_unexisting_directory(
$courseInfo,
$userId,
$sessionId,
0,
null,
$baseDir,
$folderName,
$title,
$visibility
);
}
}
}
/**
* {@inheritdoc}
*/
public function getConfiguration()
{
if ($this->allow()) {
//$translator = $this->connector->translator;
//$code = $this->connector->course->getCode();
$courseCode = $this->connector->course['code'];
$alias = $courseCode.' '.get_lang('Documents');
$userId = api_get_user_id();
$config = [
'driver' => 'CourseDriver',
'path' => $this->getCourseDocumentSysPath(),
'URL' => $this->getCourseDocumentRelativeWebPath(),
'accessControl' => [$this, 'access'],
'alias' => $alias,
'attributes' => [
// Hide shared_folder
[
'pattern' => '/shared_folder/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false,
],
[
'pattern' => '/^\/index.html$/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false,
],
],
];
// admin/teachers can create dirs from ckeditor
if ($this->allowToEdit()) {
$config['attributes'][] = [
'pattern' => '/^\/learning_path$/', // block delete learning_path
'read' => true,
'write' => false,
'hidden' => false,
'locked' => true,
];
$config['attributes'][] = [
'pattern' => '/learning_path\/(.*)/', // allow edit/delete inside learning_path
'read' => true,
'write' => true,
'hidden' => false,
'locked' => false,
];
$defaultDisabled = $this->connector->getDefaultDriverSettings()['disabled'];
$defaultDisabled = array_flip($defaultDisabled);
unset($defaultDisabled['mkdir']);
$defaultDisabled = array_flip($defaultDisabled);
$config['disabled'] = $defaultDisabled;
} else {
$protectedFolders = \DocumentManager::getProtectedFolderFromStudent();
foreach ($protectedFolders as $folder) {
$config['attributes'][] = [
'pattern' => $folder.'/',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false,
];
}
}
$foldersToHide = \DocumentManager::get_all_document_folders(
$this->connector->course,
null,
false,
true
);
// Teachers can see all files and folders see #1425
if ($this->allowToEdit()) {
$foldersToHide = [];
}
if (!empty($foldersToHide)) {
foreach ($foldersToHide as $folder) {
$config['attributes'][] = [
'pattern' => '!'.$folder.'!',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false,
];
}
}
// Hide all groups folders
$config['attributes'][] = [
'pattern' => '!_groupdocs_!',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false,
];
// Allow only the groups I have access
$allGroups = \GroupManager::getAllGroupPerUserSubscription($userId);
if (!empty($allGroups)) {
foreach ($allGroups as $groupInfo) {
$groupId = $groupInfo['iid'];
if (\GroupManager::user_has_access(
$userId,
$groupId,
\GroupManager::GROUP_TOOL_DOCUMENTS
)) {
$config['attributes'][] = [
'pattern' => '!'.$groupInfo['secret_directory'].'!',
'read' => true,
'write' => false,
'hidden' => false,
'locked' => false,
];
}
}
}
return $config;
}
return [];
}
/**
* This is the absolute document course path like
* /var/www/portal/data/courses/XXX/document/.
*
* @return string
*/
public function getCourseDocumentSysPath()
{
$url = '';
if ($this->allow()) {
$directory = $this->getCourseDirectory();
$coursePath = $this->connector->paths['sys_course_path'];
$url = $coursePath.$directory.'/document/';
}
return $url;
}
/**
* @return string
*/
public function getCourseDocumentRelativeWebPath()
{
$url = null;
if ($this->allow()) {
$directory = $this->getCourseDirectory();
$url = api_get_path(REL_COURSE_PATH).$directory.'/document/';
}
return $url;
}
/**
* @return string
*/
public function getCourseDocumentWebPath()
{
$url = null;
if ($this->allow()) {
$directory = $this->getCourseDirectory();
$url = api_get_path(WEB_COURSE_PATH).$directory.'/document/';
}
return $url;
}
/**
* @return string
*/
public function getCourseDirectory()
{
return $this->connector->course['directory'];
}
/**
* {@inheritdoc}
*/
public function upload($fp, $dst, $name, $tmpname, $hashes = [])
{
// Needed to load course information in elfinder
$this->setConnectorFromPlugin();
if ($this->allowToEdit()) {
// upload file by elfinder.
$result = parent::upload($fp, $dst, $name, $tmpname);
$name = $result['name'];
$filtered = \URLify::filter($result['name'], 80, '', true);
if (strcmp($name, $filtered) != 0) {
$result = $this->customRename($result['hash'], $filtered);
}
$realPath = $this->realpath($result['hash']);
if (!empty($realPath)) {
// Getting file info
//$info = $elFinder->exec('file', array('target' => $file['hash']));
/** @var elFinderVolumeLocalFileSystem $volume */
//$volume = $info['volume'];
//$root = $volume->root();
//var/www/chamilogits/data/courses/NEWONE/document
$realPathRoot = $this->getCourseDocumentSysPath();
// Removing course path
$realPath = str_replace($realPathRoot, '/', $realPath);
add_document(
$this->connector->course,
$realPath,
'file',
(int) $result['size'],
$result['name']
);
}
return $result;
}
return false;
}
/**
* {@inheritdoc}
*/
public function rm($hash)
{
// elfinder does not delete the file
//parent::rm($hash);
$this->setConnectorFromPlugin();
if ($this->allowToEdit()) {
$path = $this->decode($hash);
$stat = $this->stat($path);
$stat['realpath'] = $path;
$this->removed[] = $stat;
$realFilePath = $path;
$coursePath = $this->getCourseDocumentSysPath();
$filePath = str_replace($coursePath, '/', $realFilePath);
\DocumentManager::delete_document(
$this->connector->course,
$filePath,
$coursePath
);
return true;
}
return false;
}
/**
* @return bool
*/
public function allow()
{
//if ($this->connector->security->isGranted('ROLE_ADMIN')) {
if (api_is_anonymous()) {
return false;
}
if (isset($this->connector->course) && !empty($this->connector->course)) {
return true;
}
return false;
}
/**
* Allow to upload/delete folder or files.
*
* @return bool
*/
public function allowToEdit()
{
$allow = $this->allow();
return $allow && api_is_allowed_to_edit(null, true);
}
/**
* {@inheritdoc}
*/
public function mkdir($path, $name)
{
// Needed to load course information in elfinder
$this->setConnectorFromPlugin();
if ($this->allowToEdit() === false) {
return false;
}
$result = parent::mkdir($path, $name);
if ($result && isset($result['hash'])) {
$_course = $this->connector->course;
$realPathRoot = $this->getCourseDocumentSysPath();
$realPath = $this->realpath($result['hash']);
// Removing course path
$newPath = str_replace($realPathRoot, '/', $realPath);
$documentId = add_document(
$_course,
$newPath,
'folder',
0,
$name,
null,
0,
true,
api_get_group_id(),
api_get_session_id(),
api_get_user_id()
);
if (empty($documentId)) {
$this->rm($result['hash']);
return false;
}
return $result;
}
return false;
}
/**
* @param string $attr
* @param string $path
* @param $data
* @param CourseDriver $volume
*/
/*public function access($attr, $path, $data, $volume)
{
error_log($path);
return true;
if ($path == $this->coursePath) {
return true;
}
$allowToEdit = $this->allowToEdit();
if ($allowToEdit) {
return true;
}
$path = str_replace($this->coursePath, '', $path);
$documentId = \DocumentManager::get_document_id($this->connector->course, $path);
if ($documentId) {
$result = \DocumentManager::is_visible_by_id(
$documentId,
$this->connector->course,
api_get_session_id(),
api_get_user_id()
);
return false;
}
return false;
}*/
}