Newer
Older
moodlog_web / habit.php
  1. <?php
  2.  
  3. function habit_update($word, $moodlog) {
  4. // Call habit_check_daily function
  5. $result = habit_check_daily($word, $moodlog);
  6.  
  7. // Get today's date
  8. $todayDate = date("Y-m-d");
  9.  
  10. // Read the existing mood log file
  11. $lines = file($moodlog);
  12.  
  13. // Check if habit_check_daily returns true
  14. if ($result) {
  15. // Iterate through each line in the file
  16. foreach ($lines as $key => $line) {
  17. // Extract the date from the line
  18. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match);
  19.  
  20. if (!empty($match[1])) {
  21. $lineDate = $match[1];
  22.  
  23. // Check if the line date is today
  24. if ($lineDate === $todayDate) {
  25. // Remove the word along with the '#' symbol (case-insensitive)
  26. $modifiedLine = preg_replace("/#?$word\b/i", '', $line);
  27.  
  28. // Remove the whole line if there is nothing after the number
  29. if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2} \[\d+\]\s*$/', $modifiedLine)) {
  30. unset($lines[$key]);
  31. } else {
  32. $lines[$key] = $modifiedLine;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. // Save the modified lines back to the file
  39. file_put_contents($moodlog, implode("", $lines));
  40. } else {
  41. $foundToday = false;
  42.  
  43. // Check if there is a line with today's date
  44. foreach ($lines as $key => $line) {
  45. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match);
  46.  
  47. if (!empty($match[1]) && $match[1] === $todayDate) {
  48. $foundToday = true;
  49.  
  50. // Remove the trailing newline
  51. $lines[$key] = rtrim($line);
  52.  
  53. // Append #$text to the line
  54. $lines[$key] .= " #$word";
  55.  
  56. // Re-add the newline
  57. $lines[$key] .= "\n";
  58.  
  59. break;
  60. }
  61. }
  62.  
  63. // If today's date is not found, prepend a new line
  64. if (!$foundToday) {
  65. // Prepend a new line with Y-m-d hh:mm [0] #$word
  66. $newLine = $todayDate . " " . date("H:i") . " [0] #$word\n";
  67. array_unshift($lines, $newLine);
  68. }
  69.  
  70. // Save the modified lines back to the file
  71. file_put_contents($moodlog, implode("", $lines));
  72. }
  73. }
  74.  
  75. function habit_cal_daily($filter){
  76. global $moodlog;
  77.  
  78. $fileContent = file_get_contents($moodlog);
  79. $lines = explode("\n", $fileContent);
  80. $dateOccurrences = [];
  81.  
  82. // Get the current date
  83. $currentDate = date('Y-m-d');
  84.  
  85. foreach ($lines as $line) {
  86. // Extract the date from the line
  87. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  88. $date = isset($matches[0]) ? $matches[0] : null;
  89.  
  90. // Check if the line contains the specified word as a hashtag
  91. $wordFound = (strpos($line, "#$filter") !== false);
  92.  
  93. // Populate the dateOccurrences array
  94. if ($date !== null) {
  95. // Calculate the difference in weeks
  96. $weekDifference = floor((strtotime($currentDate) - strtotime($date)) / (7 * 24 * 60 * 60));
  97.  
  98. // Check if the week is more than 24 weeks ago
  99. if ($weekDifference > 24) {
  100. break;
  101. }
  102.  
  103. if (!isset($dateOccurrences[$date])) {
  104. $dateOccurrences[$date] = 0;
  105. }
  106.  
  107. if ($wordFound) {
  108. $dateOccurrences[$date] = 1;
  109. }
  110. }
  111. }
  112.  
  113. // Remove entries with a value of 0
  114. $dateOccurrences = array_filter($dateOccurrences, function ($value) {
  115. return $value !== 0;
  116. });
  117.  
  118. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  119. $result = '{';
  120. foreach ($dateOccurrences as $date => $value) {
  121. $timestamp = strtotime($date);
  122. $result .= "\"$timestamp\":$value,";
  123. }
  124.  
  125. // Remove trailing comma
  126. $result = rtrim($result, ',');
  127.  
  128. $result .= '}';
  129.  
  130. echo $result;
  131. }
  132.  
  133. function habit_cal_weekly($filter){
  134. global $moodlog;
  135.  
  136. $fileContent = file_get_contents($moodlog);
  137. $lines = explode("\n", $fileContent);
  138. $dateOccurrences = [];
  139. $weeklyOccurrences = [];
  140.  
  141. foreach ($lines as $line) {
  142. // Extract the date from the line
  143. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  144. $date = isset($matches[0]) ? $matches[0] : null;
  145.  
  146. // Explode the line into words
  147. $words = explode(' ', $line);
  148.  
  149. // Check if the line contains the specified word as a hashtag
  150. $wordFound = false;
  151. foreach ($words as $wordInLine) {
  152. // Check if the word contains "#takeout" (case-insensitive)
  153. if (stripos($wordInLine, "#$filter") !== false) {
  154. $wordFound = true;
  155. break;
  156. }
  157. }
  158.  
  159. // Populate the dateOccurrences array
  160. if (!isset($dateOccurrences[$date])) {
  161. $dateOccurrences[$date] = 0;
  162. }
  163. if ($date !== null && $wordFound) {
  164. $dateOccurrences[$date]++;
  165. }
  166.  
  167. // Output the line whether the word is found or not
  168. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  169. }
  170.  
  171. foreach ($dateOccurrences as $date => $count) {
  172. // Convert each date to the format $year-$weekNo
  173. $dateTime = new DateTime($date);
  174. $yearWeek = $dateTime->format('Y-m-W');
  175.  
  176. // Add +1 for each date converted into $year-$weekNo as a key
  177. if (isset($weeklyOccurrences[$yearWeek])) {
  178. if( $dateOccurrences[$date] == 1)
  179. $weeklyOccurrences[$yearWeek]++;
  180. } else {
  181. if($dateOccurrences[$date] == 1){
  182. $weeklyOccurrences[$yearWeek] = 1;
  183. }else{
  184. $weeklyOccurrences[$yearWeek] = 0;
  185. }
  186. }
  187. }
  188.  
  189. // Remove entries with a value of 0
  190. $weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) {
  191. return $value !== 0;
  192. });
  193. //print_r($weeklyOccurrences);
  194.  
  195. $result = '{';
  196. foreach ($weeklyOccurrences as $date => $value) {
  197. $result .= "\"$date\":$value,";
  198. }
  199. // Remove trailing comma
  200. $result = rtrim($result, ',');
  201. $result .= '}';
  202. echo $result;
  203. }
  204.  
  205. function habit_last_10_daily($filter, $filename){
  206. // Split the file content into an array of lines
  207. $fileContent = file_get_contents($filename);
  208. $lines = explode("\n", $fileContent);
  209. $dateOccurrences = [];
  210.  
  211. // Get the current date
  212. $currentDate = date('Y-m-d');
  213.  
  214. foreach ($lines as $line) {
  215. // Extract the date from the line
  216. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  217. $date = isset($matches[0]) ? $matches[0] : null;
  218.  
  219. // Check if the line contains the specified word as a hashtag
  220. $wordFound = (strpos($line, "#$filter") !== false);
  221.  
  222. // Populate the dateOccurrences array
  223. if ($date !== null) {
  224. // Calculate the difference in weeks
  225. $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60));
  226.  
  227. // Check if the week is more than 24 weeks ago
  228. if ($dayDifference > 10) {
  229. break;
  230. }
  231.  
  232. if (!isset($dateOccurrences[$date])) {
  233. $dateOccurrences[$date] = 0;
  234. }
  235.  
  236. if ($wordFound) {
  237. $dateOccurrences[$date] = 1;
  238. }
  239. }
  240. }
  241.  
  242. // Remove entries with a value of 0
  243. //$dateOccurrences = array_filter($dateOccurrences, function ($value) {
  244. // return $value !== 0;
  245. //});
  246.  
  247. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  248. $i = 0;
  249. $dateOccurrences = array_reverse($dateOccurrences);
  250. foreach ($dateOccurrences as $date => $value) {
  251. $i++;
  252. $result["last_10_$i"] = $value;
  253. }
  254. return $result;
  255. }
  256.  
  257. function habit_last_10_weekly($filter, $filename){
  258. // Split the file content into an array of lines
  259. $fileContent = file_get_contents($filename);
  260. $lines = explode("\n", $fileContent);
  261.  
  262. $dateOccurrences = [];
  263. $weeklyOccurrences = [];
  264.  
  265. $currentDate = date('Y-m-d');
  266. $firstOccurrenceDate = null;
  267.  
  268. foreach ($lines as $line) {
  269. // Extract the date from the line
  270. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  271. $date = isset($matches[0]) ? $matches[0] : null;
  272.  
  273. // Explode the line into words
  274. $words = explode(' ', $line);
  275.  
  276. // Check if the line contains the specified word as a hashtag
  277. $wordFound = false;
  278. foreach ($words as $wordInLine) {
  279. // Check if the word contains "#takeout" (case-insensitive)
  280. if (stripos($wordInLine, "#$filter") !== false) {
  281. $wordFound = true;
  282. break;
  283. }
  284. }
  285.  
  286. // Populate the dateOccurrences array
  287. if ($date !== null) {
  288. // Calculate the difference in weeks
  289. // Check if the date is within the last 10 weeks
  290. if (strtotime($date) < strtotime('-9 weeks')) {
  291. continue;
  292. }
  293. }
  294. // Populate the dateOccurrences array
  295. if (!isset($dateOccurrences[$date])) {
  296. $dateOccurrences[$date] = 0;
  297. }
  298. if ($date !== null && $wordFound) {
  299. $dateOccurrences[$date]++;
  300. $firstOccurrenceDate = $date;
  301. }
  302.  
  303. // Output the line whether the word is found or not
  304. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  305. }
  306.  
  307. foreach ($dateOccurrences as $date => $count) {
  308. // Convert each date to the format $year-$weekNo
  309. $dateTime = new DateTime($date);
  310. $yearWeek = $dateTime->format('Y-m-W');
  311.  
  312. // Add +1 for each date converted into $year-$weekNo as a key
  313. if (isset($weeklyOccurrences[$yearWeek])) {
  314. if( $dateOccurrences[$date] == 1)
  315. $weeklyOccurrences[$yearWeek]++;
  316. } else {
  317. if($dateOccurrences[$date] == 1){
  318. $weeklyOccurrences[$yearWeek] = 1;
  319. }else{
  320. $weeklyOccurrences[$yearWeek] = 0;
  321. }
  322. }
  323. if($date < $firstOccurrenceDate)
  324. $weeklyOccurrences[$yearWeek] = "x";
  325. }
  326.  
  327. // Remove entries with a value of 0
  328. //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) {
  329. // return $value !== 0;
  330. //});
  331. //print_r($weeklyOccurrences);
  332.  
  333. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  334. $i = 0;
  335. $weeklyOccurrences = array_reverse($weeklyOccurrences);
  336. foreach ($weeklyOccurrences as $date => $value) {
  337. $i++;
  338. $result["last_10_$i"] = $value;
  339. }
  340. return $result;
  341. }
  342.  
  343. function habit_parse_file($fileContent) {
  344. $flattenedArray = array();
  345.  
  346. // Split the file content into lines
  347. $lines = explode("\n", $fileContent);
  348.  
  349. foreach ($lines as $line) {
  350. // Use regular expressions to extract relevant information
  351. preg_match('/\[([^\]]+)\]\[([^\]]+)\] (.+) - (.+)/', $line, $matches);
  352.  
  353. if (count($matches) == 5) {
  354. $category = $matches[1];
  355. $value = $matches[2];
  356. $activity = $matches[3];
  357. $description = $matches[4];
  358.  
  359. // Add to the flattened array
  360. $flattenedArray[] = array(
  361. 'Category' => $category,
  362. 'Value' => $value,
  363. 'Activity' => $activity,
  364. 'Description' => $description
  365. );
  366. }
  367. }
  368.  
  369. return $flattenedArray;
  370. }
  371.  
  372. function habit_check_daily($word, $filename) {
  373. // Split the file content into an array of lines
  374. $fileContent = file_get_contents($filename);
  375. $lines = explode("\n", $fileContent);
  376. $dateOccurrences = [];
  377.  
  378. foreach ($lines as $line) {
  379. // Extract the date from the line
  380. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  381. $date = isset($matches[0]) ? $matches[0] : null;
  382.  
  383. // Check if the line contains the specified word as a hashtag
  384. $wordFound = (strpos($line, "#$word") !== false);
  385.  
  386. // Populate the dateOccurrences array
  387. if ($date !== null) {
  388. if (!isset($dateOccurrences[$date])) {
  389. $dateOccurrences[$date] = 0;
  390. }
  391. if($wordFound){
  392. $dateOccurrences[$date] = 1;
  393. }
  394. }
  395. }
  396.  
  397. // Check streak
  398. $currentStreak = 0;
  399. $today = date('Y-m-d');
  400. //print_r($dateOccurrences);
  401.  
  402. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  403. $currentStreak++;
  404. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  405. }
  406.  
  407. if( $currentStreak >= 1){
  408. return true;
  409. }else{
  410. return false;
  411. }
  412. }
  413.  
  414. function habit_count_weekly($word, $filename) {
  415. $fileContent = file_get_contents($filename);
  416. $lines = explode("\n", $fileContent);
  417. $currentWeekNumber = (int)date("W");
  418. $wordCount = 0;
  419.  
  420. foreach ($lines as $line) {
  421. preg_match('/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/', $line, $matches);
  422.  
  423. if (!empty($matches)) {
  424. $lineDate = new DateTime($matches[1]);
  425. $lineWeekNumber = (int)$lineDate->format('W');
  426.  
  427. // Check if the line's week number is the same as the current week number
  428. if ($lineWeekNumber == $currentWeekNumber) {
  429. $wordCount += substr_count($line, "#$word");
  430. }
  431. }
  432. }
  433.  
  434. return $wordCount;
  435. }
  436.  
  437. function habit_get_stat_streak($word, $filename) {
  438. // Split the file content into an array of lines
  439. $fileContent = file_get_contents($filename);
  440. $lines = explode("\n", $fileContent);
  441. $dateOccurrences = [];
  442.  
  443. foreach ($lines as $line) {
  444. // Extract the date from the line
  445. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  446. $date = isset($matches[0]) ? $matches[0] : null;
  447.  
  448. // Check if the line contains the specified word as a hashtag
  449. $wordFound = (strpos($line, "#$word") !== false);
  450.  
  451. // Populate the dateOccurrences array
  452. if ($date !== null) {
  453. if (!isset($dateOccurrences[$date])) {
  454. $dateOccurrences[$date] = 0;
  455. }
  456. if($wordFound){
  457. $dateOccurrences[$date] = 1;
  458. }
  459. }
  460. }
  461.  
  462. // Check streak
  463. $currentStreak = 0;
  464. $today = date('Y-m-d');
  465.  
  466. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  467. $currentStreak++;
  468. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  469. }
  470.  
  471. return $currentStreak;
  472. }
  473.  
  474. function habit_get_stat_streak_week($word, $multiplier, $filename) {
  475. // Split the file content into an array of lines
  476. $fileContent = file_get_contents($filename);
  477. $lines = explode("\n", $fileContent);
  478. $dateOccurrences = [];
  479. $weeklyOccurrences = [];
  480.  
  481. foreach ($lines as $line) {
  482. // Extract the date from the line
  483. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  484. $date = isset($matches[0]) ? $matches[0] : null;
  485.  
  486. // Explode the line into words
  487. $words = explode(' ', $line);
  488.  
  489. // Check if the line contains the specified word as a hashtag
  490. $wordFound = false;
  491. foreach ($words as $wordInLine) {
  492. // Check if the word contains "#takeout" (case-insensitive)
  493. if (stripos($wordInLine, "#$word") !== false) {
  494. $wordFound = true;
  495. break;
  496. }
  497. }
  498.  
  499. // Populate the dateOccurrences array
  500. if ($date !== null && $wordFound) {
  501. if (!isset($dateOccurrences[$date])) {
  502. $dateOccurrences[$date] = 0;
  503. }
  504. $dateOccurrences[$date]++;
  505. }
  506.  
  507. // Output the line whether the word is found or not
  508. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  509. }
  510.  
  511. // Output the occurrences
  512. //print_r($dateOccurrences);
  513.  
  514. // Create weekly occurrences array
  515.  
  516. foreach ($dateOccurrences as $date => $count) {
  517. // Convert each date to the format $year-$weekNo
  518. $dateTime = new DateTime($date);
  519. $yearWeek = $dateTime->format('Y-W');
  520.  
  521. // Add +1 for each date converted into $year-$weekNo as a key
  522. if (isset($weeklyOccurrences[$yearWeek])) {
  523. $weeklyOccurrences[$yearWeek]++;
  524. } else {
  525. $weeklyOccurrences[$yearWeek] = 1;
  526. }
  527. }
  528.  
  529. $consecutiveCount = 0;
  530. $maxConsecutiveCount = 0;
  531.  
  532. ksort($weeklyOccurrences);
  533.  
  534. foreach ($weeklyOccurrences as $week => $value) {
  535. if ($value >= $multiplier) {
  536. $consecutiveCount++;
  537. $maxConsecutiveCount = max($maxConsecutiveCount, $consecutiveCount);
  538. } else {
  539. $consecutiveCount = 0; // Reset count if the value is less than 2
  540. }
  541. }
  542.  
  543. //print_r($dateOccurrences);
  544. //print_r($weeklyOccurrences);
  545.  
  546. return $consecutiveCount;
  547. }
  548.  
  549. function habit_get_stat_missed($word, $filename) {
  550. // Split the file content into an array of lines
  551. $fileContent = file_get_contents($filename);
  552. $lines = explode("\n", $fileContent);
  553.  
  554. // Set variables
  555. $missedDays = 0;
  556. $currentDate = date('Y-m-d');
  557. $wordFound = false;
  558.  
  559. // Find the minimum date in the lines
  560. $minDate = null;
  561. foreach ($lines as $line) {
  562. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  563. $date = isset($matches[0]) ? $matches[0] : null;
  564.  
  565. if ($date !== null && ($minDate === null || $date < $minDate)) {
  566. $minDate = $date;
  567. }
  568. }
  569.  
  570. // Loop for each day
  571. while (true) {
  572. // Check if the word is found on any line for the current date
  573. foreach ($lines as $line) {
  574. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  575. $date = isset($matches[0]) ? $matches[0] : null;
  576.  
  577. $wordFound = $wordFound || ($date === $currentDate && strpos($line, "#$word") !== false);
  578. }
  579.  
  580. // If the word is found or we reach the beginning of the available dates, break the loop
  581. if ($wordFound || $currentDate < $minDate) {
  582. break;
  583. }
  584.  
  585. // Move to the previous day
  586. $missedDays++;
  587. $currentDate = date('Y-m-d', strtotime($currentDate . ' - 1 day'));
  588. }
  589.  
  590. return $wordFound ? $missedDays : 0;
  591. }
  592.  
  593. function habit_get_stat_missed_week($word, $multiplier, $filename) {
  594. // Split the file content into an array of lines
  595. $fileContent = file_get_contents($filename);
  596. $lines = explode("\n", $fileContent);
  597. $dateOccurrences = [];
  598. $weeklyOccurrences = [];
  599.  
  600. foreach ($lines as $line) {
  601. // Extract the date from the line
  602. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  603. $date = isset($matches[0]) ? $matches[0] : null;
  604.  
  605. // Explode the line into words
  606. $words = explode(' ', $line);
  607.  
  608. // Check if the line contains the specified word as a hashtag
  609. $wordFound = false;
  610. foreach ($words as $wordInLine) {
  611. // Check if the word contains "#takeout" (case-insensitive)
  612. if (stripos($wordInLine, "#$word") !== false) {
  613. $wordFound = true;
  614. break;
  615. }
  616. }
  617.  
  618. // Populate the dateOccurrences array
  619. if (!isset($dateOccurrences[$date])) {
  620. $dateOccurrences[$date] = 0;
  621. }
  622. if ($date !== null && $wordFound) {
  623. $dateOccurrences[$date]++;
  624. }
  625.  
  626. // Output the line whether the word is found or not
  627. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  628. }
  629.  
  630. foreach ($dateOccurrences as $date => $count) {
  631. // Convert each date to the format $year-$weekNo
  632. $dateTime = new DateTime($date);
  633. $yearWeek = $dateTime->format('Y-W');
  634.  
  635. // Add +1 for each date converted into $year-$weekNo as a key
  636. if (isset($weeklyOccurrences[$yearWeek])) {
  637. if( $dateOccurrences[$date] == 1)
  638. $weeklyOccurrences[$yearWeek]++;
  639. } else {
  640. if($dateOccurrences[$date] == 1){
  641. $weeklyOccurrences[$yearWeek] = 1;
  642. }else{
  643. $weeklyOccurrences[$yearWeek] = 0;
  644. }
  645. }
  646. }
  647.  
  648. //print_r($dateOccurrences);
  649. //print_r($weeklyOccurrences);
  650.  
  651. $countZeros = 0;
  652.  
  653. foreach ($weeklyOccurrences as $value) {
  654. if ($value < $multiplier) {
  655. $countZeros++;
  656. } else {
  657. break; // Stop counting when a non-zero value is encountered
  658. }
  659. }
  660. return $countZeros;
  661. }
  662.  
  663. function habit_get_stat_top($word, $filename) {
  664. $fileContent = file_get_contents($filename);
  665.  
  666. // Split the file content into an array of lines
  667. $lines = explode("\n", $fileContent);
  668.  
  669. $dateOccurrences = [];
  670. $currentStreak = 0;
  671. $longestStreak = 0;
  672.  
  673. foreach ($lines as $line) {
  674. // Extract the date from the line
  675. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  676. $date = isset($matches[0]) ? $matches[0] : null;
  677.  
  678. // Check if the line contains the specified word as a hashtag
  679. $wordFound = (strpos($line, "#$word") !== false);
  680.  
  681. // Populate the dateOccurrences array
  682. if ($date !== null) {
  683. if (!isset($dateOccurrences[$date])) {
  684. $dateOccurrences[$date] = 0;
  685. }
  686. if($wordFound){
  687. $dateOccurrences[$date] = 1;
  688. }
  689. }
  690. }
  691.  
  692. foreach ($dateOccurrences as $date => $value) {
  693. if ($value == 1) {
  694. $currentStreak++;
  695. } else {
  696. $currentStreak = 0;
  697. }
  698.  
  699. // Update the longest streak if the current streak is greater
  700. $longestStreak = max($longestStreak, $currentStreak);
  701. }
  702.  
  703. //print_r($dateOccurrences);
  704. return $longestStreak;
  705. }
  706.  
  707. function habit_get_stat_top_week($word, $multiplier, $filename) {
  708. // Split the file content into an array of lines
  709. $fileContent = file_get_contents($filename);
  710. $lines = explode("\n", $fileContent);
  711. $dateOccurrences = [];
  712. $weeklyOccurrences = [];
  713.  
  714. foreach ($lines as $line) {
  715. // Extract the date from the line
  716. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  717. $date = isset($matches[0]) ? $matches[0] : null;
  718.  
  719. // Explode the line into words
  720. $words = explode(' ', $line);
  721.  
  722. // Check if the line contains the specified word as a hashtag
  723. $wordFound = false;
  724. foreach ($words as $wordInLine) {
  725. // Check if the word contains "#takeout" (case-insensitive)
  726. if (stripos($wordInLine, "#$word") !== false) {
  727. $wordFound = true;
  728. break;
  729. }
  730. }
  731.  
  732. // Populate the dateOccurrences array
  733. if (!isset($dateOccurrences[$date])) {
  734. $dateOccurrences[$date] = 0;
  735. }
  736. if ($date !== null && $wordFound) {
  737. $dateOccurrences[$date]++;
  738. }
  739.  
  740. // Output the line whether the word is found or not
  741. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  742. }
  743.  
  744. foreach ($dateOccurrences as $date => $count) {
  745. // Convert each date to the format $year-$weekNo
  746. $dateTime = new DateTime($date);
  747. $yearWeek = $dateTime->format('Y-W');
  748.  
  749. // Add +1 for each date converted into $year-$weekNo as a key
  750. if (isset($weeklyOccurrences[$yearWeek])) {
  751. if( $dateOccurrences[$date] == 1)
  752. $weeklyOccurrences[$yearWeek]++;
  753. } else {
  754. if($dateOccurrences[$date] == 1){
  755. $weeklyOccurrences[$yearWeek] = 1;
  756. }else{
  757. $weeklyOccurrences[$yearWeek] = 0;
  758. }
  759. }
  760. }
  761.  
  762. //print_r($dateOccurrences);
  763. //print_r($weeklyOccurrences);
  764.  
  765. $currentStreak = 0;
  766. $longestStreak = 0;
  767.  
  768. foreach ($weeklyOccurrences as $week => $value) {
  769. if ($value >= $multiplier) {
  770. // Increment the streak length if the value is greater than or equal to the multiplier
  771. $currentStreak++;
  772. } else {
  773. // Reset the streak length if the value is less than the multiplier
  774. $currentStreak = 0;
  775. }
  776.  
  777. // Update the longest streak if the current streak is longer
  778. $longestStreak = max($longestStreak, $currentStreak);
  779. }
  780. return $longestStreak;
  781. }
  782.  
  783. function habit_get_stat_top_missing($word, $filename) {
  784. $fileContent = file_get_contents($filename);
  785.  
  786. // Split the file content into an array of lines
  787. $lines = explode("\n", $fileContent);
  788.  
  789. // Find the earliest instance of the word
  790. $earliestDateWithWord = null;
  791. foreach ($lines as $line) {
  792. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  793. $date = isset($matches[0]) ? $matches[0] : null;
  794.  
  795. // Check if the line contains the specified word as a hashtag
  796. $wordFound = (strpos($line, "#$word") !== false);
  797.  
  798. if ($wordFound && ($earliestDateWithWord === null || $date < $earliestDateWithWord)) {
  799. $earliestDateWithWord = $date;
  800. }
  801. }
  802.  
  803. if ($earliestDateWithWord === null) {
  804. // No instance of the word found, return 0
  805. return 0;
  806. }
  807.  
  808. $currentDate = $earliestDateWithWord;
  809. $today = date('Y-m-d');
  810. $dateArray = [];
  811.  
  812. // Initialize date array with 0 values starting from the earliest date with the word
  813. while ($currentDate <= $today) {
  814. $dateArray[$currentDate] = 0;
  815. $currentDate = date('Y-m-d', strtotime($currentDate . ' + 1 day'));
  816. }
  817.  
  818. // Loop over the file and update dateArray
  819. foreach ($lines as $line) {
  820. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  821. $date = isset($matches[0]) ? $matches[0] : null;
  822.  
  823. // Check if the line contains the specified word as a hashtag
  824. $wordFound = (strpos($line, "#$word") !== false);
  825.  
  826. if ($date !== null && isset($dateArray[$date])) {
  827. // Update dateArray based on word occurrence
  828. if($wordFound)
  829. $dateArray[$date] = 1;
  830. }
  831. }
  832.  
  833. //print_r($dateArray);
  834.  
  835. // Find the longest streak of 0's in dateArray
  836. $currentStreak = 0;
  837. $longestStreak = 0;
  838.  
  839. foreach ($dateArray as $value) {
  840. if ($value === 0) {
  841. $currentStreak++;
  842. } else {
  843. $longestStreak = max($longestStreak, $currentStreak);
  844. $currentStreak = 0;
  845. }
  846. }
  847.  
  848. // Check for a streak at the end of the array
  849. $longestStreak = max($longestStreak, $currentStreak);
  850.  
  851. return $longestStreak;
  852. }
  853.  
  854. function habit_get_stat_year($word, $filename) {
  855. $fileContent = file_get_contents($filename);
  856.  
  857. // Split the file content into an array of lines
  858. $lines = explode("\n", $fileContent);
  859.  
  860. $daysWithWord = [];
  861.  
  862. foreach ($lines as $line) {
  863. // Extract the date from the line
  864. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  865. $date = isset($matches[0]) ? $matches[0] : null;
  866.  
  867. // Check if the line contains the specified word as a hashtag
  868. $wordFound = (strpos($line, "#$word") !== false);
  869.  
  870. if ($date !== null && date('Y', strtotime($date)) == date('Y')) {
  871. // Check if the date is in the current year
  872. if (!isset($daysWithWord[$date])) {
  873. $daysWithWord[$date] = $wordFound ? 1 : 0;
  874. } else {
  875. $daysWithWord[$date] += $wordFound ? 1 : 0;
  876. }
  877. }
  878. }
  879.  
  880. $daysCount = array_sum($daysWithWord);
  881.  
  882. return $daysCount;
  883. }
  884.  
  885. function habit_get_json($word, $moodlog, $habitlog){
  886. $fileContent = file_get_contents($habitlog);
  887. $resultArray = habit_parse_file($fileContent);
  888. $found = false;
  889.  
  890. foreach ($resultArray as $section) {
  891. $jsonArray = array();
  892. $jsonArray['align'] = "";
  893. $jsonArray['buttonClass'] = "";
  894. $jsonArray['buttonText'] = "";
  895. if($section['Category'][0] == "d" && $section['Activity'] == $word){
  896. $found = true;
  897. $jsonArray['freq'] = "d";
  898.  
  899. if($section['Category'][1] == "+"){
  900. $jsonArray['align'] = "good";
  901. $jsonArray['calCol'] = "#58e81b";
  902. $jsonArray['title1'] = "Streak";
  903. $jsonArray['title2'] = "Missed";
  904. $jsonArray['title3'] = "Top";
  905. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  906. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  907. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  908.  
  909. }
  910. if($section['Category'][1] == "-"){
  911. $jsonArray['align'] = "bad";
  912. $jsonArray['calCol'] = "#e81b1b";
  913. $jsonArray['title1'] = "Streak";
  914. $jsonArray['title2'] = "Total (Year)";
  915. $jsonArray['title3'] = "Top";
  916. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  917. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  918. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  919. }
  920. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  921. if ($todayCheck) {
  922. $jsonArray['buttonClass'] = 'button-'.$jsonArray['align'];
  923. }else{
  924. if ($section['Category'][1] == "-")
  925. $jsonArray['buttonClass'] = 'button-done';
  926. }
  927. break;
  928. }
  929. if($section['Category'][0] == "w" && $section['Activity'] == $word){
  930. $found = true;
  931. $jsonArray['freq'] = "w";
  932.  
  933. $multiplier = $section['Category'][1];
  934. $jsonArray['align'] = "good";
  935. $jsonArray['title1'] = "Streak";
  936. $jsonArray['title2'] = "Missed";
  937. $jsonArray['title3'] = "Top";
  938. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  939. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  940. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  941. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  942. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  943. $jsonArray['weekSoFar'] = $weekCount;
  944. $jsonArray['weekGoal'] = $multiplier;
  945.  
  946. if( $weekCount < $multiplier )
  947. $jsonArray['buttonClass'] = 'button-yellow';
  948. if($todayCheck)
  949. $jsonArray['buttonText'] = $weekCount;
  950. if($weekCount >= $multiplier)
  951. $jsonArray['buttonClass'] = 'button-done';
  952. if( $todayCheck ) {
  953. $jsonArray['buttonClass'] = 'button-neut';
  954. if($weekCount >= $multiplier){
  955. $jsonArray['buttonClass'] = 'button-good';
  956. $jsonArray['buttonText'] = "";
  957. }
  958. }
  959. break;
  960. }
  961. }
  962.  
  963. if($found)
  964. return json_encode($jsonArray);
  965. return false;
  966. }
  967.  
  968. function habit_get_json_all($moodlog, $habitlog) {
  969. $fileContent = file_get_contents($habitlog);
  970. $resultArray = habit_parse_file($fileContent);
  971.  
  972. $jsonWordsArray = array();
  973.  
  974. foreach ($resultArray as $section) {
  975. $jsonArray = array();
  976. $jsonArray['activity'] = $section['Activity'];
  977. $jsonArray['align'] = "";
  978. $jsonArray['buttonClass'] = "";
  979. $jsonArray['buttonText'] = "";
  980.  
  981. if ($section['Category'][0] == "d") {
  982. $jsonArray['freq'] = "d";
  983.  
  984. if ($section['Category'][1] == "+") {
  985. $jsonArray['align'] = "good";
  986. $jsonArray['calCol'] = "#58e81b";
  987. $jsonArray['title1'] = "Streak";
  988. $jsonArray['title2'] = "Missed";
  989. $jsonArray['title3'] = "Top";
  990. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  991. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  992. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  993. } elseif ($section['Category'][1] == "-") {
  994. $jsonArray['align'] = "bad";
  995. $jsonArray['calCol'] = "#e81b1b";
  996. $jsonArray['title1'] = "Streak";
  997. $jsonArray['title2'] = "Total (Year)";
  998. $jsonArray['title3'] = "Top";
  999. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  1000. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  1001. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  1002. }
  1003.  
  1004. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  1005. $last10 = habit_last_10_daily($section['Activity'], $moodlog);
  1006. foreach ($last10 as $key => $value) {
  1007. $jsonArray[$key] = "neut";
  1008. if ($section['Category'][1] == "+" && $value == 1)
  1009. $jsonArray[$key] = "green";
  1010. if ($section['Category'][1] == "+" && $value == 0)
  1011. $jsonArray[$key] = "red";
  1012. if ($section['Category'][1] == "-" && $value == 1)
  1013. $jsonArray[$key] = "red";
  1014. if ($section['Category'][1] == "-" && $value == 0)
  1015. $jsonArray[$key] = "green";
  1016. }
  1017.  
  1018. if ($todayCheck) {
  1019. $jsonArray['buttonClass'] = 'button-' . $jsonArray['align'];
  1020. }else{
  1021. //if ($section['Category'][1] == "+")
  1022. // $jsonArray['buttonClass'] = 'button-bad';
  1023. if ($section['Category'][1] == "-")
  1024. $jsonArray['buttonClass'] = 'button-done';
  1025. }
  1026. } elseif ($section['Category'][0] == "w") {
  1027. $jsonArray['freq'] = "w";
  1028.  
  1029. $multiplier = $section['Category'][1];
  1030. $jsonArray['align'] = "good";
  1031. $jsonArray['title1'] = "Streak";
  1032. $jsonArray['title2'] = "Missed";
  1033. $jsonArray['title3'] = "Top";
  1034. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  1035. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  1036. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  1037.  
  1038. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  1039. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  1040. $jsonArray['weekSoFar'] = $weekCount;
  1041. $jsonArray['weekGoal'] = $multiplier;
  1042.  
  1043. if ($weekCount < $multiplier) {
  1044. $jsonArray['buttonText'] = $weekCount;
  1045. $jsonArray['buttonClass'] = 'button-yellow';
  1046. } elseif ($weekCount >= $multiplier) {
  1047. $jsonArray['buttonClass'] = 'button-done';
  1048. if($todayCheck){
  1049. $jsonArray['buttonClass'] = 'button-good';
  1050. $jsonArray['buttonText'] = "";
  1051. }
  1052. }
  1053. if ($todayCheck) {
  1054. $jsonArray['buttonClass'] = 'button-neut';
  1055.  
  1056. if ($weekCount >= $multiplier) {
  1057. $jsonArray['buttonClass'] = 'button-good';
  1058. }
  1059. }
  1060.  
  1061. $last10 = habit_last_10_weekly($section['Activity'], $moodlog);
  1062. foreach ($last10 as $key => $value) {
  1063. $jsonArray[$key] = "neut";
  1064. if ($value >= $multiplier)
  1065. $jsonArray[$key] = "green";
  1066. if ($value < $multiplier)
  1067. $jsonArray[$key] = "yellow";
  1068. if ($value === "x")
  1069. $jsonArray[$key] = "neut";
  1070. if ($value === 0)
  1071. $jsonArray[$key] = "red";
  1072. }
  1073. }
  1074.  
  1075. $jsonWordsArray[] = $jsonArray;
  1076. }
  1077.  
  1078. return json_encode($jsonWordsArray);
  1079. }
  1080.  
  1081. /***
  1082. * creating calendar stuff
  1083. */
  1084. // Function to get the ISO week numbers in a month
  1085. function getISOWeeksInMonth($year, $month) {
  1086. $firstDay = new \DateTime("$year-$month-01");
  1087. $lastDay = new \DateTime($firstDay->format('Y-m-t'));
  1088.  
  1089. // Calculate ISO week numbers for each day
  1090. $isoWeeks = [];
  1091. $currentDay = clone $firstDay;
  1092.  
  1093. while ($currentDay <= $lastDay) {
  1094. $isoWeek = $currentDay->format('W');
  1095. $isoWeeks[] = $isoWeek;
  1096. $currentDay->modify('+1 day');
  1097. }
  1098.  
  1099. return array_unique($isoWeeks);
  1100. }
  1101.  
  1102. // Function to check if a date is a Sunday
  1103. function isSunday($date) {
  1104. return $date->format('w') == 0;
  1105. }
  1106.  
  1107. function create_new_weekly_cal(){
  1108. $calHTML = "";
  1109. // Get current date
  1110. $currentDate = new \DateTime();
  1111. // Create an array to store months
  1112. $months = [];
  1113.  
  1114. // Loop through the current month to 23 months ago and store in the array
  1115. for ($i = 0; $i < 23; $i++) {
  1116. $year = $currentDate->format('Y');
  1117. $month = $currentDate->format('m');
  1118.  
  1119. // Get the ISO week numbers in the month
  1120. $isoWeeksInMonth = getISOWeeksInMonth($year, $month);
  1121.  
  1122. // Store the month data in the array
  1123. $months[] = [
  1124. 'year' => $year,
  1125. 'month' => $month,
  1126. 'isoWeeks' => $isoWeeksInMonth,
  1127. ];
  1128.  
  1129. // Move forward one month for the next iteration
  1130. $currentDate->sub(new DateInterval('P1M'));
  1131. }
  1132.  
  1133. // Reverse the array to display the oldest month first
  1134. $months = array_reverse($months);
  1135.  
  1136. // Create the grid
  1137. $calHTML .= '<div class="grid-container">';
  1138.  
  1139. // Loop through the months array to output the grid
  1140. foreach ($months as $monthData) {
  1141. $year = $monthData['year'];
  1142. $month = $monthData['month'];
  1143. $isoWeeksInMonth = $monthData['isoWeeks'];
  1144.  
  1145. $calHTML .= '<div class="month">';
  1146. //echo "<h3>{$year}-{$month}</h3>";
  1147. $calHTML .= '<div class="week-container">';
  1148. // Keep the last ISO week if it's December and the last day is Sunday
  1149. if ($month === '12' && isSunday(new \DateTime("$year-$month-31"))) {
  1150. foreach ($isoWeeksInMonth as $isoWeek) {
  1151. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  1152. }
  1153. } else {
  1154. // Exclude the last ISO week when outputting weeks
  1155. $keys = array_keys($isoWeeksInMonth);
  1156. $lastKey = end($keys);
  1157. foreach ($isoWeeksInMonth as $isoWeekKey => $isoWeek) {
  1158. if ($isoWeekKey === $lastKey) {
  1159. continue;
  1160. }
  1161. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  1162. }
  1163. }
  1164.  
  1165. $calHTML .= '</div>';
  1166. $calHTML .= '</div>';
  1167. }
  1168.  
  1169. $calHTML .= '</div>';
  1170. return $calHTML;
  1171. }
  1172.  
  1173.  
  1174. ?>
Buy Me A Coffee