<?php function habit_update($word, $moodlog) { // Call habit_check_daily function $result = habit_check_daily($word, $moodlog); // Get today's date $todayDate = date("Y-m-d"); // Read the existing mood log file $lines = file($moodlog); // Check if habit_check_daily returns true if ($result) { // Iterate through each line in the file foreach ($lines as $key => $line) { // Extract the date from the line preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match); if (!empty($match[1])) { $lineDate = $match[1]; // Check if the line date is today if ($lineDate === $todayDate) { // Remove the word along with the '#' symbol (case-insensitive) $modifiedLine = preg_replace("/#?$word\b/i", '', $line); // Remove the whole line if there is nothing after the number if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2} \[\d+\]\s*$/', $modifiedLine)) { unset($lines[$key]); } else { $lines[$key] = $modifiedLine; } } } } // Save the modified lines back to the file file_put_contents($moodlog, implode("", $lines)); } else { $foundToday = false; // Check if there is a line with today's date foreach ($lines as $key => $line) { preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match); if (!empty($match[1]) && $match[1] === $todayDate) { $foundToday = true; // Remove the trailing newline $lines[$key] = rtrim($line); // Append #$text to the line $lines[$key] .= " #$word"; // Re-add the newline $lines[$key] .= "\n"; break; } } // If today's date is not found, prepend a new line if (!$foundToday) { // Prepend a new line with Y-m-d hh:mm [0] #$word $newLine = $todayDate . " " . date("H:i") . " [0] #$word\n"; array_unshift($lines, $newLine); } // Save the modified lines back to the file file_put_contents($moodlog, implode("", $lines)); } } function habit_cal_daily($filter){ global $moodlog; $fileContent = file_get_contents($moodlog); $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 $weekDifference = floor((strtotime($currentDate) - strtotime($date)) / (7 * 24 * 60 * 60)); // Check if the week is more than 24 weeks ago if ($weekDifference > 24) { 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 {} $result = '{'; foreach ($dateOccurrences as $date => $value) { $timestamp = strtotime($date); $result .= "\"$timestamp\":$value,"; } // Remove trailing comma $result = rtrim($result, ','); $result .= '}'; echo $result; } function habit_cal_weekly($filter){ global $moodlog; $fileContent = file_get_contents($moodlog); $lines = explode("\n", $fileContent); $dateOccurrences = []; $weeklyOccurrences = []; 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; //echo "found: $line\n"; break; } } // Populate the dateOccurrences array if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } if ($date !== null && $wordFound) { $dateOccurrences[$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); $dateTime->modify('this week sunday'); // Set to Sunday of the current week $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; } } } // Remove entries with a value of 0 $weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) { return $value !== 0; }); //print_r($weeklyOccurrences); $result = '{'; foreach ($weeklyOccurrences as $date => $value) { $result .= "\"$date\":$value,"; } // Remove trailing comma $result = rtrim($result, ','); $result .= '}'; 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 > 9) { 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('-11 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"; } $weekPrevStart = new DateTime($firstOccurrenceDate); // Add one week to the date $weekPrevStart->modify('-1 week'); // Format and print the new date $weekPrevStart2 = $weekPrevStart->format('Y-m-d'); foreach ($dateOccurrences as $date => $count) { // Convert each date to the format $year-$weekNo $dateTime = new DateTime($date); $dateTime->modify('this week sunday'); // Set to Sunday of the current week $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(strtotime($date) < strtotime($weekPrevStart2)) $weeklyOccurrences[$yearWeek] = "x"; } // Remove entries with a value of 0 //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) { // return $value !== 0; //}); //print_r($dateOccurrences); //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(); // Split the file content into lines $lines = explode("\n", $fileContent); foreach ($lines as $line) { // Use regular expressions to extract relevant information preg_match('/\[([^\]]+)\]\[([^\]]+)\] (.+) - (.+)/', $line, $matches); if (count($matches) == 5) { $category = $matches[1]; $value = $matches[2]; $activity = $matches[3]; $description = $matches[4]; // Add to the flattened array $flattenedArray[] = array( 'Category' => $category, 'Value' => $value, 'Activity' => $activity, 'Description' => $description ); } } return $flattenedArray; } function habit_check_daily($word, $filename) { // Split the file content into an array of lines $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); $dateOccurrences = []; 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, "#$word") !== false); // Populate the dateOccurrences array if ($date !== null) { if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } if($wordFound){ $dateOccurrences[$date] = 1; } } } // Check streak $currentStreak = 0; $today = date('Y-m-d'); //print_r($dateOccurrences); while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) { $currentStreak++; $today = date('Y-m-d', strtotime($today . ' - 1 day')); } if( $currentStreak >= 1){ return true; }else{ return false; } } function habit_count_weekly($word, $filename) { $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); $currentWeekNumber = (int)date("W"); $wordCount = 0; foreach ($lines as $line) { preg_match('/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/', $line, $matches); if (!empty($matches)) { $lineDate = new DateTime($matches[1]); $lineWeekNumber = (int)$lineDate->format('W'); // Check if the line's week number is the same as the current week number if ($lineWeekNumber == $currentWeekNumber) { $wordCount += substr_count($line, "#$word"); } } } return $wordCount; } function habit_get_stat_streak($word, $filename) { // Split the file content into an array of lines $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); $dateOccurrences = []; 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, "#$word") !== false); // Populate the dateOccurrences array if ($date !== null) { if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } if($wordFound){ $dateOccurrences[$date] = 1; } } } // Check streak $currentStreak = 0; $today = date('Y-m-d'); while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) { $currentStreak++; $today = date('Y-m-d', strtotime($today . ' - 1 day')); } return $currentStreak; } function habit_get_stat_streak_week($word, $multiplier, $filename) { // Split the file content into an array of lines $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); $dateOccurrences = []; $weeklyOccurrences = []; $first = ""; 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, "#$word") !== false) { $wordFound = true; $first = $date; // $first will end up containing the first instance of the word break; } } // Populate the dateOccurrences array if ($date !== null && $wordFound) { if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } $dateOccurrences[$date]++; } } // Create weekly occurrences array foreach ($dateOccurrences as $date => $count) { // Convert each date to the format $year-$weekNo $dateTime = new DateTime($date); $dateTime->modify('this week sunday'); // Set to Sunday of the current week $yearWeek = $dateTime->format('Y-m-W'); // Add +1 for each date converted into $year-$weekNo as a key if (isset($weeklyOccurrences[$yearWeek])) { $weeklyOccurrences[$yearWeek]++; } else { $weeklyOccurrences[$yearWeek] = 1; } } // Fill in missing weeks up to the current week $today = new DateTime(); $currentDate = date('Y-m-d'); while (strtotime($currentDate) >= strtotime($first)) { $dateTime = new DateTime($currentDate); $dateTime->modify('this week sunday'); // Set to Sunday of the current week $yearWeek = $dateTime->format('Y-m-W'); if (!isset($weeklyOccurrences[$yearWeek])) { $weeklyOccurrences[$yearWeek] = 0; } $currentDate = date('Y-m-d', strtotime($currentDate . ' -1 day')); } $consecutiveCount = 0; $maxConsecutiveCount = 0; ksort($weeklyOccurrences); foreach ($weeklyOccurrences as $week => $value) { if ($value >= $multiplier) { $consecutiveCount++; $maxConsecutiveCount = max($maxConsecutiveCount, $consecutiveCount); } else { $consecutiveCount = 0; // Reset count if the value is less than the multiplier } // If the current week's count is less than the multiplier and it's the current week, reset the streak if ($value < $multiplier && $week === date('Y-m-W')) { $maxConsecutiveCount = 0; } } return ($maxConsecutiveCount >= $multiplier) ? $maxConsecutiveCount : 0; } function habit_get_stat_missed($word, $filename) { // Split the file content into an array of lines $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); // Set variables $missedDays = 0; $currentDate = date('Y-m-d'); $wordFound = false; // Find the minimum date in the lines $minDate = null; foreach ($lines as $line) { preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches); $date = isset($matches[0]) ? $matches[0] : null; if ($date !== null && ($minDate === null || $date < $minDate)) { $minDate = $date; } } // Loop for each day while (true) { // Check if the word is found on any line for the current date foreach ($lines as $line) { preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches); $date = isset($matches[0]) ? $matches[0] : null; $wordFound = $wordFound || ($date === $currentDate && strpos($line, "#$word") !== false); } // If the word is found or we reach the beginning of the available dates, break the loop if ($wordFound || $currentDate < $minDate) { break; } // Move to the previous day $missedDays++; $currentDate = date('Y-m-d', strtotime($currentDate . ' - 1 day')); } return $wordFound ? $missedDays : 0; } function habit_get_stat_missed_week($word, $multiplier, $filename) { // Split the file content into an array of lines $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); $dateOccurrences = []; $weeklyOccurrences = []; $first = ""; 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, "#$word") !== false) { $wordFound = true; $first = $date; break; } } // Populate the dateOccurrences array if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } if ($date !== null && $wordFound) { $dateOccurrences[$date]++; } // Output the line whether the word is found or not //echo ($wordFound) ? "+ found: $line\n" : $line . "\n"; } $currentDate = date('Y-m-d'); while (strtotime($first) <= strtotime($currentDate)) { $dateTime = new DateTime($currentDate); $yearWeekKey = $dateTime->format('Y-W'); $weeklyOccurrences[$yearWeekKey] = 0; //echo $yearWeekKey; $currentDate = date('Y-m-d', strtotime($currentDate . ' -1 day')); } foreach ($dateOccurrences as $date => $count) { // Convert each date to the format $year-$weekNo $dateTime = new DateTime($date); $yearWeek = $dateTime->format('Y-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; } } } //print_r($dateOccurrences); //print_r($weeklyOccurrences); $countZeros = 0; foreach ($weeklyOccurrences as $value) { if ($value < $multiplier) { $countZeros++; } else { break; // Stop counting when a non-zero value is encountered } } return $countZeros; } function habit_get_stat_top($word, $filename) { $fileContent = file_get_contents($filename); // Split the file content into an array of lines $lines = explode("\n", $fileContent); $dateOccurrences = []; $currentStreak = 0; $longestStreak = 0; 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, "#$word") !== false); // Populate the dateOccurrences array if ($date !== null) { if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } if($wordFound){ $dateOccurrences[$date] = 1; } } } foreach ($dateOccurrences as $date => $value) { if ($value == 1) { $currentStreak++; } else { $currentStreak = 0; } // Update the longest streak if the current streak is greater $longestStreak = max($longestStreak, $currentStreak); } //print_r($dateOccurrences); return $longestStreak; } function habit_get_stat_top_week($word, $multiplier, $filename) { // Split the file content into an array of lines $fileContent = file_get_contents($filename); $lines = explode("\n", $fileContent); $dateOccurrences = []; $weeklyOccurrences = []; 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, "#$word") !== false) { $wordFound = true; break; } } // Populate the dateOccurrences array if (!isset($dateOccurrences[$date])) { $dateOccurrences[$date] = 0; } if ($date !== null && $wordFound) { $dateOccurrences[$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-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; } } } //print_r($dateOccurrences); //print_r($weeklyOccurrences); $currentStreak = 0; $longestStreak = 0; foreach ($weeklyOccurrences as $week => $value) { if ($value >= $multiplier) { // Increment the streak length if the value is greater than or equal to the multiplier $currentStreak++; } else { // Reset the streak length if the value is less than the multiplier $currentStreak = 0; } // Update the longest streak if the current streak is longer $longestStreak = max($longestStreak, $currentStreak); } return $longestStreak; } function habit_get_stat_top_missing($word, $filename) { $fileContent = file_get_contents($filename); // Split the file content into an array of lines $lines = explode("\n", $fileContent); // Find the earliest instance of the word $earliestDateWithWord = null; foreach ($lines as $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, "#$word") !== false); if ($wordFound && ($earliestDateWithWord === null || $date < $earliestDateWithWord)) { $earliestDateWithWord = $date; } } if ($earliestDateWithWord === null) { // No instance of the word found, return 0 return 0; } $currentDate = $earliestDateWithWord; $today = date('Y-m-d'); $dateArray = []; // Initialize date array with 0 values starting from the earliest date with the word while ($currentDate <= $today) { $dateArray[$currentDate] = 0; $currentDate = date('Y-m-d', strtotime($currentDate . ' + 1 day')); } // Loop over the file and update dateArray foreach ($lines as $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, "#$word") !== false); if ($date !== null && isset($dateArray[$date])) { // Update dateArray based on word occurrence if($wordFound) $dateArray[$date] = 1; } } //print_r($dateArray); // Find the longest streak of 0's in dateArray $currentStreak = 0; $longestStreak = 0; foreach ($dateArray as $value) { if ($value === 0) { $currentStreak++; } else { $longestStreak = max($longestStreak, $currentStreak); $currentStreak = 0; } } // Check for a streak at the end of the array $longestStreak = max($longestStreak, $currentStreak); return $longestStreak; } function habit_get_stat_year($word, $filename) { $fileContent = file_get_contents($filename); // Split the file content into an array of lines $lines = explode("\n", $fileContent); $daysWithWord = []; 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, "#$word") !== false); if ($date !== null && date('Y', strtotime($date)) == date('Y')) { // Check if the date is in the current year if (!isset($daysWithWord[$date])) { $daysWithWord[$date] = $wordFound ? 1 : 0; } else { $daysWithWord[$date] += $wordFound ? 1 : 0; } } } $daysCount = array_sum($daysWithWord); return $daysCount; } function habit_get_json($word, $moodlog, $habitlog){ $fileContent = file_get_contents($habitlog); $resultArray = habit_parse_file($fileContent); $found = false; foreach ($resultArray as $section) { $jsonArray = array(); $jsonArray['align'] = ""; $jsonArray['buttonClass'] = ""; $jsonArray['buttonText'] = ""; if($section['Category'][0] == "d" && $section['Activity'] == $word){ $found = true; $jsonArray['freq'] = "d"; if($section['Category'][1] == "+"){ $jsonArray['align'] = "good"; $jsonArray['calCol'] = "#58e81b"; $jsonArray['title1'] = "Streak"; $jsonArray['title2'] = "Missed"; $jsonArray['title3'] = "Top"; $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog); $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog); $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog); } if($section['Category'][1] == "-"){ $jsonArray['align'] = "bad"; $jsonArray['calCol'] = "#e81b1b"; $jsonArray['title1'] = "Streak"; $jsonArray['title2'] = "Total (Year)"; $jsonArray['title3'] = "Top"; $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog); $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog); $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog); } $todayCheck = habit_check_daily($section['Activity'], $moodlog); if ($todayCheck) { $jsonArray['buttonClass'] = 'button-'.$jsonArray['align']; }else{ if ($section['Category'][1] == "-") $jsonArray['buttonClass'] = 'button-done'; } break; } if($section['Category'][0] == "w" && $section['Activity'] == $word){ $found = true; $jsonArray['freq'] = "w"; $multiplier = $section['Category'][1]; $jsonArray['align'] = "good"; $jsonArray['title1'] = "Streak"; $jsonArray['title2'] = "Missed"; $jsonArray['title3'] = "Top"; $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog); $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog); $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog); $todayCheck = habit_check_daily($section['Activity'], $moodlog); $weekCount = habit_count_weekly($section['Activity'], $moodlog); $jsonArray['weekSoFar'] = $weekCount; $jsonArray['weekGoal'] = $multiplier; if( $weekCount < $multiplier ) $jsonArray['buttonClass'] = 'button-yellow'; if($todayCheck) $jsonArray['buttonText'] = $weekCount; if($weekCount >= $multiplier) $jsonArray['buttonClass'] = 'button-done'; if( $todayCheck ) { $jsonArray['buttonClass'] = 'button-neut'; if($weekCount >= $multiplier){ $jsonArray['buttonClass'] = 'button-good'; $jsonArray['buttonText'] = ""; } } if($weekCount == 0) $jsonArray['buttonClass'] = ''; break; } } if($found) return json_encode($jsonArray); return false; } function habit_get_json_all($moodlog, $habitlog) { $fileContent = file_get_contents($habitlog); $resultArray = habit_parse_file($fileContent); $jsonWordsArray = array(); foreach ($resultArray as $section) { $jsonArray = array(); $jsonArray['activity'] = $section['Activity']; $jsonArray['align'] = ""; $jsonArray['buttonClass'] = ""; $jsonArray['buttonText'] = ""; if ($section['Category'][0] == "d") { $jsonArray['freq'] = "d"; if ($section['Category'][1] == "+") { $jsonArray['align'] = "good"; $jsonArray['calCol'] = "#58e81b"; $jsonArray['title1'] = "Streak"; $jsonArray['title2'] = "Missed"; $jsonArray['title3'] = "Top"; $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog); $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog); $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog); } elseif ($section['Category'][1] == "-") { $jsonArray['align'] = "bad"; $jsonArray['calCol'] = "#e81b1b"; $jsonArray['title1'] = "Streak"; $jsonArray['title2'] = "Total (Year)"; $jsonArray['title3'] = "Top"; $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog); $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog); $jsonArray['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) { $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"; $multiplier = $section['Category'][1]; $jsonArray['align'] = "good"; $jsonArray['title1'] = "Streak"; $jsonArray['title2'] = "Missed"; $jsonArray['title3'] = "Top"; $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog); $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog); $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog); $todayCheck = habit_check_daily($section['Activity'], $moodlog); $weekCount = habit_count_weekly($section['Activity'], $moodlog); $jsonArray['weekSoFar'] = $weekCount; $jsonArray['weekGoal'] = $multiplier; if ($weekCount < $multiplier) { $jsonArray['buttonText'] = $weekCount; $jsonArray['buttonClass'] = 'button-yellow'; } elseif ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-done'; if($todayCheck){ $jsonArray['buttonClass'] = 'button-good'; $jsonArray['buttonText'] = ""; } } if ($todayCheck) { $jsonArray['buttonClass'] = 'button-neut'; if ($weekCount >= $multiplier) { $jsonArray['buttonClass'] = 'button-good'; } } if($weekCount == 0) $jsonArray['buttonClass'] = ''; $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; } return json_encode($jsonWordsArray); } /*** * creating calendar stuff */ // Function to get the ISO week numbers in a month function getISOWeeksInMonth($year, $month) { $firstDay = new \DateTime("$year-$month-01"); $lastDay = new \DateTime($firstDay->format('Y-m-t')); // Calculate ISO week numbers for each day $isoWeeks = []; $currentDay = clone $firstDay; while ($currentDay <= $lastDay) { $isoWeek = $currentDay->format('W'); $isoWeeks[] = $isoWeek; $currentDay->modify('+1 day'); } return array_unique($isoWeeks); } // Function to check if a date is a Sunday function isSunday($date) { return $date->format('w') == 0; } function create_new_weekly_cal(){ $calHTML = ""; // Get current date $currentDate = new \DateTime(); $currentDate->modify('this week sunday'); // Create an array to store months $months = []; // Loop through the current month to 23 months ago and store in the array for ($i = 0; $i < 23; $i++) { $year = $currentDate->format('Y'); $month = $currentDate->format('m'); // Get the ISO week numbers in the month $isoWeeksInMonth = getISOWeeksInMonth($year, $month); // Store the month data in the array $months[] = [ 'year' => $year, 'month' => $month, 'isoWeeks' => $isoWeeksInMonth, ]; // Move forward one month for the next iteration $currentDate->sub(new DateInterval('P1M')); } // Reverse the array to display the oldest month first $months = array_reverse($months); // Create the grid $calHTML .= '<div class="grid-container">'; // Loop through the months array to output the grid foreach ($months as $monthData) { $year = $monthData['year']; $month = $monthData['month']; $lastDay = date('t', strtotime("$year-$month-01")); $isoWeeksInMonth = $monthData['isoWeeks']; $calHTML .= '<div class="month">'; //echo "<h3>{$year}-{$month}</h3>"; $calHTML .= '<div class="week-container">'; // Keep the last ISO week if it's December and the last day is Sunday if ($month === '12' && isSunday(new \DateTime("$year-$month-31"))) { foreach ($isoWeeksInMonth as $isoWeek) { $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>"; } } else if( isSunday(new \DateTime("$year-$month-$lastDay")) ){ foreach ($isoWeeksInMonth as $isoWeek) { $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>"; } } else { // Exclude the last ISO week when outputting weeks $keys = array_keys($isoWeeksInMonth); $lastKey = end($keys); foreach ($isoWeeksInMonth as $isoWeekKey => $isoWeek) { if ($isoWeekKey === $lastKey) { continue; } $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>"; } } $calHTML .= '</div>'; $calHTML .= '</div>'; } $calHTML .= '</div>'; return $calHTML; } ?>