PR Documents Size: Fix blank spaces, var types for compatibility, reuse fuction

pull/3944/head
Tony ID 4 years ago committed by Yannick Warnier
parent 87ab21458d
commit 04de152ce2
  1. 4
      main/document/document.php
  2. 2
      main/inc/ajax/document.ajax.php
  3. 67
      main/inc/lib/document.lib.php

@ -2273,7 +2273,7 @@ if (false === $disableQuotaMessage && count($documentAndFolders) > 1) {
echo '<script> echo '<script>
$(function() { $(function() {
let requests = []; var requests = [];
$(".document_size").each(function(i, obj) { $(".document_size").each(function(i, obj) {
requests.push(obj.getAttribute("data-id")); requests.push(obj.getAttribute("data-id"));
}); });
@ -2283,7 +2283,7 @@ if (false === $disableQuotaMessage && count($documentAndFolders) > 1) {
$.ajax({ $.ajax({
url:"'.$getSizesURL.'&requests="+requests, url:"'.$getSizesURL.'&requests="+requests,
success:function(data){ success:function(data){
const response = JSON.parse(data) var response = JSON.parse(data)
response.forEach(function(data) { response.forEach(function(data) {
$("#document_size_"+data.id).html(data.size); $("#document_size_"+data.id).html(data.size);
}); });

@ -22,7 +22,7 @@ switch ($action) {
$response = []; $response = [];
$requests = explode(',', $requests); $requests = explode(',', $requests);
foreach ($requests as $request) { foreach ($requests as $request) {
$fileSize = DocumentManager::getTotalFolderSizeById($request, $isAllowedToEdit); $fileSize = DocumentManager::getTotalFolderSize($request, $isAllowedToEdit);
$data = [ $data = [
"id" => $request, "id" => $request,
"size" => format_file_size($fileSize), "size" => format_file_size($fileSize),

@ -6485,14 +6485,14 @@ class DocumentManager
* Calculates the total size of a directory by adding the sizes (that * Calculates the total size of a directory by adding the sizes (that
* are stored in the database) of all files & folders in this directory. * are stored in the database) of all files & folders in this directory.
* *
* @param int $id * @param string $value = document path or document id
* @param bool $can_see_invisible * @param bool $can_see_invisible
* @param bool $by_id = default true, if is getting size by document id or false if getting by path
* *
* @return int Total size * @return int Total size
*/ */
public static function getTotalFolderSizeById($id, $can_see_invisible = false) public static function getTotalFolderSize($value, $can_see_invisible = false, $by_id = true)
{ {
$id = (int)$id;
$table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY); $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
$table_document = Database::get_course_table(TABLE_DOCUMENT); $table_document = Database::get_course_table(TABLE_DOCUMENT);
$tool_document = TOOL_DOCUMENT; $tool_document = TOOL_DOCUMENT;
@ -6512,71 +6512,20 @@ class DocumentManager
$visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1'); $visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1');
if ($by_id) {
$id = (int) $value;
$query = "SELECT path FROM $table_document WHERE id = $id"; $query = "SELECT path FROM $table_document WHERE id = $id";
$result1 = Database::query($query); $result1 = Database::query($query);
$path = null;
if ($result1 && Database::num_rows($result1) != 0) { if ($result1 && Database::num_rows($result1) != 0) {
$row = Database::fetch_row($result1); $row = Database::fetch_row($result1);
$path = $row[0]; $path = $row[0];
} } else {
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; return 0;
} }
/** } else {
* Calculates the total size of a directory by adding the sizes (that $path = Database::escape_string($value);
* are stored in the database) of all files & folders in this directory.
*
* @param string $path
* @param bool $can_see_invisible
*
* @return int Total size
*/
public static function getTotalFolderSize($path, $can_see_invisible = false)
{
$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;
} }
$path = Database::escape_string($path);
$visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1');
$sql = "SELECT SUM(table1.size) FROM ( $sql = "SELECT SUM(table1.size) FROM (
SELECT props.ref, size SELECT props.ref, size
FROM $table_itemproperty AS props FROM $table_itemproperty AS props

Loading…
Cancel
Save