Formatted duration template variable and method, with tests.

Start writing Story help.
This commit is contained in:
Colin Kuskie 2009-03-07 00:01:41 +00:00
parent 925a218224
commit 4b1ec30748
4 changed files with 177 additions and 1 deletions

View file

@ -180,6 +180,45 @@ sub exportAssetData {
#-------------------------------------------------------------------
=head2 formatDuration ( $lastUpdated )
Format the time since this story was last updated. If it is longer than 1 week, then
return the date.
=head3 $lastUpdated
The date this was last updated. If left blank, it uses the revisionDate.
=cut
sub formatDuration {
my ($self, $lastUpdated) = @_;
$lastUpdated = defined $lastUpdated ? $lastUpdated : $self->get('revisionDate');
my $session = $self->session;
my $datetime = $session->datetime;
my $duration = time() - $lastUpdated;
if ($duration > 86400) { ##1 day
return join ' ', $datetime->secondsToInterval($duration);
}
else {
my $formattedDuration = '';
my $hours = int($duration/3600) * 3600;
my @hours = $datetime->secondsToInterval($hours);
if ($hours[0]) {
$formattedDuration = join ' ', @hours;
}
my $minutes = round(($duration - $hours)/60)*60;
my @minutes = $datetime->secondsToInterval($minutes);
if ($minutes[0]) {
$formattedDuration .= ', ', if $formattedDuration;
$formattedDuration .= join ' ', @minutes;
}
return $formattedDuration;
}
}
#-------------------------------------------------------------------
=head2 getArchive ( )
Returns the parent archive for this Story. Cache the entry for speed.
@ -566,6 +605,8 @@ sub viewTemplateVariables {
};
}
##TODO: publish time, calculated from revisionDate
$var->{updatedTime} = $self->formatDuration();
$var->{updatedTimeEpoch} = $self->get('revisionDate');
return $var;
}

View file

@ -34,6 +34,60 @@ our $HELP = {
related => []
},
'view template' => {
title => 'view template',
body => '',
isa => [
{ namespace => "Asset_Template",
tag => "template variables"
},
],
fields => [],
variables => [
{ 'name' => 'highlights_loop',
'variables' => [
{ 'name' => 'highlight', },
],
},
{ 'name' => 'keywords_loop',
'variables' => [
{ 'name' => 'keyword', },
{ 'name' => 'url', },
],
},
{ 'name' => 'updatedTime', },
{ 'name' => 'updatedTimeEpoch', },
],
related => []
},
'story asset template variables' => {
private => 1,
title => 'story asset template variables title',
body => '',
isa => [
{ namespace => "Asset",
tag => "asset template variables"
},
],
fields => [],
variables => [
{ 'name' => 'headline',
'description' => 'headline tmplvar',
},
{ 'name' => 'subtitle',
'description' => 'subtitle tmplvar',
},
{ 'name' => 'byline'
'description' => 'byline tmplvar',
},
{ 'name' => 'location',
'description' => 'location tmplvar',
},
],
related => []
},
};
1;

View file

@ -21,6 +21,12 @@ our $I18N = {
lastUpdated => 0
},
'headline tmplvar' => {
message => q|The headline for the Story.|,
context => q|Template variable help.|,
lastUpdated => 0
},
'subtitle' => {
message => q|Subtitle|,
context => q|Similar to headline, but usually contains more information. Label in the edit screen and template.|,
@ -33,6 +39,12 @@ our $I18N = {
lastUpdated => 0
},
'subtitle tmplvar' => {
message => q|The subtitle from the Story.|,
context => q|Template variable help.|,
lastUpdated => 0
},
'byline' => {
message => q|By line|,
context => q|Who wrote the story. Label in the edit screen and template.|,
@ -45,6 +57,12 @@ our $I18N = {
lastUpdated => 0
},
'byline tmplvar' => {
message => q|The byline from the Story.|,
context => q|Template variable help.|,
lastUpdated => 0
},
'location' => {
message => q|Location|,
context => q|Where the story takes place. Label in the edit screen and template.|,
@ -57,6 +75,12 @@ our $I18N = {
lastUpdated => 0
},
'location tmplvar' => {
message => q|The location from the Story.|,
context => q|Template variable help.|,
lastUpdated => 0
},
'highlights' => {
message => q|Story Highlights|,
context => q|Bullet point level summaries from the story. Label in the edit screen and template.|,
@ -184,6 +208,46 @@ our $I18N = {
lastUpdated => 0,
},
'view template' => {
message => q|View Story Template.|,
lastUpdated => 0,
},
'highlights_loop' => {
message => q|A loop containing all the highlights from the story.|,
lastUpdated => 0,
},
'highlight' => {
message => q|One highlight, without formatting or extra HTML.|,
lastUpdated => 0,
},
'keywords_loop' => {
message => q|A loop containing all the keywords from the story.|,
lastUpdated => 0,
},
'keyword' => {
message => q|One keyword, with no formatting.|,
lastUpdated => 0,
},
'url' => {
message => q|A URL to view all stories in this archive.|,
lastUpdated => 0,
},
'updatedTime' => {
message => q|The time this Story was last updated, as a formatted duration, like 1 Hour(s) ago.|,
lastUpdated => 0,
},
'updatedTimeEpoch' => {
message => q|The time this Story was last updated, as an epoch.|,
lastUpdated => 0,
},
};
1;

View file

@ -21,7 +21,7 @@ use Test::Deep;
use Data::Dumper;
my $tests = 1;
plan tests => 16
plan tests => 23
+ $tests
;
@ -32,6 +32,7 @@ my $session = WebGUI::Test->session;
my $class = 'WebGUI::Asset::Story';
my $loaded = use_ok($class);
my $story;
my $wgBday;
my $defaultNode = WebGUI::Asset->getDefault($session);
my $archive = $defaultNode->addChild({
@ -124,6 +125,19 @@ cmp_deeply(
'setPhotoData: wipes the stored data if nothing is passed'
);
############################################################
#
# formatDuration
#
############################################################
is($story->formatDuration(time() - (24*3600+15)), '1 Day(s)', 'formatDuration, 1 day');
is($story->formatDuration(time() - (48*3600+15)), '2 Day(s)', 'formatDuration, 2 day');
like($story->formatDuration(997966800), qr{Year.s.}, 'formatDuration: a long time ago');
is($story->formatDuration(time() - (3600+5)), '1 Hour(s)', 'formatDuration: 1 hour');
is($story->formatDuration(time() - (60+5)), '1 Minute(s)', 'formatDuration: 1 minute');
is($story->formatDuration(time() - (7200+120)), '2 Hour(s), 2 Minute(s)', 'formatDuration: 2 hours, 2 minutes');
############################################################
#
# viewTemplateVariables
@ -157,10 +171,13 @@ cmp_bag(
'viewTemplateVariables: keywords_loop is okay'
);
is ($viewVariables->{updatedTimeEpoch}, $story->get('revisionDate'), 'viewTemplateVariables: updatedTimeEpoch');
}
END {
$story->purge if $story;
$wgBday->purge if $wgBday;
$archive->purge if $archive;
WebGUI::VersionTag->getWorking($session)->rollback;
}