ui updates, more stats, logic fixes
1 parent e56a1f0 commit dfa969bfdd8bfa533b8f57cb04649c2dc2037739
0xRoM authored on 14 Jan 2024
Showing 4 changed files
View
195
habit.php
$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;
View
77
habit_ui.php
$result2 = habit_get_stat_year($section['Activity'], $moodlog);
$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;
}
}
$result3 = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
 
$todayCheck = habit_check_daily($section['Activity'], $moodlog);
$weekCount = habit_count_weekly($section['Activity'], $moodlog);
 
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';
}
$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 < $multiplier) {
$buttText = $weekCount;
$buttonClass = 'button-yellow';
} elseif ($weekCount >= $multiplier) {
$buttonClass = 'button-done';
} elseif ($todayCheck) {
$buttonClass = 'button-neut';
 
if ($weekCount >= $multiplier) {
$buttonClass = 'button-good';
}
}
 
}
<div class="habit-score"><div class="score-title">'.$title1.'</div><div class="score-result result1">'.$result1.'</div></div>
<div class="habit-score"><div class="score-title">'.$title2.'</div><div class="score-result result2">'.$result2.'</div></div>
<div class="habit-score"><div class="score-title">'.$title3.'</div><div class="score-result result3">'.$result3.'</div></div>
</div>
<div class="habit_long_container">
<div class="long_tick lt'.$last10_conv['last_10_1'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_2'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_3'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_4'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_5'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_6'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_7'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_8'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_9'].'"></div>
<div class="long_tick lt'.$last10_conv['last_10_10'].'"></div>
</div>
 
</div>
';
 
View
4
habit_ui_script.js
// Check if the element exists
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) {
element.classList.add("green");
View
58
habit_ui_style.css
width: 100%;
}
 
.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;
font-size: 2em;
font-size: 1.95em;
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;}
.bad{ color: #e81b1b; }
font-family: 'Source Code Pro', monospace;
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;
font-size: 1.6em;
border: 0px solid #fff;
box-sizing: border-box;
border-radius: 2px;
}
 
.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;
 
}
Buy Me A Coffee