Permalink
Browse files

format_time() now supports language strings for day(s)/hour(s)/min(s)…

…/sec(s)

String lookups are cached in user/index.php for efficiency
  • Loading branch information...
1 parent 05d09aa commit 8dbed6be602e62d0ba2ae3e9e028e277cbdac861 moodler committed Oct 3, 2002
Showing with 44 additions and 17 deletions.
  1. +8 −0 lang/en/moodle.php
  2. +26 −15 lib/moodlelib.php
  3. +9 −1 user/index.php
  4. +1 −1 user/lib.php
View
8 lang/en/moodle.php
@@ -79,6 +79,8 @@
$string['databasesetup'] = "Setting up database";
$string['databasesuccess'] = "Database was successfully upgraded";
$string['databaseupgrades'] = "Upgrading database";
+$string['day'] = "day";
+$string['days'] = "days";
$string['defaultcoursefullname'] = "Course Fullname 101";
$string['defaultcourseshortname'] = "CF101";
$string['defaultcoursestudent'] = "Student";
@@ -196,6 +198,8 @@
$string['helpquestions'] = "How to ask questions";
$string['hide'] = "Hide";
$string['home'] = "Home";
+$string['hour'] = "hour";
+$string['hours'] = "hours";
$string['htmlformat'] = "Pretty HTML format";
$string['icqnumber'] = "ICQ number";
$string['idnumber'] = "ID number";
@@ -252,6 +256,8 @@
$string['maximumchars'] = "Maximum of \$a characters";
$string['maximumgrade'] = "Maximum grade";
$string['maxsize'] = "Max size: \$a";
+$string['min'] = "min";
+$string['mins'] = "mins";
$string['miscellaneous'] = "Miscellaneous"; // Default course category
$string['missingcategory'] = "You need to choose a category";
$string['missingcity'] = "Missing city/town";
@@ -372,6 +378,8 @@
$string['search'] = "Search";
$string['searchagain'] = "Search again";
$string['searchresults'] = "Search results";
+$string['sec'] = "sec";
+$string['secs'] = "secs";
$string['section'] = "Section";
$string['selectacountry'] = "Select a country";
$string['senddetails'] = "Send my details via email";
View
41 lib/moodlelib.php
@@ -339,28 +339,39 @@ function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0)
return mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year);
}
-function format_time($totalsecs) {
+function format_time($totalsecs, $str=NULL) {
// Given an amount of time in seconds, prints it
// nicely as months, days, hours etc as needed
$totalsecs = abs($totalsecs);
- $days = floor($totalsecs/86400);
+ if (!$str) { // Create the str structure the slow way
+ $str->day = get_string("day");
+ $str->days = get_string("days");
+ $str->hour = get_string("hour");
+ $str->hours = get_string("hours");
+ $str->min = get_string("min");
+ $str->mins = get_string("mins");
+ $str->sec = get_string("sec");
+ $str->secs = get_string("secs");
+ }
+
+ $days = floor($totalsecs/86400);
$remainder = $totalsecs - ($days*86400);
- $hours = floor($remainder/3600);
+ $hours = floor($remainder/3600);
$remainder = $remainder - ($hours*3600);
- $mins = floor($remainder/60);
- $secs = $remainder - ($mins*60);
-
- if ($secs != 1) $ss = "s";
- if ($mins != 1) $ms = "s";
- if ($hours != 1) $hs = "s";
- if ($days != 1) $ds = "s";
-
- if ($days) $odays = "$days day$ds";
- if ($hours) $ohours = "$hours hr$hs";
- if ($mins) $omins = "$mins min$ms";
- if ($secs) $osecs = "$secs sec$ss";
+ $mins = floor($remainder/60);
+ $secs = $remainder - ($mins*60);
+
+ $ss = ($secs == 1) ? $str->sec : $str->secs;
+ $sm = ($mins == 1) ? $str->min : $str->mins;
+ $sh = ($hours == 1) ? $str->hour : $str->hours;
+ $sd = ($days == 1) ? $str->day : $str->days;
+
+ if ($days) $odays = "$days $sd";
+ if ($hours) $ohours = "$hours $sh";
+ if ($mins) $omins = "$mins $sm";
+ if ($secs) $osecs = "$secs $ss";
if ($days) return "$odays $ohours";
if ($hours) return "$ohours $omins";
View
10 user/index.php
@@ -37,6 +37,14 @@
$string->role = get_string("role");
$string->never = get_string("never");
$string->name = get_string("name");
+ $string->day = get_string("day");
+ $string->days = get_string("days");
+ $string->hour = get_string("hour");
+ $string->hours = get_string("hours");
+ $string->min = get_string("min");
+ $string->mins = get_string("mins");
+ $string->sec = get_string("sec");
+ $string->secs = get_string("secs");
if ( $teachers = get_course_teachers($course->id)) {
echo "<H2 align=center>$course->teachers</H2>";
@@ -101,7 +109,7 @@
foreach ($students as $student) {
if ($student->lastaccess) {
- $lastaccess = format_time(time() - $student->lastaccess);
+ $lastaccess = format_time(time() - $student->lastaccess, $string);
} else {
$lastaccess = $string->never;
}
View
2 user/lib.php
@@ -70,7 +70,7 @@ function print_user($user, $course, $string) {
echo "$string->location: $user->city, ".$COUNTRIES["$user->country"]."<BR>";
if ($user->lastaccess) {
echo "$string->lastaccess: ".userdate($user->lastaccess);
- echo "&nbsp (".format_time(time() - $user->lastaccess).")";
+ echo "&nbsp (".format_time(time() - $user->lastaccess, $string).")";
} else {
echo "$string->lastaccess: $string->never";
}

0 comments on commit 8dbed6b

Please sign in to comment.