| |
---|
| | $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 > 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(); |
---|
| | |
---|
| | // Split the file content into lines |
---|
| |
---|
| | } |
---|
| | $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){ |
---|
| |
---|
| | $jsonArray['weekSoFar'] = $weekCount; |
---|
| | $jsonArray['weekGoal'] = $multiplier; |
---|
| | |
---|
| | if( $weekCount < $multiplier ) |
---|
| | if($todayCheck) |
---|
| | $jsonArray['buttonClass'] = 'button-yellow'; |
---|
| | //if($todayCheck) |
---|
| | $jsonArray['buttonText'] = $weekCount; |
---|
| | if($weekCount >= $multiplier) |
---|
| | $jsonArray['buttonClass'] = 'button-done'; |
---|
| | if( $todayCheck ) { |
---|
| |
---|
| | $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"; |
---|
| | |
---|
| |
---|
| | $weekCount = habit_count_weekly($section['Activity'], $moodlog); |
---|
| | $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; |
---|
| |
---|
| | |