Document Size List: Fix request number and replace path by document id in query

pull/3944/head
Tony ID 5 years ago committed by Yannick Warnier
parent 6af2d323a4
commit 87ab21458d
  1. 26
      main/document/document.php
  2. 16
      main/inc/ajax/document.ajax.php
  3. 65
      main/inc/lib/document.lib.php

@ -1894,7 +1894,7 @@ $userIsSubscribed = CourseManager::is_user_subscribed_in_course(
$courseInfo['code']
);
$getSizeURL = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=get_dir_size&'.api_get_cidreq();
$getSizesURL = api_get_path(WEB_AJAX_PATH) . 'document.ajax.php?a=get_dirs_size&' . api_get_cidreq();
if (!empty($documentAndFolders)) {
if ($groupId == 0 || $userAccess) {
@ -2005,7 +2005,7 @@ if (!empty($documentAndFolders)) {
if ($document_data['filetype'] === 'folder') {
$displaySize = '<span id="document_size_'.$document_data['id']
.'" data-path= "'.$document_data['path']
. '" data-id= "' . $document_data['id']
.'" class="document_size"></span>';
} else {
$displaySize = format_file_size($document_data['size']);
@ -2273,17 +2273,23 @@ if (false === $disableQuotaMessage && count($documentAndFolders) > 1) {
echo '<script>
$(function() {
let requests = [];
$(".document_size").each(function(i, obj) {
var path = obj.getAttribute("data-path");
$.ajax({
url:"'.$getSizeURL.'&path="+path,
success:function(data){
$(obj).html(data);
}
});
requests.push(obj.getAttribute("data-id"));
});
getPathsSizes(requests)
});
function getPathsSizes(requests){
$.ajax({
url:"' . $getSizesURL . '&requests="+requests,
success:function(data){
const response = JSON.parse(data)
response.forEach(function(data) {
$("#document_size_"+data.id).html(data.size);
});
}
});
}
</script>';
echo '<span id="course_quota"></span>';
}

@ -15,6 +15,22 @@ switch ($action) {
$size = DocumentManager::getTotalFolderSize($path, $isAllowedToEdit);
echo format_file_size($size);
break;
case 'get_dirs_size':
api_protect_course_script(true);
$requests = isset($_GET['requests']) ? $_GET['requests'] : '';
$isAllowedToEdit = api_is_allowed_to_edit();
$response = [];
$requests = explode(',', $requests);
foreach ($requests as $request) {
$fileSize = DocumentManager::getTotalFolderSizeById($request, $isAllowedToEdit);
$data = [
"id" => $request,
"size" => format_file_size($fileSize),
];
array_push($response, $data);
}
echo json_encode($response);
break;
case 'get_document_quota':
// Getting the course quota
$courseQuota = DocumentManager::get_course_quota();

@ -6481,6 +6481,71 @@ class DocumentManager
return $result;
}
/**
* Calculates the total size of a directory by adding the sizes (that
* are stored in the database) of all files & folders in this directory.
*
* @param int $id
* @param bool $can_see_invisible
*
* @return int Total size
*/
public static function getTotalFolderSizeById($id, $can_see_invisible = false)
{
$id = (int)$id;
$table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
$table_document = Database::get_course_table(TABLE_DOCUMENT);
$tool_document = TOOL_DOCUMENT;
$course_id = api_get_course_int_id();
$session_id = api_get_session_id();
$session_condition = api_get_session_condition(
$session_id,
true,
true,
'props.session_id'
);
if (empty($course_id)) {
return 0;
}
$visibility_rule = ' props.visibility ' . ($can_see_invisible ? '<> 2' : '= 1');
$query = "SELECT path FROM $table_document WHERE id = $id";
$result1 = Database::query($query);
$path = null;
if ($result1 && Database::num_rows($result1) != 0) {
$row = Database::fetch_row($result1);
$path = $row[0];
}
if ($path) {
$sql = "SELECT SUM(table1.size) FROM (
SELECT props.ref, size
FROM $table_itemproperty AS props
INNER JOIN $table_document AS docs
ON (docs.id = props.ref AND docs.c_id = props.c_id)
WHERE
docs.c_id = $course_id AND
docs.path LIKE '$path/%' AND
props.c_id = $course_id AND
props.tool = '$tool_document' AND
$visibility_rule
$session_condition
GROUP BY ref
) as table1";
$result = Database::query($sql);
if ($result && Database::num_rows($result) != 0) {
$row = Database::fetch_row($result);
return $row[0];
}
}
return 0;
}
/**
* Calculates the total size of a directory by adding the sizes (that
* are stored in the database) of all files & folders in this directory.

Loading…
Cancel
Save