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_parse_file($fileContent) {
  206. $flattenedArray = array();
  207.  
  208. // Split the file content into lines
  209. $lines = explode("\n", $fileContent);
  210.  
  211. foreach ($lines as $line) {
  212. // Use regular expressions to extract relevant information
  213. preg_match('/\[([^\]]+)\]\[([^\]]+)\] (.+) - (.+)/', $line, $matches);
  214.  
  215. if (count($matches) == 5) {
  216. $category = $matches[1];
  217. $value = $matches[2];
  218. $activity = $matches[3];
  219. $description = $matches[4];
  220.  
  221. // Add to the flattened array
  222. $flattenedArray[] = array(
  223. 'Category' => $category,
  224. 'Value' => $value,
  225. 'Activity' => $activity,
  226. 'Description' => $description
  227. );
  228. }
  229. }
  230.  
  231. return $flattenedArray;
  232. }
  233.  
  234. function habit_check_daily($word, $filename) {
  235. // Split the file content into an array of lines
  236. $fileContent = file_get_contents($filename);
  237. $lines = explode("\n", $fileContent);
  238. $dateOccurrences = [];
  239.  
  240. foreach ($lines as $line) {
  241. // Extract the date from the line
  242. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  243. $date = isset($matches[0]) ? $matches[0] : null;
  244.  
  245. // Check if the line contains the specified word as a hashtag
  246. $wordFound = (strpos($line, "#$word") !== false);
  247.  
  248. // Populate the dateOccurrences array
  249. if ($date !== null) {
  250. if (!isset($dateOccurrences[$date])) {
  251. $dateOccurrences[$date] = 0;
  252. }
  253. if($wordFound){
  254. $dateOccurrences[$date] = 1;
  255. }
  256. }
  257. }
  258.  
  259. // Check streak
  260. $currentStreak = 0;
  261. $today = date('Y-m-d');
  262. //print_r($dateOccurrences);
  263.  
  264. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  265. $currentStreak++;
  266. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  267. }
  268.  
  269. if( $currentStreak >= 1){
  270. return true;
  271. }else{
  272. return false;
  273. }
  274. }
  275.  
  276. function habit_count_weekly($word, $filename) {
  277. $fileContent = file_get_contents($filename);
  278. $lines = explode("\n", $fileContent);
  279. $currentWeekNumber = (int)date("W");
  280. $wordCount = 0;
  281.  
  282. foreach ($lines as $line) {
  283. preg_match('/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/', $line, $matches);
  284.  
  285. if (!empty($matches)) {
  286. $lineDate = new DateTime($matches[1]);
  287. $lineWeekNumber = (int)$lineDate->format('W');
  288.  
  289. // Check if the line's week number is the same as the current week number
  290. if ($lineWeekNumber == $currentWeekNumber) {
  291. $wordCount += substr_count($line, "#$word");
  292. }
  293. }
  294. }
  295.  
  296. return $wordCount;
  297. }
  298.  
  299. function habit_get_stat_streak($word, $filename) {
  300. // Split the file content into an array of lines
  301. $fileContent = file_get_contents($filename);
  302. $lines = explode("\n", $fileContent);
  303. $dateOccurrences = [];
  304.  
  305. foreach ($lines as $line) {
  306. // Extract the date from the line
  307. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  308. $date = isset($matches[0]) ? $matches[0] : null;
  309.  
  310. // Check if the line contains the specified word as a hashtag
  311. $wordFound = (strpos($line, "#$word") !== false);
  312.  
  313. // Populate the dateOccurrences array
  314. if ($date !== null) {
  315. if (!isset($dateOccurrences[$date])) {
  316. $dateOccurrences[$date] = 0;
  317. }
  318. if($wordFound){
  319. $dateOccurrences[$date] = 1;
  320. }
  321. }
  322. }
  323.  
  324. // Check streak
  325. $currentStreak = 0;
  326. $today = date('Y-m-d');
  327.  
  328. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  329. $currentStreak++;
  330. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  331. }
  332.  
  333. return $currentStreak;
  334. }
  335.  
  336. function habit_get_stat_streak_week($word, $multiplier, $filename) {
  337. // Split the file content into an array of lines
  338. $fileContent = file_get_contents($filename);
  339. $lines = explode("\n", $fileContent);
  340. $dateOccurrences = [];
  341. $weeklyOccurrences = [];
  342.  
  343. foreach ($lines as $line) {
  344. // Extract the date from the line
  345. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  346. $date = isset($matches[0]) ? $matches[0] : null;
  347.  
  348. // Explode the line into words
  349. $words = explode(' ', $line);
  350.  
  351. // Check if the line contains the specified word as a hashtag
  352. $wordFound = false;
  353. foreach ($words as $wordInLine) {
  354. // Check if the word contains "#takeout" (case-insensitive)
  355. if (stripos($wordInLine, "#$word") !== false) {
  356. $wordFound = true;
  357. break;
  358. }
  359. }
  360.  
  361. // Populate the dateOccurrences array
  362. if ($date !== null && $wordFound) {
  363. if (!isset($dateOccurrences[$date])) {
  364. $dateOccurrences[$date] = 0;
  365. }
  366. $dateOccurrences[$date]++;
  367. }
  368.  
  369. // Output the line whether the word is found or not
  370. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  371. }
  372.  
  373. // Output the occurrences
  374. //print_r($dateOccurrences);
  375.  
  376. // Create weekly occurrences array
  377.  
  378. foreach ($dateOccurrences as $date => $count) {
  379. // Convert each date to the format $year-$weekNo
  380. $dateTime = new DateTime($date);
  381. $yearWeek = $dateTime->format('Y-W');
  382.  
  383. // Add +1 for each date converted into $year-$weekNo as a key
  384. if (isset($weeklyOccurrences[$yearWeek])) {
  385. $weeklyOccurrences[$yearWeek]++;
  386. } else {
  387. $weeklyOccurrences[$yearWeek] = 1;
  388. }
  389. }
  390.  
  391. $consecutiveCount = 0;
  392. $maxConsecutiveCount = 0;
  393.  
  394. ksort($weeklyOccurrences);
  395.  
  396. foreach ($weeklyOccurrences as $week => $value) {
  397. if ($value >= 2) {
  398. $consecutiveCount++;
  399. $maxConsecutiveCount = max($maxConsecutiveCount, $consecutiveCount);
  400. } else {
  401. $consecutiveCount = 0; // Reset count if the value is less than 2
  402. }
  403. }
  404.  
  405. //print_r($dateOccurrences);
  406. //print_r($weeklyOccurrences);
  407.  
  408. return $consecutiveCount;
  409. }
  410.  
  411. function habit_get_stat_missed($word, $filename) {
  412. // Split the file content into an array of lines
  413. $fileContent = file_get_contents($filename);
  414. $lines = explode("\n", $fileContent);
  415.  
  416. // Set variables
  417. $missedDays = 0;
  418. $currentDate = date('Y-m-d');
  419. $wordFound = false;
  420.  
  421. // Find the minimum date in the lines
  422. $minDate = null;
  423. foreach ($lines as $line) {
  424. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  425. $date = isset($matches[0]) ? $matches[0] : null;
  426.  
  427. if ($date !== null && ($minDate === null || $date < $minDate)) {
  428. $minDate = $date;
  429. }
  430. }
  431.  
  432. // Loop for each day
  433. while (true) {
  434. // Check if the word is found on any line for the current date
  435. foreach ($lines as $line) {
  436. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  437. $date = isset($matches[0]) ? $matches[0] : null;
  438.  
  439. $wordFound = $wordFound || ($date === $currentDate && strpos($line, "#$word") !== false);
  440. }
  441.  
  442. // If the word is found or we reach the beginning of the available dates, break the loop
  443. if ($wordFound || $currentDate < $minDate) {
  444. break;
  445. }
  446.  
  447. // Move to the previous day
  448. $missedDays++;
  449. $currentDate = date('Y-m-d', strtotime($currentDate . ' - 1 day'));
  450. }
  451.  
  452. return $wordFound ? $missedDays : 0;
  453. }
  454.  
  455. function habit_get_stat_missed_week($word, $multiplier, $filename) {
  456. // Split the file content into an array of lines
  457. $fileContent = file_get_contents($filename);
  458. $lines = explode("\n", $fileContent);
  459. $dateOccurrences = [];
  460. $weeklyOccurrences = [];
  461.  
  462. foreach ($lines as $line) {
  463. // Extract the date from the line
  464. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  465. $date = isset($matches[0]) ? $matches[0] : null;
  466.  
  467. // Explode the line into words
  468. $words = explode(' ', $line);
  469.  
  470. // Check if the line contains the specified word as a hashtag
  471. $wordFound = false;
  472. foreach ($words as $wordInLine) {
  473. // Check if the word contains "#takeout" (case-insensitive)
  474. if (stripos($wordInLine, "#$word") !== false) {
  475. $wordFound = true;
  476. break;
  477. }
  478. }
  479.  
  480. // Populate the dateOccurrences array
  481. if (!isset($dateOccurrences[$date])) {
  482. $dateOccurrences[$date] = 0;
  483. }
  484. if ($date !== null && $wordFound) {
  485. $dateOccurrences[$date]++;
  486. }
  487.  
  488. // Output the line whether the word is found or not
  489. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  490. }
  491.  
  492. foreach ($dateOccurrences as $date => $count) {
  493. // Convert each date to the format $year-$weekNo
  494. $dateTime = new DateTime($date);
  495. $yearWeek = $dateTime->format('Y-W');
  496.  
  497. // Add +1 for each date converted into $year-$weekNo as a key
  498. if (isset($weeklyOccurrences[$yearWeek])) {
  499. if( $dateOccurrences[$date] == 1)
  500. $weeklyOccurrences[$yearWeek]++;
  501. } else {
  502. if($dateOccurrences[$date] == 1){
  503. $weeklyOccurrences[$yearWeek] = 1;
  504. }else{
  505. $weeklyOccurrences[$yearWeek] = 0;
  506. }
  507. }
  508. }
  509.  
  510. //print_r($dateOccurrences);
  511. //print_r($weeklyOccurrences);
  512.  
  513. $countZeros = 0;
  514.  
  515. foreach ($weeklyOccurrences as $value) {
  516. if ($value < $multiplier) {
  517. $countZeros++;
  518. } else {
  519. break; // Stop counting when a non-zero value is encountered
  520. }
  521. }
  522. return $countZeros;
  523. }
  524.  
  525. function habit_get_stat_top($word, $filename) {
  526. $fileContent = file_get_contents($filename);
  527.  
  528. // Split the file content into an array of lines
  529. $lines = explode("\n", $fileContent);
  530.  
  531. $dateOccurrences = [];
  532. $currentStreak = 0;
  533. $longestStreak = 0;
  534.  
  535. foreach ($lines as $line) {
  536. // Extract the date from the line
  537. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  538. $date = isset($matches[0]) ? $matches[0] : null;
  539.  
  540. // Check if the line contains the specified word as a hashtag
  541. $wordFound = (strpos($line, "#$word") !== false);
  542.  
  543. // Populate the dateOccurrences array
  544. if ($date !== null) {
  545. if (!isset($dateOccurrences[$date])) {
  546. $dateOccurrences[$date] = 0;
  547. }
  548. if($wordFound){
  549. $dateOccurrences[$date] = 1;
  550. }
  551. }
  552. }
  553.  
  554. foreach ($dateOccurrences as $date => $value) {
  555. if ($value == 1) {
  556. $currentStreak++;
  557. } else {
  558. $currentStreak = 0;
  559. }
  560.  
  561. // Update the longest streak if the current streak is greater
  562. $longestStreak = max($longestStreak, $currentStreak);
  563. }
  564.  
  565. //print_r($dateOccurrences);
  566. return $longestStreak;
  567. }
  568.  
  569. function habit_get_stat_top_week($word, $multiplier, $filename) {
  570. // Split the file content into an array of lines
  571. $fileContent = file_get_contents($filename);
  572. $lines = explode("\n", $fileContent);
  573. $dateOccurrences = [];
  574. $weeklyOccurrences = [];
  575.  
  576. foreach ($lines as $line) {
  577. // Extract the date from the line
  578. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  579. $date = isset($matches[0]) ? $matches[0] : null;
  580.  
  581. // Explode the line into words
  582. $words = explode(' ', $line);
  583.  
  584. // Check if the line contains the specified word as a hashtag
  585. $wordFound = false;
  586. foreach ($words as $wordInLine) {
  587. // Check if the word contains "#takeout" (case-insensitive)
  588. if (stripos($wordInLine, "#$word") !== false) {
  589. $wordFound = true;
  590. break;
  591. }
  592. }
  593.  
  594. // Populate the dateOccurrences array
  595. if (!isset($dateOccurrences[$date])) {
  596. $dateOccurrences[$date] = 0;
  597. }
  598. if ($date !== null && $wordFound) {
  599. $dateOccurrences[$date]++;
  600. }
  601.  
  602. // Output the line whether the word is found or not
  603. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  604. }
  605.  
  606. foreach ($dateOccurrences as $date => $count) {
  607. // Convert each date to the format $year-$weekNo
  608. $dateTime = new DateTime($date);
  609. $yearWeek = $dateTime->format('Y-W');
  610.  
  611. // Add +1 for each date converted into $year-$weekNo as a key
  612. if (isset($weeklyOccurrences[$yearWeek])) {
  613. if( $dateOccurrences[$date] == 1)
  614. $weeklyOccurrences[$yearWeek]++;
  615. } else {
  616. if($dateOccurrences[$date] == 1){
  617. $weeklyOccurrences[$yearWeek] = 1;
  618. }else{
  619. $weeklyOccurrences[$yearWeek] = 0;
  620. }
  621. }
  622. }
  623.  
  624. //print_r($dateOccurrences);
  625. //print_r($weeklyOccurrences);
  626.  
  627. $currentStreak = 0;
  628. $longestStreak = 0;
  629.  
  630. foreach ($weeklyOccurrences as $week => $value) {
  631. if ($value >= $multiplier) {
  632. // Increment the streak length if the value is greater than or equal to the multiplier
  633. $currentStreak++;
  634. } else {
  635. // Reset the streak length if the value is less than the multiplier
  636. $currentStreak = 0;
  637. }
  638.  
  639. // Update the longest streak if the current streak is longer
  640. $longestStreak = max($longestStreak, $currentStreak);
  641. }
  642. return $longestStreak;
  643. }
  644.  
  645. function habit_get_stat_top_missing($word, $filename) {
  646. $fileContent = file_get_contents($filename);
  647.  
  648. // Split the file content into an array of lines
  649. $lines = explode("\n", $fileContent);
  650.  
  651. // Find the earliest instance of the word
  652. $earliestDateWithWord = null;
  653. foreach ($lines as $line) {
  654. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  655. $date = isset($matches[0]) ? $matches[0] : null;
  656.  
  657. // Check if the line contains the specified word as a hashtag
  658. $wordFound = (strpos($line, "#$word") !== false);
  659.  
  660. if ($wordFound && ($earliestDateWithWord === null || $date < $earliestDateWithWord)) {
  661. $earliestDateWithWord = $date;
  662. }
  663. }
  664.  
  665. if ($earliestDateWithWord === null) {
  666. // No instance of the word found, return 0
  667. return 0;
  668. }
  669.  
  670. $currentDate = $earliestDateWithWord;
  671. $today = date('Y-m-d');
  672. $dateArray = [];
  673.  
  674. // Initialize date array with 0 values starting from the earliest date with the word
  675. while ($currentDate <= $today) {
  676. $dateArray[$currentDate] = 0;
  677. $currentDate = date('Y-m-d', strtotime($currentDate . ' + 1 day'));
  678. }
  679.  
  680. // Loop over the file and update dateArray
  681. foreach ($lines as $line) {
  682. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  683. $date = isset($matches[0]) ? $matches[0] : null;
  684.  
  685. // Check if the line contains the specified word as a hashtag
  686. $wordFound = (strpos($line, "#$word") !== false);
  687.  
  688. if ($date !== null && isset($dateArray[$date])) {
  689. // Update dateArray based on word occurrence
  690. if($wordFound)
  691. $dateArray[$date] = 1;
  692. }
  693. }
  694.  
  695. //print_r($dateArray);
  696.  
  697. // Find the longest streak of 0's in dateArray
  698. $currentStreak = 0;
  699. $longestStreak = 0;
  700.  
  701. foreach ($dateArray as $value) {
  702. if ($value === 0) {
  703. $currentStreak++;
  704. } else {
  705. $longestStreak = max($longestStreak, $currentStreak);
  706. $currentStreak = 0;
  707. }
  708. }
  709.  
  710. // Check for a streak at the end of the array
  711. $longestStreak = max($longestStreak, $currentStreak);
  712.  
  713. return $longestStreak;
  714. }
  715.  
  716. function habit_get_stat_year($word, $filename) {
  717. $fileContent = file_get_contents($filename);
  718.  
  719. // Split the file content into an array of lines
  720. $lines = explode("\n", $fileContent);
  721.  
  722. $daysWithWord = [];
  723.  
  724. foreach ($lines as $line) {
  725. // Extract the date from the line
  726. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  727. $date = isset($matches[0]) ? $matches[0] : null;
  728.  
  729. // Check if the line contains the specified word as a hashtag
  730. $wordFound = (strpos($line, "#$word") !== false);
  731.  
  732. if ($date !== null && date('Y', strtotime($date)) == date('Y')) {
  733. // Check if the date is in the current year
  734. if (!isset($daysWithWord[$date])) {
  735. $daysWithWord[$date] = $wordFound ? 1 : 0;
  736. } else {
  737. $daysWithWord[$date] += $wordFound ? 1 : 0;
  738. }
  739. }
  740. }
  741.  
  742. $daysCount = array_sum($daysWithWord);
  743.  
  744. return $daysCount;
  745. }
  746.  
  747. function habit_get_json($word, $moodlog, $habitlog){
  748. $fileContent = file_get_contents($habitlog);
  749. $resultArray = habit_parse_file($fileContent);
  750. $found = false;
  751.  
  752. foreach ($resultArray as $section) {
  753. $jsonArray = array();
  754. $jsonArray['align'] = "";
  755. $jsonArray['buttonClass'] = "";
  756. $jsonArray['buttonText'] = "";
  757. if($section['Category'][0] == "d" && $section['Activity'] == $word){
  758. $found = true;
  759. $jsonArray['freq'] = "d";
  760.  
  761. if($section['Category'][1] == "+"){
  762. $jsonArray['align'] = "good";
  763. $jsonArray['calCol'] = "#58e81b";
  764. $jsonArray['title1'] = "Streak";
  765. $jsonArray['title2'] = "Missed";
  766. $jsonArray['title3'] = "Top";
  767. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  768. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  769. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  770.  
  771. }
  772. if($section['Category'][1] == "-"){
  773. $jsonArray['align'] = "bad";
  774. $jsonArray['calCol'] = "#e81b1b";
  775. $jsonArray['title1'] = "Streak";
  776. $jsonArray['title2'] = "Total (Year)";
  777. $jsonArray['title3'] = "Top";
  778. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  779. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  780. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  781. }
  782. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  783. if ($todayCheck) {
  784. $jsonArray['buttonClass'] = 'button-'.$jsonArray['align'];
  785. }
  786. break;
  787. }
  788. if($section['Category'][0] == "w" && $section['Activity'] == $word){
  789. $found = true;
  790. $jsonArray['freq'] = "w";
  791.  
  792. $multiplier = $section['Category'][1];
  793. $jsonArray['align'] = "good";
  794. $jsonArray['title1'] = "Streak";
  795. $jsonArray['title2'] = "Missed";
  796. $jsonArray['title3'] = "Top";
  797. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  798. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  799. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  800. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  801. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  802. $jsonArray['weekSoFar'] = $weekCount;
  803. $jsonArray['weekGoal'] = $multiplier;
  804.  
  805. if( $weekCount < $multiplier )
  806. if($todayCheck)
  807. $jsonArray['buttonText'] = $weekCount;
  808. if($weekCount >= $multiplier)
  809. $jsonArray['buttonClass'] = 'button-done';
  810. if( $todayCheck ) {
  811. $jsonArray['buttonClass'] = 'button-neut';
  812. if($weekCount >= $multiplier)
  813. $jsonArray['buttonClass'] = 'button-good';
  814. }
  815. break;
  816. }
  817. }
  818.  
  819. if($found)
  820. return json_encode($jsonArray);
  821. return false;
  822. }
  823.  
  824. function habit_get_json_all($moodlog, $habitlog) {
  825. $fileContent = file_get_contents($habitlog);
  826. $resultArray = habit_parse_file($fileContent);
  827.  
  828. $jsonWordsArray = array();
  829.  
  830. foreach ($resultArray as $section) {
  831. $jsonArray = array();
  832. $jsonArray['activity'] = $section['Activity'];
  833. $jsonArray['align'] = "";
  834. $jsonArray['buttonClass'] = "";
  835. $jsonArray['buttonText'] = "";
  836.  
  837. if ($section['Category'][0] == "d") {
  838. $jsonArray['freq'] = "d";
  839.  
  840. if ($section['Category'][1] == "+") {
  841. $jsonArray['align'] = "good";
  842. $jsonArray['calCol'] = "#58e81b";
  843. $jsonArray['title1'] = "Streak";
  844. $jsonArray['title2'] = "Missed";
  845. $jsonArray['title3'] = "Top";
  846. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  847. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  848. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  849. } elseif ($section['Category'][1] == "-") {
  850. $jsonArray['align'] = "bad";
  851. $jsonArray['calCol'] = "#e81b1b";
  852. $jsonArray['title1'] = "Streak";
  853. $jsonArray['title2'] = "Total (Year)";
  854. $jsonArray['title3'] = "Top";
  855. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  856. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  857. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  858. }
  859.  
  860. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  861.  
  862. if ($todayCheck) {
  863. $jsonArray['buttonClass'] = 'button-' . $jsonArray['align'];
  864. }
  865. } elseif ($section['Category'][0] == "w") {
  866. $jsonArray['freq'] = "w";
  867.  
  868. $multiplier = $section['Category'][1];
  869. $jsonArray['align'] = "good";
  870. $jsonArray['title1'] = "Streak";
  871. $jsonArray['title2'] = "Missed";
  872. $jsonArray['title3'] = "Top";
  873. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  874. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  875. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  876.  
  877. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  878. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  879. $jsonArray['weekSoFar'] = $weekCount;
  880. $jsonArray['weekGoal'] = $multiplier;
  881.  
  882. if ($weekCount < $multiplier && $todayCheck) {
  883. $jsonArray['buttonText'] = $weekCount;
  884. }
  885.  
  886. if ($weekCount >= $multiplier) {
  887. $jsonArray['buttonClass'] = 'button-done';
  888. }
  889.  
  890. if ($todayCheck) {
  891. $jsonArray['buttonClass'] = 'button-neut';
  892.  
  893. if ($weekCount >= $multiplier) {
  894. $jsonArray['buttonClass'] = 'button-good';
  895. }
  896. }
  897. }
  898.  
  899. $jsonWordsArray[] = $jsonArray;
  900. }
  901.  
  902. return json_encode($jsonWordsArray);
  903. }
  904.  
  905. /***
  906. * creating calendar stuff
  907. */
  908. // Function to get the ISO week numbers in a month
  909. function getISOWeeksInMonth($year, $month) {
  910. $firstDay = new \DateTime("$year-$month-01");
  911. $lastDay = new \DateTime($firstDay->format('Y-m-t'));
  912.  
  913. // Calculate ISO week numbers for each day
  914. $isoWeeks = [];
  915. $currentDay = clone $firstDay;
  916.  
  917. while ($currentDay <= $lastDay) {
  918. $isoWeek = $currentDay->format('W');
  919. $isoWeeks[] = $isoWeek;
  920. $currentDay->modify('+1 day');
  921. }
  922.  
  923. return array_unique($isoWeeks);
  924. }
  925.  
  926. // Function to check if a date is a Sunday
  927. function isSunday($date) {
  928. return $date->format('w') == 0;
  929. }
  930.  
  931. function create_new_weekly_cal(){
  932. $calHTML = "";
  933. // Get current date
  934. $currentDate = new \DateTime();
  935. // Create an array to store months
  936. $months = [];
  937.  
  938. // Loop through the current month to 23 months ago and store in the array
  939. for ($i = 0; $i < 23; $i++) {
  940. $year = $currentDate->format('Y');
  941. $month = $currentDate->format('m');
  942.  
  943. // Get the ISO week numbers in the month
  944. $isoWeeksInMonth = getISOWeeksInMonth($year, $month);
  945.  
  946. // Store the month data in the array
  947. $months[] = [
  948. 'year' => $year,
  949. 'month' => $month,
  950. 'isoWeeks' => $isoWeeksInMonth,
  951. ];
  952.  
  953. // Move forward one month for the next iteration
  954. $currentDate->sub(new DateInterval('P1M'));
  955. }
  956.  
  957. // Reverse the array to display the oldest month first
  958. $months = array_reverse($months);
  959.  
  960. // Create the grid
  961. $calHTML .= '<div class="grid-container">';
  962.  
  963. // Loop through the months array to output the grid
  964. foreach ($months as $monthData) {
  965. $year = $monthData['year'];
  966. $month = $monthData['month'];
  967. $isoWeeksInMonth = $monthData['isoWeeks'];
  968.  
  969. $calHTML .= '<div class="month">';
  970. //echo "<h3>{$year}-{$month}</h3>";
  971. $calHTML .= '<div class="week-container">';
  972. // Keep the last ISO week if it's December and the last day is Sunday
  973. if ($month === '12' && isSunday(new \DateTime("$year-$month-31"))) {
  974. foreach ($isoWeeksInMonth as $isoWeek) {
  975. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  976. }
  977. } else {
  978. // Exclude the last ISO week when outputting weeks
  979. $keys = array_keys($isoWeeksInMonth);
  980. $lastKey = end($keys);
  981. foreach ($isoWeeksInMonth as $isoWeekKey => $isoWeek) {
  982. if ($isoWeekKey === $lastKey) {
  983. continue;
  984. }
  985. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  986. }
  987. }
  988.  
  989. $calHTML .= '</div>';
  990. $calHTML .= '</div>';
  991. }
  992.  
  993. $calHTML .= '</div>';
  994. return $calHTML;
  995. }
  996.  
  997.  
  998. ?>
Buy Me A Coffee