Documents: Adding some gmail quota style see #3056

skala
Julio Montoya 14 years ago
parent 7edeafeb55
commit 4a22251944
  1. 16
      main/document/document.php
  2. 55
      main/document/quota.php
  3. 2
      main/document/upload.php
  4. 122
      main/inc/lib/document.lib.php
  5. 54
      main/inc/lib/fileUpload.lib.php

@ -232,7 +232,6 @@ if (!$is_allowed_to_edit && api_is_coach()) {
/* Constants and variables */
$course_quota = DocumentManager::get_course_quota();
$current_session_id = api_get_session_id();
@ -1069,11 +1068,24 @@ if (count($docs_and_folders) > 1) {
if ($is_allowed_to_edit || $group_member_with_upload_rights) {
$form_actions = array();
$form_action['delete'] = get_lang('Delete');
$table->set_form_actions($form_action, 'path');
$table->set_form_actions($form_action, 'path');
}
}
$table->display();
if (count($docs_and_folders) > 1) {
if ($is_allowed_to_edit || $group_member_with_upload_rights) {
// Getting the course quota
$course_quota = DocumentManager::get_course_quota();
// Calculating the total space
$already_consumed_space = DocumentManager::documents_total_space($_course);
// Displaying the quota
DocumentManager::display_simple_quota($course_quota, $already_consumed_space);
}
}
if (!empty($table_footer)) {
echo $table_footer;
}

@ -35,55 +35,6 @@ Display::display_header($nameTools,'Doc');
/* FUNCTIONS */
/**
* Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
*/
function display_quota($course_quota, $already_consumed_space) {
$course_quota_m = round($course_quota / 1000000);
$already_consumed_space_m = round($already_consumed_space / 1000000);
$message = get_lang('MaximumAllowedQuota') . ' <strong>'.$course_quota_m.' megabyte</strong>.<br />';
$message .= get_lang('CourseCurrentlyUses') . ' <strong>' . $already_consumed_space_m . ' megabyte</strong>.<br />';
$percentage = $already_consumed_space / $course_quota * 100;
$percentage = round($percentage, 1);
$other_percentage = $percentage < 100 ? 100 - $percentage : 0;
// Decide where to place percentage in graph
if ($percentage >= 50) {
$text_in_filled = '&nbsp;'.$other_percentage.'%';
$text_in_unfilled = '';
} else {
$text_in_unfilled = '&nbsp;'.$other_percentage.'%';
$text_in_filled = '';
}
// Decide the background colour of the graph
if ($percentage < 65) {
$colour = '#00BB00'; // Safe - green
} elseif ($percentage < 90) {
$colour = '#ffd400'; // Filling up - yelloworange
} else {
$colour = '#DD0000'; // Full - red
}
// This is used for the table width: a table of only 100 pixels looks too small
$visual_percentage = 4 * $percentage;
$visual_other_percentage = 4 * $other_percentage;
$message .= get_lang('PercentageQuotaInUse') . ': <strong>'.$percentage.'%</strong>.<br />' .
get_lang('PercentageQuotaFree') . ': <strong>'.$other_percentage.'%</strong>.<br />';
$show_percentage = '&nbsp;'.$percentage.'%';
$message .= '<div style="width: 80%; text-align: center; -moz-border-radius: 5px 5px 5px 5px; border: 1px solid #aaa; background-image: url(\''.api_get_path(WEB_CODE_PATH).'css/'.api_get_visual_theme().'/images/bg-header4.png\');" class="document-quota-bar">'.
'<div style="width:'.$percentage.'%; background-color: #bbb; border-right:3px groove #bbb; -moz-border-radius:5px;">&nbsp;</div>'.
'<span style="margin-top: -15px; margin-left:-15px; position: absolute;font-weight:bold;">'.$show_percentage.'</span></div>';
echo $message;
}
// Actions
echo '<div class="actions">';
@ -95,13 +46,13 @@ echo '</div>';
$course_quota = DocumentManager::get_course_quota();
// Setting the full path
$full_path = $baseWorkDir.$courseDir;
//$full_path = $baseWorkDir.$courseDir;
// Calculating the total space
$already_consumed_space = documents_total_space($_course);
$already_consumed_space = DocumentManager::documents_total_space($_course);
// Displaying the quota
display_quota($course_quota, $already_consumed_space);
DocumentManager::display_quota($course_quota, $already_consumed_space);
// Display the footer
Display::display_footer();

@ -271,7 +271,7 @@ if (!$is_certificate_mode) {
$form = new FormValidator('upload', 'POST', api_get_self(), '', 'enctype="multipart/form-data"');
$form->addElement('hidden', 'curdirpath', $path);
$form->addElement('file', 'file', get_lang('File'), 'id="user_upload" size="45"');
$form->addElement('html', '<div class="row" style="font-size:smaller;font-style:italic;"><div class="label">&nbsp;</div><div class="formw">'.get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'.get_lang('DocumentQuota').': '.(round(DocumentManager::get_course_quota()/1000000)-round(documents_total_space($_course)/1000000)).' M</div></div>');
$form->addElement('html', '<div class="row" style="font-size:smaller;font-style:italic;"><div class="label">&nbsp;</div><div class="formw">'.get_lang('MaxFileSize').': '.ini_get('upload_max_filesize').'<br/>'.get_lang('DocumentQuota').': '.(round(DocumentManager::get_course_quota()/1000000)-round(DocumentManager::documents_total_space($_course)/1000000)).' M</div></div>');
if (api_get_setting('use_document_title') == 'true') {
$form->addElement('text', 'title', get_lang('Title'), array('size' => '20', 'style' => 'width:300px', 'id' => 'title_file'));
$form->addElement('textarea', 'comment', get_lang('Comment'), 'wrap="virtual" style="width:300px;"');

@ -2094,5 +2094,127 @@ class DocumentManager {
return false;
}
}
/**
* Calculates the total size of all documents in a course
*
* @author Bert vanderkimpen
* @param int $to_group_id (to calculate group document space)
* @return int total size
*/
function documents_total_space($to_group_id = '0') {
$TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$sql = "SELECT SUM(size)
FROM ".$TABLE_ITEMPROPERTY." AS props, ".$TABLE_DOCUMENT." AS docs
WHERE docs.id = props.ref
AND props.tool = '".TOOL_DOCUMENT."'
AND props.to_group_id='".$to_group_id."'
AND props.visibility <> 2";
$result = Database::query($sql);
if ($result && Database::num_rows($result) != 0) {
$row = Database::fetch_row($result);
return $row[0];
} else {
return 0;
}
}
/**
* Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
*/
function display_quota($course_quota, $already_consumed_space) {
$course_quota_m = round($course_quota / 1000000);
$already_consumed_space_m = round($already_consumed_space / 1000000);
$message = get_lang('MaximumAllowedQuota') . ' <strong>'.$course_quota_m.' megabyte</strong>.<br />';
$message .= get_lang('CourseCurrentlyUses') . ' <strong>' . $already_consumed_space_m . ' megabyte</strong>.<br />';
$percentage = $already_consumed_space / $course_quota * 100;
$percentage = round($percentage, 1);
$other_percentage = $percentage < 100 ? 100 - $percentage : 0;
// Decide where to place percentage in graph
if ($percentage >= 50) {
$text_in_filled = '&nbsp;'.$other_percentage.'%';
$text_in_unfilled = '';
} else {
$text_in_unfilled = '&nbsp;'.$other_percentage.'%';
$text_in_filled = '';
}
// Decide the background colour of the graph
if ($percentage < 65) {
$colour = '#00BB00'; // Safe - green
} elseif ($percentage < 90) {
$colour = '#ffd400'; // Filling up - yelloworange
} else {
$colour = '#DD0000'; // Full - red
}
// This is used for the table width: a table of only 100 pixels looks too small
$visual_percentage = 4 * $percentage;
$visual_other_percentage = 4 * $other_percentage;
$message .= get_lang('PercentageQuotaInUse') . ': <strong>'.$percentage.'%</strong>.<br />' .
get_lang('PercentageQuotaFree') . ': <strong>'.$other_percentage.'%</strong>.<br />';
$show_percentage = '&nbsp;'.$percentage.'%';
$message .= '<div style="width: 80%; text-align: center; -moz-border-radius: 5px 5px 5px 5px; border: 1px solid #aaa; background-image: url(\''.api_get_path(WEB_CODE_PATH).'css/'.api_get_visual_theme().'/images/bg-header4.png\');" class="document-quota-bar">'.
'<div style="width:'.$percentage.'%; background-color: #bbb; border-right:3px groove #bbb; -moz-border-radius:5px;">&nbsp;</div>'.
'<span style="margin-top: -15px; margin-left:-15px; position: absolute;font-weight:bold;">'.$show_percentage.'</span></div>';
echo $message;
}
/**
* Display the document quota in a simple way
*
* Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
*/
function display_simple_quota($course_quota, $already_consumed_space) {
$course_quota_m = round($course_quota / 1000000);
$already_consumed_space_m = round($already_consumed_space / 1000000, 2);
$percentage = $already_consumed_space / $course_quota * 100;
$percentage = round($percentage, 1);
//$message = "You are currently using %s (%s) of your %s.";
$message = get_lang('YouAreCurrentlyUsingXOfYourX');
$message = sprintf($message, $already_consumed_space_m.' MB',$percentage.'%',$course_quota_m.' MB');
echo Display::div($message, array('style'=>'font-weight:bold; color: #006633 ; text-align:center'));
}
/**
* Checks if there is enough place to add a file on a directory
* on the base of a maximum directory size allowed
*
* @author Bert Vanderkimpen
* @param int file_size size of the file in byte
* @param array $_course
* @param int max_dir_space maximum size
* @return boolean true if there is enough space, false otherwise
*
* @see enough_space() uses documents_total_space() function
*/
function enough_space($file_size, $max_dir_space) {
if ($max_dir_space) {
$already_filled_space = self::documents_total_space();
if (($file_size + $already_filled_space) > $max_dir_space) {
return false;
}
}
return true;
}
}
//end class DocumentManager

@ -11,6 +11,8 @@
* @todo test and reorganise
*/
require_once api_get_path(LIBRARY_PATH).'document.lib.php';
/**
* Changes the file name extension from .php to .phps
* Useful for securing a site.
@ -139,7 +141,7 @@ function handle_uploaded_document($_course, $uploaded_file, $base_work_dir, $upl
$current_session_id = api_get_session_id();
// Check if there is enough space to save the file
if (!enough_space($uploaded_file['size'], $maxFilledSpace)) {
if (!DocumentManager::enough_space($uploaded_file['size'], $maxFilledSpace)) {
if ($output) {
Display::display_error_message(get_lang('UplNotEnoughSpace'));
}
@ -334,27 +336,6 @@ function enough_size($file_size, $dir, $max_dir_space) {
return true;
}
/**
* Checks if there is enough place to add a file on a directory
* on the base of a maximum directory size allowed
*
* @author Bert Vanderkimpen
* @param int file_size size of the file in byte
* @param array $_course
* @param int max_dir_space maximum size
* @return boolean true if there is enough space, false otherwise
*
* @see enough_space() uses documents_total_space() function
*/
function enough_space($file_size, $max_dir_space) {
if ($max_dir_space) {
$already_filled_space = documents_total_space();
if (($file_size + $already_filled_space) > $max_dir_space) {
return false;
}
}
return true;
}
/**
* Computes the size already occupied by a directory and is subdirectories
@ -393,33 +374,6 @@ function dir_total_space($dir_path) {
return $sumSize;
}
/**
* Calculates the total size of all documents in a course
*
* @author Bert vanderkimpen
* @param int $to_group_id (to calculate group document space)
* @return int total size
*/
function documents_total_space($to_group_id = '0') {
$TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$sql = "SELECT SUM(size)
FROM ".$TABLE_ITEMPROPERTY." AS props, ".$TABLE_DOCUMENT." AS docs
WHERE docs.id = props.ref
AND props.tool = '".TOOL_DOCUMENT."'
AND props.to_group_id='".$to_group_id."'
AND props.visibility <> 2";
$result = Database::query($sql);
if ($result && Database::num_rows($result) != 0) {
$row = Database::fetch_row($result);
return $row[0];
} else {
return 0;
}
}
/**
* Tries to add an extension to files without extension
@ -705,7 +659,7 @@ function unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $
$real_filesize += $this_content['size'];
}
if (!enough_space($real_filesize, $max_filled_space)) {
if (!DocumentManager::enough_space($real_filesize, $max_filled_space)) {
Display::display_error_message(get_lang('UplNotEnoughSpace'));
return false;
}

Loading…
Cancel
Save