1
0

Select an appropriate file size prefix instead of using MB for everything.

This commit is contained in:
rWatcher 2012-04-16 20:47:39 -04:00
parent a58a1d348a
commit 6178d6b443
3 changed files with 40 additions and 21 deletions

View File

@ -23,11 +23,11 @@ class quotas_theme_Core {
if (!identity::active_user()->guest) {
$record = ORM::factory("users_space_usage")->where("owner_id", "=", identity::active_user()->id)->find();
if ($record->get_usage_limit() == 0) {
print t("You are using %usage MB", array("usage" => number_format($record->total_usage_mb(), 2)));
print t("You are using %usage", array("usage" => $record->total_usage_string()));
} else {
print t("You are using %usage of your %limit MB limit (%percentage%)",
array("usage" => number_format($record->current_usage_mb(), 2),
"limit" => number_format($record->get_usage_limit_mb(), 2),
print t("You are using %usage of your %limit limit (%percentage%)",
array("usage" => $record->current_usage_string(),
"limit" => $record->get_usage_limit_string(),
"percentage" => number_format((($record->current_usage() / $record->get_usage_limit()) * 100), 2)));
}
}

View File

@ -18,10 +18,22 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Users_Space_Usage_Model_Core extends ORM {
public function partial_usage_mb($file_type) {
// Return a value (in megabytes) for the user's total usage in fullsizes, resizes, or thumbs.
static function _format_usage($bytes) {
$usage_unit = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
$unit_counter = 0;
while ($bytes >= 1024) {
$bytes = $bytes / 1024;
$unit_counter++;
}
return (number_format($bytes, 2) . " " . $usage_unit[$unit_counter]);
}
public function partial_usage_string($file_type) {
// Return a string for the user's total usage in fullsizes, resizes, or thumbs
// with the appropriate file size prefix.
// $file_type should be either fullsize, resize, or thumb.
return ($this->$file_type / 1024 / 1024);
return (Users_Space_Usage_Model_Core::_format_usage($this->$file_type));
}
public function total_usage() {
@ -29,9 +41,9 @@ class Users_Space_Usage_Model_Core extends ORM {
return ($this->fullsize + $this->resize + $this->thumb);
}
public function total_usage_mb() {
// Return the user's total usage in megabytes.
return (($this->total_usage()) / 1024 / 1024);
public function total_usage_string() {
// Return the user's total usage as a string with the appropriate file size prefix.
return (Users_Space_Usage_Model_Core::_format_usage($this->total_usage()));
}
public function current_usage() {
@ -43,9 +55,10 @@ class Users_Space_Usage_Model_Core extends ORM {
}
}
public function current_usage_mb() {
// Return the users relevant usage in megabytes based on the use_all_sizes setting.
return ($this->current_usage() / 1024 / 1024);
public function current_usage_string() {
// Return the users relevant usage as a string with the appropriate file size prefix
// based on the use_all_sizes setting.
return (Users_Space_Usage_Model_Core::_format_usage($this->current_usage()));
}
public function get_usage_limit() {
@ -68,9 +81,15 @@ class Users_Space_Usage_Model_Core extends ORM {
return 0;
}
public function get_usage_limit_mb() {
// Returns a user's maximum limit in megabytes.
return ($this->get_usage_limit() / 1024 / 1024);
public function get_usage_limit_string() {
// Returns a user's maximum limit as a string with the appropriate file size prefix
// or an infinity symbol if the user has no limit.
$user_limit = $this->get_usage_limit();
if ($user_limit == 0) {
return "∞";
} else {
return (Users_Space_Usage_Model_Core::_format_usage($this->get_usage_limit()));
}
}
public function add_item($item) {

View File

@ -34,19 +34,19 @@
<?= html::clean($user->full_name) ?>
</td>
<td>
<?= number_format($record->partial_usage_mb("fullsize"), 2); ?> MB
<?=$record->partial_usage_string("fullsize"); ?>
</td>
<td>
<?= number_format($record->partial_usage_mb("resize"), 2); ?> MB
<?=$record->partial_usage_string("resize"); ?>
</td>
<td>
<?= number_format($record->partial_usage_mb("thumb"), 2); ?> MB
<?=$record->partial_usage_string("thumb"); ?>
</td>
<td>
<?= number_format($record->total_usage_mb(), 2) ?> MB
<?=$record->total_usage_string(); ?>
</td>
<td>
<?= number_format($record->get_usage_limit_mb(), 2) ?> MB
<?=$record->get_usage_limit_string(); ?>
</td>
</tr>
<? endforeach ?>