From 8dbed6be602e62d0ba2ae3e9e028e277cbdac861 Mon Sep 17 00:00:00 2001 From: moodler Date: Thu, 3 Oct 2002 04:04:59 +0000 Subject: [PATCH] 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 --- lang/en/moodle.php | 8 ++++++++ lib/moodlelib.php | 37 ++++++++++++++++++++++++------------- user/index.php | 10 +++++++++- user/lib.php | 2 +- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 6bd2a1d58bc..22d81cbadab 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -79,6 +79,8 @@ $string['databasechecking'] = "Upgrading Moodle database from version \$a->oldve $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['helptext'] = "How to write text"; $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['markthistopic'] = "Mark this topic as the current topic"; $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['savechanges'] = "Save changes"; $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"; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 594a30b0949..198e3308266 100644 --- a/lib/moodlelib.php +++ b/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); + $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"; + $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 day$ds"; - if ($hours) $ohours = "$hours hr$hs"; - if ($mins) $omins = "$mins min$ms"; - if ($secs) $osecs = "$secs sec$ss"; + 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"; diff --git a/user/index.php b/user/index.php index 6b7f2ca7714..c2126892ed4 100644 --- a/user/index.php +++ b/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 "

$course->teachers

"; @@ -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; } diff --git a/user/lib.php b/user/lib.php index 9af59db8672..b867fc9429e 100644 --- a/user/lib.php +++ b/user/lib.php @@ -70,7 +70,7 @@ function print_user($user, $course, $string) { echo "$string->location: $user->city, ".$COUNTRIES["$user->country"]."
"; if ($user->lastaccess) { echo "$string->lastaccess: ".userdate($user->lastaccess); - echo "  (".format_time(time() - $user->lastaccess).")"; + echo "  (".format_time(time() - $user->lastaccess, $string).")"; } else { echo "$string->lastaccess: $string->never"; }