diff --git a/habit.php b/habit.php index e01a11f..8d24054 100644 --- a/habit.php +++ b/habit.php @@ -204,6 +204,147 @@ echo $result; } +function habit_last_10_daily($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + $dateOccurrences = []; + + // Get the current date + $currentDate = date('Y-m-d'); + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Check if the line contains the specified word as a hashtag + $wordFound = (strpos($line, "#$filter") !== false); + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60)); + + // Check if the week is more than 24 weeks ago + if ($dayDifference > 10) { + break; + } + + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + + if ($wordFound) { + $dateOccurrences[$date] = 1; + } + } + } + + // Remove entries with a value of 0 + //$dateOccurrences = array_filter($dateOccurrences, function ($value) { + // return $value !== 0; + //}); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $dateOccurrences = array_reverse($dateOccurrences); + foreach ($dateOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + +function habit_last_10_weekly($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + + $dateOccurrences = []; + $weeklyOccurrences = []; + + $currentDate = date('Y-m-d'); + $firstOccurrenceDate = null; + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Explode the line into words + $words = explode(' ', $line); + + // Check if the line contains the specified word as a hashtag + $wordFound = false; + foreach ($words as $wordInLine) { + // Check if the word contains "#takeout" (case-insensitive) + if (stripos($wordInLine, "#$filter") !== false) { + $wordFound = true; + break; + } + } + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + // Check if the date is within the last 10 weeks + if (strtotime($date) < strtotime('-9 weeks')) { + continue; + } + } + // Populate the dateOccurrences array + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + if ($date !== null && $wordFound) { + + $dateOccurrences[$date]++; + $firstOccurrenceDate = $date; + } + + + // Output the line whether the word is found or not + //echo ($wordFound) ? "+ found: $line\n" : $line . "\n"; + } + + foreach ($dateOccurrences as $date => $count) { + // Convert each date to the format $year-$weekNo + $dateTime = new DateTime($date); + $yearWeek = $dateTime->format('Y-m-W'); + + // Add +1 for each date converted into $year-$weekNo as a key + if (isset($weeklyOccurrences[$yearWeek])) { + if( $dateOccurrences[$date] == 1) + $weeklyOccurrences[$yearWeek]++; + } else { + if($dateOccurrences[$date] == 1){ + $weeklyOccurrences[$yearWeek] = 1; + }else{ + $weeklyOccurrences[$yearWeek] = 0; + } + } + if($date < $firstOccurrenceDate) + $weeklyOccurrences[$yearWeek] = "x"; + } + + // Remove entries with a value of 0 + //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) { + // return $value !== 0; + //}); + + //print_r($weeklyOccurrences); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $weeklyOccurrences = array_reverse($weeklyOccurrences); + foreach ($weeklyOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + function habit_parse_file($fileContent) { $flattenedArray = array(); @@ -788,6 +929,9 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); if ($todayCheck) { $jsonArray['buttonClass'] = 'button-'.$jsonArray['align']; + }else{ + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } break; } @@ -810,7 +954,8 @@ $jsonArray['weekGoal'] = $multiplier; if( $weekCount < $multiplier ) - if($todayCheck) + $jsonArray['buttonClass'] = 'button-yellow'; + //if($todayCheck) $jsonArray['buttonText'] = $weekCount; if($weekCount >= $multiplier) $jsonArray['buttonClass'] = 'button-done'; @@ -865,9 +1010,27 @@ } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $jsonArray[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $jsonArray[$key] = "green"; + + } if ($todayCheck) { $jsonArray['buttonClass'] = 'button-' . $jsonArray['align']; + }else{ + //if ($section['Category'][1] == "+") + // $jsonArray['buttonClass'] = 'button-bad'; + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } } elseif ($section['Category'][0] == "w") { $jsonArray['freq'] = "w"; @@ -886,21 +1049,33 @@ $jsonArray['weekSoFar'] = $weekCount; $jsonArray['weekGoal'] = $multiplier; - if ($weekCount < $multiplier && $todayCheck) { + if ($weekCount < $multiplier) { $jsonArray['buttonText'] = $weekCount; - } - - if ($weekCount >= $multiplier) { + $jsonArray['buttonClass'] = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-done'; - } - - if ($todayCheck) { + } elseif ($todayCheck) { $jsonArray['buttonClass'] = 'button-neut'; if ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-good'; } } + + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($value >= $multiplier) + $jsonArray[$key] = "green"; + if ($value < $multiplier) + $jsonArray[$key] = "yellow"; + if ($value === "x") + $jsonArray[$key] = "neut"; + if ($value === 0) + $jsonArray[$key] = "red"; + + + } } $jsonWordsArray[] = $jsonArray; diff --git a/habit.php b/habit.php index e01a11f..8d24054 100644 --- a/habit.php +++ b/habit.php @@ -204,6 +204,147 @@ echo $result; } +function habit_last_10_daily($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + $dateOccurrences = []; + + // Get the current date + $currentDate = date('Y-m-d'); + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Check if the line contains the specified word as a hashtag + $wordFound = (strpos($line, "#$filter") !== false); + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60)); + + // Check if the week is more than 24 weeks ago + if ($dayDifference > 10) { + break; + } + + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + + if ($wordFound) { + $dateOccurrences[$date] = 1; + } + } + } + + // Remove entries with a value of 0 + //$dateOccurrences = array_filter($dateOccurrences, function ($value) { + // return $value !== 0; + //}); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $dateOccurrences = array_reverse($dateOccurrences); + foreach ($dateOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + +function habit_last_10_weekly($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + + $dateOccurrences = []; + $weeklyOccurrences = []; + + $currentDate = date('Y-m-d'); + $firstOccurrenceDate = null; + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Explode the line into words + $words = explode(' ', $line); + + // Check if the line contains the specified word as a hashtag + $wordFound = false; + foreach ($words as $wordInLine) { + // Check if the word contains "#takeout" (case-insensitive) + if (stripos($wordInLine, "#$filter") !== false) { + $wordFound = true; + break; + } + } + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + // Check if the date is within the last 10 weeks + if (strtotime($date) < strtotime('-9 weeks')) { + continue; + } + } + // Populate the dateOccurrences array + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + if ($date !== null && $wordFound) { + + $dateOccurrences[$date]++; + $firstOccurrenceDate = $date; + } + + + // Output the line whether the word is found or not + //echo ($wordFound) ? "+ found: $line\n" : $line . "\n"; + } + + foreach ($dateOccurrences as $date => $count) { + // Convert each date to the format $year-$weekNo + $dateTime = new DateTime($date); + $yearWeek = $dateTime->format('Y-m-W'); + + // Add +1 for each date converted into $year-$weekNo as a key + if (isset($weeklyOccurrences[$yearWeek])) { + if( $dateOccurrences[$date] == 1) + $weeklyOccurrences[$yearWeek]++; + } else { + if($dateOccurrences[$date] == 1){ + $weeklyOccurrences[$yearWeek] = 1; + }else{ + $weeklyOccurrences[$yearWeek] = 0; + } + } + if($date < $firstOccurrenceDate) + $weeklyOccurrences[$yearWeek] = "x"; + } + + // Remove entries with a value of 0 + //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) { + // return $value !== 0; + //}); + + //print_r($weeklyOccurrences); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $weeklyOccurrences = array_reverse($weeklyOccurrences); + foreach ($weeklyOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + function habit_parse_file($fileContent) { $flattenedArray = array(); @@ -788,6 +929,9 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); if ($todayCheck) { $jsonArray['buttonClass'] = 'button-'.$jsonArray['align']; + }else{ + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } break; } @@ -810,7 +954,8 @@ $jsonArray['weekGoal'] = $multiplier; if( $weekCount < $multiplier ) - if($todayCheck) + $jsonArray['buttonClass'] = 'button-yellow'; + //if($todayCheck) $jsonArray['buttonText'] = $weekCount; if($weekCount >= $multiplier) $jsonArray['buttonClass'] = 'button-done'; @@ -865,9 +1010,27 @@ } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $jsonArray[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $jsonArray[$key] = "green"; + + } if ($todayCheck) { $jsonArray['buttonClass'] = 'button-' . $jsonArray['align']; + }else{ + //if ($section['Category'][1] == "+") + // $jsonArray['buttonClass'] = 'button-bad'; + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } } elseif ($section['Category'][0] == "w") { $jsonArray['freq'] = "w"; @@ -886,21 +1049,33 @@ $jsonArray['weekSoFar'] = $weekCount; $jsonArray['weekGoal'] = $multiplier; - if ($weekCount < $multiplier && $todayCheck) { + if ($weekCount < $multiplier) { $jsonArray['buttonText'] = $weekCount; - } - - if ($weekCount >= $multiplier) { + $jsonArray['buttonClass'] = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-done'; - } - - if ($todayCheck) { + } elseif ($todayCheck) { $jsonArray['buttonClass'] = 'button-neut'; if ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-good'; } } + + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($value >= $multiplier) + $jsonArray[$key] = "green"; + if ($value < $multiplier) + $jsonArray[$key] = "yellow"; + if ($value === "x") + $jsonArray[$key] = "neut"; + if ($value === 0) + $jsonArray[$key] = "red"; + + + } } $jsonWordsArray[] = $jsonArray; diff --git a/habit_ui.php b/habit_ui.php index 05f3176..9e73124 100644 --- a/habit_ui.php +++ b/habit_ui.php @@ -100,6 +100,20 @@ $result3 = habit_get_stat_top_missing($section['Activity'], $moodlog); } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $last10_conv[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $last10_conv[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $last10_conv[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $last10_conv[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $last10_conv[$key] = "green"; + + } + if ($todayCheck) { $buttonClass = 'button-'.$align; } @@ -118,17 +132,31 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); $weekCount = habit_count_weekly($section['Activity'], $moodlog); + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $last10_conv[$key] = "neut"; + if ($value >= $multiplier) + $last10_conv[$key] = "green"; + if ($value < $multiplier) + $last10_conv[$key] = "yellow"; + if ($value === "x") + $last10_conv[$key] = "neut"; + if ($value === 0) + $last10_conv[$key] = "red"; + } - if( $weekCount < $section['Category'][1] ) - if($todayCheck) - $buttText = $weekCount; - if($weekCount >= $multiplier) - $buttonClass = 'button-done'; - if( $todayCheck ) { - $buttonClass = 'button-neut'; - if($weekCount >= $multiplier) - $buttonClass = 'button-good'; - } + if ($weekCount < $multiplier) { + $buttText = $weekCount; + $buttonClass = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { + $buttonClass = 'button-done'; + } elseif ($todayCheck) { + $buttonClass = 'button-neut'; + + if ($weekCount >= $multiplier) { + $buttonClass = 'button-good'; + } + } } @@ -148,6 +176,19 @@
'.$title2.'
'.$result2.'
'.$title3.'
'.$result3.'
+
+
+
+
+
+
+
+
+
+
+
+
+ '; diff --git a/habit.php b/habit.php index e01a11f..8d24054 100644 --- a/habit.php +++ b/habit.php @@ -204,6 +204,147 @@ echo $result; } +function habit_last_10_daily($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + $dateOccurrences = []; + + // Get the current date + $currentDate = date('Y-m-d'); + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Check if the line contains the specified word as a hashtag + $wordFound = (strpos($line, "#$filter") !== false); + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60)); + + // Check if the week is more than 24 weeks ago + if ($dayDifference > 10) { + break; + } + + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + + if ($wordFound) { + $dateOccurrences[$date] = 1; + } + } + } + + // Remove entries with a value of 0 + //$dateOccurrences = array_filter($dateOccurrences, function ($value) { + // return $value !== 0; + //}); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $dateOccurrences = array_reverse($dateOccurrences); + foreach ($dateOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + +function habit_last_10_weekly($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + + $dateOccurrences = []; + $weeklyOccurrences = []; + + $currentDate = date('Y-m-d'); + $firstOccurrenceDate = null; + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Explode the line into words + $words = explode(' ', $line); + + // Check if the line contains the specified word as a hashtag + $wordFound = false; + foreach ($words as $wordInLine) { + // Check if the word contains "#takeout" (case-insensitive) + if (stripos($wordInLine, "#$filter") !== false) { + $wordFound = true; + break; + } + } + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + // Check if the date is within the last 10 weeks + if (strtotime($date) < strtotime('-9 weeks')) { + continue; + } + } + // Populate the dateOccurrences array + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + if ($date !== null && $wordFound) { + + $dateOccurrences[$date]++; + $firstOccurrenceDate = $date; + } + + + // Output the line whether the word is found or not + //echo ($wordFound) ? "+ found: $line\n" : $line . "\n"; + } + + foreach ($dateOccurrences as $date => $count) { + // Convert each date to the format $year-$weekNo + $dateTime = new DateTime($date); + $yearWeek = $dateTime->format('Y-m-W'); + + // Add +1 for each date converted into $year-$weekNo as a key + if (isset($weeklyOccurrences[$yearWeek])) { + if( $dateOccurrences[$date] == 1) + $weeklyOccurrences[$yearWeek]++; + } else { + if($dateOccurrences[$date] == 1){ + $weeklyOccurrences[$yearWeek] = 1; + }else{ + $weeklyOccurrences[$yearWeek] = 0; + } + } + if($date < $firstOccurrenceDate) + $weeklyOccurrences[$yearWeek] = "x"; + } + + // Remove entries with a value of 0 + //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) { + // return $value !== 0; + //}); + + //print_r($weeklyOccurrences); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $weeklyOccurrences = array_reverse($weeklyOccurrences); + foreach ($weeklyOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + function habit_parse_file($fileContent) { $flattenedArray = array(); @@ -788,6 +929,9 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); if ($todayCheck) { $jsonArray['buttonClass'] = 'button-'.$jsonArray['align']; + }else{ + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } break; } @@ -810,7 +954,8 @@ $jsonArray['weekGoal'] = $multiplier; if( $weekCount < $multiplier ) - if($todayCheck) + $jsonArray['buttonClass'] = 'button-yellow'; + //if($todayCheck) $jsonArray['buttonText'] = $weekCount; if($weekCount >= $multiplier) $jsonArray['buttonClass'] = 'button-done'; @@ -865,9 +1010,27 @@ } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $jsonArray[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $jsonArray[$key] = "green"; + + } if ($todayCheck) { $jsonArray['buttonClass'] = 'button-' . $jsonArray['align']; + }else{ + //if ($section['Category'][1] == "+") + // $jsonArray['buttonClass'] = 'button-bad'; + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } } elseif ($section['Category'][0] == "w") { $jsonArray['freq'] = "w"; @@ -886,21 +1049,33 @@ $jsonArray['weekSoFar'] = $weekCount; $jsonArray['weekGoal'] = $multiplier; - if ($weekCount < $multiplier && $todayCheck) { + if ($weekCount < $multiplier) { $jsonArray['buttonText'] = $weekCount; - } - - if ($weekCount >= $multiplier) { + $jsonArray['buttonClass'] = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-done'; - } - - if ($todayCheck) { + } elseif ($todayCheck) { $jsonArray['buttonClass'] = 'button-neut'; if ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-good'; } } + + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($value >= $multiplier) + $jsonArray[$key] = "green"; + if ($value < $multiplier) + $jsonArray[$key] = "yellow"; + if ($value === "x") + $jsonArray[$key] = "neut"; + if ($value === 0) + $jsonArray[$key] = "red"; + + + } } $jsonWordsArray[] = $jsonArray; diff --git a/habit_ui.php b/habit_ui.php index 05f3176..9e73124 100644 --- a/habit_ui.php +++ b/habit_ui.php @@ -100,6 +100,20 @@ $result3 = habit_get_stat_top_missing($section['Activity'], $moodlog); } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $last10_conv[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $last10_conv[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $last10_conv[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $last10_conv[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $last10_conv[$key] = "green"; + + } + if ($todayCheck) { $buttonClass = 'button-'.$align; } @@ -118,17 +132,31 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); $weekCount = habit_count_weekly($section['Activity'], $moodlog); + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $last10_conv[$key] = "neut"; + if ($value >= $multiplier) + $last10_conv[$key] = "green"; + if ($value < $multiplier) + $last10_conv[$key] = "yellow"; + if ($value === "x") + $last10_conv[$key] = "neut"; + if ($value === 0) + $last10_conv[$key] = "red"; + } - if( $weekCount < $section['Category'][1] ) - if($todayCheck) - $buttText = $weekCount; - if($weekCount >= $multiplier) - $buttonClass = 'button-done'; - if( $todayCheck ) { - $buttonClass = 'button-neut'; - if($weekCount >= $multiplier) - $buttonClass = 'button-good'; - } + if ($weekCount < $multiplier) { + $buttText = $weekCount; + $buttonClass = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { + $buttonClass = 'button-done'; + } elseif ($todayCheck) { + $buttonClass = 'button-neut'; + + if ($weekCount >= $multiplier) { + $buttonClass = 'button-good'; + } + } } @@ -148,6 +176,19 @@
'.$title2.'
'.$result2.'
'.$title3.'
'.$result3.'
+
+
+
+
+
+
+
+
+
+
+
+
+ '; diff --git a/habit_ui_script.js b/habit_ui_script.js index a4899a8..166c636 100644 --- a/habit_ui_script.js +++ b/habit_ui_script.js @@ -88,13 +88,13 @@ if (element && parentDiv.contains(element)) { //console.log('Element found:', element); // Remove the classes "neut" and "green" - element.classList.remove("neut", "green"); + element.classList.remove("yellow", "green"); // Set the text content to the value of data.buttonText element.textContent = data.buttonText; if( data.weekSoFar > 0 ){ if (data.weekSoFar < data.weekGoal) { - element.classList.add("neut"); + element.classList.add("yellow"); element.textContent = data.weekSoFar; } if (data.weekSoFar >= data.weekGoal) { diff --git a/habit.php b/habit.php index e01a11f..8d24054 100644 --- a/habit.php +++ b/habit.php @@ -204,6 +204,147 @@ echo $result; } +function habit_last_10_daily($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + $dateOccurrences = []; + + // Get the current date + $currentDate = date('Y-m-d'); + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Check if the line contains the specified word as a hashtag + $wordFound = (strpos($line, "#$filter") !== false); + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60)); + + // Check if the week is more than 24 weeks ago + if ($dayDifference > 10) { + break; + } + + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + + if ($wordFound) { + $dateOccurrences[$date] = 1; + } + } + } + + // Remove entries with a value of 0 + //$dateOccurrences = array_filter($dateOccurrences, function ($value) { + // return $value !== 0; + //}); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $dateOccurrences = array_reverse($dateOccurrences); + foreach ($dateOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + +function habit_last_10_weekly($filter, $filename){ + // Split the file content into an array of lines + $fileContent = file_get_contents($filename); + $lines = explode("\n", $fileContent); + + $dateOccurrences = []; + $weeklyOccurrences = []; + + $currentDate = date('Y-m-d'); + $firstOccurrenceDate = null; + + foreach ($lines as $line) { + // Extract the date from the line + preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches); + $date = isset($matches[0]) ? $matches[0] : null; + + // Explode the line into words + $words = explode(' ', $line); + + // Check if the line contains the specified word as a hashtag + $wordFound = false; + foreach ($words as $wordInLine) { + // Check if the word contains "#takeout" (case-insensitive) + if (stripos($wordInLine, "#$filter") !== false) { + $wordFound = true; + break; + } + } + + // Populate the dateOccurrences array + if ($date !== null) { + // Calculate the difference in weeks + // Check if the date is within the last 10 weeks + if (strtotime($date) < strtotime('-9 weeks')) { + continue; + } + } + // Populate the dateOccurrences array + if (!isset($dateOccurrences[$date])) { + $dateOccurrences[$date] = 0; + } + if ($date !== null && $wordFound) { + + $dateOccurrences[$date]++; + $firstOccurrenceDate = $date; + } + + + // Output the line whether the word is found or not + //echo ($wordFound) ? "+ found: $line\n" : $line . "\n"; + } + + foreach ($dateOccurrences as $date => $count) { + // Convert each date to the format $year-$weekNo + $dateTime = new DateTime($date); + $yearWeek = $dateTime->format('Y-m-W'); + + // Add +1 for each date converted into $year-$weekNo as a key + if (isset($weeklyOccurrences[$yearWeek])) { + if( $dateOccurrences[$date] == 1) + $weeklyOccurrences[$yearWeek]++; + } else { + if($dateOccurrences[$date] == 1){ + $weeklyOccurrences[$yearWeek] = 1; + }else{ + $weeklyOccurrences[$yearWeek] = 0; + } + } + if($date < $firstOccurrenceDate) + $weeklyOccurrences[$yearWeek] = "x"; + } + + // Remove entries with a value of 0 + //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) { + // return $value !== 0; + //}); + + //print_r($weeklyOccurrences); + + // Convert keys to timestamps and print in a simple JSON format with surrounding {} + $i = 0; + $weeklyOccurrences = array_reverse($weeklyOccurrences); + foreach ($weeklyOccurrences as $date => $value) { + $i++; + $result["last_10_$i"] = $value; + } + return $result; +} + function habit_parse_file($fileContent) { $flattenedArray = array(); @@ -788,6 +929,9 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); if ($todayCheck) { $jsonArray['buttonClass'] = 'button-'.$jsonArray['align']; + }else{ + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } break; } @@ -810,7 +954,8 @@ $jsonArray['weekGoal'] = $multiplier; if( $weekCount < $multiplier ) - if($todayCheck) + $jsonArray['buttonClass'] = 'button-yellow'; + //if($todayCheck) $jsonArray['buttonText'] = $weekCount; if($weekCount >= $multiplier) $jsonArray['buttonClass'] = 'button-done'; @@ -865,9 +1010,27 @@ } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $jsonArray[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $jsonArray[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $jsonArray[$key] = "green"; + + } if ($todayCheck) { $jsonArray['buttonClass'] = 'button-' . $jsonArray['align']; + }else{ + //if ($section['Category'][1] == "+") + // $jsonArray['buttonClass'] = 'button-bad'; + if ($section['Category'][1] == "-") + $jsonArray['buttonClass'] = 'button-done'; } } elseif ($section['Category'][0] == "w") { $jsonArray['freq'] = "w"; @@ -886,21 +1049,33 @@ $jsonArray['weekSoFar'] = $weekCount; $jsonArray['weekGoal'] = $multiplier; - if ($weekCount < $multiplier && $todayCheck) { + if ($weekCount < $multiplier) { $jsonArray['buttonText'] = $weekCount; - } - - if ($weekCount >= $multiplier) { + $jsonArray['buttonClass'] = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-done'; - } - - if ($todayCheck) { + } elseif ($todayCheck) { $jsonArray['buttonClass'] = 'button-neut'; if ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-good'; } } + + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $jsonArray[$key] = "neut"; + if ($value >= $multiplier) + $jsonArray[$key] = "green"; + if ($value < $multiplier) + $jsonArray[$key] = "yellow"; + if ($value === "x") + $jsonArray[$key] = "neut"; + if ($value === 0) + $jsonArray[$key] = "red"; + + + } } $jsonWordsArray[] = $jsonArray; diff --git a/habit_ui.php b/habit_ui.php index 05f3176..9e73124 100644 --- a/habit_ui.php +++ b/habit_ui.php @@ -100,6 +100,20 @@ $result3 = habit_get_stat_top_missing($section['Activity'], $moodlog); } $todayCheck = habit_check_daily($section['Activity'], $moodlog); + $last10 = habit_last_10_daily($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $last10_conv[$key] = "neut"; + if ($section['Category'][1] == "+" && $value == 1) + $last10_conv[$key] = "green"; + if ($section['Category'][1] == "+" && $value == 0) + $last10_conv[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 1) + $last10_conv[$key] = "red"; + if ($section['Category'][1] == "-" && $value == 0) + $last10_conv[$key] = "green"; + + } + if ($todayCheck) { $buttonClass = 'button-'.$align; } @@ -118,17 +132,31 @@ $todayCheck = habit_check_daily($section['Activity'], $moodlog); $weekCount = habit_count_weekly($section['Activity'], $moodlog); + $last10 = habit_last_10_weekly($section['Activity'], $moodlog); + foreach ($last10 as $key => $value) { + $last10_conv[$key] = "neut"; + if ($value >= $multiplier) + $last10_conv[$key] = "green"; + if ($value < $multiplier) + $last10_conv[$key] = "yellow"; + if ($value === "x") + $last10_conv[$key] = "neut"; + if ($value === 0) + $last10_conv[$key] = "red"; + } - if( $weekCount < $section['Category'][1] ) - if($todayCheck) - $buttText = $weekCount; - if($weekCount >= $multiplier) - $buttonClass = 'button-done'; - if( $todayCheck ) { - $buttonClass = 'button-neut'; - if($weekCount >= $multiplier) - $buttonClass = 'button-good'; - } + if ($weekCount < $multiplier) { + $buttText = $weekCount; + $buttonClass = 'button-yellow'; + } elseif ($weekCount >= $multiplier) { + $buttonClass = 'button-done'; + } elseif ($todayCheck) { + $buttonClass = 'button-neut'; + + if ($weekCount >= $multiplier) { + $buttonClass = 'button-good'; + } + } } @@ -148,6 +176,19 @@
'.$title2.'
'.$result2.'
'.$title3.'
'.$result3.'
+
+
+
+
+
+
+
+
+
+
+
+
+ '; diff --git a/habit_ui_script.js b/habit_ui_script.js index a4899a8..166c636 100644 --- a/habit_ui_script.js +++ b/habit_ui_script.js @@ -88,13 +88,13 @@ if (element && parentDiv.contains(element)) { //console.log('Element found:', element); // Remove the classes "neut" and "green" - element.classList.remove("neut", "green"); + element.classList.remove("yellow", "green"); // Set the text content to the value of data.buttonText element.textContent = data.buttonText; if( data.weekSoFar > 0 ){ if (data.weekSoFar < data.weekGoal) { - element.classList.add("neut"); + element.classList.add("yellow"); element.textContent = data.weekSoFar; } if (data.weekSoFar >= data.weekGoal) { diff --git a/habit_ui_style.css b/habit_ui_style.css index 4f4d096..e5bc202 100644 --- a/habit_ui_style.css +++ b/habit_ui_style.css @@ -16,14 +16,15 @@ } .habit-container { - width: 100%; /* Two containers per line */ + width: 100%; box-sizing: border-box; background-color: #222; border-radius: 5px; - text-align: left; + text-align: center; padding: 5px; - margin:5px; + margin: 5px; margin-bottom: 30px; + padding-bottom: 0px; } .habit-title{ font-family: 'Source Code Pro', monospace; @@ -42,10 +43,11 @@ padding-bottom: 10px; } .habit-main{ - float: left; - width: -webkit-fill-available; - margin-top: -90px; - margin-right: 90px; + float: left; + width: -webkit-fill-available; + margin-top: -90px; + margin-right: 90px; + text-align: left; } .good{ color: #58e81b;} @@ -67,10 +69,18 @@ font-weight:700; } -.button-done{ background-color: #399113; color:#000; } -.button-good{ background-color: #58e81b; color:#000; } +.button-done{ background-color: #399113; color:#399113; } +.button-good{ background-color: #58e81b; color:#58e81b; } .button-bad{ background-color: #e81b1b; color:#000; } -.button-neut{ background-color: #efe23e; color:#000; } +.button-neut, .button-yellow{ background-color: #efe23e; color:#000; } +.button-green{ background-color: #58e81b; color:#58e81b; } + + +.ltred{ background-color: #e81b1b; color:#000; } +.ltgreen{ background-color: #888; } +.ltneut{ background-color: #444; } +.ltyellow{ background-color: #efe23e; } + .habit-info { text-align: right; @@ -161,4 +171,22 @@ border: 0px solid #fff; box-sizing: border-box; border-radius: 2px; - } \ No newline at end of file + } + +.habit_long_container{ + text-align: center; + height: 10px; + display: inline-flex; + position: relative; +} + +.long_tick { + height: 10px; + width: 60px; + display: block; + margin-right: 1px; + padding: 0; + position: relative; + border-radius: 5px; + +}