Newer
Older
moodlog_web / habit.php
0xRoM on 6 Jan 2024 32 KB ui improvements
  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. $streak = 0;
  392. $thisWeek = date('Y-W'); // Get the current week
  393.  
  394. while (isset($weeklyOccurrences[$thisWeek]) && $weeklyOccurrences[$thisWeek] >= $multiplier) {
  395. $streak++;
  396. $thisWeek = date('Y-W', strtotime($thisWeek . ' -1 week'));
  397. }
  398.  
  399. //print_r($dateOccurrences);
  400. //print_r($weeklyOccurrences);
  401.  
  402. return $streak;
  403. }
  404.  
  405. function habit_get_stat_missed($word, $filename) {
  406. // Split the file content into an array of lines
  407. $fileContent = file_get_contents($filename);
  408. $lines = explode("\n", $fileContent);
  409.  
  410. // Set variables
  411. $missedDays = 0;
  412. $currentDate = date('Y-m-d');
  413. $wordFound = false;
  414.  
  415. // Find the minimum date in the lines
  416. $minDate = null;
  417. foreach ($lines as $line) {
  418. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  419. $date = isset($matches[0]) ? $matches[0] : null;
  420.  
  421. if ($date !== null && ($minDate === null || $date < $minDate)) {
  422. $minDate = $date;
  423. }
  424. }
  425.  
  426. // Loop for each day
  427. while (true) {
  428. // Check if the word is found on any line for the current date
  429. foreach ($lines as $line) {
  430. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  431. $date = isset($matches[0]) ? $matches[0] : null;
  432.  
  433. $wordFound = $wordFound || ($date === $currentDate && strpos($line, "#$word") !== false);
  434. }
  435.  
  436. // If the word is found or we reach the beginning of the available dates, break the loop
  437. if ($wordFound || $currentDate < $minDate) {
  438. break;
  439. }
  440.  
  441. // Move to the previous day
  442. $missedDays++;
  443. $currentDate = date('Y-m-d', strtotime($currentDate . ' - 1 day'));
  444. }
  445.  
  446. return $wordFound ? $missedDays : 0;
  447. }
  448.  
  449. function habit_get_stat_missed_week($word, $multiplier, $filename) {
  450. // Split the file content into an array of lines
  451. $fileContent = file_get_contents($filename);
  452. $lines = explode("\n", $fileContent);
  453. $dateOccurrences = [];
  454. $weeklyOccurrences = [];
  455.  
  456. foreach ($lines as $line) {
  457. // Extract the date from the line
  458. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  459. $date = isset($matches[0]) ? $matches[0] : null;
  460.  
  461. // Explode the line into words
  462. $words = explode(' ', $line);
  463.  
  464. // Check if the line contains the specified word as a hashtag
  465. $wordFound = false;
  466. foreach ($words as $wordInLine) {
  467. // Check if the word contains "#takeout" (case-insensitive)
  468. if (stripos($wordInLine, "#$word") !== false) {
  469. $wordFound = true;
  470. break;
  471. }
  472. }
  473.  
  474. // Populate the dateOccurrences array
  475. if (!isset($dateOccurrences[$date])) {
  476. $dateOccurrences[$date] = 0;
  477. }
  478. if ($date !== null && $wordFound) {
  479. $dateOccurrences[$date]++;
  480. }
  481.  
  482. // Output the line whether the word is found or not
  483. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  484. }
  485.  
  486. foreach ($dateOccurrences as $date => $count) {
  487. // Convert each date to the format $year-$weekNo
  488. $dateTime = new DateTime($date);
  489. $yearWeek = $dateTime->format('Y-W');
  490.  
  491. // Add +1 for each date converted into $year-$weekNo as a key
  492. if (isset($weeklyOccurrences[$yearWeek])) {
  493. if( $dateOccurrences[$date] == 1)
  494. $weeklyOccurrences[$yearWeek]++;
  495. } else {
  496. if($dateOccurrences[$date] == 1){
  497. $weeklyOccurrences[$yearWeek] = 1;
  498. }else{
  499. $weeklyOccurrences[$yearWeek] = 0;
  500. }
  501. }
  502. }
  503.  
  504. //print_r($dateOccurrences);
  505. //print_r($weeklyOccurrences);
  506.  
  507. $countZeros = 0;
  508.  
  509. foreach ($weeklyOccurrences as $value) {
  510. if ($value < $multiplier) {
  511. $countZeros++;
  512. } else {
  513. break; // Stop counting when a non-zero value is encountered
  514. }
  515. }
  516. return $countZeros;
  517. }
  518.  
  519. function habit_get_stat_top($word, $filename) {
  520. $fileContent = file_get_contents($filename);
  521.  
  522. // Split the file content into an array of lines
  523. $lines = explode("\n", $fileContent);
  524.  
  525. $dateOccurrences = [];
  526. $currentStreak = 0;
  527. $longestStreak = 0;
  528.  
  529. foreach ($lines as $line) {
  530. // Extract the date from the line
  531. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  532. $date = isset($matches[0]) ? $matches[0] : null;
  533.  
  534. // Check if the line contains the specified word as a hashtag
  535. $wordFound = (strpos($line, "#$word") !== false);
  536.  
  537. // Populate the dateOccurrences array
  538. if ($date !== null) {
  539. if (!isset($dateOccurrences[$date])) {
  540. $dateOccurrences[$date] = 0;
  541. }
  542. if($wordFound){
  543. $dateOccurrences[$date] = 1;
  544. }
  545. }
  546. }
  547.  
  548. foreach ($dateOccurrences as $date => $value) {
  549. if ($value == 1) {
  550. $currentStreak++;
  551. } else {
  552. $currentStreak = 0;
  553. }
  554.  
  555. // Update the longest streak if the current streak is greater
  556. $longestStreak = max($longestStreak, $currentStreak);
  557. }
  558.  
  559. //print_r($dateOccurrences);
  560. return $longestStreak;
  561. }
  562.  
  563. function habit_get_stat_top_week($word, $multiplier, $filename) {
  564. // Split the file content into an array of lines
  565. $fileContent = file_get_contents($filename);
  566. $lines = explode("\n", $fileContent);
  567. $dateOccurrences = [];
  568. $weeklyOccurrences = [];
  569.  
  570. foreach ($lines as $line) {
  571. // Extract the date from the line
  572. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  573. $date = isset($matches[0]) ? $matches[0] : null;
  574.  
  575. // Explode the line into words
  576. $words = explode(' ', $line);
  577.  
  578. // Check if the line contains the specified word as a hashtag
  579. $wordFound = false;
  580. foreach ($words as $wordInLine) {
  581. // Check if the word contains "#takeout" (case-insensitive)
  582. if (stripos($wordInLine, "#$word") !== false) {
  583. $wordFound = true;
  584. break;
  585. }
  586. }
  587.  
  588. // Populate the dateOccurrences array
  589. if (!isset($dateOccurrences[$date])) {
  590. $dateOccurrences[$date] = 0;
  591. }
  592. if ($date !== null && $wordFound) {
  593. $dateOccurrences[$date]++;
  594. }
  595.  
  596. // Output the line whether the word is found or not
  597. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  598. }
  599.  
  600. foreach ($dateOccurrences as $date => $count) {
  601. // Convert each date to the format $year-$weekNo
  602. $dateTime = new DateTime($date);
  603. $yearWeek = $dateTime->format('Y-W');
  604.  
  605. // Add +1 for each date converted into $year-$weekNo as a key
  606. if (isset($weeklyOccurrences[$yearWeek])) {
  607. if( $dateOccurrences[$date] == 1)
  608. $weeklyOccurrences[$yearWeek]++;
  609. } else {
  610. if($dateOccurrences[$date] == 1){
  611. $weeklyOccurrences[$yearWeek] = 1;
  612. }else{
  613. $weeklyOccurrences[$yearWeek] = 0;
  614. }
  615. }
  616. }
  617.  
  618. //print_r($dateOccurrences);
  619. //print_r($weeklyOccurrences);
  620.  
  621. $currentStreak = 0;
  622. $longestStreak = 0;
  623.  
  624. foreach ($weeklyOccurrences as $week => $value) {
  625. if ($value >= $multiplier) {
  626. // Increment the streak length if the value is greater than or equal to the multiplier
  627. $currentStreak++;
  628. } else {
  629. // Reset the streak length if the value is less than the multiplier
  630. $currentStreak = 0;
  631. }
  632.  
  633. // Update the longest streak if the current streak is longer
  634. $longestStreak = max($longestStreak, $currentStreak);
  635. }
  636. return $longestStreak;
  637. }
  638.  
  639. function habit_get_stat_top_missing($word, $filename) {
  640. $fileContent = file_get_contents($filename);
  641.  
  642. // Split the file content into an array of lines
  643. $lines = explode("\n", $fileContent);
  644.  
  645. // Find the earliest instance of the word
  646. $earliestDateWithWord = null;
  647. foreach ($lines as $line) {
  648. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  649. $date = isset($matches[0]) ? $matches[0] : null;
  650.  
  651. // Check if the line contains the specified word as a hashtag
  652. $wordFound = (strpos($line, "#$word") !== false);
  653.  
  654. if ($wordFound && ($earliestDateWithWord === null || $date < $earliestDateWithWord)) {
  655. $earliestDateWithWord = $date;
  656. }
  657. }
  658.  
  659. if ($earliestDateWithWord === null) {
  660. // No instance of the word found, return 0
  661. return 0;
  662. }
  663.  
  664. $currentDate = $earliestDateWithWord;
  665. $today = date('Y-m-d');
  666. $dateArray = [];
  667.  
  668. // Initialize date array with 0 values starting from the earliest date with the word
  669. while ($currentDate <= $today) {
  670. $dateArray[$currentDate] = 0;
  671. $currentDate = date('Y-m-d', strtotime($currentDate . ' + 1 day'));
  672. }
  673.  
  674. // Loop over the file and update dateArray
  675. foreach ($lines as $line) {
  676. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  677. $date = isset($matches[0]) ? $matches[0] : null;
  678.  
  679. // Check if the line contains the specified word as a hashtag
  680. $wordFound = (strpos($line, "#$word") !== false);
  681.  
  682. if ($date !== null && isset($dateArray[$date])) {
  683. // Update dateArray based on word occurrence
  684. if($wordFound)
  685. $dateArray[$date] = 1;
  686. }
  687. }
  688.  
  689. //print_r($dateArray);
  690.  
  691. // Find the longest streak of 0's in dateArray
  692. $currentStreak = 0;
  693. $longestStreak = 0;
  694.  
  695. foreach ($dateArray as $value) {
  696. if ($value === 0) {
  697. $currentStreak++;
  698. } else {
  699. $longestStreak = max($longestStreak, $currentStreak);
  700. $currentStreak = 0;
  701. }
  702. }
  703.  
  704. // Check for a streak at the end of the array
  705. $longestStreak = max($longestStreak, $currentStreak);
  706.  
  707. return $longestStreak;
  708. }
  709.  
  710. function habit_get_stat_year($word, $filename) {
  711. $fileContent = file_get_contents($filename);
  712.  
  713. // Split the file content into an array of lines
  714. $lines = explode("\n", $fileContent);
  715.  
  716. $daysWithWord = [];
  717.  
  718. foreach ($lines as $line) {
  719. // Extract the date from the line
  720. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  721. $date = isset($matches[0]) ? $matches[0] : null;
  722.  
  723. // Check if the line contains the specified word as a hashtag
  724. $wordFound = (strpos($line, "#$word") !== false);
  725.  
  726. if ($date !== null && date('Y', strtotime($date)) == date('Y')) {
  727. // Check if the date is in the current year
  728. if (!isset($daysWithWord[$date])) {
  729. $daysWithWord[$date] = $wordFound ? 1 : 0;
  730. } else {
  731. $daysWithWord[$date] += $wordFound ? 1 : 0;
  732. }
  733. }
  734. }
  735.  
  736. $daysCount = array_sum($daysWithWord);
  737.  
  738. return $daysCount;
  739. }
  740.  
  741. function habit_get_json($word, $moodlog, $habitlog){
  742. $fileContent = file_get_contents($habitlog);
  743. $resultArray = habit_parse_file($fileContent);
  744. $found = false;
  745.  
  746. foreach ($resultArray as $section) {
  747. $jsonArray = array();
  748. $jsonArray['align'] = "";
  749. $jsonArray['buttonClass'] = "";
  750. $jsonArray['buttonText'] = "";
  751. if($section['Category'][0] == "d" && $section['Activity'] == $word){
  752. $found = true;
  753. $jsonArray['freq'] = "d";
  754.  
  755. if($section['Category'][1] == "+"){
  756. $jsonArray['align'] = "good";
  757. $jsonArray['calCol'] = "#58e81b";
  758. $jsonArray['title1'] = "Streak";
  759. $jsonArray['title2'] = "Missed";
  760. $jsonArray['title3'] = "Top";
  761. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  762. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  763. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  764.  
  765. }
  766. if($section['Category'][1] == "-"){
  767. $jsonArray['align'] = "bad";
  768. $jsonArray['calCol'] = "#e81b1b";
  769. $jsonArray['title1'] = "Streak";
  770. $jsonArray['title2'] = "Total (Year)";
  771. $jsonArray['title3'] = "Top";
  772. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  773. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  774. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  775. }
  776. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  777. if ($todayCheck) {
  778. $jsonArray['buttonClass'] = 'button-'.$jsonArray['align'];
  779. }
  780. break;
  781. }
  782. if($section['Category'][0] == "w" && $section['Activity'] == $word){
  783. $found = true;
  784. $jsonArray['freq'] = "w";
  785.  
  786. $multiplier = $section['Category'][1];
  787. $jsonArray['align'] = "good";
  788. $jsonArray['title1'] = "Streak";
  789. $jsonArray['title2'] = "Missed";
  790. $jsonArray['title3'] = "Top";
  791. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  792. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  793. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  794. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  795. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  796. $jsonArray['weekSoFar'] = $weekCount;
  797. $jsonArray['weekGoal'] = $multiplier;
  798.  
  799. if( $weekCount < $multiplier )
  800. if($todayCheck)
  801. $jsonArray['buttonText'] = $weekCount;
  802. if($weekCount >= $multiplier)
  803. $jsonArray['buttonClass'] = 'button-done';
  804. if( $todayCheck ) {
  805. $jsonArray['buttonClass'] = 'button-neut';
  806. if($weekCount >= $multiplier)
  807. $jsonArray['buttonClass'] = 'button-good';
  808. }
  809. break;
  810. }
  811. }
  812.  
  813. if($found)
  814. return json_encode($jsonArray);
  815. return false;
  816. }
  817.  
  818. function habit_get_json_all($moodlog, $habitlog) {
  819. $fileContent = file_get_contents($habitlog);
  820. $resultArray = habit_parse_file($fileContent);
  821.  
  822. $jsonWordsArray = array();
  823.  
  824. foreach ($resultArray as $section) {
  825. $jsonArray = array();
  826. $jsonArray['activity'] = $section['Activity'];
  827. $jsonArray['align'] = "";
  828. $jsonArray['buttonClass'] = "";
  829. $jsonArray['buttonText'] = "";
  830.  
  831. if ($section['Category'][0] == "d") {
  832. $jsonArray['freq'] = "d";
  833.  
  834. if ($section['Category'][1] == "+") {
  835. $jsonArray['align'] = "good";
  836. $jsonArray['calCol'] = "#58e81b";
  837. $jsonArray['title1'] = "Streak";
  838. $jsonArray['title2'] = "Missed";
  839. $jsonArray['title3'] = "Top";
  840. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  841. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  842. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  843. } elseif ($section['Category'][1] == "-") {
  844. $jsonArray['align'] = "bad";
  845. $jsonArray['calCol'] = "#e81b1b";
  846. $jsonArray['title1'] = "Streak";
  847. $jsonArray['title2'] = "Total (Year)";
  848. $jsonArray['title3'] = "Top";
  849. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  850. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  851. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  852. }
  853.  
  854. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  855.  
  856. if ($todayCheck) {
  857. $jsonArray['buttonClass'] = 'button-' . $jsonArray['align'];
  858. }
  859. } elseif ($section['Category'][0] == "w") {
  860. $jsonArray['freq'] = "w";
  861.  
  862. $multiplier = $section['Category'][1];
  863. $jsonArray['align'] = "good";
  864. $jsonArray['title1'] = "Streak";
  865. $jsonArray['title2'] = "Missed";
  866. $jsonArray['title3'] = "Top";
  867. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  868. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  869. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  870.  
  871. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  872. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  873. $jsonArray['weekSoFar'] = $weekCount;
  874. $jsonArray['weekGoal'] = $multiplier;
  875.  
  876. if ($weekCount < $multiplier && $todayCheck) {
  877. $jsonArray['buttonText'] = $weekCount;
  878. }
  879.  
  880. if ($weekCount >= $multiplier) {
  881. $jsonArray['buttonClass'] = 'button-done';
  882. }
  883.  
  884. if ($todayCheck) {
  885. $jsonArray['buttonClass'] = 'button-neut';
  886.  
  887. if ($weekCount >= $multiplier) {
  888. $jsonArray['buttonClass'] = 'button-good';
  889. }
  890. }
  891. }
  892.  
  893. $jsonWordsArray[] = $jsonArray;
  894. }
  895.  
  896. return json_encode($jsonWordsArray);
  897. }
  898.  
  899. /***
  900. * creating calendar stuff
  901. */
  902. // Function to get the ISO week numbers in a month
  903. function getISOWeeksInMonth($year, $month) {
  904. $firstDay = new \DateTime("$year-$month-01");
  905. $lastDay = new \DateTime($firstDay->format('Y-m-t'));
  906.  
  907. // Calculate ISO week numbers for each day
  908. $isoWeeks = [];
  909. $currentDay = clone $firstDay;
  910.  
  911. while ($currentDay <= $lastDay) {
  912. $isoWeek = $currentDay->format('W');
  913. $isoWeeks[] = $isoWeek;
  914. $currentDay->modify('+1 day');
  915. }
  916.  
  917. return array_unique($isoWeeks);
  918. }
  919.  
  920. // Function to check if a date is a Sunday
  921. function isSunday($date) {
  922. return $date->format('w') == 0;
  923. }
  924.  
  925. function create_new_weekly_cal(){
  926. $calHTML = "";
  927. // Get current date
  928. $currentDate = new \DateTime();
  929. // Create an array to store months
  930. $months = [];
  931.  
  932. // Loop through the current month to 23 months ago and store in the array
  933. for ($i = 0; $i < 23; $i++) {
  934. $year = $currentDate->format('Y');
  935. $month = $currentDate->format('m');
  936.  
  937. // Get the ISO week numbers in the month
  938. $isoWeeksInMonth = getISOWeeksInMonth($year, $month);
  939.  
  940. // Store the month data in the array
  941. $months[] = [
  942. 'year' => $year,
  943. 'month' => $month,
  944. 'isoWeeks' => $isoWeeksInMonth,
  945. ];
  946.  
  947. // Move forward one month for the next iteration
  948. $currentDate->sub(new DateInterval('P1M'));
  949. }
  950.  
  951. // Reverse the array to display the oldest month first
  952. $months = array_reverse($months);
  953.  
  954. // Create the grid
  955. $calHTML .= '<div class="grid-container">';
  956.  
  957. // Loop through the months array to output the grid
  958. foreach ($months as $monthData) {
  959. $year = $monthData['year'];
  960. $month = $monthData['month'];
  961. $isoWeeksInMonth = $monthData['isoWeeks'];
  962.  
  963. $calHTML .= '<div class="month">';
  964. //echo "<h3>{$year}-{$month}</h3>";
  965. $calHTML .= '<div class="week-container">';
  966. // Keep the last ISO week if it's December and the last day is Sunday
  967. if ($month === '12' && isSunday(new \DateTime("$year-$month-31"))) {
  968. foreach ($isoWeeksInMonth as $isoWeek) {
  969. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  970. }
  971. } else {
  972. // Exclude the last ISO week when outputting weeks
  973. $keys = array_keys($isoWeeksInMonth);
  974. $lastKey = end($keys);
  975. foreach ($isoWeeksInMonth as $isoWeekKey => $isoWeek) {
  976. if ($isoWeekKey === $lastKey) {
  977. continue;
  978. }
  979. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  980. }
  981. }
  982.  
  983. $calHTML .= '</div>';
  984. $calHTML .= '</div>';
  985. }
  986.  
  987. $calHTML .= '</div>';
  988. return $calHTML;
  989. }
  990.  
  991.  
  992. ?>
Buy Me A Coffee