· 9 years ago · Jan 04, 2017, 09:50 AM
1CREATE TABLE `goals` (
2 `id` int(10) NOT NULL,
3 `name` varchar(255) NOT NULL,
4 `ratio` int(10) NOT NULL,
5 `command` varchar(255) NOT NULL,
6 `created_at` timestamp NULL DEFAULT NULL,
7 `updated_at` timestamp NULL DEFAULT NULL
8)
9
10
11INSERT INTO `goals` (`id`, `name`, `ratio`, `command`, `created_at`,
12`updated_at`) VALUES
13(1, 'Test Goal', 50, 'daily', '2017-01-04 08:09:12', '0000-00-00 00:00:00');
14
15CREATE TABLE IF NOT EXISTS `logs` (
16 `id` int(10) unsigned NOT NULL,
17 `user_id` int(10) unsigned NOT NULL,
18 `action_id` int(10) unsigned NOT NULL,
19 `date` datetime NOT NULL,
20 `created_at` timestamp NULL DEFAULT NULL,
21 `updated_at` timestamp NULL DEFAULT NULL
22)
23
24INSERT INTO `logs` (`id`, `user_id`, `action_id`, `date`, `created_at`,
25`updated_at`) VALUES
26(1, 1, 1, '2017-01-02-11-22-58', '2017-01-02 06:30:43', '2017-01-02
2710:22:58', '2017-01-02 10:22:58'),
28(2, 1, 2, '2017-01-02-11-23-14', '2017-01-02 07:57:59', '2017-01-02
2910:23:14', '2017-01-02 10:23:14'),
30(3, 1, 4, '2017-01-02-11-23-23', '2017-01-02 08:12:09', '2017-01-02
3110:23:23', '2017-01-02 10:23:23');
32
33<?php
34
35use AppModelsLog;
36use AppModelsGoal;
37
38use CarbonCarbon;
39
40
41 Route::get('test', function() {
42
43
44 // Grab all goals where the command is set to "daily"
45 $goals = Goal::where('command', 'daily')->get();
46
47 // Grab all logs from today
48 $logs = auth()->user()->logs()->whereDate('date', '=', Carbon::today()->toDateString())->paginate(25);
49
50 $action_array = [];
51
52 // Loop over all goals
53 foreach($goals as $goal)
54 {
55 // Loop over all actions related to the given goal and put the action_id in the $action_array
56 foreach($goal->actions as $action)
57 {
58 $action_array[] = $action->id;
59 }
60 }
61
62 $log_array = [];
63
64 // Loop over all logs and put the action_id related to each log in the $log_array
65 foreach($logs as $log)
66 {
67 $log_array[] = $log->action->id;
68 }
69
70 // Count the total number of actions from each log from today that are equal to the goal actions
71 $log_count = count(array_intersect($action_array, $log_array));
72
73 // Loop over each goal again
74 foreach($goals as $goal)
75 {
76 // Count the total number of actions related to each goal
77 $action_count = count($goal->actions);
78
79 // Calculate the percentage of
80 $total = $action_count / $log_count * 100;
81
82 // Check wether the total percentage of completed logs is equal to or bigger than the goal percentage
83 if($total >= $goal->ratio)
84 {
85 // Insert a value into the database, not important for this example
86 }
87 }
88
89 });