Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"a2lix/translation-form-bundle": "^3.0",
"api-platform/core": "^3.0",
"beberlei/doctrineextensions": "^1.3",
"bigbluebutton/bigbluebutton-api-php": "^2.0",
"chamilo/google-map-form-type-bundle": "1.7",
"chamilo/settings-bundle": "1.3.0",
"clue/graph": "^0.9.2",
Expand Down
146 changes: 143 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions public/main/inc/ajax/plugin.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Chamilo\CoreBundle\Framework\Container;
use Michelf\MarkdownExtra;
use Chamilo\CoreBundle\Entity\Plugin as PluginEntity;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CourseBundle\Entity\CDocument;

/**
* Responses to AJAX calls.
Expand Down Expand Up @@ -49,9 +51,98 @@
exit;
}

/**
* From here on, everything returns JSON.
*/
header('Content-Type: application/json; charset=utf-8');
api_block_anonymous_users();

if ($action === 'list_documents') {
try {
header('Content-Type: application/json; charset=utf-8');

$courseId = api_get_course_int_id();
$isAdmin = api_is_platform_admin();

// Require edit rights inside a course; otherwise only admins can list globally
if ($courseId > 0) {
if (!api_is_allowed_to_edit()) {
http_response_code(403);
echo json_encode(['error' => 'Forbidden']);
exit;
}
} else {
if (!$isAdmin) {
http_response_code(403);
echo json_encode(['error' => 'Forbidden (admin required for global listing)']);
exit;
}
}

$em = Database::getManager();
$repo = $em->getRepository(ResourceNode::class);

$qb = $em->createQueryBuilder()
->select('DISTINCT d')
->from(CDocument::class, 'd')
->innerJoin('d.resourceNode', 'rn')
->innerJoin('rn.resourceFiles', 'rf')
->innerJoin('rn.resourceLinks', 'rl')
->where('d.filetype = :type')
->setParameter('type', 'file');

if ($courseId > 0) {
$qb->andWhere('IDENTITY(rl.course) = :cId')
->setParameter('cId', (int)$courseId);
}

$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 500;
$limit = max(1, min($limit, 2000));
$qb->setMaxResults($limit)
->orderBy('d.iid', 'DESC');

$docs = $qb->getQuery()->getResult();
$out = [];

$sysBase = rtrim(str_replace('/public/', '', api_get_path(SYS_PATH)), '/');
foreach ($docs as $doc) {
$files = $doc->getResourceNode()->getResourceFiles();
if ($files->isEmpty()) {
continue;
}

$file = $files->first();
$orig = $file->getOriginalName();
$ext = strtolower(pathinfo($orig, PATHINFO_EXTENSION));
if (!in_array($ext, ['pdf','ppt','pptx','odp'], true)) {
continue;
}

$relPath = $repo->getFilename($file); // e.g. "/a1/b2/file.pdf"
$diskPath = $sysBase . '/var/upload/resource' . $relPath;

// Only list entries that truly exist on disk
if (!is_file($diskPath) || !is_readable($diskPath)) {
continue;
}

$out[] = [
'id' => $doc->getIid(),
'url' => $diskPath,
'filename' => $orig,
'size' => @filesize($diskPath) ?: null,
];
}

echo json_encode($out);
} catch (\Throwable $e) {
error_log('[plugin.ajax list_documents] '.$e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Internal error']);
}
exit;
}

try {
if (!api_is_platform_admin()) {
http_response_code(403);
Expand Down Expand Up @@ -165,6 +256,7 @@
}
}

// If it is a course plugin, propagate enable/disable to all courses
if ($instance && !empty($instance->isCoursePlugin)) {
if ($action === 'enable') {
$instance->install_course_fields_in_all_courses(true);
Expand Down
Loading
Loading