Created
April 3, 2014 12:46
-
-
Save shardiwal/9953636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Model::Calculation::PhysicalActivity; | |
use Dancer ':syntax'; | |
use Dancer::Plugin::DBIC; | |
use Moose; | |
use Data::Dumper; | |
use Model::Response; | |
use feature qw(say switch); | |
has 'user_id' => ( | |
is => 'rw', | |
isa => 'Int', | |
required => 1, | |
); | |
has 'question_tags' => ( | |
is => 'ro', | |
isa => "ArrayRef", | |
required => 1 | |
); | |
has 'response' => ( | |
is => 'ro', | |
isa => "HashRef", | |
required => 1 | |
); | |
has 'sitting_hours' => ( | |
is => 'ro', | |
isa => "Num", | |
lazy_build => 1, | |
); | |
sub _build_sitting_hours { | |
my ( $self ) = @_; | |
my $reponse = $self->response; | |
my $sitting_hours = 0; | |
foreach my $tag ( @{$self->question_tags} ) { | |
if ( $tag !~/^physical_activity_.*/ ) { | |
next; | |
} | |
next if !$reponse->{$tag}; | |
if ( $tag =~/^physical_activity_hours.*/ig and $tag ne 'physical_activity_hours_sleeping') { | |
my $hours = $reponse->{$tag}->{'answer_text_value'}; | |
if ( $hours ) { | |
$sitting_hours = $sitting_hours + $hours; | |
} | |
} | |
} | |
return $sitting_hours || 1; | |
} | |
has 'waking_hours' => ( | |
is => 'ro', | |
isa => "Num", | |
lazy_build => 1, | |
); | |
sub _build_waking_hours { | |
my ( $self ) = @_; | |
my $sitting_hours = $self->sitting_hours; | |
my $waking_hour = 24 - $sitting_hours; | |
return $waking_hour || 1; | |
} | |
sub _calculate_score { | |
my ( $self ) = @_; | |
my $reponse = $self->response; | |
my $physical_exercise_score = 0; | |
my $activity_score = 0; | |
foreach my $tag ('physical_activity_vigorous','physical_activity_moderate') { | |
next if !$reponse->{$tag}; | |
my $score = $reponse->{$tag}->{'health_info'}->{'score'}; | |
if ( $score ) { | |
$physical_exercise_score = $physical_exercise_score + $score; | |
} | |
} | |
$activity_score = ( $self->sitting_hours / $self->waking_hours ) * 60; #for minutes | |
my $res = Model::Response->new(user_id => $self->user_id); | |
$res->has_goals(0); | |
$res->question_tag('physical_activity_exercise_score'); | |
$res->response_value( $physical_exercise_score ); | |
$res->set_response(); | |
$res->question_tag('physical_activity_activity_score'); | |
$res->response_value( $activity_score ); | |
$res->set_response(); | |
return ( | |
$physical_exercise_score, | |
$activity_score, | |
$self->_get_actionplan_score() | |
); #Per week | |
} | |
sub _get_actionplan_score { | |
my ($self) = @_; | |
my $response = Model::Response->new( | |
user_id => $self->user_id, | |
question_tags => ['physical_activity_exercise_score', 'physical_activity_activity_score'] | |
)->get_response(); | |
$response->has_goals(0); | |
$response = $response->{$self->user_id}; | |
my $ex_risk = $response->{'physical_activity_exercise_score'}->{health_info}->{risk_status} || 'Low'; | |
my $ac_risk = $response->{ 'physical_activity_activity_score'}->{health_info}->{risk_status} || 'Low'; | |
return 0 if $ex_risk =~ /high/ig or $ac_risk =~ /high/ig; | |
if($ex_risk !~ /high/ig and $ac_risk =~ /high|moderate/ig) { | |
return 1 if $ac_risk =~ /moderate/ig; | |
return 0 if $ac_risk =~ /high/ig; | |
} | |
return 1 if $ex_risk =~ /moderate/ig; | |
return 3 if $ex_risk =~ /low/ig; | |
return 0; | |
} | |
__PACKAGE__->meta->make_immutable; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment